EE445M RTOS
Taken at the University of Texas Spring 2015
utstring.h File Reference
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
Include dependency graph for utstring.h:

Go to the source code of this file.

Data Structures

struct  UT_string
 

Macros

#define UTSTRING_VERSION   1.9.9
 
#define _UNUSED_
 
#define oom()   exit(-1)
 
#define utstring_reserve(s, amt)
 
#define utstring_init(s)
 
#define utstring_done(s)
 
#define utstring_free(s)
 
#define utstring_new(s)
 
#define utstring_renew(s)
 
#define utstring_clear(s)
 
#define utstring_bincpy(s, b, l)
 
#define utstring_concat(dst, src)
 
#define utstring_len(s)   ((unsigned)((s)->i))
 
#define utstring_body(s)   ((s)->d)
 

Functions

static void utstring_printf_va (UT_string *s, const char *fmt, va_list ap)
 
static void utstring_printf (UT_string *s, const char *fmt,...)
 
static void _utstring_BuildTable (const char *P_Needle, size_t P_NeedleLen, long *P_KMP_Table)
 
static void _utstring_BuildTableR (const char *P_Needle, size_t P_NeedleLen, long *P_KMP_Table)
 
static long _utstring_find (const char *P_Haystack, size_t P_HaystackLen, const char *P_Needle, size_t P_NeedleLen, long *P_KMP_Table)
 
static long _utstring_findR (const char *P_Haystack, size_t P_HaystackLen, const char *P_Needle, size_t P_NeedleLen, long *P_KMP_Table)
 
static long utstring_find (UT_string *s, long P_StartPosition, const char *P_Needle, size_t P_NeedleLen)
 
static long utstring_findR (UT_string *s, long P_StartPosition, const char *P_Needle, size_t P_NeedleLen)
 

Macro Definition Documentation

#define _UNUSED_

Definition at line 34 of file utstring.h.

#define oom ( )    exit(-1)

Definition at line 41 of file utstring.h.

#define utstring_bincpy (   s,
  b,
 
)
Value:
do { \
utstring_reserve((s),(l)+1); \
if (l) memcpy(&(s)->d[(s)->i], b, l); \
(s)->i += l; \
(s)->d[(s)->i]='\0'; \
} while(0)
void * memcpy(void *str1, const void *str2, long n)
Definition: nexus.c:25
#define utstring_reserve(s, amt)
Definition: utstring.h:49

Definition at line 99 of file utstring.h.

#define utstring_body (   s)    ((s)->d)

Definition at line 117 of file utstring.h.

#define utstring_clear (   s)
Value:
do { \
(s)->i = 0; \
(s)->d[0] = '\0'; \
} while(0)

Definition at line 93 of file utstring.h.

#define utstring_concat (   dst,
  src 
)
Value:
do { \
utstring_reserve((dst),((src)->i)+1); \
if ((src)->i) memcpy(&(dst)->d[(dst)->i], (src)->d, (src)->i); \
(dst)->i += (src)->i; \
(dst)->d[(dst)->i]='\0'; \
} while(0)
void * memcpy(void *str1, const void *str2, long n)
Definition: nexus.c:25
#define utstring_reserve(s, amt)
Definition: utstring.h:49

Definition at line 107 of file utstring.h.

#define utstring_done (   s)
Value:
do { \
if ((s)->d != NULL) free((s)->d); \
(s)->n = 0; \
} while(0)
#define NULL
Definition: defines.h:32

Definition at line 65 of file utstring.h.

#define utstring_free (   s)
Value:
do { \
free(s); \
} while(0)
#define utstring_done(s)
Definition: utstring.h:65

Definition at line 71 of file utstring.h.

#define utstring_init (   s)
Value:
do { \
(s)->n = 0; (s)->i = 0; (s)->d = NULL; \
(s)->d[0] = '\0'; \
} while(0)
#define utstring_reserve(s, amt)
Definition: utstring.h:49
#define NULL
Definition: defines.h:32

Definition at line 58 of file utstring.h.

#define utstring_len (   s)    ((unsigned)((s)->i))

Definition at line 115 of file utstring.h.

#define utstring_new (   s)
Value:
do { \
s = (UT_string*)calloc(sizeof(UT_string),1); \
if (!s) oom(); \
} while(0)
#define oom()
Definition: utstring.h:41
#define utstring_init(s)
Definition: utstring.h:58

Definition at line 77 of file utstring.h.

#define utstring_renew (   s)
Value:
do { \
if (s) { \
} else { \
} \
} while(0)
#define utstring_new(s)
Definition: utstring.h:77
#define utstring_clear(s)
Definition: utstring.h:93

Definition at line 84 of file utstring.h.

#define utstring_reserve (   s,
  amt 
)
Value:
do { \
if (((s)->n - (s)->i) < (size_t)(amt)) { \
(s)->d = (char*)realloc((s)->d, (s)->n + amt); \
if ((s)->d == NULL) oom(); \
(s)->n += amt; \
} \
} while(0)
#define oom()
Definition: utstring.h:41
#define NULL
Definition: defines.h:32

