Blender  V2.93
text_format.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software Foundation,
13  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14  *
15  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
16  * All rights reserved.
17  */
18 
23 #include <string.h>
24 
25 #include "MEM_guardedalloc.h"
26 
27 #include "BLI_blenlib.h"
28 #include "BLI_string_utils.h"
29 
30 #include "DNA_space_types.h"
31 #include "DNA_text_types.h"
32 
33 #include "ED_text.h"
34 
35 #include "text_format.h"
36 
37 /****************** flatten string **********************/
38 
39 static void flatten_string_append(FlattenString *fs, const char *c, int accum, int len)
40 {
41  int i;
42 
43  if (fs->pos + len > fs->len) {
44  char *nbuf;
45  int *naccum;
46  fs->len *= 2;
47 
48  nbuf = MEM_callocN(sizeof(*fs->buf) * fs->len, "fs->buf");
49  naccum = MEM_callocN(sizeof(*fs->accum) * fs->len, "fs->accum");
50 
51  memcpy(nbuf, fs->buf, fs->pos * sizeof(*fs->buf));
52  memcpy(naccum, fs->accum, fs->pos * sizeof(*fs->accum));
53 
54  if (fs->buf != fs->fixedbuf) {
55  MEM_freeN(fs->buf);
56  MEM_freeN(fs->accum);
57  }
58 
59  fs->buf = nbuf;
60  fs->accum = naccum;
61  }
62 
63  for (i = 0; i < len; i++) {
64  fs->buf[fs->pos + i] = c[i];
65  fs->accum[fs->pos + i] = accum;
66  }
67 
68  fs->pos += len;
69 }
70 
71 int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
72 {
73  int r, i, total = 0;
74 
75  memset(fs, 0, sizeof(FlattenString));
76  fs->buf = fs->fixedbuf;
77  fs->accum = fs->fixedaccum;
78  fs->len = sizeof(fs->fixedbuf);
79 
80  for (r = 0, i = 0; *in; r++) {
81  if (*in == '\t') {
82  i = st->tabnumber - (total % st->tabnumber);
83  total += i;
84 
85  while (i--) {
86  flatten_string_append(fs, " ", r, 1);
87  }
88 
89  in++;
90  }
91  else {
92  size_t len = BLI_str_utf8_size_safe(in);
93  flatten_string_append(fs, in, r, len);
94  in += len;
95  total++;
96  }
97  }
98 
99  flatten_string_append(fs, "\0", r, 1);
100 
101  return total;
102 }
103 
105 {
106  if (fs->buf != fs->fixedbuf) {
107  MEM_freeN(fs->buf);
108  }
109  if (fs->accum != fs->fixedaccum) {
110  MEM_freeN(fs->accum);
111  }
112 }
113 
114 /* takes a string within fs->buf and returns its length */
116 {
117  const int len = (fs->pos - (int)(str - fs->buf)) - 1;
118  BLI_assert(strlen(str) == len);
119  return len;
120 }
121 
122 /* Ensures the format string for the given line is long enough, reallocating
123  * as needed. Allocation is done here, alone, to ensure consistency. */
125 {
126  if (line->format) {
127  if (strlen(line->format) < len) {
128  MEM_freeN(line->format);
129  line->format = MEM_mallocN(len + 2, "SyntaxFormat");
130  if (!line->format) {
131  return 0;
132  }
133  }
134  }
135  else {
136  line->format = MEM_mallocN(len + 2, "SyntaxFormat");
137  if (!line->format) {
138  return 0;
139  }
140  }
141 
142  return 1;
143 }
144 
151 void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len)
152 {
153  const char *str = *str_p;
154  char *fmt = *fmt_p;
155  int i = 0;
156 
157  while (i < len) {
158  const int size = BLI_str_utf8_size_safe(str);
159  *fmt++ = type;
160 
161  str += size;
162  i += 1;
163  }
164 
165  str--;
166  fmt--;
167 
168  BLI_assert(*str != '\0');
169 
170  *str_p = str;
171  *fmt_p = fmt;
172 }
177 void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
178 {
179  const char *str = *str_p;
180  char *fmt = *fmt_p;
181 
182  memset(fmt, type, len);
183 
184  str += len - 1;
185  fmt += len - 1;
186 
187  BLI_assert(*str != '\0');
188 
189  *str_p = str;
190  *fmt_p = fmt;
191 }
192 
193 /* *** Registration *** */
194 static ListBase tft_lb = {NULL, NULL};
196 {
197  BLI_addtail(&tft_lb, tft);
198 }
199 
201 {
202  TextFormatType *tft;
203 
204  if (text) {
205  const char *text_ext = strchr(text->id.name + 2, '.');
206  if (text_ext) {
207  text_ext++; /* skip the '.' */
208  /* Check all text formats in the static list */
209  for (tft = tft_lb.first; tft; tft = tft->next) {
210  /* All formats should have an ext, but just in case */
211  const char **ext;
212  for (ext = tft->ext; *ext; ext++) {
213  /* If extension matches text name, return the matching tft */
214  if (BLI_strcasecmp(text_ext, *ext) == 0) {
215  return tft;
216  }
217  }
218  }
219  }
220 
221  /* If we make it here we never found an extension that worked - return
222  * the "default" text format */
223  return tft_lb.first;
224  }
225 
226  /* Return the "default" text format */
227  return tft_lb.first;
228 }
229 
231 {
232  if (text == NULL) {
233  return false;
234  }
235 
236  TextFormatType *tft;
237 
238  const char *text_ext = BLI_path_extension(text->id.name + 2);
239  if (text_ext == NULL) {
240  /* Extensionless data-blocks are considered highlightable as Python. */
241  return true;
242  }
243  text_ext++; /* skip the '.' */
244  if (BLI_string_is_decimal(text_ext)) {
245  /* "Text.001" is treated as extensionless, and thus highlightable. */
246  return true;
247  }
248 
249  /* Check all text formats in the static list */
250  for (tft = tft_lb.first; tft; tft = tft->next) {
251  /* All formats should have an ext, but just in case */
252  const char **ext;
253  for (ext = tft->ext; *ext; ext++) {
254  /* If extension matches text name, return the matching tft */
255  if (BLI_strcasecmp(text_ext, *ext) == 0) {
256  return true;
257  }
258  }
259  }
260 
261  /* The filename has a non-numerical extension that we could not highlight. */
262  return false;
263 }
#define BLI_assert(a)
Definition: BLI_assert.h:58
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
const char * BLI_path_extension(const char *filepath) ATTR_NONNULL()
Definition: path_util.c:1699
int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:666
int BLI_str_utf8_size_safe(const char *p) ATTR_NONNULL()
Definition: string_utf8.c:508
bool BLI_string_is_decimal(const char *string) ATTR_NONNULL()
Definition: string_utils.c:85
unsigned int uint
Definition: BLI_sys_types.h:83
_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
#define str(s)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static unsigned c
Definition: RandGen.cpp:97
int fixedaccum[256]
Definition: text_format.h:29
char fixedbuf[256]
Definition: text_format.h:28
char name[66]
Definition: DNA_ID.h:283
void * first
Definition: DNA_listBase.h:47
struct TextFormatType * next
Definition: text_format.h:59
const char ** ext
Definition: text_format.h:74
char * format
int flatten_string_strlen(FlattenString *fs, const char *str)
Definition: text_format.c:115
static void flatten_string_append(FlattenString *fs, const char *c, int accum, int len)
Definition: text_format.c:39
int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
Definition: text_format.c:71
void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len)
Definition: text_format.c:151
void flatten_string_free(FlattenString *fs)
Definition: text_format.c:104
TextFormatType * ED_text_format_get(Text *text)
Definition: text_format.c:200
static ListBase tft_lb
Definition: text_format.c:194
void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
Definition: text_format.c:177
bool ED_text_is_syntax_highlight_supported(Text *text)
Definition: text_format.c:230
void ED_text_format_register(TextFormatType *tft)
Definition: text_format.c:195
int text_check_format_len(TextLine *line, uint len)
Definition: text_format.c:124
uint len