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

text_format.c « space_text « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bdbf55d9198b3361257f1e17ad6292748dd29f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 */

/** \file
 * \ingroup sptext
 */

#include <string.h>

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"
#include "BLI_string_utils.h"

#include "DNA_space_types.h"
#include "DNA_text_types.h"

#include "ED_text.h"

#include "text_format.h"

/****************** flatten string **********************/

static void flatten_string_append(FlattenString *fs, const char *c, int accum, int len)
{
  int i;

  if (fs->pos + len > fs->len) {
    char *nbuf;
    int *naccum;
    fs->len *= 2;

    nbuf = MEM_callocN(sizeof(*fs->buf) * fs->len, "fs->buf");
    naccum = MEM_callocN(sizeof(*fs->accum) * fs->len, "fs->accum");

    memcpy(nbuf, fs->buf, fs->pos * sizeof(*fs->buf));
    memcpy(naccum, fs->accum, fs->pos * sizeof(*fs->accum));

    if (fs->buf != fs->fixedbuf) {
      MEM_freeN(fs->buf);
      MEM_freeN(fs->accum);
    }

    fs->buf = nbuf;
    fs->accum = naccum;
  }

  for (i = 0; i < len; i++) {
    fs->buf[fs->pos + i] = c[i];
    fs->accum[fs->pos + i] = accum;
  }

  fs->pos += len;
}

int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
{
  int r, i, total = 0;

  memset(fs, 0, sizeof(FlattenString));
  fs->buf = fs->fixedbuf;
  fs->accum = fs->fixedaccum;
  fs->len = sizeof(fs->fixedbuf);

  for (r = 0, i = 0; *in; r++) {
    if (*in == '\t') {
      i = st->tabnumber - (total % st->tabnumber);
      total += i;

      while (i--) {
        flatten_string_append(fs, " ", r, 1);
      }

      in++;
    }
    else {
      size_t len = BLI_str_utf8_size_safe(in);
      flatten_string_append(fs, in, r, len);
      in += len;
      total++;
    }
  }

  flatten_string_append(fs, "\0", r, 1);

  return total;
}

void flatten_string_free(FlattenString *fs)
{
  if (fs->buf != fs->fixedbuf) {
    MEM_freeN(fs->buf);
  }
  if (fs->accum != fs->fixedaccum) {
    MEM_freeN(fs->accum);
  }
}

/* takes a string within fs->buf and returns its length */
int flatten_string_strlen(FlattenString *fs, const char *str)
{
  const int len = (fs->pos - (int)(str - fs->buf)) - 1;
  BLI_assert(strlen(str) == len);
  return len;
}

/* Ensures the format string for the given line is long enough, reallocating
 * as needed. Allocation is done here, alone, to ensure consistency. */
int text_check_format_len(TextLine *line, uint len)
{
  if (line->format) {
    if (strlen(line->format) < len) {
      MEM_freeN(line->format);
      line->format = MEM_mallocN(len + 2, "SyntaxFormat");
      if (!line->format) {
        return 0;
      }
    }
  }
  else {
    line->format = MEM_mallocN(len + 2, "SyntaxFormat");
    if (!line->format) {
      return 0;
    }
  }

  return 1;
}

/**
 * Fill the string with formatting constant,
 * advancing \a str_p and \a fmt_p
 *
 * \param len: length in bytes of \a fmt_p to fill.
 */
void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len)
{
  const char *str = *str_p;
  char *fmt = *fmt_p;
  int i = 0;

  while (i < len) {
    const int size = BLI_str_utf8_size_safe(str);
    *fmt++ = type;

    str += size;
    i += 1;
  }

  str--;
  fmt--;

  BLI_assert(*str != '\0');

  *str_p = str;
  *fmt_p = fmt;
}
/**
 * ascii version of #text_format_fill,
 * use when we no the text being stepped over is ascii (as is the case for most keywords)
 */
void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
{
  const char *str = *str_p;
  char *fmt = *fmt_p;

  memset(fmt, type, len);

  str += len - 1;
  fmt += len - 1;

  BLI_assert(*str != '\0');

  *str_p = str;
  *fmt_p = fmt;
}

/* *** Registration *** */
static ListBase tft_lb = {NULL, NULL};
void ED_text_format_register(TextFormatType *tft)
{
  BLI_addtail(&tft_lb, tft);
}

TextFormatType *ED_text_format_get(Text *text)
{
  TextFormatType *tft;

  if (text) {
    const char *text_ext = strchr(text->id.name + 2, '.');
    if (text_ext) {
      text_ext++; /* skip the '.' */
      /* Check all text formats in the static list */
      for (tft = tft_lb.first; tft; tft = tft->next) {
        /* All formats should have an ext, but just in case */
        const char **ext;
        for (ext = tft->ext; *ext; ext++) {
          /* If extension matches text name, return the matching tft */
          if (BLI_strcasecmp(text_ext, *ext) == 0) {
            return tft;
          }
        }
      }
    }

    /* If we make it here we never found an extension that worked - return
     * the "default" text format */
    return tft_lb.first;
  }
  else {
    /* Return the "default" text format */
    return tft_lb.first;
  }
}

bool ED_text_is_syntax_highlight_supported(Text *text)
{
  if (text == NULL) {
    return false;
  }

  TextFormatType *tft;

  const char *text_ext = BLI_path_extension(text->id.name + 2);
  if (text_ext == NULL) {
    /* Extensionless datablocks are considered highlightable as Python. */
    return true;
  }
  text_ext++; /* skip the '.' */
  if (BLI_string_is_decimal(text_ext)) {
    /* "Text.001" is treated as extensionless, and thus highlightable. */
    return true;
  }

  /* Check all text formats in the static list */
  for (tft = tft_lb.first; tft; tft = tft->next) {
    /* All formats should have an ext, but just in case */
    const char **ext;
    for (ext = tft->ext; *ext; ext++) {
      /* If extension matches text name, return the matching tft */
      if (BLI_strcasecmp(text_ext, *ext) == 0) {
        return true;
      }
    }
  }

  /* The filename has a non-numerical extension that we could not highlight. */
  return false;
}