Definition at line 49 of file utstring.h.

Referenced by utstring_printf_va().

#define UTSTRING_VERSION   1.9.9

Definition at line 29 of file utstring.h.

Function Documentation

static void _utstring_BuildTable ( const char *  P_Needle,
size_t  P_NeedleLen,
long *  P_KMP_Table 
)
static

Definition at line 157 of file utstring.h.

Referenced by utstring_find().

161 {
162  long i, j;
163 
164  i = 0;
165  j = i - 1;
166  P_KMP_Table[i] = j;
167  while (i < (long) P_NeedleLen)
168  {
169  while ( (j > -1) && (P_Needle[i] != P_Needle[j]) )
170  {
171  j = P_KMP_Table[j];
172  }
173  i++;
174  j++;
175  if (i < (long) P_NeedleLen)
176  {
177  if (P_Needle[i] == P_Needle[j])
178  {
179  P_KMP_Table[i] = P_KMP_Table[j];
180  }
181  else
182  {
183  P_KMP_Table[i] = j;
184  }
185  }
186  else
187  {
188  P_KMP_Table[i] = j;
189  }
190  }
191 
192  return;
193 }

Here is the caller graph for this function:

static void _utstring_BuildTableR ( const char *  P_Needle,
size_t  P_NeedleLen,
long *  P_KMP_Table 
)
static

Definition at line 197 of file utstring.h.

Referenced by utstring_findR().

201 {
202  long i, j;
203 
204  i = P_NeedleLen - 1;
205  j = i + 1;
206  P_KMP_Table[i + 1] = j;
207  while (i >= 0)
208  {
209  while ( (j < (long) P_NeedleLen) && (P_Needle[i] != P_Needle[j]) )
210  {
211  j = P_KMP_Table[j + 1];
212  }
213  i--;
214  j--;
215  if (i >= 0)
216  {
217  if (P_Needle[i] == P_Needle[j])
218  {
219  P_KMP_Table[i + 1] = P_KMP_Table[j + 1];
220  }
221  else
222  {
223  P_KMP_Table[i + 1] = j;
224  }
225  }
226  else
227  {
228  P_KMP_Table[i + 1] = j;
229  }
230  }
231 
232  return;
233 }

Here is the caller graph for this function:

static long _utstring_find ( const char *  P_Haystack,
size_t  P_HaystackLen,
const char *  P_Needle,
size_t  P_NeedleLen,
long *  P_KMP_Table 
)
static

Definition at line 237 of file utstring.h.

Referenced by utstring_find().

243 {
244  long i, j;
245  long V_FindPosition = -1;
246 
247  /* Search from left to right. */
248  i = j = 0;
249  while ( (j < (int)P_HaystackLen) && (((P_HaystackLen - j) + i) >= P_NeedleLen) )
250  {
251  while ( (i > -1) && (P_Needle[i] != P_Haystack[j]) )
252  {
253  i = P_KMP_Table[i];
254  }
255  i++;
256  j++;
257  if (i >= (int)P_NeedleLen)
258  {
259  /* Found. */
260  V_FindPosition = j - i;
261  break;
262  }
263  }
264 
265  return V_FindPosition;
266 }

Here is the caller graph for this function:

static long _utstring_findR ( const char *  P_Haystack,
size_t  P_HaystackLen,
const char *  P_Needle,
size_t  P_NeedleLen,
long *  P_KMP_Table 
)
static

Definition at line 270 of file utstring.h.

Referenced by utstring_findR().

276 {
277  long i, j;
278  long V_FindPosition = -1;
279 
280  /* Search from right to left. */
281  j = (P_HaystackLen - 1);
282  i = (P_NeedleLen - 1);
283  while ( (j >= 0) && (j >= i) )
284  {
285  while ( (i < (int)P_NeedleLen) && (P_Needle[i] != P_Haystack[j]) )
286  {
287  i = P_KMP_Table[i + 1];
288  }
289  i--;
290  j--;
291  if (i < 0)
292  {
293  /* Found. */
294  V_FindPosition = j + 1;
295  break;
296  }
297  }
298 
299  return V_FindPosition;
300 }

Here is the caller graph for this function:

static long utstring_find ( UT_string s,
long  P_StartPosition,
const char *  P_Needle,
size_t  P_NeedleLen 
)
static

Definition at line 304 of file utstring.h.

References _utstring_BuildTable(), _utstring_find(), UT_string::d, UT_string::i, and NULL.

