PX4 Firmware
PX4 Autopilot Software http://px4.io
utstring.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2008-2012, Troy D. Hanson http://uthash.sourceforge.net
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
13 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
15 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 /* a dynamic string implementation using macros
25  * see http://uthash.sourceforge.net/utstring
26  */
27 #ifndef UTSTRING_H
28 #define UTSTRING_H
29 
30 #define UTSTRING_VERSION 1.9.6
31 
32 #ifdef __GNUC__
33 #define _UNUSED_ __attribute__ ((__unused__))
34 #else
35 #define _UNUSED_
36 #endif
37 
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #define oom() exit(-1)
42 
43 typedef struct {
44  char *d;
45  size_t n; /* allocd size */
46  size_t i; /* index of first unused byte */
47 } UT_string;
48 
49 #define utstring_reserve(s,amt) \
50 do { \
51  if (((s)->n - (s)->i) < (size_t)(amt)) { \
52  (s)->d = (char*)realloc((s)->d, (s)->n + amt); \
53  if ((s)->d == NULL) oom(); \
54  (s)->n += amt; \
55  } \
56 } while(0)
57 
58 #define utstring_init(s) \
59 do { \
60  (s)->n = 0; (s)->i = 0; (s)->d = NULL; \
61  utstring_reserve(s,100); \
62  (s)->d[0] = '\0'; \
63 } while(0)
64 
65 #define utstring_done(s) \
66 do { \
67  if ((s)->d != NULL) free((s)->d); \
68  (s)->n = 0; \
69 } while(0)
70 
71 #define utstring_free(s) \
72 do { \
73  utstring_done(s); \
74  free(s); \
75 } while(0)
76 
77 #define utstring_new(s) \
78 do { \
79  s = (UT_string*)calloc(sizeof(UT_string),1); \
80  if (!s) oom(); \
81  utstring_init(s); \
82 } while(0)
83 
84 #define utstring_renew(s) \
85 do { \
86  if (s) { \
87  utstring_clear(s); \
88  } else { \
89  utstring_new(s); \
90  } \
91 } while(0)
92 
93 #define utstring_clear(s) \
94 do { \
95  (s)->i = 0; \
96  (s)->d[0] = '\0'; \
97 } while(0)
98 
99 #define utstring_bincpy(s,b,l) \
100 do { \
101  utstring_reserve((s),(l)+1); \
102  if (l) memcpy(&(s)->d[(s)->i], b, l); \
103  (s)->i += l; \
104  (s)->d[(s)->i]='\0'; \
105 } while(0)
106 
107 #define utstring_concat(dst,src) \
108 do { \
109  utstring_reserve((dst),((src)->i)+1); \
110  if ((src)->i) memcpy(&(dst)->d[(dst)->i], (src)->d, (src)->i); \
111  (dst)->i += (src)->i; \
112  (dst)->d[(dst)->i]='\0'; \
113 } while(0)
114 
115 #define utstring_len(s) ((unsigned)((s)->i))
116 
117 #define utstring_body(s) ((s)->d)
118 
119 _UNUSED_ static void utstring_printf_va(UT_string *s, const char *fmt, va_list ap) {
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) && (n < (int)(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 }
141 _UNUSED_ static void utstring_printf(UT_string *s, const char *fmt, ...) {
142  va_list ap;
143  va_start(ap,fmt);
144  utstring_printf_va(s,fmt,ap);
145  va_end(ap);
146 }
147 
148 #endif /* UTSTRING_H */
static _UNUSED_ void utstring_printf(UT_string *s, const char *fmt,...)
Definition: utstring.h:141
size_t n
Definition: utstring.h:45
#define _UNUSED_
Definition: utstring.h:35
static _UNUSED_ void utstring_printf_va(UT_string *s, const char *fmt, va_list ap)
Definition: utstring.h:119
char * d
Definition: utstring.h:44
#define utstring_reserve(s, amt)
Definition: utstring.h:49
size_t i
Definition: utstring.h:46