Blender  V3.3
text_format.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
8 #include <string.h>
9 
10 #include "MEM_guardedalloc.h"
11 
12 #include "BLI_blenlib.h"
13 #include "BLI_string_utils.h"
14 
15 #include "DNA_space_types.h"
16 #include "DNA_text_types.h"
17 
18 #include "ED_text.h"
19 
20 #include "text_format.h"
21 
22 /****************** flatten string **********************/
23 
24 static void flatten_string_append(FlattenString *fs, const char *c, int accum, int len)
25 {
26  int i;
27 
28  if (fs->pos + len > fs->len) {
29  char *nbuf;
30  int *naccum;
31  fs->len *= 2;
32 
33  nbuf = MEM_callocN(sizeof(*fs->buf) * fs->len, "fs->buf");
34  naccum = MEM_callocN(sizeof(*fs->accum) * fs->len, "fs->accum");
35 
36  memcpy(nbuf, fs->buf, fs->pos * sizeof(*fs->buf));
37  memcpy(naccum, fs->accum, fs->pos * sizeof(*fs->accum));
38 
39  if (fs->buf != fs->fixedbuf) {
40  MEM_freeN(fs->buf);
41  MEM_freeN(fs->accum);
42  }
43 
44  fs->buf = nbuf;
45  fs->accum = naccum;
46  }
47 
48  for (i = 0; i < len; i++) {
49  fs->buf[fs->pos + i] = c[i];
50  fs->accum[fs->pos + i] = accum;
51  }
52 
53  fs->pos += len;
54 }
55 
56 int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
57 {
58  int r, i, total = 0;
59 
60  memset(fs, 0, sizeof(FlattenString));
61  fs->buf = fs->fixedbuf;
62  fs->accum = fs->fixedaccum;
63  fs->len = sizeof(fs->fixedbuf);
64 
65  for (r = 0, i = 0; *in; r++) {
66  if (*in == '\t') {
67  i = st->tabnumber - (total % st->tabnumber);
68  total += i;
69 
70  while (i--) {
71  flatten_string_append(fs, " ", r, 1);
72  }
73 
74  in++;
75  }
76  else {
77  size_t len = BLI_str_utf8_size_safe(in);
78  flatten_string_append(fs, in, r, len);
79  in += len;
80  total++;
81  }
82  }
83 
84  flatten_string_append(fs, "\0", r, 1);
85 
86  return total;
87 }
88 
90 {
91  if (fs->buf != fs->fixedbuf) {
92  MEM_freeN(fs->buf);
93  }
94  if (fs->accum != fs->fixedaccum) {
95  MEM_freeN(fs->accum);
96  }
97 }
98 
100 {
101  const int len = (fs->pos - (int)(str - fs->buf)) - 1;
102  BLI_assert(strlen(str) == len);
103  return len;
104 }
105 
107 {
108  if (line->format) {
109  if (strlen(line->format) < len) {
110  MEM_freeN(line->format);
111  line->format = MEM_mallocN(len + 2, "SyntaxFormat");
112  if (!line->format) {
113  return 0;
114  }
115  }
116  }
117  else {
118  line->format = MEM_mallocN(len + 2, "SyntaxFormat");
119  if (!line->format) {
120  return 0;
121  }
122  }
123 
124  return 1;
125 }
126 
127 void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len)
128 {
129  const char *str = *str_p;
130  char *fmt = *fmt_p;
131  int i = 0;
132 
133  while (i < len) {
134  const int size = BLI_str_utf8_size_safe(str);
135  *fmt++ = type;
136 
137  str += size;
138  i += 1;
139  }
140 
141  str--;
142  fmt--;
143 
144  BLI_assert(*str != '\0');
145 
146  *str_p = str;
147  *fmt_p = fmt;
148 }
149 void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
150 {
151  const char *str = *str_p;
152  char *fmt = *fmt_p;
153 
154  memset(fmt, type, len);
155 
156  str += len - 1;
157  fmt += len - 1;
158 
159  BLI_assert(*str != '\0');
160 
161  *str_p = str;
162  *fmt_p = fmt;
163 }
164 
165 /* *** Registration *** */
166 static ListBase tft_lb = {NULL, NULL};
168 {
169  BLI_addtail(&tft_lb, tft);
170 }
171 
173 {
174  TextFormatType *tft;
175 
176  if (text) {
177  const char *text_ext = strchr(text->id.name + 2, '.');
178  if (text_ext) {
179  text_ext++; /* skip the '.' */
180  /* Check all text formats in the static list */
181  for (tft = tft_lb.first; tft; tft = tft->next) {
182  /* All formats should have an ext, but just in case */
183  const char **ext;
184  for (ext = tft->ext; *ext; ext++) {
185  /* If extension matches text name, return the matching tft */
186  if (BLI_strcasecmp(text_ext, *ext) == 0) {
187  return tft;
188  }
189  }
190  }
191  }
192 
193  /* If we make it here we never found an extension that worked - return
194  * the "default" text format */
195  return tft_lb.first;
196  }
197 
198  /* Return the "default" text format */
199  return tft_lb.first;
200 }
201 
203 {
204  if (text == NULL) {
205  return false;
206  }
207 
208  TextFormatType *tft;
209 
210  const char *text_ext = BLI_path_extension(text->id.name + 2);
211  if (text_ext == NULL) {
212  /* Extensionless data-blocks are considered highlightable as Python. */
213  return true;
214  }
215  text_ext++; /* skip the '.' */
216  if (BLI_string_is_decimal(text_ext)) {
217  /* "Text.001" is treated as extensionless, and thus highlightable. */
218  return true;
219  }
220 
221  /* Check all text formats in the static list */
222  for (tft = tft_lb.first; tft; tft = tft->next) {
223  /* All formats should have an ext, but just in case */
224  const char **ext;
225  for (ext = tft->ext; *ext; ext++) {
226  /* If extension matches text name, return the matching tft */
227  if (BLI_strcasecmp(text_ext, *ext) == 0) {
228  return true;
229  }
230  }
231  }
232 
233  /* The filename has a non-numerical extension that we could not highlight. */
234  return false;
235 }
#define BLI_assert(a)
Definition: BLI_assert.h:46
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
const char * BLI_path_extension(const char *filepath) ATTR_NONNULL()
Definition: path_util.c:1500
int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:623
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: string_utf8.c:466
bool BLI_string_is_decimal(const char *string) ATTR_NONNULL()
Definition: string_utils.c:55
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
int len
Definition: draw_manager.c:108
#define str(s)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
static unsigned c
Definition: RandGen.cpp:83
static const pxr::TfToken st("st", pxr::TfToken::Immortal)
int fixedaccum[256]
Definition: text_format.h:13
char fixedbuf[256]
Definition: text_format.h:12
char name[66]
Definition: DNA_ID.h:378
void * first
Definition: DNA_listBase.h:31
struct TextFormatType * next
Definition: text_format.h:62
const char ** ext
Definition: text_format.h:77
char * format
int flatten_string_strlen(FlattenString *fs, const char *str)
Definition: text_format.c:99
static void flatten_string_append(FlattenString *fs, const char *c, int accum, int len)
Definition: text_format.c:24
int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
Definition: text_format.c:56
void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len)
Definition: text_format.c:127
void flatten_string_free(FlattenString *fs)
Definition: text_format.c:89
TextFormatType * ED_text_format_get(Text *text)
Definition: text_format.c:172
static ListBase tft_lb
Definition: text_format.c:166
void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
Definition: text_format.c:149
bool ED_text_is_syntax_highlight_supported(Text *text)
Definition: text_format.c:202
void ED_text_format_register(TextFormatType *tft)
Definition: text_format.c:167
int text_check_format_len(TextLine *line, uint len)
Definition: text_format.c:106