309 {
310  long V_StartPosition;
311  long V_HaystackLen;
312  long *V_KMP_Table;
313  long V_FindPosition = -1;
314 
315  if (P_StartPosition < 0)
316  {
317  V_StartPosition = s->i + P_StartPosition;
318  }
319  else
320  {
321  V_StartPosition = P_StartPosition;
322  }
323  V_HaystackLen = s->i - V_StartPosition;
324  if ( (V_HaystackLen >= (long) P_NeedleLen) && (P_NeedleLen > 0) )
325  {
326  V_KMP_Table = (long *)malloc(sizeof(long) * (P_NeedleLen + 1));
327  if (V_KMP_Table != NULL)
328  {
329  _utstring_BuildTable(P_Needle, P_NeedleLen, V_KMP_Table);
330 
331  V_FindPosition = _utstring_find(s->d + V_StartPosition,
332  V_HaystackLen,
333  P_Needle,
334  P_NeedleLen,
335  V_KMP_Table);
336  if (V_FindPosition >= 0)
337  {
338  V_FindPosition += V_StartPosition;
339  }
340 
341  free(V_KMP_Table);
342  }
343  }
344 
345  return V_FindPosition;
346 }
static long _utstring_find(const char *P_Haystack, size_t P_HaystackLen, const char *P_Needle, size_t P_NeedleLen, long *P_KMP_Table)
Definition: utstring.h:237
static void _utstring_BuildTable(const char *P_Needle, size_t P_NeedleLen, long *P_KMP_Table)
Definition: utstring.h:157
char * d
Definition: utstring.h:44
#define NULL
Definition: defines.h:32
size_t i
Definition: utstring.h:46

Here is the call graph for this function:

static long utstring_findR ( UT_string s,
long  P_StartPosition,
const char *  P_Needle,
size_t  P_NeedleLen 
)
static

Definition at line 350 of file utstring.h.

References _utstring_BuildTableR(), _utstring_findR(), UT_string::d, UT_string::i, and NULL.

355 {
356  long V_StartPosition;
357  long V_HaystackLen;
358  long *V_KMP_Table;
359  long V_FindPosition = -1;
360 
361  if (P_StartPosition < 0)
362  {
363  V_StartPosition = s->i + P_StartPosition;
364  }
365  else
366  {
367  V_StartPosition = P_StartPosition;
368  }
369  V_HaystackLen = V_StartPosition + 1;
370  if ( (V_HaystackLen >= (long) P_NeedleLen) && (P_NeedleLen > 0) )
371  {
372  V_KMP_Table = (long *)malloc(sizeof(long) * (P_NeedleLen + 1));
373  if (V_KMP_Table != NULL)
374  {
375  _utstring_BuildTableR(P_Needle, P_NeedleLen, V_KMP_Table);
376 
377  V_FindPosition = _utstring_findR(s->d,
378  V_HaystackLen,
379  P_Needle,
380  P_NeedleLen,
381  V_KMP_Table);
382 
383  free(V_KMP_Table);
384  }
385  }
386 
387  return V_FindPosition;
388 }
static long _utstring_findR(const char *P_Haystack, size_t P_HaystackLen, const char *P_Needle, size_t P_NeedleLen, long *P_KMP_Table)
Definition: utstring.h:270
static void _utstring_BuildTableR(const char *P_Needle, size_t P_NeedleLen, long *P_KMP_Table)
Definition: utstring.h:197
char * d
Definition: utstring.h:44
#define NULL
Definition: defines.h:32
size_t i
Definition: utstring.h:46

Here is the call graph for this function:

static void utstring_printf ( UT_string s,
const char *  fmt,
  ... 
)
static

Definition at line 146 of file utstring.h.

References utstring_printf_va().

146  {
147  va_list ap;
148  va_start(ap,fmt);
149  utstring_printf_va(s,fmt,ap);
150  va_end(ap);
151 }
static void utstring_printf_va(UT_string *s, const char *fmt, va_list ap)
Definition: utstring.h:119

Here is the call graph for this function:

static void utstring_printf_va ( UT_string s,
const char *  fmt,
va_list  ap 
)
static

Definition at line 119 of file utstring.h.

References UT_string::d, UT_string::i, UT_string::n, and utstring_reserve.

Referenced by utstring_printf().

119  {
120  int n;
121  va_list cp;
122  while (1) {
123 #ifdef _WIN32
124  cp = ap;
125 #else
126  va_copy(cp, ap);
127 #endif
128  n = vsnprintf (&s->d[s->i], s->n-s->i, fmt, cp);
129  va_end(cp);
130 
131  if ((n > -1) && ((size_t) n < (s->n-s->i))) {
132  s->i += n;
133  return;
134  }
135 
136  /* Else try again with more space. */
137  if (n > -1) utstring_reserve(s,n+1); /* exact */
138  else utstring_reserve(s,(s->n)*2); /* 2x */
139  }
140 }
size_t n
Definition: utstring.h:45
char * d
Definition: utstring.h:44
#define utstring_reserve(s, amt)
Definition: utstring.h:49
size_t i
Definition: utstring.h:46

Here is the caller graph for this function: