aboutsummaryrefslogtreecommitdiff
path: root/src/bstrlib.c
blob: 1b19dbecd07705b8b73010c358c84ac6ac00bb4e (plain)
  1. /*
  2.  * This source file is part of the bstring string library. This code was
  3. * written by Paul Hsieh in 2002-2010, and is covered by either the 3-clause
  4. * BSD open source license or GPL v2.0. Refer to the accompanying documentation
  5. * for details on usage and license.
  6. */
  7. /*
  8. * bstrlib.c
  9. *
  10. * This file is the core module for implementing the bstring functions.
  11. */
  12. #if defined (_MSC_VER)
  13. /* These warnings from MSVC++ are totally pointless. */
  14. # define _CRT_SECURE_NO_WARNINGS
  15. #endif
  16. #include <stdio.h>
  17. #include <stddef.h>
  18. #include <stdarg.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include "bstrlib.h"
  23. /* Optionally include a mechanism for debugging memory */
  24. #if defined(MEMORY_DEBUG) || defined(BSTRLIB_MEMORY_DEBUG)
  25. #include "memdbg.h"
  26. #endif
  27. #ifndef bstr__alloc
  28. #define bstr__alloc(x) malloc (x)
  29. #endif
  30. #ifndef bstr__free
  31. #define bstr__free(p) free (p)
  32. #endif
  33. #ifndef bstr__realloc
  34. #define bstr__realloc(p,x) realloc ((p), (x))
  35. #endif
  36. #ifndef bstr__memcpy
  37. #define bstr__memcpy(d,s,l) memcpy ((d), (s), (l))
  38. #endif
  39. #ifndef bstr__memmove
  40. #define bstr__memmove(d,s,l) memmove ((d), (s), (l))
  41. #endif
  42. #ifndef bstr__memset
  43. #define bstr__memset(d,c,l) memset ((d), (c), (l))
  44. #endif
  45. #ifndef bstr__memcmp
  46. #define bstr__memcmp(d,c,l) memcmp ((d), (c), (l))
  47. #endif
  48. #ifndef bstr__memchr
  49. #define bstr__memchr(s,c,l) memchr ((s), (c), (l))
  50. #endif
  51. /* Just a length safe wrapper for memmove. */
  52. #define bBlockCopy(D,S,L) { if ((L) > 0) bstr__memmove ((D),(S),(L)); }
  53. /* Compute the snapped size for a given requested size. By snapping to powers
  54. of 2 like this, repeated reallocations are avoided. */
  55. static int snapUpSize (int i) {
  56. if (i < 8) {
  57. i = 8;
  58. } else {
  59. unsigned int j;
  60. j = (unsigned int) i;
  61. j |= (j >> 1);
  62. j |= (j >> 2);
  63. j |= (j >> 4);
  64. j |= (j >> 8); /* Ok, since int >= 16 bits */
  65. #if (UINT_MAX != 0xffff)
  66. j |= (j >> 16); /* For 32 bit int systems */
  67. #if (UINT_MAX > 0xffffffffUL)
  68. j |= (j >> 32); /* For 64 bit int systems */
  69. #endif
  70. #endif
  71. /* Least power of two greater than i */
  72. j++;
  73. if ((int) j >= i) i = (int) j;
  74. }
  75. return i;
  76. }
  77. /* int balloc (bstring b, int len)
  78. *
  79. * Increase the size of the memory backing the bstring b to at least len.
  80. */
  81. int balloc (bstring b, int olen) {
  82. int len;
  83. if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen <= 0 ||
  84. b->mlen < b->slen || olen <= 0) {
  85. return BSTR_ERR;
  86. }
  87. if (olen >= b->mlen) {
  88. unsigned char * x;
  89. if ((len = snapUpSize (olen)) <= b->mlen) return BSTR_OK;
  90. /* Assume probability of a non-moving realloc is 0.125 */
  91. if (7 * b->mlen < 8 * b->slen) {
  92. /* If slen is close to mlen in size then use realloc to reduce
  93. the memory defragmentation */
  94. reallocStrategy:;
  95. x = (unsigned char *) bstr__realloc (b->data, (size_t) len);
  96. if (x == NULL) {
  97. /* Since we failed, try allocating the tighest possible
  98. allocation */
  99. if (NULL == (x = (unsigned char *) bstr__realloc (b->data, (size_t) (len = olen)))) {
  100. return BSTR_ERR;
  101. }
  102. }
  103. } else {
  104. /* If slen is not close to mlen then avoid the penalty of copying
  105. the extra bytes that are allocated, but not considered part of
  106. the string */
  107. if (NULL == (x = (unsigned char *) bstr__alloc ((size_t) len))) {
  108. /* Perhaps there is no available memory for the two
  109. allocations to be in memory at once */
  110. goto reallocStrategy;
  111. } else {
  112. if (b->slen) bstr__memcpy ((char *) x, (char *) b->data, (size_t) b->slen);
  113. bstr__free (b->data);
  114. }
  115. }
  116. b->data = x;
  117. b->mlen = len;
  118. b->data[b->slen] = (unsigned char) '\0';
  119. }
  120. return BSTR_OK;
  121. }
  122. /* int ballocmin (bstring b, int len)
  123. *
  124. * Set the size of the memory backing the bstring b to len or b->slen+1,
  125. * whichever is larger. Note that repeated use of this function can degrade
  126. * performance.
  127. */
  128. int ballocmin (bstring b, int len) {
  129. unsigned char * s;
  130. if (b == NULL || b->data == NULL || (b->slen+1) < 0 || b->mlen <= 0 ||
  131. b->mlen < b->slen || len <= 0) {
  132. return BSTR_ERR;
  133. }
  134. if (len < b->slen + 1) len = b->slen + 1;
  135. if (len != b->mlen) {
  136. s = (unsigned char *) bstr__realloc (b->data, (size_t) len);
  137. if (NULL == s) return BSTR_ERR;
  138. s[b->slen] = (unsigned char) '\0';
  139. b->data = s;
  140. b->mlen = len;
  141. }
  142. return BSTR_OK;
  143. }
  144. /* bstring bfromcstr (const char * str)
  145. *
  146. * Create a bstring which contains the contents of the '\0' terminated char *
  147. * buffer str.
  148. */
  149. bstring bfromcstr (const char * str) {
  150. bstring b;
  151. int i;
  152. size_t j;
  153. if (str == NULL) return NULL;
  154. j = (strlen) (str);
  155. i = snapUpSize ((int) (j + (2 - (j != 0))));
  156. if (i <= (int) j) return NULL;
  157. b = (bstring) bstr__alloc (sizeof (struct tagbstring));
  158. if (NULL == b) return NULL;
  159. b->slen = (int) j;
  160. if (NULL == (b->data = (unsigned char *) bstr__alloc (b->mlen = i))) {
  161. bstr__free (b);
  162. return NULL;
  163. }
  164. bstr__memcpy (b->data, str, j+1);
  165. return b;
  166. }
  167. /* bstring bfromcstralloc (int mlen, const char * str)
  168. *
  169. * Create a bstring which contains the contents of the '\0' terminated char *
  170. * buffer str. The memory buffer backing the string is at least len
  171. * characters in length.
  172. */
  173. bstring bfromcstralloc (int mlen, const char * str) {
  174. bstring b;
  175. int i;
  176. size_t j;
  177. if (str == NULL) return NULL;
  178. j = (strlen) (str);
  179. i = snapUpSize ((int) (j + (2 - (j != 0))));
  180. if (i <= (int) j) return NULL;
  181. b = (bstring) bstr__alloc (sizeof (struct tagbstring));
  182. if (b == NULL) return NULL;
  183. b->slen = (int) j;
  184. if (i < mlen) i = mlen;
  185. if (NULL == (b->data = (unsigned char *) bstr__alloc (b->mlen = i))) {
  186. bstr__free (b);
  187. return NULL;
  188. }
  189. bstr__memcpy (b->data, str, j+1);
  190. return b;
  191. }
  192. /* bstring blk2bstr (const void * blk, int len)
  193. *
  194. * Create a bstring which contains the content of the block blk of length
  195. * len.
  196. */
  197. bstring blk2bstr (const void * blk, int len) {
  198. bstring b;
  199. int i;
  200. if (blk == NULL || len < 0) return NULL;
  201. b = (bstring) bstr__alloc (sizeof (struct tagbstring));
  202. if (b == NULL) return NULL;
  203. b->slen = len;
  204. i = len + (2 - (len != 0));
  205. i = snapUpSize (i);
  206. b->mlen = i;
  207. b->data = (unsigned char *) bstr__alloc ((size_t) b->mlen);
  208. if (b->data == NULL) {
  209. bstr__free (b);
  210. return NULL;
  211. }
  212. if (len > 0) bstr__memcpy (b->data, blk, (size_t) len);
  213. b->data[len] = (unsigned char) '\0';
  214. return b;
  215. }
  216. /* char * bstr2cstr (const_bstring s, char z)
  217. *
  218. * Create a '\0' terminated char * buffer which is equal to the contents of
  219. * the bstring s, except that any contained '\0' characters are converted
  220. * to the character in z. This returned value should be freed with a
  221. * bcstrfree () call, by the calling application.
  222. */
  223. char * bstr2cstr (const_bstring b, char z) {
  224. int i, l;
  225. char * r;
  226. if (b == NULL || b->slen < 0 || b->data == NULL) return NULL;
  227. l = b->slen;
  228. r = (char *) bstr__alloc ((size_t) (l + 1));
  229. if (r == NULL) return r;
  230. for (i=0; i < l; i ++) {
  231. r[i] = (char) ((b->data[i] == '\0') ? z : (char) (b->data[i]));
  232. }
  233. r[l] = (unsigned char) '\0';
  234. return r;
  235. }
  236. /* int bcstrfree (char * s)
  237. *
  238. * Frees a C-string generated by bstr2cstr (). This is normally unnecessary
  239. * since it just wraps a call to bstr__free (), however, if bstr__alloc ()
  240. * and bstr__free () have been redefined as a macros within the bstrlib
  241. * module (via defining them in memdbg.h after defining
  242. * BSTRLIB_MEMORY_DEBUG) with some difference in behaviour from the std
  243. * library functions, then this allows a correct way of freeing the memory
  244. * that allows higher level code to be independent from these macro
  245. * redefinitions.
  246. */
  247. int bcstrfree (char * s) {
  248. if (s) {
  249. bstr__free (s);
  250. return BSTR_OK;
  251. }
  252. return BSTR_ERR;
  253. }
  254. /* int bconcat (bstring b0, const_bstring b1)
  255. *
  256. * Concatenate the bstring b1 to the bstring b0.
  257. */
  258. int bconcat (bstring b0, const_bstring b1) {
  259. int len, d;
  260. bstring aux = (bstring) b1;
  261. if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL) return BSTR_ERR;
  262. d = b0->slen;
  263. len = b1->slen;
  264. if ((d | (b0->mlen - d) | len | (d + len)) < 0) return BSTR_ERR;
  265. if (b0->mlen <= d + len + 1) {
  266. ptrdiff_t pd = b1->data - b0->data;
  267. if (0 <= pd && pd < b0->mlen) {
  268. if (NULL == (aux = bstrcpy (b1))) return BSTR_ERR;
  269. }
  270. if (balloc (b0, d + len + 1) != BSTR_OK) {
  271. if (aux != b1) bdestroy (aux);
  272. return BSTR_ERR;
  273. }
  274. }
  275. bBlockCopy (&b0->data[d], &aux->data[0], (size_t) len);
  276. b0->data[d + len] = (unsigned char) '\0';
  277. b0->slen = d + len;
  278. if (aux != b1) bdestroy (aux);
  279. return BSTR_OK;
  280. }
  281. /* int bconchar (bstring b, char c)
  282. / *
  283. * Concatenate the single character c to the bstring b.
  284. */
  285. int bconchar (bstring b, char c) {
  286. int d;
  287. if (b == NULL) return BSTR_ERR;
  288. d = b->slen;
  289. if ((d | (b->mlen - d)) < 0 || balloc (b, d + 2) != BSTR_OK) return BSTR_ERR;
  290. b->data[d] = (unsigned char) c;
  291. b->data[d + 1] = (unsigned char) '\0';
  292. b->slen++;
  293. return BSTR_OK;
  294. }
  295. /* int bcatcstr (bstring b, const char * s)
  296. *
  297. * Concatenate a char * string to a bstring.
  298. */
  299. int bcatcstr (bstring b, const char * s) {
  300. char * d;
  301. int i, l;
  302. if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen < b->slen
  303. || b->mlen <= 0 || s == NULL) return BSTR_ERR;
  304. /* Optimistically concatenate directly */
  305. l = b->mlen - b->slen;
  306. d = (char *) &b->data[b->slen];
  307. for (i=0; i < l; i++) {
  308. if ((*d++ = *s++) == '\0') {
  309. b->slen += i;
  310. return BSTR_OK;
  311. }
  312. }
  313. b->slen += i;
  314. /* Need to explicitely resize and concatenate tail */
  315. return bcatblk (b, (const void *) s, (int) strlen (s));
  316. }
  317. /* int bcatblk (bstring b, const void * s, int len)
  318. *
  319. * Concatenate a fixed length buffer to a bstring.
  320. */
  321. int bcatblk (bstring b, const void * s, int len) {
  322. int nl;
  323. if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen < b->slen
  324. || b->mlen <= 0 || s == NULL || len < 0) return BSTR_ERR;
  325. if (0 > (nl = b->slen + len)) return BSTR_ERR; /* Overflow? */
  326. if (b->mlen <= nl && 0 > balloc (b, nl + 1)) return BSTR_ERR;
  327. bBlockCopy (&b->data[b->slen], s, (size_t) len);
  328. b->slen = nl;
  329. b->data[nl] = (unsigned char) '\0';
  330. return BSTR_OK;
  331. }
  332. /* bstring bstrcpy (const_bstring b)
  333. *
  334. * Create a copy of the bstring b.
  335. */
  336. bstring bstrcpy (const_bstring b) {
  337. bstring b0;
  338. int i,j;
  339. /* Attempted to copy an invalid string? */
  340. if (b == NULL || b->slen < 0 || b->data == NULL) return NULL;
  341. b0 = (bstring) bstr__alloc (sizeof (struct tagbstring));
  342. if (b0 == NULL) {
  343. /* Unable to allocate memory for string header */
  344. return NULL;
  345. }
  346. i = b->slen;
  347. j = snapUpSize (i + 1);
  348. b0->data = (unsigned char *) bstr__alloc (j);
  349. if (b0->data == NULL) {
  350. j = i + 1;
  351. b0->data = (unsigned char *) bstr__alloc (j);
  352. if (b0->data == NULL) {
  353. /* Unable to allocate memory for string data */
  354. bstr__free (b0);
  355. return NULL;
  356. }
  357. }
  358. b0->mlen = j;
  359. b0->slen = i;
  360. if (i) bstr__memcpy ((char *) b0->data, (char *) b->data, i);
  361. b0->data[b0->slen] = (unsigned char) '\0';
  362. return b0;
  363. }
  364. /* int bassign (bstring a, const_bstring b)
  365. *
  366. * Overwrite the string a with the contents of string b.
  367. */
  368. int bassign (bstring a, const_bstring b) {
  369. if (b == NULL || b->data == NULL || b->slen < 0)
  370. return BSTR_ERR;
  371. if (b->slen != 0) {
  372. if (balloc (a, b->slen) != BSTR_OK) return BSTR_ERR;
  373. bstr__memmove (a->data, b->data, b->slen);
  374. } else {
  375. if (a == NULL || a->data == NULL || a->mlen < a->slen ||
  376. a->slen < 0 || a->mlen == 0)
  377. return BSTR_ERR;
  378. }
  379. a->data[b->slen] = (unsigned char) '\0';
  380. a->slen = b->slen;
  381. return BSTR_OK;
  382. }
  383. /* int bassignmidstr (bstring a, const_bstring b, int left, int len)
  384. *
  385. * Overwrite the string a with the middle of contents of string b
  386. * starting from position left and running for a length len. left and
  387. * len are clamped to the ends of b as with the function bmidstr.
  388. */
  389. int bassignmidstr (bstring a, const_bstring b, int left, int len) {
  390. if (b == NULL || b->data == NULL || b->slen < 0)
  391. return BSTR_ERR;
  392. if (left < 0) {
  393. len += left;
  394. left = 0;
  395. }
  396. if (len > b->slen - left) len = b->slen - left;
  397. if (a == NULL || a->data == NULL || a->mlen < a->slen ||
  398. a->slen < 0 || a->mlen == 0)
  399. return BSTR_ERR;
  400. if (len > 0) {
  401. if (balloc (a, len) != BSTR_OK) return BSTR_ERR;
  402. bstr__memmove (a->data, b->data + left, len);
  403. a->slen = len;
  404. } else {
  405. a->slen = 0;
  406. }
  407. a->data[a->slen] = (unsigned char) '\0';
  408. return BSTR_OK;
  409. }
  410. /* int bassigncstr (bstring a, const char * str)
  411. *
  412. * Overwrite the string a with the contents of char * string str. Note that
  413. * the bstring a must be a well defined and writable bstring. If an error
  414. * occurs BSTR_ERR is returned however a may be partially overwritten.
  415. */
  416. int bassigncstr (bstring a, const char * str) {
  417. int i;
  418. size_t len;
  419. if (a == NULL || a->data == NULL || a->mlen < a->slen ||
  420. a->slen < 0 || a->mlen == 0 || NULL == str)
  421. return BSTR_ERR;
  422. for (i=0; i < a->mlen; i++) {
  423. if ('\0' == (a->data[i] = str[i])) {
  424. a->slen = i;
  425. return BSTR_OK;
  426. }
  427. }
  428. a->slen = i;
  429. len = strlen (str + i);
  430. if (len > INT_MAX || i + len + 1 > INT_MAX ||
  431. 0 > balloc (a, (int) (i + len + 1))) return BSTR_ERR;
  432. bBlockCopy (a->data + i, str + i, (size_t) len + 1);
  433. a->slen += (int) len;
  434. return BSTR_OK;
  435. }
  436. /* int bassignblk (bstring a, const void * s, int len)
  437. *
  438. * Overwrite the string a with the contents of the block (s, len). Note that
  439. * the bstring a must be a well defined and writable bstring. If an error
  440. * occurs BSTR_ERR is returned and a is not overwritten.
  441. */
  442. int bassignblk (bstring a, const void * s, int len) {
  443. if (a == NULL || a->data == NULL || a->mlen < a->slen ||
  444. a->slen < 0 || a->mlen == 0 || NULL == s || len + 1 < 1)
  445. return BSTR_ERR;
  446. if (len + 1 > a->mlen && 0 > balloc (a, len + 1)) return BSTR_ERR;
  447. bBlockCopy (a->data, s, (size_t) len);
  448. a->data[len] = (unsigned char) '\0';
  449. a->slen = len;
  450. return BSTR_OK;
  451. }
  452. /* int btrunc (bstring b, int n)
  453. *
  454. * Truncate the bstring to at most n characters.
  455. */
  456. int btrunc (bstring b, int n) {
  457. if (n < 0 || b == NULL || b->data == NULL || b->mlen < b->slen ||
  458. b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
  459. if (b->slen > n) {
  460. b->slen = n;
  461. b->data[n] = (unsigned char) '\0';
  462. }
  463. return BSTR_OK;
  464. }
  465. #define upcase(c) (toupper ((unsigned char) c))
  466. #define downcase(c) (tolower ((unsigned char) c))
  467. #define wspace(c) (isspace ((unsigned char) c))
  468. /* int btoupper (bstring b)
  469. *
  470. * Convert contents of bstring to upper case.
  471. */
  472. int btoupper (bstring b) {
  473. int i, len;
  474. if (b == NULL || b->data == NULL || b->mlen < b->slen ||
  475. b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
  476. for (i=0, len = b->slen; i < len; i++) {
  477. b->data[i] = (unsigned char) upcase (b->data[i]);
  478. }
  479. return BSTR_OK;
  480. }
  481. /* int btolower (bstring b)
  482. *
  483. * Convert contents of bstring to lower case.
  484. */
  485. int btolower (bstring b) {
  486. int i, len;
  487. if (b == NULL || b->data == NULL || b->mlen < b->slen ||
  488. b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
  489. for (i=0, len = b->slen; i < len; i++) {
  490. b->data[i] = (unsigned char) downcase (b->data[i]);
  491. }
  492. return BSTR_OK;
  493. }
  494. /* int bstricmp (const_bstring b0, const_bstring b1)
  495. *
  496. * Compare two strings without differentiating between case. The return
  497. * value is the difference of the values of the characters where the two
  498. * strings first differ after lower case transformation, otherwise 0 is
  499. * returned indicating that the strings are equal. If the lengths are
  500. * different, then a difference from 0 is given, but if the first extra
  501. * character is '\0', then it is taken to be the value UCHAR_MAX+1.
  502. */
  503. int bstricmp (const_bstring b0, const_bstring b1) {
  504. int i, v, n;
  505. if (bdata (b0) == NULL || b0->slen < 0 ||
  506. bdata (b1) == NULL || b1->slen < 0) return SHRT_MIN;
  507. if ((n = b0->slen) > b1->slen) n = b1->slen;
  508. else if (b0->slen == b1->slen && b0->data == b1->data) return BSTR_OK;
  509. for (i = 0; i < n; i ++) {
  510. v = (char) downcase (b0->data[i])
  511. - (char) downcase (b1->data[i]);
  512. if (0 != v) return v;
  513. }
  514. if (b0->slen > n) {
  515. v = (char) downcase (b0->data[n]);
  516. if (v) return v;
  517. return UCHAR_MAX + 1;
  518. }
  519. if (b1->slen > n) {
  520. v = - (char) downcase (b1->data[n]);
  521. if (v) return v;
  522. return - (int) (UCHAR_MAX + 1);
  523. }
  524. return BSTR_OK;
  525. }
  526. /* int bstrnicmp (const_bstring b0, const_bstring b1, int n)
  527. *
  528. * Compare two strings without differentiating between case for at most n
  529. * characters. If the position where the two strings first differ is
  530. * before the nth position, the return value is the difference of the values
  531. * of the characters, otherwise 0 is returned. If the lengths are different
  532. * and less than n characters, then a difference from 0 is given, but if the
  533. * first extra character is '\0', then it is taken to be the value
  534. * UCHAR_MAX+1.
  535. */
  536. int bstrnicmp (const_bstring b0, const_bstring b1, int n) {
  537. int i, v, m;
  538. if (bdata (b0) == NULL || b0->slen < 0 ||
  539. bdata (b1) == NULL || b1->slen < 0 || n < 0) return SHRT_MIN;
  540. m = n;
  541. if (m > b0->slen) m = b0->slen;
  542. if (m > b1->slen) m = b1->slen;
  543. if (b0->data != b1->data) {
  544. for (i = 0; i < m; i ++) {
  545. v = (char) downcase (b0->data[i]);
  546. v -= (char) downcase (b1->data[i]);
  547. if (v != 0) return b0->data[i] - b1->data[i];
  548. }
  549. }
  550. if (n == m || b0->slen == b1->slen) return BSTR_OK;
  551. if (b0->slen > m) {
  552. v = (char) downcase (b0->data[m]);
  553. if (v) return v;
  554. return UCHAR_MAX + 1;
  555. }
  556. v = - (char) downcase (b1->data[m]);
  557. if (v) return v;
  558. return - (int) (UCHAR_MAX + 1);
  559. }
  560. /* int biseqcaseless (const_bstring b0, const_bstring b1)
  561. *
  562. * Compare two strings for equality without differentiating between case.
  563. * If the strings differ other than in case, 0 is returned, if the strings
  564. * are the same, 1 is returned, if there is an error, -1 is returned. If
  565. * the length of the strings are different, this function is O(1). '\0'
  566. * termination characters are not treated in any special way.
  567. */
  568. int biseqcaseless (const_bstring b0, const_bstring b1) {
  569. int i, n;
  570. if (bdata (b0) == NULL || b0->slen < 0 ||
  571. bdata (b1) == NULL || b1->slen < 0) return BSTR_ERR;
  572. if (b0->slen != b1->slen) return BSTR_OK;
  573. if (b0->data == b1->data || b0->slen == 0) return 1;
  574. for (i=0, n=b0->slen; i < n; i++) {
  575. if (b0->data[i] != b1->data[i]) {
  576. unsigned char c = (unsigned char) downcase (b0->data[i]);
  577. if (c != (unsigned char) downcase (b1->data[i])) return 0;
  578. }
  579. }
  580. return 1;
  581. }
  582. /* int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len)
  583. *
  584. * Compare beginning of string b0 with a block of memory of length len
  585. * without differentiating between case for equality. If the beginning of b0
  586. * differs from the memory block other than in case (or if b0 is too short),
  587. * 0 is returned, if the strings are the same, 1 is returned, if there is an
  588. * error, -1 is returned. '\0' characters are not treated in any special
  589. * way.
  590. */
  591. int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len) {
  592. int i;
  593. if (bdata (b0) == NULL || b0->slen < 0 || NULL == blk || len < 0)
  594. return BSTR_ERR;
  595. if (b0->slen < len) return BSTR_OK;
  596. if (b0->data == (const unsigned char *) blk || len == 0) return 1;
  597. for (i = 0; i < len; i ++) {
  598. if (b0->data[i] != ((const unsigned char *) blk)[i]) {
  599. if (downcase (b0->data[i]) !=
  600. downcase (((const unsigned char *) blk)[i])) return 0;
  601. }
  602. }
  603. return 1;
  604. }
  605. /*
  606. * int bltrimws (bstring b)
  607. *
  608. * Delete whitespace contiguous from the left end of the string.
  609. */
  610. int bltrimws (bstring b) {
  611. int i, len;
  612. if (b == NULL || b->data == NULL || b->mlen < b->slen ||
  613. b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
  614. for (len = b->slen, i = 0; i < len; i++) {
  615. if (!wspace (b->data[i])) {
  616. return bdelete (b, 0, i);
  617. }
  618. }
  619. b->data[0] = (unsigned char) '\0';
  620. b->slen = 0;
  621. return BSTR_OK;
  622. }
  623. /*
  624. * int brtrimws (bstring b)
  625. *
  626. * Delete whitespace contiguous from the right end of the string.
  627. */
  628. int brtrimws (bstring b) {
  629. int i;
  630. if (b == NULL || b->data == NULL || b->mlen < b->slen ||
  631. b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
  632. for (i = b->slen - 1; i >= 0; i--) {
  633. if (!wspace (b->data[i])) {
  634. if (b->mlen > i) b->data[i+1] = (unsigned char) '\0';
  635. b->slen = i + 1;
  636. return BSTR_OK;
  637. }
  638. }
  639. b->data[0] = (unsigned char) '\0';
  640. b->slen = 0;
  641. return BSTR_OK;
  642. }
  643. /*
  644. * int btrimws (bstring b)
  645. *
  646. * Delete whitespace contiguous from both ends of the string.
  647. */
  648. int btrimws (bstring b) {
  649. int i, j;
  650. if (b == NULL || b->data == NULL || b->mlen < b->slen ||
  651. b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
  652. for (i = b->slen - 1; i >= 0; i--) {
  653. if (!wspace (b->data[i])) {
  654. if (b->mlen > i) b->data[i+1] = (unsigned char) '\0';
  655. b->slen = i + 1;
  656. for (j = 0; wspace (b->data[j]); j++) {}
  657. return bdelete (b, 0, j);
  658. }
  659. }
  660. b->data[0] = (unsigned char) '\0';
  661. b->slen = 0;
  662. return BSTR_OK;
  663. }
  664. /* int biseq (const_bstring b0, const_bstring b1)
  665. *
  666. * Compare the string b0 and b1. If the strings differ, 0 is returned, if
  667. * the strings are the same, 1 is returned, if there is an error, -1 is
  668. * returned. If the length of the strings are different, this function is
  669. * O(1). '\0' termination characters are not treated in any special way.
  670. */
  671. int biseq (const_bstring b0, const_bstring b1) {
  672. if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL ||
  673. b0->slen < 0 || b1->slen < 0) return BSTR_ERR;
  674. if (b0->slen != b1->slen) return BSTR_OK;
  675. if (b0->data == b1->data || b0->slen == 0) return 1;
  676. return !bstr__memcmp (b0->data, b1->data, b0->slen);
  677. }
  678. /* int bisstemeqblk (const_bstring b0, const void * blk, int len)
  679. *
  680. * Compare beginning of string b0 with a block of memory of length len for
  681. * equality. If the beginning of b0 differs from the memory block (or if b0
  682. * is too short), 0 is returned, if the strings are the same, 1 is returned,
  683. * if there is an error, -1 is returned. '\0' characters are not treated in
  684. * any special way.
  685. */
  686. int bisstemeqblk (const_bstring b0, const void * blk, int len) {
  687. int i;
  688. if (bdata (b0) == NULL || b0->slen < 0 || NULL == blk || len < 0)
  689. return BSTR_ERR;
  690. if (b0->slen < len) return BSTR_OK;
  691. if (b0->data == (const unsigned char *) blk || len == 0) return 1;
  692. for (i = 0; i < len; i ++) {
  693. if (b0->data[i] != ((const unsigned char *) blk)[i]) return BSTR_OK;
  694. }
  695. return 1;
  696. }
  697. /* int biseqcstr (const_bstring b, const char *s)
  698. *
  699. * Compare the bstring b and char * string s. The C string s must be '\0'
  700. * terminated at exactly the length of the bstring b, and the contents
  701. * between the two must be identical with the bstring b with no '\0'
  702. * characters for the two contents to be considered equal. This is
  703. * equivalent to the condition that their current contents will be always be
  704. * equal when comparing them in the same format after converting one or the
  705. * other. If the strings are equal 1 is returned, if they are unequal 0 is
  706. * returned and if there is a detectable error BSTR_ERR is returned.
  707. */
  708. int biseqcstr (const_bstring b, const char * s) {
  709. int i;
  710. if (b == NULL || s == NULL || b->data == NULL || b->slen < 0) return BSTR_ERR;
  711. for (i=0; i < b->slen; i++) {
  712. if (s[i] == '\0' || b->data[i] != (unsigned char) s[i]) return BSTR_OK;
  713. }
  714. return s[i] == '\0';
  715. }
  716. /* int biseqcstrcaseless (const_bstring b, const char *s)
  717. *
  718. * Compare the bstring b and char * string s. The C string s must be '\0'
  719. * terminated at exactly the length of the bstring b, and the contents
  720. * between the two must be identical except for case with the bstring b with
  721. * no '\0' characters for the two contents to be considered equal. This is
  722. * equivalent to the condition that their current contents will be always be
  723. * equal ignoring case when comparing them in the same format after
  724. * converting one or the other. If the strings are equal, except for case,
  725. * 1 is returned, if they are unequal regardless of case 0 is returned and
  726. * if there is a detectable error BSTR_ERR is returned.
  727. */
  728. int biseqcstrcaseless (const_bstring b, const char * s) {
  729. int i;
  730. if (b == NULL || s == NULL || b->data == NULL || b->slen < 0) return BSTR_ERR;
  731. for (i=0; i < b->slen; i++) {
  732. if (s[i] == '\0' ||
  733. (b->data[i] != (unsigned char) s[i] &&
  734. downcase (b->data[i]) != (unsigned char) downcase (s[i])))
  735. return BSTR_OK;
  736. }
  737. return s[i] == '\0';
  738. }
  739. /* int bstrcmp (const_bstring b0, const_bstring b1)
  740. *
  741. * Compare the string b0 and b1. If there is an error, SHRT_MIN is returned,
  742. * otherwise a value less than or greater than zero, indicating that the
  743. * string pointed to by b0 is lexicographically less than or greater than
  744. * the string pointed to by b1 is returned. If the the string lengths are
  745. * unequal but the characters up until the length of the shorter are equal
  746. * then a value less than, or greater than zero, indicating that the string
  747. * pointed to by b0 is shorter or longer than the string pointed to by b1 is
  748. * returned. 0 is returned if and only if the two strings are the same. If
  749. * the length of the strings are different, this function is O(n). Like its
  750. * standard C library counter part strcmp, the comparison does not proceed
  751. * past any '\0' termination characters encountered.
  752. */
  753. int bstrcmp (const_bstring b0, const_bstring b1) {
  754. int i, v, n;
  755. if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL ||
  756. b0->slen < 0 || b1->slen < 0) return SHRT_MIN;
  757. n = b0->slen; if (n > b1->slen) n = b1->slen;
  758. if (b0->slen == b1->slen && (b0->data == b1->data || b0->slen == 0))
  759. return BSTR_OK;
  760. for (i = 0; i < n; i ++) {
  761. v = ((char) b0->data[i]) - ((char) b1->data[i]);
  762. if (v != 0) return v;
  763. if (b0->data[i] == (unsigned char) '\0') return BSTR_OK;
  764. }
  765. if (b0->slen > n) return 1;
  766. if (b1->slen > n) return -1;
  767. return BSTR_OK;
  768. }
  769. /* int bstrncmp (const_bstring b0, const_bstring b1, int n)
  770. *
  771. * Compare the string b0 and b1 for at most n characters. If there is an
  772. * error, SHRT_MIN is returned, otherwise a value is returned as if b0 and
  773. * b1 were first truncated to at most n characters then bstrcmp was called
  774. * with these new strings are paremeters. If the length of the strings are
  775. * different, this function is O(n). Like its standard C library counter
  776. * part strcmp, the comparison does not proceed past any '\0' termination
  777. * characters encountered.
  778. */
  779. int bstrncmp (const_bstring b0, const_bstring b1, int n) {
  780. int i, v, m;
  781. if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL ||
  782. b0->slen < 0 || b1->slen < 0) return SHRT_MIN;
  783. m = n;
  784. if (m > b0->slen) m = b0->slen;
  785. if (m > b1->slen) m = b1->slen;
  786. if (b0->data != b1->data) {
  787. for (i = 0; i < m; i ++) {
  788. v = ((char) b0->data[i]) - ((char) b1->data[i]);
  789. if (v != 0) return v;
  790. if (b0->data[i] == (unsigned char) '\0') return BSTR_OK;
  791. }
  792. }
  793. if (n == m || b0->slen == b1->slen) return BSTR_OK;
  794. if (b0->slen > m) return 1;
  795. return -1;
  796. }
  797. /* bstring bmidstr (const_bstring b, int left, int len)
  798. *
  799. * Create a bstring which is the substring of b starting from position left
  800. * and running for a length len (clamped by the end of the bstring b.) If
  801. * b is detectably invalid, then NULL is returned. The section described
  802. * by (left, len) is clamped to the boundaries of b.
  803. */
  804. bstring bmidstr (const_bstring b, int left, int len) {
  805. if (b == NULL || b->slen < 0 || b->data == NULL) return NULL;
  806. if (left < 0) {
  807. len += left;
  808. left = 0;
  809. }
  810. if (len > b->slen - left) len = b->slen - left;
  811. if (len <= 0) return bfromcstr ("");
  812. return blk2bstr (b->data + left, len);
  813. }
  814. /* int bdelete (bstring b, int pos, int len)
  815. *
  816. * Removes characters from pos to pos+len-1 inclusive and shifts the tail of
  817. * the bstring starting from pos+len to pos. len must be positive for this
  818. * call to have any effect. The section of the string described by (pos,
  819. * len) is clamped to boundaries of the bstring b.
  820. */
  821. int bdelete (bstring b, int pos, int len) {
  822. /* Clamp to left side of bstring */
  823. if (pos < 0) {
  824. len += pos;
  825. pos = 0;
  826. }
  827. if (len < 0 || b == NULL || b->data == NULL || b->slen < 0 ||
  828. b->mlen < b->slen || b->mlen <= 0)
  829. return BSTR_ERR;
  830. if (len > 0 && pos < b->slen) {
  831. if (pos + len >= b->slen) {
  832. b->slen = pos;
  833. } else {
  834. bBlockCopy ((char *) (b->data + pos),
  835. (char *) (b->data + pos + len),
  836. b->slen - (pos+len));
  837. b->slen -= len;
  838. }
  839. b->data[b->slen] = (unsigned char) '\0';
  840. }
  841. return BSTR_OK;
  842. }
  843. /* int bdestroy (bstring b)
  844. *
  845. * Free up the bstring. Note that if b is detectably invalid or not writable
  846. * then no action is performed and BSTR_ERR is returned. Like a freed memory
  847. * allocation, dereferences, writes or any other action on b after it has
  848. * been bdestroyed is undefined.
  849. */
  850. int bdestroy (bstring b) {
  851. if (b == NULL || b->slen < 0 || b->mlen <= 0 || b->mlen < b->slen ||
  852. b->data == NULL)
  853. return BSTR_ERR;
  854. bstr__free (b->data);
  855. /* In case there is any stale usage, there is one more chance to
  856. notice this error. */
  857. b->slen = -1;
  858. b->mlen = -__LINE__;
  859. b->data = NULL;
  860. bstr__free (b);
  861. return BSTR_OK;
  862. }
  863. /* int binstr (const_bstring b1, int pos, const_bstring b2)
  864. *
  865. * Search for the bstring b2 in b1 starting from position pos, and searching
  866. * forward. If it is found then return with the first position where it is
  867. * found, otherwise return BSTR_ERR. Note that this is just a brute force
  868. * string searcher that does not attempt clever things like the Boyer-Moore
  869. * search algorithm. Because of this there are many degenerate cases where
  870. * this can take much longer than it needs to.
  871. */
  872. int binstr (const_bstring b1, int pos, const_bstring b2) {
  873. int j, ii, ll, lf;
  874. unsigned char * d0;
  875. unsigned char c0;
  876. register unsigned char * d1;
  877. register unsigned char c1;
  878. register int i;
  879. if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||
  880. b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;
  881. if (b1->slen == pos) return (b2->slen == 0)?pos:BSTR_ERR;
  882. if (b1->slen < pos || pos < 0) return BSTR_ERR;
  883. if (b2->slen == 0) return pos;
  884. /* No space to find such a string? */
  885. if ((lf = b1->slen - b2->slen + 1) <= pos) return BSTR_ERR;
  886. /* An obvious alias case */
  887. if (b1->data == b2->data && pos == 0) return 0;
  888. i = pos;
  889. d0 = b2->data;
  890. d1 = b1->data;
  891. ll = b2->slen;
  892. /* Peel off the b2->slen == 1 case */
  893. c0 = d0[0];
  894. if (1 == ll) {
  895. for (;i < lf; i++) if (c0 == d1[i]) return i;
  896. return BSTR_ERR;
  897. }
  898. c1 = c0;
  899. j = 0;
  900. lf = b1->slen - 1;
  901. ii = -1;
  902. if (i < lf) do {
  903. /* Unrolled current character test */
  904. if (c1 != d1[i]) {
  905. if (c1 != d1[1+i]) {
  906. i += 2;
  907. continue;
  908. }
  909. i++;
  910. }
  911. /* Take note if this is the start of a potential match */
  912. if (0 == j) ii = i;
  913. /* Shift the test character down by one */
  914. j++;
  915. i++;
  916. /* If this isn't past the last character continue */
  917. if (j < ll) {
  918. c1 = d0[j];
  919. continue;
  920. }
  921. N0:;
  922. /* If no characters mismatched, then we matched */
  923. if (i == ii+j) return ii;
  924. /* Shift back to the beginning */
  925. i -= j;
  926. j = 0;
  927. c1 = c0;
  928. } while (i < lf);
  929. /* Deal with last case if unrolling caused a misalignment */
  930. if (i == lf && ll == j+1 && c1 == d1[i]) goto N0;
  931. return BSTR_ERR;
  932. }
  933. /* int binstrr (const_bstring b1, int pos, const_bstring b2)
  934. *
  935. * Search for the bstring b2 in b1 starting from position pos, and searching
  936. * backward. If it is found then return with the first position where it is
  937. * found, otherwise return BSTR_ERR. Note that this is just a brute force
  938. * string searcher that does not attempt clever things like the Boyer-Moore
  939. * search algorithm. Because of this there are many degenerate cases where
  940. * this can take much longer than it needs to.
  941. */
  942. int binstrr (const_bstring b1, int pos, const_bstring b2) {
  943. int j, i, l;
  944. unsigned char * d0, * d1;
  945. if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||
  946. b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;
  947. if (b1->slen == pos && b2->slen == 0) return pos;
  948. if (b1->slen < pos || pos < 0) return BSTR_ERR;
  949. if (b2->slen == 0) return pos;
  950. /* Obvious alias case */
  951. if (b1->data == b2->data && pos == 0 && b2->slen <= b1->slen) return 0;
  952. i = pos;
  953. if ((l = b1->slen - b2->slen) < 0) return BSTR_ERR;
  954. /* If no space to find such a string then snap back */
  955. if (l + 1 <= i) i = l;
  956. j = 0;
  957. d0 = b2->data;
  958. d1 = b1->data;
  959. l = b2->slen;
  960. for (;;) {
  961. if (d0[j] == d1[i + j]) {
  962. j ++;
  963. if (j >= l) return i;
  964. } else {
  965. i --;
  966. if (i < 0) break;
  967. j=0;
  968. }
  969. }
  970. return BSTR_ERR;
  971. }
  972. /* int binstrcaseless (const_bstring b1, int pos, const_bstring b2)
  973. *
  974. * Search for the bstring b2 in b1 starting from position pos, and searching
  975. * forward but without regard to case. If it is found then return with the
  976. * first position where it is found, otherwise return BSTR_ERR. Note that
  977. * this is just a brute force string searcher that does not attempt clever
  978. * things like the Boyer-Moore search algorithm. Because of this there are
  979. * many degenerate cases where this can take much longer than it needs to.
  980. */
  981. int binstrcaseless (const_bstring b1, int pos, const_bstring b2) {
  982. int j, i, l, ll;
  983. unsigned char * d0, * d1;
  984. if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||
  985. b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;
  986. if (b1->slen == pos) return (b2->slen == 0)?pos:BSTR_ERR;
  987. if (b1->slen < pos || pos < 0) return BSTR_ERR;
  988. if (b2->slen == 0) return pos;
  989. l = b1->slen - b2->slen + 1;
  990. /* No space to find such a string? */
  991. if (l <= pos) return BSTR_ERR;
  992. /* An obvious alias case */
  993. if (b1->data == b2->data && pos == 0) return BSTR_OK;
  994. i = pos;
  995. j = 0;
  996. d0 = b2->data;
  997. d1 = b1->data;
  998. ll = b2->slen;
  999. for (;;) {
  1000. if (d0[j] == d1[i + j] || downcase (d0[j]) == downcase (d1[i + j])) {
  1001. j ++;
  1002. if (j >= ll) return i;
  1003. } else {
  1004. i ++;
  1005. if (i >= l) break;
  1006. j=0;
  1007. }
  1008. }
  1009. return BSTR_ERR;
  1010. }
  1011. /* int binstrrcaseless (const_bstring b1, int pos, const_bstring b2)
  1012. *
  1013. * Search for the bstring b2 in b1 starting from position pos, and searching
  1014. * backward but without regard to case. If it is found then return with the
  1015. * first position where it is found, otherwise return BSTR_ERR. Note that
  1016. * this is just a brute force string searcher that does not attempt clever
  1017. * things like the Boyer-Moore search algorithm. Because of this there are
  1018. * many degenerate cases where this can take much longer than it needs to.
  1019. */
  1020. int binstrrcaseless (const_bstring b1, int pos, const_bstring b2) {
  1021. int j, i, l;
  1022. unsigned char * d0, * d1;
  1023. if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||
  1024. b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;
  1025. if (b1->slen == pos && b2->slen == 0) return pos;
  1026. if (b1->slen < pos || pos < 0) return BSTR_ERR;
  1027. if (b2->slen == 0) return pos;
  1028. /* Obvious alias case */
  1029. if (b1->data == b2->data && pos == 0 && b2->slen <= b1->slen) return BSTR_OK;
  1030. i = pos;
  1031. if ((l = b1->slen - b2->slen) < 0) return BSTR_ERR;
  1032. /* If no space to find such a string then snap back */
  1033. if (l + 1 <= i) i = l;
  1034. j = 0;
  1035. d0 = b2->data;
  1036. d1 = b1->data;
  1037. l = b2->slen;
  1038. for (;;) {
  1039. if (d0[j] == d1[i + j] || downcase (d0[j]) == downcase (d1[i + j])) {
  1040. j ++;
  1041. if (j >= l) return i;
  1042. } else {
  1043. i --;
  1044. if (i < 0) break;
  1045. j=0;
  1046. }
  1047. }
  1048. return BSTR_ERR;
  1049. }
  1050. /* int bstrchrp (const_bstring b, int c, int pos)
  1051. *
  1052. * Search for the character c in b forwards from the position pos
  1053. * (inclusive).
  1054. */
  1055. int bstrchrp (const_bstring b, int c, int pos) {
  1056. unsigned char * p;
  1057. if (b == NULL || b->data == NULL || b->slen <= pos || pos < 0) return BSTR_ERR;
  1058. p = (unsigned char *) bstr__memchr ((b->data + pos), (unsigned char) c, (b->slen - pos));
  1059. if (p) return (int) (p - b->data);
  1060. return BSTR_ERR;
  1061. }
  1062. /* int bstrrchrp (const_bstring b, int c, int pos)
  1063. *
  1064. * Search for the character c in b backwards from the position pos in string
  1065. * (inclusive).
  1066. */
  1067. int bstrrchrp (const_bstring b, int c, int pos) {
  1068. int i;
  1069. if (b == NULL || b->data == NULL || b->slen <= pos || pos < 0) return BSTR_ERR;
  1070. for (i=pos; i >= 0; i--) {
  1071. if (b->data[i] == (unsigned char) c) return i;
  1072. }
  1073. return BSTR_ERR;
  1074. }
  1075. #if !defined (BSTRLIB_AGGRESSIVE_MEMORY_FOR_SPEED_TRADEOFF)
  1076. #define LONG_LOG_BITS_QTY (3)
  1077. #define LONG_BITS_QTY (1 << LONG_LOG_BITS_QTY)
  1078. #define LONG_TYPE unsigned char
  1079. #define CFCLEN ((1 << CHAR_BIT) / LONG_BITS_QTY)
  1080. struct charField { LONG_TYPE content[CFCLEN]; };
  1081. #define testInCharField(cf,c) ((cf)->content[(c) >> LONG_LOG_BITS_QTY] & (((long)1) << ((c) & (LONG_BITS_QTY-1))))
  1082. #define setInCharField(cf,idx) { \
  1083. unsigned int c = (unsigned int) (idx); \
  1084. (cf)->content[c >> LONG_LOG_BITS_QTY] |= (LONG_TYPE) (1ul << (c & (LONG_BITS_QTY-1))); \
  1085. }
  1086. #else
  1087. #define CFCLEN (1 << CHAR_BIT)
  1088. struct charField { unsigned char content[CFCLEN]; };
  1089. #define testInCharField(cf,c) ((cf)->content[(unsigned char) (c)])
  1090. #define setInCharField(cf,idx) (cf)->content[(unsigned int) (idx)] = ~0
  1091. #endif
  1092. /* Convert a bstring to charField */
  1093. static int buildCharField (struct charField * cf, const_bstring b) {
  1094. int i;
  1095. if (b == NULL || b->data == NULL || b->slen <= 0) return BSTR_ERR;
  1096. memset ((void *) cf->content, 0, sizeof (struct charField));
  1097. for (i=0; i < b->slen; i++) {
  1098. setInCharField (cf, b->data[i]);
  1099. }
  1100. return BSTR_OK;
  1101. }
  1102. static void invertCharField (struct charField * cf) {
  1103. int i;
  1104. for (i=0; i < CFCLEN; i++) cf->content[i] = ~cf->content[i];
  1105. }
  1106. /* Inner engine for binchr */
  1107. static int binchrCF (const unsigned char * data, int len, int pos, const struct charField * cf) {
  1108. int i;
  1109. for (i=pos; i < len; i++) {
  1110. unsigned char c = (unsigned char) data[i];
  1111. if (testInCharField (cf, c)) return i;
  1112. }
  1113. return BSTR_ERR;
  1114. }
  1115. /* int binchr (const_bstring b0, int pos, const_bstring b1);
  1116. *
  1117. * Search for the first position in b0 starting from pos or after, in which
  1118. * one of the characters in b1 is found and return it. If such a position
  1119. * does not exist in b0, then BSTR_ERR is returned.
  1120. */
  1121. int binchr (const_bstring b0, int pos, const_bstring b1) {
  1122. struct charField chrs;
  1123. if (pos < 0 || b0 == NULL || b0->data == NULL ||
  1124. b0->slen <= pos) return BSTR_ERR;
  1125. if (1 == b1->slen) return bstrchrp (b0, b1->data[0], pos);
  1126. if (0 > buildCharField (&chrs, b1)) return BSTR_ERR;
  1127. return binchrCF (b0->data, b0->slen, pos, &chrs);
  1128. }
  1129. /* Inner engine for binchrr */
  1130. static int binchrrCF (const unsigned char * data, int pos, const struct charField * cf) {
  1131. int i;
  1132. for (i=pos; i >= 0; i--) {
  1133. unsigned int c = (unsigned int) data[i];
  1134. if (testInCharField (cf, c)) return i;
  1135. }
  1136. return BSTR_ERR;
  1137. }
  1138. /* int binchrr (const_bstring b0, int pos, const_bstring b1);
  1139. *
  1140. * Search for the last position in b0 no greater than pos, in which one of
  1141. * the characters in b1 is found and return it. If such a position does not
  1142. * exist in b0, then BSTR_ERR is returned.
  1143. */
  1144. int binchrr (const_bstring b0, int pos, const_bstring b1) {
  1145. struct charField chrs;
  1146. if (pos < 0 || b0 == NULL || b0->data == NULL || b1 == NULL ||
  1147. b0->slen < pos) return BSTR_ERR;
  1148. if (pos == b0->slen) pos--;
  1149. if (1 == b1->slen) return bstrrchrp (b0, b1->data[0], pos);
  1150. if (0 > buildCharField (&chrs, b1)) return BSTR_ERR;
  1151. return binchrrCF (b0->data, pos, &chrs);
  1152. }
  1153. /* int bninchr (const_bstring b0, int pos, const_bstring b1);
  1154. *
  1155. * Search for the first position in b0 starting from pos or after, in which
  1156. * none of the characters in b1 is found and return it. If such a position
  1157. * does not exist in b0, then BSTR_ERR is returned.
  1158. */
  1159. int bninchr (const_bstring b0, int pos, const_bstring b1) {
  1160. struct charField chrs;
  1161. if (pos < 0 || b0 == NULL || b0->data == NULL ||
  1162. b0->slen <= pos) return BSTR_ERR;
  1163. if (buildCharField (&chrs, b1) < 0) return BSTR_ERR;
  1164. invertCharField (&chrs);
  1165. return binchrCF (b0->data, b0->slen, pos, &chrs);
  1166. }
  1167. /* int bninchrr (const_bstring b0, int pos, const_bstring b1);
  1168. *
  1169. * Search for the last position in b0 no greater than pos, in which none of
  1170. * the characters in b1 is found and return it. If such a position does not
  1171. * exist in b0, then BSTR_ERR is returned.
  1172. */
  1173. int bninchrr (const_bstring b0, int pos, const_bstring b1) {
  1174. struct charField chrs;
  1175. if (pos < 0 || b0 == NULL || b0->data == NULL ||
  1176. b0->slen < pos) return BSTR_ERR;
  1177. if (pos == b0->slen) pos--;
  1178. if (buildCharField (&chrs, b1) < 0) return BSTR_ERR;
  1179. invertCharField (&chrs);
  1180. return binchrrCF (b0->data, pos, &chrs);
  1181. }
  1182. /* int bsetstr (bstring b0, int pos, bstring b1, unsigned char fill)
  1183. *
  1184. * Overwrite the string b0 starting at position pos with the string b1. If
  1185. * the position pos is past the end of b0, then the character "fill" is
  1186. * appended as necessary to make up the gap between the end of b0 and pos.
  1187. * If b1 is NULL, it behaves as if it were a 0-length string.
  1188. */
  1189. int bsetstr (bstring b0, int pos, const_bstring b1, unsigned char fill) {
  1190. int d, newlen;
  1191. ptrdiff_t pd;
  1192. bstring aux = (bstring) b1;
  1193. if (pos < 0 || b0 == NULL || b0->slen < 0 || NULL == b0->data ||
  1194. b0->mlen < b0->slen || b0->mlen <= 0) return BSTR_ERR;
  1195. if (b1 != NULL && (b1->slen < 0 || b1->data == NULL)) return BSTR_ERR;
  1196. d = pos;
  1197. /* Aliasing case */
  1198. if (NULL != aux) {
  1199. if ((pd = (ptrdiff_t) (b1->data - b0->data)) >= 0 && pd < (ptrdiff_t) b0->mlen) {
  1200. if (NULL == (aux = bstrcpy (b1))) return BSTR_ERR;
  1201. }
  1202. d += aux->slen;
  1203. }
  1204. /* Increase memory size if necessary */
  1205. if (balloc (b0, d + 1) != BSTR_OK) {
  1206. if (aux != b1) bdestroy (aux);
  1207. return BSTR_ERR;
  1208. }
  1209. newlen = b0->slen;
  1210. /* Fill in "fill" character as necessary */
  1211. if (pos > newlen) {
  1212. bstr__memset (b0->data + b0->slen, (int) fill, (size_t) (pos - b0->slen));
  1213. newlen = pos;
  1214. }
  1215. /* Copy b1 to position pos in b0. */
  1216. if (aux != NULL) {
  1217. bBlockCopy ((char *) (b0->data + pos), (char *) aux->data, aux->slen);
  1218. if (aux != b1) bdestroy (aux);
  1219. }
  1220. /* Indicate the potentially increased size of b0 */
  1221. if (d > newlen) newlen = d;
  1222. b0->slen = newlen;
  1223. b0->data[newlen] = (unsigned char) '\0';
  1224. return BSTR_OK;
  1225. }
  1226. /* int binsert (bstring b1, int pos, bstring b2, unsigned char fill)
  1227. *
  1228. * Inserts the string b2 into b1 at position pos. If the position pos is
  1229. * past the end of b1, then the character "fill" is appended as necessary to
  1230. * make up the gap between the end of b1 and pos. Unlike bsetstr, binsert
  1231. * does not allow b2 to be NULL.
  1232. */
  1233. int binsert (bstring b1, int pos, const_bstring b2, unsigned char fill) {
  1234. int d, l;
  1235. ptrdiff_t pd;
  1236. bstring aux = (bstring) b2;
  1237. if (pos < 0 || b1 == NULL || b2 == NULL || b1->slen < 0 ||
  1238. b2->slen < 0 || b1->mlen < b1->slen || b1->mlen <= 0) return BSTR_ERR;
  1239. /* Aliasing case */
  1240. if ((pd = (ptrdiff_t) (b2->data - b1->data)) >= 0 && pd < (ptrdiff_t) b1->mlen) {
  1241. if (NULL == (aux = bstrcpy (b2))) return BSTR_ERR;
  1242. }
  1243. /* Compute the two possible end pointers */
  1244. d = b1->slen + aux->slen;
  1245. l = pos + aux->slen;
  1246. if ((d|l) < 0) return BSTR_ERR;
  1247. if (l > d) {
  1248. /* Inserting past the end of the string */
  1249. if (balloc (b1, l + 1) != BSTR_OK) {
  1250. if (aux != b2) bdestroy (aux);
  1251. return BSTR_ERR;
  1252. }
  1253. bstr__memset (b1->data + b1->slen, (int) fill, (size_t) (pos - b1->slen));
  1254. b1->slen = l;
  1255. } else {
  1256. /* Inserting in the middle of the string */
  1257. if (balloc (b1, d + 1) != BSTR_OK) {
  1258. if (aux != b2) bdestroy (aux);
  1259. return BSTR_ERR;
  1260. }
  1261. bBlockCopy (b1->data + l, b1->data + pos, d - l);
  1262. b1->slen = d;
  1263. }
  1264. bBlockCopy (b1->data + pos, aux->data, aux->slen);
  1265. b1->data[b1->slen] = (unsigned char) '\0';
  1266. if (aux != b2) bdestroy (aux);
  1267. return BSTR_OK;
  1268. }
  1269. /* int breplace (bstring b1, int pos, int len, bstring b2,
  1270. * unsigned char fill)
  1271. *
  1272. * Replace a section of a string from pos for a length len with the string b2.
  1273. * fill is used is pos > b1->slen.
  1274. */
  1275. int breplace (bstring b1, int pos, int len, const_bstring b2,
  1276. unsigned char fill) {
  1277. int pl, ret;
  1278. ptrdiff_t pd;
  1279. bstring aux = (bstring) b2;
  1280. if (pos < 0 || len < 0 || (pl = pos + len) < 0 || b1 == NULL ||
  1281. b2 == NULL || b1->data == NULL || b2->data == NULL ||
  1282. b1->slen < 0 || b2->slen < 0 || b1->mlen < b1->slen ||
  1283. b1->mlen <= 0) return BSTR_ERR;
  1284. /* Straddles the end? */
  1285. if (pl >= b1->slen) {
  1286. if ((ret = bsetstr (b1, pos, b2, fill)) < 0) return ret;
  1287. if (pos + b2->slen < b1->slen) {
  1288. b1->slen = pos + b2->slen;
  1289. b1->data[b1->slen] = (unsigned char) '\0';
  1290. }
  1291. return ret;
  1292. }
  1293. /* Aliasing case */
  1294. if ((pd = (ptrdiff_t) (b2->data - b1->data)) >= 0 && pd < (ptrdiff_t) b1->slen) {
  1295. if (NULL == (aux = bstrcpy (b2))) return BSTR_ERR;
  1296. }
  1297. if (aux->slen > len) {
  1298. if (balloc (b1, b1->slen + aux->slen - len) != BSTR_OK) {
  1299. if (aux != b2) bdestroy (aux);
  1300. return BSTR_ERR;
  1301. }
  1302. }
  1303. if (aux->slen != len) bstr__memmove (b1->data + pos + aux->slen, b1->data + pos + len, b1->slen - (pos + len));
  1304. bstr__memcpy (b1->data + pos, aux->data, aux->slen);
  1305. b1->slen += aux->slen - len;
  1306. b1->data[b1->slen] = (unsigned char) '\0';
  1307. if (aux != b2) bdestroy (aux);
  1308. return BSTR_OK;
  1309. }
  1310. /*
  1311. * findreplaceengine is used to implement bfindreplace and
  1312. * bfindreplacecaseless. It works by breaking the three cases of
  1313. * expansion, reduction and replacement, and solving each of these
  1314. * in the most efficient way possible.
  1315. */
  1316. typedef int (*instr_fnptr) (const_bstring s1, int pos, const_bstring s2);
  1317. #define INITIAL_STATIC_FIND_INDEX_COUNT 32
  1318. static int findreplaceengine (bstring b, const_bstring find, const_bstring repl, int pos, instr_fnptr instr) {
  1319. int i, ret, slen, mlen, delta, acc;
  1320. int * d;
  1321. int static_d[INITIAL_STATIC_FIND_INDEX_COUNT+1]; /* This +1 is unnecessary, but it shuts up LINT. */
  1322. ptrdiff_t pd;
  1323. bstring auxf = (bstring) find;
  1324. bstring auxr = (bstring) repl;
  1325. if (b == NULL || b->data == NULL || find == NULL ||
  1326. find->data == NULL || repl == NULL || repl->data == NULL ||
  1327. pos < 0 || find->slen <= 0 || b->mlen < 0 || b->slen > b->mlen ||
  1328. b->mlen <= 0 || b->slen < 0 || repl->slen < 0) return BSTR_ERR;
  1329. if (pos > b->slen - find->slen) return BSTR_OK;
  1330. /* Alias with find string */
  1331. pd = (ptrdiff_t) (find->data - b->data);
  1332. if ((ptrdiff_t) (pos - find->slen) < pd && pd < (ptrdiff_t) b->slen) {
  1333. if (NULL == (auxf = bstrcpy (find))) return BSTR_ERR;
  1334. }
  1335. /* Alias with repl string */
  1336. pd = (ptrdiff_t) (repl->data - b->data);
  1337. if ((ptrdiff_t) (pos - repl->slen) < pd && pd < (ptrdiff_t) b->slen) {
  1338. if (NULL == (auxr = bstrcpy (repl))) {
  1339. if (auxf != find) bdestroy (auxf);
  1340. return BSTR_ERR;
  1341. }
  1342. }
  1343. delta = auxf->slen - auxr->slen;
  1344. /* in-place replacement since find and replace strings are of equal
  1345. length */
  1346. if (delta == 0) {
  1347. while ((pos = instr (b, pos, auxf)) >= 0) {
  1348. bstr__memcpy (b->data + pos, auxr->data, auxr->slen);
  1349. pos += auxf->slen;
  1350. }
  1351. if (auxf != find) bdestroy (auxf);
  1352. if (auxr != repl) bdestroy (auxr);
  1353. return BSTR_OK;
  1354. }
  1355. /* shrinking replacement since auxf->slen > auxr->slen */
  1356. if (delta > 0) {
  1357. acc = 0;
  1358. while ((i = instr (b, pos, auxf)) >= 0) {
  1359. if (acc && i > pos)
  1360. bstr__memmove (b->data + pos - acc, b->data + pos, i - pos);
  1361. if (auxr->slen)
  1362. bstr__memcpy (b->data + i - acc, auxr->data, auxr->slen);
  1363. acc += delta;
  1364. pos = i + auxf->slen;
  1365. }
  1366. if (acc) {
  1367. i = b->slen;
  1368. if (i > pos)
  1369. bstr__memmove (b->data + pos - acc, b->data + pos, i - pos);
  1370. b->slen -= acc;
  1371. b->data[b->slen] = (unsigned char) '\0';
  1372. }
  1373. if (auxf != find) bdestroy (auxf);
  1374. if (auxr != repl) bdestroy (auxr);
  1375. return BSTR_OK;
  1376. }
  1377. /* expanding replacement since find->slen < repl->slen. Its a lot
  1378. more complicated. This works by first finding all the matches and
  1379. storing them to a growable array, then doing at most one resize of
  1380. the destination bstring and then performing the direct memory transfers
  1381. of the string segment pieces to form the final result. The growable
  1382. array of matches uses a deferred doubling reallocing strategy. What
  1383. this means is that it starts as a reasonably fixed sized auto array in
  1384. the hopes that many if not most cases will never need to grow this
  1385. array. But it switches as soon as the bounds of the array will be
  1386. exceeded. An extra find result is always appended to this array that
  1387. corresponds to the end of the destination string, so slen is checked
  1388. against mlen - 1 rather than mlen before resizing.
  1389. */
  1390. mlen = INITIAL_STATIC_FIND_INDEX_COUNT;
  1391. d = (int *) static_d; /* Avoid malloc for trivial/initial cases */
  1392. acc = slen = 0;
  1393. while ((pos = instr (b, pos, auxf)) >= 0) {
  1394. if (slen >= mlen - 1) {
  1395. int sl, *t;
  1396. mlen += mlen;
  1397. sl = sizeof (int *) * mlen;
  1398. if (static_d == d) d = NULL; /* static_d cannot be realloced */
  1399. if (mlen <= 0 || sl < mlen || NULL == (t = (int *) bstr__realloc (d, sl))) {
  1400. ret = BSTR_ERR;
  1401. goto done;
  1402. }
  1403. if (NULL == d) bstr__memcpy (t, static_d, sizeof (static_d));
  1404. d = t;
  1405. }
  1406. d[slen] = pos;
  1407. slen++;
  1408. acc -= delta;
  1409. pos += auxf->slen;
  1410. if (pos < 0 || acc < 0) {
  1411. ret = BSTR_ERR;
  1412. goto done;
  1413. }
  1414. }
  1415. /* slen <= INITIAL_STATIC_INDEX_COUNT-1 or mlen-1 here. */
  1416. d[slen] = b->slen;
  1417. if (BSTR_OK == (ret = balloc (b, b->slen + acc + 1))) {
  1418. b->slen += acc;
  1419. for (i = slen-1; i >= 0; i--) {
  1420. int s, l;
  1421. s = d[i] + auxf->slen;
  1422. l = d[i+1] - s; /* d[slen] may be accessed here. */
  1423. if (l) {
  1424. bstr__memmove (b->data + s + acc, b->data + s, l);
  1425. }
  1426. if (auxr->slen) {
  1427. bstr__memmove (b->data + s + acc - auxr->slen,
  1428. auxr->data, auxr->slen);
  1429. }
  1430. acc += delta;
  1431. }
  1432. b->data[b->slen] = (unsigned char) '\0';
  1433. }
  1434. done:;
  1435. if (static_d == d) d = NULL;
  1436. bstr__free (d);
  1437. if (auxf != find) bdestroy (auxf);
  1438. if (auxr != repl) bdestroy (auxr);
  1439. return ret;
  1440. }
  1441. /* int bfindreplace (bstring b, const_bstring find, const_bstring repl,
  1442. * int pos)
  1443. *
  1444. * Replace all occurrences of a find string with a replace string after a
  1445. * given point in a bstring.
  1446. */
  1447. int bfindreplace (bstring b, const_bstring find, const_bstring repl, int pos) {
  1448. return findreplaceengine (b, find, repl, pos, binstr);
  1449. }
  1450. /* int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl,
  1451. * int pos)
  1452. *
  1453. * Replace all occurrences of a find string, ignoring case, with a replace
  1454. * string after a given point in a bstring.
  1455. */
  1456. int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl, int pos) {
  1457. return findreplaceengine (b, find, repl, pos, binstrcaseless);
  1458. }
  1459. /* int binsertch (bstring b, int pos, int len, unsigned char fill)
  1460. *
  1461. * Inserts the character fill repeatedly into b at position pos for a
  1462. * length len. If the position pos is past the end of b, then the
  1463. * character "fill" is appended as necessary to make up the gap between the
  1464. * end of b and the position pos + len.
  1465. */
  1466. int binsertch (bstring b, int pos, int len, unsigned char fill) {
  1467. int d, l, i;
  1468. if (pos < 0 || b == NULL || b->slen < 0 || b->mlen < b->slen ||
  1469. b->mlen <= 0 || len < 0) return BSTR_ERR;
  1470. /* Compute the two possible end pointers */
  1471. d = b->slen + len;
  1472. l = pos + len;
  1473. if ((d|l) < 0) return BSTR_ERR;
  1474. if (l > d) {
  1475. /* Inserting past the end of the string */
  1476. if (balloc (b, l + 1) != BSTR_OK) return BSTR_ERR;
  1477. pos = b->slen;
  1478. b->slen = l;
  1479. } else {
  1480. /* Inserting in the middle of the string */
  1481. if (balloc (b, d + 1) != BSTR_OK) return BSTR_ERR;
  1482. for (i = d - 1; i >= l; i--) {
  1483. b->data[i] = b->data[i - len];
  1484. }
  1485. b->slen = d;
  1486. }
  1487. for (i=pos; i < l; i++) b->data[i] = fill;
  1488. b->data[b->slen] = (unsigned char) '\0';
  1489. return BSTR_OK;
  1490. }
  1491. /* int bpattern (bstring b, int len)
  1492. *
  1493. * Replicate the bstring, b in place, end to end repeatedly until it
  1494. * surpasses len characters, then chop the result to exactly len characters.
  1495. * This function operates in-place. The function will return with BSTR_ERR
  1496. * if b is NULL or of length 0, otherwise BSTR_OK is returned.
  1497. */
  1498. int bpattern (bstring b, int len) {
  1499. int i, d;
  1500. d = blength (b);
  1501. if (d <= 0 || len < 0 || balloc (b, len + 1) != BSTR_OK) return BSTR_ERR;
  1502. if (len > 0) {
  1503. if (d == 1) return bsetstr (b, len, NULL, b->data[0]);
  1504. for (i = d; i < len; i++) b->data[i] = b->data[i - d];
  1505. }
  1506. b->data[len] = (unsigned char) '\0';
  1507. b->slen = len;
  1508. return BSTR_OK;
  1509. }
  1510. #define BS_BUFF_SZ (1024)
  1511. /* int breada (bstring b, bNread readPtr, void * parm)
  1512. *
  1513. * Use a finite buffer fread-like function readPtr to concatenate to the
  1514. * bstring b the entire contents of file-like source data in a roughly
  1515. * efficient way.
  1516. */
  1517. int breada (bstring b, bNread readPtr, void * parm) {
  1518. int i, l, n;
  1519. if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen ||
  1520. b->mlen <= 0 || readPtr == NULL) return BSTR_ERR;
  1521. i = b->slen;
  1522. for (n=i+16; ; n += ((n < BS_BUFF_SZ) ? n : BS_BUFF_SZ)) {
  1523. if (BSTR_OK != balloc (b, n + 1)) return BSTR_ERR;
  1524. l = (int) readPtr ((void *) (b->data + i), 1, n - i, parm);
  1525. i += l;
  1526. b->slen = i;
  1527. if (i < n) break;
  1528. }
  1529. b->data[i] = (unsigned char) '\0';
  1530. return BSTR_OK;
  1531. }
  1532. /* bstring bread (bNread readPtr, void * parm)
  1533. *
  1534. * Use a finite buffer fread-like function readPtr to create a bstring
  1535. * filled with the entire contents of file-like source data in a roughly
  1536. * efficient way.
  1537. */
  1538. bstring bread (bNread readPtr, void * parm) {
  1539. bstring buff;
  1540. if (0 > breada (buff = bfromcstr (""), readPtr, parm)) {
  1541. bdestroy (buff);
  1542. return NULL;
  1543. }
  1544. return buff;
  1545. }
  1546. /* int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator)
  1547. *
  1548. * Use an fgetc-like single character stream reading function (getcPtr) to
  1549. * obtain a sequence of characters which are concatenated to the end of the
  1550. * bstring b. The stream read is terminated by the passed in terminator
  1551. * parameter.
  1552. *
  1553. * If getcPtr returns with a negative number, or the terminator character
  1554. * (which is appended) is read, then the stream reading is halted and the
  1555. * function returns with a partial result in b. If there is an empty partial
  1556. * result, 1 is returned. If no characters are read, or there is some other
  1557. * detectable error, BSTR_ERR is returned.
  1558. */
  1559. int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator) {
  1560. int c, d, e;
  1561. if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen ||
  1562. b->mlen <= 0 || getcPtr == NULL) return BSTR_ERR;
  1563. d = 0;
  1564. e = b->mlen - 2;
  1565. while ((c = getcPtr (parm)) >= 0) {
  1566. if (d > e) {
  1567. b->slen = d;
  1568. if (balloc (b, d + 2) != BSTR_OK) return BSTR_ERR;
  1569. e = b->mlen - 2;
  1570. }
  1571. b->data[d] = (unsigned char) c;
  1572. d++;
  1573. if (c == terminator) break;
  1574. }
  1575. b->data[d] = (unsigned char) '\0';
  1576. b->slen = d;
  1577. return d == 0 && c < 0;
  1578. }
  1579. /* int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator)
  1580. *
  1581. * Use an fgetc-like single character stream reading function (getcPtr) to
  1582. * obtain a sequence of characters which are concatenated to the end of the
  1583. * bstring b. The stream read is terminated by the passed in terminator
  1584. * parameter.
  1585. *
  1586. * If getcPtr returns with a negative number, or the terminator character
  1587. * (which is appended) is read, then the stream reading is halted and the
  1588. * function returns with a partial result concatentated to b. If there is
  1589. * an empty partial result, 1 is returned. If no characters are read, or
  1590. * there is some other detectable error, BSTR_ERR is returned.
  1591. */
  1592. int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator) {
  1593. int c, d, e;
  1594. if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen ||
  1595. b->mlen <= 0 || getcPtr == NULL) return BSTR_ERR;
  1596. d = b->slen;
  1597. e = b->mlen - 2;
  1598. while ((c = getcPtr (parm)) >= 0) {
  1599. if (d > e) {
  1600. b->slen = d;
  1601. if (balloc (b, d + 2) != BSTR_OK) return BSTR_ERR;
  1602. e = b->mlen - 2;
  1603. }
  1604. b->data[d] = (unsigned char) c;
  1605. d++;
  1606. if (c == terminator) break;
  1607. }
  1608. b->data[d] = (unsigned char) '\0';
  1609. b->slen = d;
  1610. return d == 0 && c < 0;
  1611. }
  1612. /* bstring bgets (bNgetc getcPtr, void * parm, char terminator)
  1613. *
  1614. * Use an fgetc-like single character stream reading function (getcPtr) to
  1615. * obtain a sequence of characters which are concatenated into a bstring.
  1616. * The stream read is terminated by the passed in terminator function.
  1617. *
  1618. * If getcPtr returns with a negative number, or the terminator character
  1619. * (which is appended) is read, then the stream reading is halted and the
  1620. * result obtained thus far is returned. If no characters are read, or
  1621. * there is some other detectable error, NULL is returned.
  1622. */
  1623. bstring bgets (bNgetc getcPtr, void * parm, char terminator) {
  1624. bstring buff;
  1625. if (0 > bgetsa (buff = bfromcstr (""), getcPtr, parm, terminator) || 0 >= buff->slen) {
  1626. bdestroy (buff);
  1627. buff = NULL;
  1628. }
  1629. return buff;
  1630. }
  1631. struct bStream {
  1632. bstring buff; /* Buffer for over-reads */
  1633. void * parm; /* The stream handle for core stream */
  1634. bNread readFnPtr; /* fread compatible fnptr for core stream */
  1635. int isEOF; /* track file's EOF state */
  1636. int maxBuffSz;
  1637. };
  1638. /* struct bStream * bsopen (bNread readPtr, void * parm)
  1639. *
  1640. * Wrap a given open stream (described by a fread compatible function
  1641. * pointer and stream handle) into an open bStream suitable for the bstring
  1642. * library streaming functions.
  1643. */
  1644. struct bStream * bsopen (bNread readPtr, void * parm) {
  1645. struct bStream * s;
  1646. if (readPtr == NULL) return NULL;
  1647. s = (struct bStream *) bstr__alloc (sizeof (struct bStream));
  1648. if (s == NULL) return NULL;
  1649. s->parm = parm;
  1650. s->buff = bfromcstr ("");
  1651. s->readFnPtr = readPtr;
  1652. s->maxBuffSz = BS_BUFF_SZ;
  1653. s->isEOF = 0;
  1654. return s;
  1655. }
  1656. /* int bsbufflength (struct bStream * s, int sz)
  1657. *
  1658. * Set the length of the buffer used by the bStream. If sz is zero, the
  1659. * length is not set. This function returns with the previous length.
  1660. */
  1661. int bsbufflength (struct bStream * s, int sz) {
  1662. int oldSz;
  1663. if (s == NULL || sz < 0) return BSTR_ERR;
  1664. oldSz = s->maxBuffSz;
  1665. if (sz > 0) s->maxBuffSz = sz;
  1666. return oldSz;
  1667. }
  1668. int bseof (const struct bStream * s) {
  1669. if (s == NULL || s->readFnPtr == NULL) return BSTR_ERR;
  1670. return s->isEOF && (s->buff->slen == 0);
  1671. }
  1672. /* void * bsclose (struct bStream * s)
  1673. *
  1674. * Close the bStream, and return the handle to the stream that was originally
  1675. * used to open the given stream.
  1676. */
  1677. void * bsclose (struct bStream * s) {
  1678. void * parm;
  1679. if (s == NULL) return NULL;
  1680. s->readFnPtr = NULL;
  1681. if (s->buff) bdestroy (s->buff);
  1682. s->buff = NULL;
  1683. parm = s->parm;
  1684. s->parm = NULL;
  1685. s->isEOF = 1;
  1686. bstr__free (s);
  1687. return parm;
  1688. }
  1689. /* int bsreadlna (bstring r, struct bStream * s, char terminator)
  1690. *
  1691. * Read a bstring terminated by the terminator character or the end of the
  1692. * stream from the bStream (s) and return it into the parameter r. This
  1693. * function may read additional characters from the core stream that are not
  1694. * returned, but will be retained for subsequent read operations.
  1695. */
  1696. int bsreadlna (bstring r, struct bStream * s, char terminator) {
  1697. int i, l, ret, rlo;
  1698. char * b;
  1699. struct tagbstring x;
  1700. if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0 ||
  1701. r->slen < 0 || r->mlen < r->slen) return BSTR_ERR;
  1702. l = s->buff->slen;
  1703. if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
  1704. b = (char *) s->buff->data;
  1705. x.data = (unsigned char *) b;
  1706. /* First check if the current buffer holds the terminator */
  1707. b[l] = terminator; /* Set sentinel */
  1708. for (i=0; b[i] != terminator; i++) ;
  1709. if (i < l) {
  1710. x.slen = i + 1;
  1711. ret = bconcat (r, &x);
  1712. s->buff->slen = l;
  1713. if (BSTR_OK == ret) bdelete (s->buff, 0, i + 1);
  1714. return BSTR_OK;
  1715. }
  1716. rlo = r->slen;
  1717. /* If not then just concatenate the entire buffer to the output */
  1718. x.slen = l;
  1719. if (BSTR_OK != bconcat (r, &x)) return BSTR_ERR;
  1720. /* Perform direct in-place reads into the destination to allow for
  1721. the minimum of data-copies */
  1722. for (;;) {
  1723. if (BSTR_OK != balloc (r, r->slen + s->maxBuffSz + 1)) return BSTR_ERR;
  1724. b = (char *) (r->data + r->slen);
  1725. l = (int) s->readFnPtr (b, 1, s->maxBuffSz, s->parm);
  1726. if (l <= 0) {
  1727. r->data[r->slen] = (unsigned char) '\0';
  1728. s->buff->slen = 0;
  1729. s->isEOF = 1;
  1730. /* If nothing was read return with an error message */
  1731. return BSTR_ERR & -(r->slen == rlo);
  1732. }
  1733. b[l] = terminator; /* Set sentinel */
  1734. for (i=0; b[i] != terminator; i++) ;
  1735. if (i < l) break;
  1736. r->slen += l;
  1737. }
  1738. /* Terminator found, push over-read back to buffer */
  1739. i++;
  1740. r->slen += i;
  1741. s->buff->slen = l - i;
  1742. bstr__memcpy (s->buff->data, b + i, l - i);
  1743. r->data[r->slen] = (unsigned char) '\0';
  1744. return BSTR_OK;
  1745. }
  1746. /* int bsreadlnsa (bstring r, struct bStream * s, bstring term)
  1747. *
  1748. * Read a bstring terminated by any character in the term string or the end
  1749. * of the stream from the bStream (s) and return it into the parameter r.
  1750. * This function may read additional characters from the core stream that
  1751. * are not returned, but will be retained for subsequent read operations.
  1752. */
  1753. int bsreadlnsa (bstring r, struct bStream * s, const_bstring term) {
  1754. int i, l, ret, rlo;
  1755. unsigned char * b;
  1756. struct tagbstring x;
  1757. struct charField cf;
  1758. if (s == NULL || s->buff == NULL || r == NULL || term == NULL ||
  1759. term->data == NULL || r->mlen <= 0 || r->slen < 0 ||
  1760. r->mlen < r->slen) return BSTR_ERR;
  1761. if (term->slen == 1) return bsreadlna (r, s, term->data[0]);
  1762. if (term->slen < 1 || buildCharField (&cf, term)) return BSTR_ERR;
  1763. l = s->buff->slen;
  1764. if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
  1765. b = (unsigned char *) s->buff->data;
  1766. x.data = b;
  1767. /* First check if the current buffer holds the terminator */
  1768. b[l] = term->data[0]; /* Set sentinel */
  1769. for (i=0; !testInCharField (&cf, b[i]); i++) ;
  1770. if (i < l) {
  1771. x.slen = i + 1;
  1772. ret = bconcat (r, &x);
  1773. s->buff->slen = l;
  1774. if (BSTR_OK == ret) bdelete (s->buff, 0, i + 1);
  1775. return BSTR_OK;
  1776. }
  1777. rlo = r->slen;
  1778. /* If not then just concatenate the entire buffer to the output */
  1779. x.slen = l;
  1780. if (BSTR_OK != bconcat (r, &x)) return BSTR_ERR;
  1781. /* Perform direct in-place reads into the destination to allow for
  1782. the minimum of data-copies */
  1783. for (;;) {
  1784. if (BSTR_OK != balloc (r, r->slen + s->maxBuffSz + 1)) return BSTR_ERR;
  1785. b = (unsigned char *) (r->data + r->slen);
  1786. l = (int) s->readFnPtr (b, 1, s->maxBuffSz, s->parm);
  1787. if (l <= 0) {
  1788. r->data[r->slen] = (unsigned char) '\0';
  1789. s->buff->slen = 0;
  1790. s->isEOF = 1;
  1791. /* If nothing was read return with an error message */
  1792. return BSTR_ERR & -(r->slen == rlo);
  1793. }
  1794. b[l] = term->data[0]; /* Set sentinel */
  1795. for (i=0; !testInCharField (&cf, b[i]); i++) ;
  1796. if (i < l) break;
  1797. r->slen += l;
  1798. }
  1799. /* Terminator found, push over-read back to buffer */
  1800. i++;
  1801. r->slen += i;
  1802. s->buff->slen = l - i;
  1803. bstr__memcpy (s->buff->data, b + i, l - i);
  1804. r->data[r->slen] = (unsigned char) '\0';
  1805. return BSTR_OK;
  1806. }
  1807. /* int bsreada (bstring r, struct bStream * s, int n)
  1808. *
  1809. * Read a bstring of length n (or, if it is fewer, as many bytes as is
  1810. * remaining) from the bStream. This function may read additional
  1811. * characters from the core stream that are not returned, but will be
  1812. * retained for subsequent read operations. This function will not read
  1813. * additional characters from the core stream beyond virtual stream pointer.
  1814. */
  1815. int bsreada (bstring r, struct bStream * s, int n) {
  1816. int l, ret, orslen;
  1817. char * b;
  1818. struct tagbstring x;
  1819. if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0
  1820. || r->slen < 0 || r->mlen < r->slen || n <= 0) return BSTR_ERR;
  1821. n += r->slen;
  1822. if (n <= 0) return BSTR_ERR;
  1823. l = s->buff->slen;
  1824. orslen = r->slen;
  1825. if (0 == l) {
  1826. if (s->isEOF) return BSTR_ERR;
  1827. if (r->mlen > n) {
  1828. l = (int) s->readFnPtr (r->data + r->slen, 1, n - r->slen, s->parm);
  1829. if (0 >= l || l > n - r->slen) {
  1830. s->isEOF = 1;
  1831. return BSTR_ERR;
  1832. }
  1833. r->slen += l;
  1834. r->data[r->slen] = (unsigned char) '\0';
  1835. return 0;
  1836. }
  1837. }
  1838. if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
  1839. b = (char *) s->buff->data;
  1840. x.data = (unsigned char *) b;
  1841. do {
  1842. if (l + r->slen >= n) {
  1843. x.slen = n - r->slen;
  1844. ret = bconcat (r, &x);
  1845. s->buff->slen = l;
  1846. if (BSTR_OK == ret) bdelete (s->buff, 0, x.slen);
  1847. return BSTR_ERR & -(r->slen == orslen);
  1848. }
  1849. x.slen = l;
  1850. if (BSTR_OK != bconcat (r, &x)) break;
  1851. l = n - r->slen;
  1852. if (l > s->maxBuffSz) l = s->maxBuffSz;
  1853. l = (int) s->readFnPtr (b, 1, l, s->parm);
  1854. } while (l > 0);
  1855. if (l < 0) l = 0;
  1856. if (l == 0) s->isEOF = 1;
  1857. s->buff->slen = l;
  1858. return BSTR_ERR & -(r->slen == orslen);
  1859. }
  1860. /* int bsreadln (bstring r, struct bStream * s, char terminator)
  1861. *
  1862. * Read a bstring terminated by the terminator character or the end of the
  1863. * stream from the bStream (s) and return it into the parameter r. This
  1864. * function may read additional characters from the core stream that are not
  1865. * returned, but will be retained for subsequent read operations.
  1866. */
  1867. int bsreadln (bstring r, struct bStream * s, char terminator) {
  1868. if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0)
  1869. return BSTR_ERR;
  1870. if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
  1871. r->slen = 0;
  1872. return bsreadlna (r, s, terminator);
  1873. }
  1874. /* int bsreadlns (bstring r, struct bStream * s, bstring term)
  1875. *
  1876. * Read a bstring terminated by any character in the term string or the end
  1877. * of the stream from the bStream (s) and return it into the parameter r.
  1878. * This function may read additional characters from the core stream that
  1879. * are not returned, but will be retained for subsequent read operations.
  1880. */
  1881. int bsreadlns (bstring r, struct bStream * s, const_bstring term) {
  1882. if (s == NULL || s->buff == NULL || r == NULL || term == NULL
  1883. || term->data == NULL || r->mlen <= 0) return BSTR_ERR;
  1884. if (term->slen == 1) return bsreadln (r, s, term->data[0]);
  1885. if (term->slen < 1) return BSTR_ERR;
  1886. if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
  1887. r->slen = 0;
  1888. return bsreadlnsa (r, s, term);
  1889. }
  1890. /* int bsread (bstring r, struct bStream * s, int n)
  1891. *
  1892. * Read a bstring of length n (or, if it is fewer, as many bytes as is
  1893. * remaining) from the bStream. This function may read additional
  1894. * characters from the core stream that are not returned, but will be
  1895. * retained for subsequent read operations. This function will not read
  1896. * additional characters from the core stream beyond virtual stream pointer.
  1897. */
  1898. int bsread (bstring r, struct bStream * s, int n) {
  1899. if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0
  1900. || n <= 0) return BSTR_ERR;
  1901. if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
  1902. r->slen = 0;
  1903. return bsreada (r, s, n);
  1904. }
  1905. /* int bsunread (struct bStream * s, const_bstring b)
  1906. *
  1907. * Insert a bstring into the bStream at the current position. These
  1908. * characters will be read prior to those that actually come from the core
  1909. * stream.
  1910. */
  1911. int bsunread (struct bStream * s, const_bstring b) {
  1912. if (s == NULL || s->buff == NULL) return BSTR_ERR;
  1913. return binsert (s->buff, 0, b, (unsigned char) '?');
  1914. }
  1915. /* int bspeek (bstring r, const struct bStream * s)
  1916. *
  1917. * Return the currently buffered characters from the bStream that will be
  1918. * read prior to reads from the core stream.
  1919. */
  1920. int bspeek (bstring r, const struct bStream * s) {
  1921. if (s == NULL || s->buff == NULL) return BSTR_ERR;
  1922. return bassign (r, s->buff);
  1923. }
  1924. /* bstring bjoin (const struct bstrList * bl, const_bstring sep);
  1925. *
  1926. * Join the entries of a bstrList into one bstring by sequentially
  1927. * concatenating them with the sep string in between. If there is an error
  1928. * NULL is returned, otherwise a bstring with the correct result is returned.
  1929. */
  1930. bstring bjoin (const struct bstrList * bl, const_bstring sep) {
  1931. bstring b;
  1932. int i, c, v;
  1933. if (bl == NULL || bl->qty < 0) return NULL;
  1934. if (sep != NULL && (sep->slen < 0 || sep->data == NULL)) return NULL;
  1935. for (i = 0, c = 1; i < bl->qty; i++) {
  1936. v = bl->entry[i]->slen;
  1937. if (v < 0) return NULL; /* Invalid input */
  1938. c += v;
  1939. if (c < 0) return NULL; /* Wrap around ?? */
  1940. }
  1941. if (sep != NULL) c += (bl->qty - 1) * sep->slen;
  1942. b = (bstring) bstr__alloc (sizeof (struct tagbstring));
  1943. if (NULL == b) return NULL; /* Out of memory */
  1944. b->data = (unsigned char *) bstr__alloc (c);
  1945. if (b->data == NULL) {
  1946. bstr__free (b);
  1947. return NULL;
  1948. }
  1949. b->mlen = c;
  1950. b->slen = c-1;
  1951. for (i = 0, c = 0; i < bl->qty; i++) {
  1952. if (i > 0 && sep != NULL) {
  1953. bstr__memcpy (b->data + c, sep->data, sep->slen);
  1954. c += sep->slen;
  1955. }
  1956. v = bl->entry[i]->slen;
  1957. bstr__memcpy (b->data + c, bl->entry[i]->data, v);
  1958. c += v;
  1959. }
  1960. b->data[c] = (unsigned char) '\0';
  1961. return b;
  1962. }
  1963. #define BSSSC_BUFF_LEN (256)
  1964. /* int bssplitscb (struct bStream * s, const_bstring splitStr,
  1965. * int (* cb) (void * parm, int ofs, const_bstring entry), void * parm)
  1966. *
  1967. * Iterate the set of disjoint sequential substrings read from a stream
  1968. * divided by any of the characters in splitStr. An empty splitStr causes
  1969. * the whole stream to be iterated once.
  1970. *
  1971. * Note: At the point of calling the cb function, the bStream pointer is
  1972. * pointed exactly at the position right after having read the split
  1973. * character. The cb function can act on the stream by causing the bStream
  1974. * pointer to move, and bssplitscb will continue by starting the next split
  1975. * at the position of the pointer after the return from cb.
  1976. *
  1977. * However, if the cb causes the bStream s to be destroyed then the cb must
  1978. * return with a negative value, otherwise bssplitscb will continue in an
  1979. * undefined manner.
  1980. */
  1981. int bssplitscb (struct bStream * s, const_bstring splitStr,
  1982. int (* cb) (void * parm, int ofs, const_bstring entry), void * parm) {
  1983. struct charField chrs;
  1984. bstring buff;
  1985. int i, p, ret;
  1986. if (cb == NULL || s == NULL || s->readFnPtr == NULL
  1987. || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;
  1988. if (NULL == (buff = bfromcstr (""))) return BSTR_ERR;
  1989. if (splitStr->slen == 0) {
  1990. while (bsreada (buff, s, BSSSC_BUFF_LEN) >= 0) ;
  1991. if ((ret = cb (parm, 0, buff)) > 0)
  1992. ret = 0;
  1993. } else {
  1994. buildCharField (&chrs, splitStr);
  1995. ret = p = i = 0;
  1996. for (;;) {
  1997. if (i >= buff->slen) {
  1998. bsreada (buff, s, BSSSC_BUFF_LEN);
  1999. if (i >= buff->slen) {
  2000. if (0 < (ret = cb (parm, p, buff))) ret = 0;
  2001. break;
  2002. }
  2003. }
  2004. if (testInCharField (&chrs, buff->data[i])) {
  2005. struct tagbstring t;
  2006. unsigned char c;
  2007. blk2tbstr (t, buff->data + i + 1, buff->slen - (i + 1));
  2008. if ((ret = bsunread (s, &t)) < 0) break;
  2009. buff->slen = i;
  2010. c = buff->data[i];
  2011. buff->data[i] = (unsigned char) '\0';
  2012. if ((ret = cb (parm, p, buff)) < 0) break;
  2013. buff->data[i] = c;
  2014. buff->slen = 0;
  2015. p += i + 1;
  2016. i = -1;
  2017. }
  2018. i++;
  2019. }
  2020. }
  2021. bdestroy (buff);
  2022. return ret;
  2023. }
  2024. /* int bssplitstrcb (struct bStream * s, const_bstring splitStr,
  2025. * int (* cb) (void * parm, int ofs, const_bstring entry), void * parm)
  2026. *
  2027. * Iterate the set of disjoint sequential substrings read from a stream
  2028. * divided by the entire substring splitStr. An empty splitStr causes
  2029. * each character of the stream to be iterated.
  2030. *
  2031. * Note: At the point of calling the cb function, the bStream pointer is
  2032. * pointed exactly at the position right after having read the split
  2033. * character. The cb function can act on the stream by causing the bStream
  2034. * pointer to move, and bssplitscb will continue by starting the next split
  2035. * at the position of the pointer after the return from cb.
  2036. *
  2037. * However, if the cb causes the bStream s to be destroyed then the cb must
  2038. * return with a negative value, otherwise bssplitscb will continue in an
  2039. * undefined manner.
  2040. */
  2041. int bssplitstrcb (struct bStream * s, const_bstring splitStr,
  2042. int (* cb) (void * parm, int ofs, const_bstring entry), void * parm) {
  2043. bstring buff;
  2044. int i, p, ret;
  2045. if (cb == NULL || s == NULL || s->readFnPtr == NULL
  2046. || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;
  2047. if (splitStr->slen == 1) return bssplitscb (s, splitStr, cb, parm);
  2048. if (NULL == (buff = bfromcstr (""))) return BSTR_ERR;
  2049. if (splitStr->slen == 0) {
  2050. for (i=0; bsreada (buff, s, BSSSC_BUFF_LEN) >= 0; i++) {
  2051. if ((ret = cb (parm, 0, buff)) < 0) {
  2052. bdestroy (buff);
  2053. return ret;
  2054. }
  2055. buff->slen = 0;
  2056. }
  2057. return BSTR_OK;
  2058. } else {
  2059. ret = p = i = 0;
  2060. for (i=p=0;;) {
  2061. if ((ret = binstr (buff, 0, splitStr)) >= 0) {
  2062. struct tagbstring t;
  2063. blk2tbstr (t, buff->data, ret);
  2064. i = ret + splitStr->slen;
  2065. if ((ret = cb (parm, p, &t)) < 0) break;
  2066. p += i;
  2067. bdelete (buff, 0, i);
  2068. } else {
  2069. bsreada (buff, s, BSSSC_BUFF_LEN);
  2070. if (bseof (s)) {
  2071. if ((ret = cb (parm, p, buff)) > 0) ret = 0;
  2072. break;
  2073. }
  2074. }
  2075. }
  2076. }
  2077. bdestroy (buff);
  2078. return ret;
  2079. }
  2080. /* int bstrListCreate (void)
  2081. *
  2082. * Create a bstrList.
  2083. */
  2084. struct bstrList * bstrListCreate (void) {
  2085. struct bstrList * sl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));
  2086. if (sl) {
  2087. sl->entry = (bstring *) bstr__alloc (1*sizeof (bstring));
  2088. if (!sl->entry) {
  2089. bstr__free (sl);
  2090. sl = NULL;
  2091. } else {
  2092. sl->qty = 0;
  2093. sl->mlen = 1;
  2094. }
  2095. }
  2096. return sl;
  2097. }
  2098. /* int bstrListDestroy (struct bstrList * sl)
  2099. *
  2100. * Destroy a bstrList that has been created by bsplit, bsplits or bstrListCreate.
  2101. */
  2102. int bstrListDestroy (struct bstrList * sl) {
  2103. int i;
  2104. if (sl == NULL || sl->qty < 0) return BSTR_ERR;
  2105. for (i=0; i < sl->qty; i++) {
  2106. if (sl->entry[i]) {
  2107. bdestroy (sl->entry[i]);
  2108. sl->entry[i] = NULL;
  2109. }
  2110. }
  2111. sl->qty = -1;
  2112. sl->mlen = -1;
  2113. bstr__free (sl->entry);
  2114. sl->entry = NULL;
  2115. bstr__free (sl);
  2116. return BSTR_OK;
  2117. }
  2118. /* int bstrListAlloc (struct bstrList * sl, int msz)
  2119. *
  2120. * Ensure that there is memory for at least msz number of entries for the
  2121. * list.
  2122. */
  2123. int bstrListAlloc (struct bstrList * sl, int msz) {
  2124. bstring * l;
  2125. int smsz;
  2126. size_t nsz;
  2127. if (!sl || msz <= 0 || !sl->entry || sl->qty < 0 || sl->mlen <= 0 || sl->qty > sl->mlen) return BSTR_ERR;
  2128. if (sl->mlen >= msz) return BSTR_OK;
  2129. smsz = snapUpSize (msz);
  2130. nsz = ((size_t) smsz) * sizeof (bstring);
  2131. if (nsz < (size_t) smsz) return BSTR_ERR;
  2132. l = (bstring *) bstr__realloc (sl->entry, nsz);
  2133. if (!l) {
  2134. smsz = msz;
  2135. nsz = ((size_t) smsz) * sizeof (bstring);
  2136. l = (bstring *) bstr__realloc (sl->entry, nsz);
  2137. if (!l) return BSTR_ERR;
  2138. }
  2139. sl->mlen = smsz;
  2140. sl->entry = l;
  2141. return BSTR_OK;
  2142. }
  2143. /* int bstrListAllocMin (struct bstrList * sl, int msz)
  2144. *
  2145. * Try to allocate the minimum amount of memory for the list to include at
  2146. * least msz entries or sl->qty whichever is greater.
  2147. */
  2148. int bstrListAllocMin (struct bstrList * sl, int msz) {
  2149. bstring * l;
  2150. size_t nsz;
  2151. if (!sl || msz <= 0 || !sl->entry || sl->qty < 0 || sl->mlen <= 0 || sl->qty > sl->mlen) return BSTR_ERR;
  2152. if (msz < sl->qty) msz = sl->qty;
  2153. if (sl->mlen == msz) return BSTR_OK;
  2154. nsz = ((size_t) msz) * sizeof (bstring);
  2155. if (nsz < (size_t) msz) return BSTR_ERR;
  2156. l = (bstring *) bstr__realloc (sl->entry, nsz);
  2157. if (!l) return BSTR_ERR;
  2158. sl->mlen = msz;
  2159. sl->entry = l;
  2160. return BSTR_OK;
  2161. }
  2162. /* int bsplitcb (const_bstring str, unsigned char splitChar, int pos,
  2163. * int (* cb) (void * parm, int ofs, int len), void * parm)
  2164. *
  2165. * Iterate the set of disjoint sequential substrings over str divided by the
  2166. * character in splitChar.
  2167. *
  2168. * Note: Non-destructive modification of str from within the cb function
  2169. * while performing this split is not undefined. bsplitcb behaves in
  2170. * sequential lock step with calls to cb. I.e., after returning from a cb
  2171. * that return a non-negative integer, bsplitcb continues from the position
  2172. * 1 character after the last detected split character and it will halt
  2173. * immediately if the length of str falls below this point. However, if the
  2174. * cb function destroys str, then it *must* return with a negative value,
  2175. * otherwise bsplitcb will continue in an undefined manner.
  2176. */
  2177. int bsplitcb (const_bstring str, unsigned char splitChar, int pos,
  2178. int (* cb) (void * parm, int ofs, int len), void * parm) {
  2179. int i, p, ret;
  2180. if (cb == NULL || str == NULL || pos < 0 || pos > str->slen)
  2181. return BSTR_ERR;
  2182. p = pos;
  2183. do {
  2184. for (i=p; i < str->slen; i++) {
  2185. if (str->data[i] == splitChar) break;
  2186. }
  2187. if ((ret = cb (parm, p, i - p)) < 0) return ret;
  2188. p = i + 1;
  2189. } while (p <= str->slen);
  2190. return BSTR_OK;
  2191. }
  2192. /* int bsplitscb (const_bstring str, const_bstring splitStr, int pos,
  2193. * int (* cb) (void * parm, int ofs, int len), void * parm)
  2194. *
  2195. * Iterate the set of disjoint sequential substrings over str divided by any
  2196. * of the characters in splitStr. An empty splitStr causes the whole str to
  2197. * be iterated once.
  2198. *
  2199. * Note: Non-destructive modification of str from within the cb function
  2200. * while performing this split is not undefined. bsplitscb behaves in
  2201. * sequential lock step with calls to cb. I.e., after returning from a cb
  2202. * that return a non-negative integer, bsplitscb continues from the position
  2203. * 1 character after the last detected split character and it will halt
  2204. * immediately if the length of str falls below this point. However, if the
  2205. * cb function destroys str, then it *must* return with a negative value,
  2206. * otherwise bsplitscb will continue in an undefined manner.
  2207. */
  2208. int bsplitscb (const_bstring str, const_bstring splitStr, int pos,
  2209. int (* cb) (void * parm, int ofs, int len), void * parm) {
  2210. struct charField chrs;
  2211. int i, p, ret;
  2212. if (cb == NULL || str == NULL || pos < 0 || pos > str->slen
  2213. || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;
  2214. if (splitStr->slen == 0) {
  2215. if ((ret = cb (parm, 0, str->slen)) > 0) ret = 0;
  2216. return ret;
  2217. }
  2218. if (splitStr->slen == 1)
  2219. return bsplitcb (str, splitStr->data[0], pos, cb, parm);
  2220. buildCharField (&chrs, splitStr);
  2221. p = pos;
  2222. do {
  2223. for (i=p; i < str->slen; i++) {
  2224. if (testInCharField (&chrs, str->data[i])) break;
  2225. }
  2226. if ((ret = cb (parm, p, i - p)) < 0) return ret;
  2227. p = i + 1;
  2228. } while (p <= str->slen);
  2229. return BSTR_OK;
  2230. }
  2231. /* int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos,
  2232. * int (* cb) (void * parm, int ofs, int len), void * parm)
  2233. *
  2234. * Iterate the set of disjoint sequential substrings over str divided by the
  2235. * substring splitStr. An empty splitStr causes the whole str to be
  2236. * iterated once.
  2237. *
  2238. * Note: Non-destructive modification of str from within the cb function
  2239. * while performing this split is not undefined. bsplitstrcb behaves in
  2240. * sequential lock step with calls to cb. I.e., after returning from a cb
  2241. * that return a non-negative integer, bsplitscb continues from the position
  2242. * 1 character after the last detected split character and it will halt
  2243. * immediately if the length of str falls below this point. However, if the
  2244. * cb function destroys str, then it *must* return with a negative value,
  2245. * otherwise bsplitscb will continue in an undefined manner.
  2246. */
  2247. int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos,
  2248. int (* cb) (void * parm, int ofs, int len), void * parm) {
  2249. int i, p, ret;
  2250. if (cb == NULL || str == NULL || pos < 0 || pos > str->slen
  2251. || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;
  2252. if (0 == splitStr->slen) {
  2253. for (i=pos; i < str->slen; i++) {
  2254. if ((ret = cb (parm, i, 1)) < 0) return ret;
  2255. }
  2256. return BSTR_OK;
  2257. }
  2258. if (splitStr->slen == 1)
  2259. return bsplitcb (str, splitStr->data[0], pos, cb, parm);
  2260. for (i=p=pos; i <= str->slen - splitStr->slen; i++) {
  2261. if (0 == bstr__memcmp (splitStr->data, str->data + i, splitStr->slen)) {
  2262. if ((ret = cb (parm, p, i - p)) < 0) return ret;
  2263. i += splitStr->slen;
  2264. p = i;
  2265. }
  2266. }
  2267. if ((ret = cb (parm, p, str->slen - p)) < 0) return ret;
  2268. return BSTR_OK;
  2269. }
  2270. struct genBstrList {
  2271. bstring b;
  2272. struct bstrList * bl;
  2273. };
  2274. static int bscb (void * parm, int ofs, int len) {
  2275. struct genBstrList * g = (struct genBstrList *) parm;
  2276. if (g->bl->qty >= g->bl->mlen) {
  2277. int mlen = g->bl->mlen * 2;
  2278. bstring * tbl;
  2279. while (g->bl->qty >= mlen) {
  2280. if (mlen < g->bl->mlen) return BSTR_ERR;
  2281. mlen += mlen;
  2282. }
  2283. tbl = (bstring *) bstr__realloc (g->bl->entry, sizeof (bstring) * mlen);
  2284. if (tbl == NULL) return BSTR_ERR;
  2285. g->bl->entry = tbl;
  2286. g->bl->mlen = mlen;
  2287. }
  2288. g->bl->entry[g->bl->qty] = bmidstr (g->b, ofs, len);
  2289. g->bl->qty++;
  2290. return BSTR_OK;
  2291. }
  2292. /* struct bstrList * bsplit (const_bstring str, unsigned char splitChar)
  2293. *
  2294. * Create an array of sequential substrings from str divided by the character
  2295. * splitChar.
  2296. */
  2297. struct bstrList * bsplit (const_bstring str, unsigned char splitChar) {
  2298. struct genBstrList g;
  2299. if (str == NULL || str->data == NULL || str->slen < 0) return NULL;
  2300. g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));
  2301. if (g.bl == NULL) return NULL;
  2302. g.bl->mlen = 4;
  2303. g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring));
  2304. if (NULL == g.bl->entry) {
  2305. bstr__free (g.bl);
  2306. return NULL;
  2307. }
  2308. g.b = (bstring) str;
  2309. g.bl->qty = 0;
  2310. if (bsplitcb (str, splitChar, 0, bscb, &g) < 0) {
  2311. bstrListDestroy (g.bl);
  2312. return NULL;
  2313. }
  2314. return g.bl;
  2315. }
  2316. /* struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr)
  2317. *
  2318. * Create an array of sequential substrings from str divided by the entire
  2319. * substring splitStr.
  2320. */
  2321. struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr) {
  2322. struct genBstrList g;
  2323. if (str == NULL || str->data == NULL || str->slen < 0) return NULL;
  2324. g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));
  2325. if (g.bl == NULL) return NULL;
  2326. g.bl->mlen = 4;
  2327. g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring));
  2328. if (NULL == g.bl->entry) {
  2329. bstr__free (g.bl);
  2330. return NULL;
  2331. }
  2332. g.b = (bstring) str;
  2333. g.bl->qty = 0;
  2334. if (bsplitstrcb (str, splitStr, 0, bscb, &g) < 0) {
  2335. bstrListDestroy (g.bl);
  2336. return NULL;
  2337. }
  2338. return g.bl;
  2339. }
  2340. /* struct bstrList * bsplits (const_bstring str, bstring splitStr)
  2341. *
  2342. * Create an array of sequential substrings from str divided by any of the
  2343. * characters in splitStr. An empty splitStr causes a single entry bstrList
  2344. * containing a copy of str to be returned.
  2345. */
  2346. struct bstrList * bsplits (const_bstring str, const_bstring splitStr) {
  2347. struct genBstrList g;
  2348. if ( str == NULL || str->slen < 0 || str->data == NULL ||
  2349. splitStr == NULL || splitStr->slen < 0 || splitStr->data == NULL)
  2350. return NULL;
  2351. g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));
  2352. if (g.bl == NULL) return NULL;
  2353. g.bl->mlen = 4;
  2354. g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring));
  2355. if (NULL == g.bl->entry) {
  2356. bstr__free (g.bl);
  2357. return NULL;
  2358. }
  2359. g.b = (bstring) str;
  2360. g.bl->qty = 0;
  2361. if (bsplitscb (str, splitStr, 0, bscb, &g) < 0) {
  2362. bstrListDestroy (g.bl);
  2363. return NULL;
  2364. }
  2365. return g.bl;
  2366. }
  2367. #if defined (__TURBOC__) && !defined (__BORLANDC__)
  2368. # ifndef BSTRLIB_NOVSNP
  2369. # define BSTRLIB_NOVSNP
  2370. # endif
  2371. #endif
  2372. /* Give WATCOM C/C++, MSVC some latitude for their non-support of vsnprintf */
  2373. #if defined(__WATCOMC__) || defined(_MSC_VER)
  2374. #define exvsnprintf(r,b,n,f,a) {r = _vsnprintf (b,n,f,a);}
  2375. #else
  2376. #ifdef BSTRLIB_NOVSNP
  2377. /* This is just a hack. If you are using a system without a vsnprintf, it is
  2378. not recommended that bformat be used at all. */
  2379. #define exvsnprintf(r,b,n,f,a) {vsprintf (b,f,a); r = -1;}
  2380. #define START_VSNBUFF (256)
  2381. #else
  2382. #if defined(__GNUC__) && !defined(__APPLE__)
  2383. /* Something is making gcc complain about this prototype not being here, so
  2384. I've just gone ahead and put it in. */
  2385. extern int vsnprintf (char *buf, size_t count, const char *format, va_list arg);
  2386. #endif
  2387. #define exvsnprintf(r,b,n,f,a) {r = vsnprintf (b,n,f,a);}
  2388. #endif
  2389. #endif
  2390. #if !defined (BSTRLIB_NOVSNP)
  2391. #ifndef START_VSNBUFF
  2392. #define START_VSNBUFF (16)
  2393. #endif
  2394. /* On IRIX vsnprintf returns n-1 when the operation would overflow the target
  2395. buffer, WATCOM and MSVC both return -1, while C99 requires that the
  2396. returned value be exactly what the length would be if the buffer would be
  2397. large enough. This leads to the idea that if the return value is larger
  2398. than n, then changing n to the return value will reduce the number of
  2399. iterations required. */
  2400. /* int bformata (bstring b, const char * fmt, ...)
  2401. *
  2402. * After the first parameter, it takes the same parameters as printf (), but
  2403. * rather than outputting results to stdio, it appends the results to
  2404. * a bstring which contains what would have been output. Note that if there
  2405. * is an early generation of a '\0' character, the bstring will be truncated
  2406. * to this end point.
  2407. */
  2408. int bformata (bstring b, const char * fmt, ...) {
  2409. va_list arglist;
  2410. bstring buff;
  2411. int n, r;
  2412. if (b == NULL || fmt == NULL || b->data == NULL || b->mlen <= 0
  2413. || b->slen < 0 || b->slen > b->mlen) return BSTR_ERR;
  2414. /* Since the length is not determinable beforehand, a search is
  2415. performed using the truncating "vsnprintf" call (to avoid buffer
  2416. overflows) on increasing potential sizes for the output result. */
  2417. if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF;
  2418. if (NULL == (buff = bfromcstralloc (n + 2, ""))) {
  2419. n = 1;
  2420. if (NULL == (buff = bfromcstralloc (n + 2, ""))) return BSTR_ERR;
  2421. }
  2422. for (;;) {
  2423. va_start (arglist, fmt);
  2424. exvsnprintf (r, (char *) buff->data, n + 1, fmt, arglist);
  2425. va_end (arglist);
  2426. buff->data[n] = (unsigned char) '\0';
  2427. buff->slen = (int) (strlen) ((char *) buff->data);
  2428. if (buff->slen < n) break;
  2429. if (r > n) n = r; else n += n;
  2430. if (BSTR_OK != balloc (buff, n + 2)) {
  2431. bdestroy (buff);
  2432. return BSTR_ERR;
  2433. }
  2434. }
  2435. r = bconcat (b, buff);
  2436. bdestroy (buff);
  2437. return r;
  2438. }
  2439. /* int bassignformat (bstring b, const char * fmt, ...)
  2440. *
  2441. * After the first parameter, it takes the same parameters as printf (), but
  2442. * rather than outputting results to stdio, it outputs the results to
  2443. * the bstring parameter b. Note that if there is an early generation of a
  2444. * '\0' character, the bstring will be truncated to this end point.
  2445. */
  2446. int bassignformat (bstring b, const char * fmt, ...) {
  2447. va_list arglist;
  2448. bstring buff;
  2449. int n, r;
  2450. if (b == NULL || fmt == NULL || b->data == NULL || b->mlen <= 0
  2451. || b->slen < 0 || b->slen > b->mlen) return BSTR_ERR;
  2452. /* Since the length is not determinable beforehand, a search is
  2453. performed using the truncating "vsnprintf" call (to avoid buffer
  2454. overflows) on increasing potential sizes for the output result. */
  2455. if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF;
  2456. if (NULL == (buff = bfromcstralloc (n + 2, ""))) {
  2457. n = 1;
  2458. if (NULL == (buff = bfromcstralloc (n + 2, ""))) return BSTR_ERR;
  2459. }
  2460. for (;;) {
  2461. va_start (arglist, fmt);
  2462. exvsnprintf (r, (char *) buff->data, n + 1, fmt, arglist);
  2463. va_end (arglist);
  2464. buff->data[n] = (unsigned char) '\0';
  2465. buff->slen = (int) (strlen) ((char *) buff->data);
  2466. if (buff->slen < n) break;
  2467. if (r > n) n = r; else n += n;
  2468. if (BSTR_OK != balloc (buff, n + 2)) {
  2469. bdestroy (buff);
  2470. return BSTR_ERR;
  2471. }
  2472. }
  2473. r = bassign (b, buff);
  2474. bdestroy (buff);
  2475. return r;
  2476. }
  2477. /* bstring bformat (const char * fmt, ...)
  2478. *
  2479. * Takes the same parameters as printf (), but rather than outputting results
  2480. * to stdio, it forms a bstring which contains what would have been output.
  2481. * Note that if there is an early generation of a '\0' character, the
  2482. * bstring will be truncated to this end point.
  2483. */
  2484. bstring bformat (const char * fmt, ...) {
  2485. va_list arglist;
  2486. bstring buff;
  2487. int n, r;
  2488. if (fmt == NULL) return NULL;
  2489. /* Since the length is not determinable beforehand, a search is
  2490. performed using the truncating "vsnprintf" call (to avoid buffer
  2491. overflows) on increasing potential sizes for the output result. */
  2492. if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF;
  2493. if (NULL == (buff = bfromcstralloc (n + 2, ""))) {
  2494. n = 1;
  2495. if (NULL == (buff = bfromcstralloc (n + 2, ""))) return NULL;
  2496. }
  2497. for (;;) {
  2498. va_start (arglist, fmt);
  2499. exvsnprintf (r, (char *) buff->data, n + 1, fmt, arglist);
  2500. va_end (arglist);
  2501. buff->data[n] = (unsigned char) '\0';
  2502. buff->slen = (int) (strlen) ((char *) buff->data);
  2503. if (buff->slen < n) break;
  2504. if (r > n) n = r; else n += n;
  2505. if (BSTR_OK != balloc (buff, n + 2)) {
  2506. bdestroy (buff);
  2507. return NULL;
  2508. }
  2509. }
  2510. return buff;
  2511. }
  2512. /* int bvcformata (bstring b, int count, const char * fmt, va_list arglist)
  2513. *
  2514. * The bvcformata function formats data under control of the format control
  2515. * string fmt and attempts to append the result to b. The fmt parameter is
  2516. * the same as that of the printf function. The variable argument list is
  2517. * replaced with arglist, which has been initialized by the va_start macro.
  2518. * The size of the appended output is upper bounded by count. If the
  2519. * required output exceeds count, the string b is not augmented with any
  2520. * contents and a value below BSTR_ERR is returned. If a value below -count
  2521. * is returned then it is recommended that the negative of this value be
  2522. * used as an update to the count in a subsequent pass. On other errors,
  2523. * such as running out of memory, parameter errors or numeric wrap around
  2524. * BSTR_ERR is returned. BSTR_OK is returned when the output is successfully
  2525. * generated and appended to b.
  2526. *
  2527. * Note: There is no sanity checking of arglist, and this function is
  2528. * destructive of the contents of b from the b->slen point onward. If there
  2529. * is an early generation of a '\0' character, the bstring will be truncated
  2530. * to this end point.
  2531. */
  2532. int bvcformata (bstring b, int count, const char * fmt, va_list arg) {
  2533. int n, r, l;
  2534. if (b == NULL || fmt == NULL || count <= 0 || b->data == NULL
  2535. || b->mlen <= 0 || b->slen < 0 || b->slen > b->mlen) return BSTR_ERR;
  2536. if (count > (n = b->slen + count) + 2) return BSTR_ERR;
  2537. if (BSTR_OK != balloc (b, n + 2)) return BSTR_ERR;
  2538. exvsnprintf (r, (char *) b->data + b->slen, count + 2, fmt, arg);
  2539. /* Did the operation complete successfully within bounds? */
  2540. for (l = b->slen; l <= n; l++) {
  2541. if ('\0' == b->data[l]) {
  2542. b->slen = l;
  2543. return BSTR_OK;
  2544. }
  2545. }
  2546. /* Abort, since the buffer was not large enough. The return value
  2547. tries to help set what the retry length should be. */
  2548. b->data[b->slen] = '\0';
  2549. if (r > count + 1) { /* Does r specify a particular target length? */
  2550. n = r;
  2551. } else {
  2552. n = count + count; /* If not, just double the size of count */
  2553. if (count > n) n = INT_MAX;
  2554. }
  2555. n = -n;
  2556. if (n > BSTR_ERR-1) n = BSTR_ERR-1;
  2557. return n;
  2558. }
  2559. #endif