2 * Copyright 2001-2004 Brandon Long
5 * ClearSilver Templating System
7 * This code is made available under the terms of the ClearSilver License.
8 * http://www.clearsilver.net/license.hdf
12 #include "cs_config.h"
34 # define va_copy(dest,src) __va_copy(dest,src)
36 # define va_copy(dest,src) ((dest) = (src))
40 char *neos_strip (char *s
)
45 while (x
>=0 && isspace(s
[x
])) s
[x
--] = '\0';
47 while (*s
&& isspace(*s
)) s
++;
53 void string_init (NEOSTRING
*str
)
60 void string_clear (NEOSTRING
*str
)
67 static NEOERR
* string_check_length (NEOSTRING
*str
, int l
)
75 str
->buf
= (char *) malloc (sizeof(char) * str
->max
);
77 return nerr_raise (NERR_NOMEM
, "Unable to allocate render buf of size %d",
79 /* ne_warn("Creating string %x at %d (%5.2fK)", str, str->max, (str->max / 1024.0)); */
81 else if (str
->len
+ l
>= str
->max
)
84 int new_max
= str
->max
;
88 } while (str
->len
+ l
>= new_max
);
89 new_ptr
= realloc (str
->buf
, sizeof(char) * new_max
);
90 if (new_ptr
== NULL
) {
91 return nerr_raise (NERR_NOMEM
, "Unable to allocate NEOSTRING buf of size %d",
94 str
->buf
= (char *) new_ptr
;
96 /* ne_warn("Growing string %x to %d (%5.2fK)", str, str->max, (str->max / 1024.0)); */
101 NEOERR
*string_append (NEOSTRING
*str
, const char *buf
)
107 err
= string_check_length (str
, l
);
108 if (err
!= STATUS_OK
) return nerr_pass (err
);
109 strcpy(str
->buf
+ str
->len
, buf
);
115 NEOERR
*string_appendn (NEOSTRING
*str
, const char *buf
, int l
)
119 err
= string_check_length (str
, l
+1);
120 if (err
!= STATUS_OK
) return nerr_pass (err
);
121 memcpy(str
->buf
+ str
->len
, buf
, l
);
123 str
->buf
[str
->len
] = '\0';
128 /* this is much more efficient with C99 snprintfs... */
129 NEOERR
*string_appendvf (NEOSTRING
*str
, const char *fmt
, va_list ap
)
137 /* determine length */
139 bl
= vsnprintf (buf
, size
, fmt
, tmp
);
140 if (bl
> -1 && bl
< size
)
141 return string_appendn (str
, buf
, bl
);
143 /* Handle non-C99 snprintfs (requires extra malloc/free and copy) */
149 a_buf
= vnsprintf_alloc(size
*2, fmt
, tmp
);
151 return nerr_raise(NERR_NOMEM
,
152 "Unable to allocate memory for formatted string");
153 err
= string_append(str
, a_buf
);
155 return nerr_pass(err
);
158 err
= string_check_length (str
, bl
+1);
159 if (err
!= STATUS_OK
) return nerr_pass (err
);
161 vsprintf (str
->buf
+ str
->len
, fmt
, tmp
);
163 str
->buf
[str
->len
] = '\0';
168 NEOERR
*string_appendf (NEOSTRING
*str
, const char *fmt
, ...)
174 err
= string_appendvf (str
, fmt
, ap
);
176 return nerr_pass(err
);
179 NEOERR
*string_append_char (NEOSTRING
*str
, char c
)
182 err
= string_check_length (str
, 1);
183 if (err
!= STATUS_OK
) return nerr_pass (err
);
184 str
->buf
[str
->len
] = c
;
185 str
->buf
[str
->len
+ 1] = '\0';
191 /* Mostly used by vprintf_alloc for non-C99 compliant snprintfs,
192 * this is like vsprintf_alloc except it takes a "suggested" size */
193 int vnisprintf_alloc (char **buf
, int start_size
, const char *fmt
, va_list ap
)
201 *buf
= (char *) malloc (size
* sizeof(char));
202 if (*buf
== NULL
) return 0;
207 bl
= vsnprintf (*buf
, size
, fmt
, tmp
);
208 if (bl
> -1 && bl
< size
)
214 new_ptr
= realloc (*buf
, size
* sizeof(char));
215 if (new_ptr
== NULL
) {
220 *buf
= (char *) new_ptr
;
224 char *vnsprintf_alloc (int start_size
, const char *fmt
, va_list ap
)
227 vnisprintf_alloc(&r
, start_size
, fmt
, ap
);
231 /* This works better with a C99 compliant vsnprintf, but should work ok
232 * with versions that return a -1 if it overflows the buffer */
233 int visprintf_alloc (char **buf
, const char *fmt
, va_list ap
)
239 /* PPC doesn't like you re-using a va_list... and it might not be
240 * supposed to work at all */
243 size
= sizeof (ibuf
);
244 bl
= vsnprintf (ibuf
, sizeof (ibuf
), fmt
, tmp
);
245 if (bl
> -1 && bl
< size
)
247 *buf
= (char *) calloc(bl
+1, sizeof(char));
248 if (*buf
== NULL
) return 0;
249 strncpy(*buf
, ibuf
, bl
);
258 return vnisprintf_alloc(buf
, size
, fmt
, ap
);
261 char *vsprintf_alloc (const char *fmt
, va_list ap
)
264 visprintf_alloc(&r
, fmt
, ap
);
268 char *repr_string_alloc (const char *s
)
276 return strdup("NULL");
280 for (x
= 0; x
< l
; x
++)
282 if (isprint(s
[x
]) && s
[x
] != '"' && s
[x
] != '\\')
288 if (s
[x
] == '\n' || s
[x
] == '\t' || s
[x
] == '\r' || s
[x
] == '"' ||
297 rs
= (char *) malloc ((nl
+3) * sizeof(char));
303 for (x
= 0; x
< l
; x
++)
305 if (isprint(s
[x
]) && s
[x
] != '"' && s
[x
] != '\\')
330 sprintf(&(rs
[i
]), "%03o", (s
[x
] & 0377));