Mercurial > hg > nnchat
comparison th_string.c @ 126:9ae3d87a686f
Sync.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 29 Oct 2010 19:54:42 +0300 |
parents | fe4d5f3b486c |
children | f741718d13c5 |
comparison
equal
deleted
inserted
replaced
125:d03ebefb92a6 | 126:9ae3d87a686f |
---|---|
7 */ | 7 */ |
8 #ifdef HAVE_CONFIG_H | 8 #ifdef HAVE_CONFIG_H |
9 #include "config.h" | 9 #include "config.h" |
10 #endif | 10 #endif |
11 #include "th_string.h" | 11 #include "th_string.h" |
12 | |
13 #define LPREV (pNode->pPrev) | |
14 #define LNEXT (pNode->pNext) | |
15 | |
16 | 12 |
17 /* strdup with a NULL check | 13 /* strdup with a NULL check |
18 */ | 14 */ |
19 char *th_strdup(const char *s) | 15 char *th_strdup(const char *s) |
20 { | 16 { |
56 | 52 |
57 return dst; | 53 return dst; |
58 } | 54 } |
59 | 55 |
60 | 56 |
61 #ifdef STRDUP_PRINTF | 57 /* Simulate a sprintf() that allocates memory |
62 /* | 58 */ |
63 */ | 59 char * th_strdup_vprintf(const char *fmt, va_list args) |
64 enum { | 60 { |
65 TH_PAD_RIGHT = 1, | 61 ssize_t size = 100; |
66 TH_PAD_ZERO = 2 | 62 char *buf, *nbuf = NULL; |
67 }; | 63 |
68 | 64 if ((buf = th_malloc(size)) == NULL) |
69 | 65 return NULL; |
70 static size_t th_printbuf_str(char **buf, const char *str, size_t width, int flags) | |
71 { | |
72 size_t len = strlen(str); | |
73 char *out = (buf != NULL) ? *buf : NULL; | |
74 char pad = ' '; | |
75 | |
76 if (width > 0) { | |
77 width = (len >= width) ? 0 : width - len; | |
78 if (flags & TH_PAD_ZERO) | |
79 pad = '0'; | |
80 } | |
81 | |
82 if ((flags & TH_PAD_RIGHT) == 0 && out != NULL) { | |
83 while (width-- > 0) | |
84 *out++ = pad; | |
85 } | |
86 | |
87 if (out != NULL) { | |
88 while (*str) | |
89 *out++ = *str++; | |
90 } | |
91 | |
92 if (flags & TH_PAD_RIGHT && out != NULL) { | |
93 while (width-- > 0) | |
94 *out++ = pad; | |
95 } | |
96 | |
97 if (buf != NULL) | |
98 *buf = out; | |
99 | |
100 return len + width; | |
101 } | |
102 | |
103 #define TH_INTBUF_LEN (32) | |
104 | |
105 static size_t th_printbuf_int(char **buf, int i, int b, int sg, int width, int pad, int letbase) | |
106 { | |
107 char tmpbuf[TH_INTBUF_LEN], *s; | |
108 int t, neg = 0, pc = 0; | |
109 unsigned int u = i; | |
110 | |
111 if (i == 0) { | |
112 tmpbuf[0] = '0'; | |
113 tmpbuf[1] = 0; | |
114 return th_printbuf_str(buf, tmpbuf, width, pad); | |
115 } | |
116 | |
117 if (sg && b == 10 && i < 0) { | |
118 neg = 1; | |
119 u = -i; | |
120 } | |
121 | 66 |
122 s = tmpbuf + TH_INTBUF_LEN - 1; | 67 while (1) { |
123 *s = 0; | 68 ssize_t n = vsnprintf(buf, size, fmt, args); |
124 | 69 if (n > -1 && n < size) |
125 while (u) { | 70 return buf; |
126 t = u % b; | 71 if (n > -1) |
127 if (t >= 10) | 72 size = n + 1; |
128 t += letbase - '0' - 10; | 73 else |
74 size *= 2; | |
129 | 75 |
130 *--s = t + '0'; | 76 if ((nbuf = th_realloc(nbuf, size)) == NULL) { |
131 u /= b; | 77 th_free(buf); |
132 } | 78 return NULL; |
133 | |
134 if (neg) { | |
135 if (width && (pad & PAD_ZERO)) { | |
136 printchar (out, '-'); | |
137 ++pc; | |
138 --width; | |
139 } else { | |
140 *--s = '-'; | |
141 } | 79 } |
142 } | 80 |
143 | 81 buf = nbuf; |
144 return pc + th_printbuf_str(buf, s, width, pad); | 82 } |
145 } | |
146 | |
147 | |
148 char * th_strdup_vprintf(const char *fmt, va_list args) | |
149 { | |
150 const char *s = fmt; | |
151 char *res; | |
152 size_t len = 0; | |
153 | |
154 /* 1. Determine required space for final string */ | |
155 while (*s) { | |
156 if (*s == '%') { | |
157 s++; | |
158 } else { | |
159 s++; | |
160 len++; | |
161 } | |
162 } | |
163 | |
164 /* 2. Allocate space */ | |
165 | |
166 /* 3. Create final string */ | |
167 | |
168 return res; | |
169 } | 83 } |
170 | 84 |
171 | 85 |
172 char * th_strdup_printf(const char *fmt, ...) | 86 char * th_strdup_printf(const char *fmt, ...) |
173 { | 87 { |
178 res = th_strdup_vprintf(fmt, ap); | 92 res = th_strdup_vprintf(fmt, ap); |
179 va_end(ap); | 93 va_end(ap); |
180 | 94 |
181 return res; | 95 return res; |
182 } | 96 } |
183 #endif | 97 |
184 | 98 |
185 /* Compare two strings ignoring case [strcasecmp, strncasecmp] | 99 /* Compare two strings ignoring case [strcasecmp, strncasecmp] |
186 */ | 100 */ |
187 int th_strcasecmp(const char * str1, const char * str2) | 101 int th_strcasecmp(const char * str1, const char * str2) |
188 { | 102 { |