Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <campbell@blender.org>2022-03-11 07:09:55 +0300
committerCampbell Barton <campbell@blender.org>2022-03-11 07:14:14 +0300
commit8cc5483331d1a3d5c6eba055ae303788ba843526 (patch)
tree0b132608148bd35d7aad85b760d35300b9c94102 /source/blender/blenkernel/intern/text.c
parent231eac160ee394d41c84e0cc36845facb7594ba5 (diff)
Text: use simplified logic for txt_to_buf
This function was copied from txt_sel_to_buf, including unnecessary complexity to support selection as well as checks for the cursor which don't make sense when copying the whole buffer. Use a simple loop to copy all text into the destination buffer.
Diffstat (limited to 'source/blender/blenkernel/intern/text.c')
-rw-r--r--source/blender/blenkernel/intern/text.c82
1 files changed, 12 insertions, 70 deletions
diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c
index 5d0e515040d..486449c3f86 100644
--- a/source/blender/blenkernel/intern/text.c
+++ b/source/blender/blenkernel/intern/text.c
@@ -1436,78 +1436,20 @@ void txt_from_buf_for_undo(Text *text, const char *buf, int buf_len)
char *txt_to_buf(Text *text, int *r_buf_strlen)
{
- int length;
- TextLine *tmp, *linef, *linel;
- int charf, charl;
- char *buf;
-
- if (r_buf_strlen) {
- *r_buf_strlen = 0;
- }
-
- if (!text->curl) {
- return NULL;
- }
- if (!text->sell) {
- return NULL;
- }
- if (!text->lines.first) {
- return NULL;
- }
-
- linef = text->lines.first;
- charf = 0;
-
- linel = text->lines.last;
- charl = linel->len;
-
- if (linef == text->lines.last) {
- length = charl - charf;
-
- buf = MEM_mallocN(length + 2, "text buffer");
-
- BLI_strncpy(buf, linef->line + charf, length + 1);
- buf[length] = 0;
- }
- else {
- length = linef->len - charf;
- length += charl;
- length += 2; /* For the 2 '\n' */
-
- tmp = linef->next;
- while (tmp && tmp != linel) {
- length += tmp->len + 1;
- tmp = tmp->next;
- }
-
- buf = MEM_mallocN(length + 1, "cut buffer");
-
- strncpy(buf, linef->line + charf, linef->len - charf);
- length = linef->len - charf;
-
- buf[length++] = '\n';
-
- tmp = linef->next;
- while (tmp && tmp != linel) {
- strncpy(buf + length, tmp->line, tmp->len);
- length += tmp->len;
-
- buf[length++] = '\n';
-
- tmp = tmp->next;
- }
- strncpy(buf + length, linel->line, charl);
- length += charl;
-
- /* python compiler wants an empty end line */
- buf[length++] = '\n';
- buf[length] = 0;
+ /* Identical to #txt_to_buf_for_undo except that the string is nil terminated. */
+ int buf_len = 0;
+ LISTBASE_FOREACH (const TextLine *, l, &text->lines) {
+ buf_len += l->len + 1;
}
-
- if (r_buf_strlen) {
- *r_buf_strlen = length;
+ char *buf = MEM_mallocN(buf_len + 1, __func__);
+ char *buf_step = buf;
+ LISTBASE_FOREACH (const TextLine *, l, &text->lines) {
+ memcpy(buf_step, l->line, l->len);
+ buf_step += l->len;
+ *buf_step++ = '\n';
}
-
+ *buf_step = '\0';
+ *r_buf_strlen = buf_len;
return buf;
}