ChipMaster's bwBASIC This also includes history going back to v2.10. *WARN* some binary files might have been corrupted by CRLF.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bwb_str.c 7.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /***************************************************************
  2. bwb_str.c String-Management Routines
  3. for Bywater BASIC Interpreter
  4. Copyright (c) 1993, Ted A. Campbell
  5. Bywater Software
  6. email: tcamp@delphi.com
  7. Copyright and Permissions Information:
  8. All U.S. and international rights are claimed by the author,
  9. Ted A. Campbell.
  10. This software is released under the terms of the GNU General
  11. Public License (GPL), which is distributed with this software
  12. in the file "COPYING". The GPL specifies the terms under
  13. which users may copy and use the software in this distribution.
  14. A separate license is available for commercial distribution,
  15. for information on which you should contact the author.
  16. ***************************************************************/
  17. /*---------------------------------------------------------------*/
  18. /* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, */
  19. /* 11/1995 (eidetics@cerf.net). */
  20. /* */
  21. /* Those additionally marked with "DD" were at the suggestion of */
  22. /* Dale DePriest (daled@cadence.com). */
  23. /* */
  24. /* Version 3.00 by Howard Wulf, AF5NE */
  25. /* */
  26. /*---------------------------------------------------------------*/
  27. /***************************************************************
  28. WARNING - WARNING - WARNING - WARNING - WARNING - WARNING
  29. BASIC allows embedded NUL (0) characters. C str*() does not.
  30. ALL the bstring code should use mem*() and ->length, but most does not.
  31. ALL the bstring code should prevent string overflow, but most does not.
  32. ***************************************************************/
  33. #include "bwbasic.h"
  34. /***************************************************************
  35. FUNCTION: str_btob()
  36. DESCRIPTION: This C function assigns a bwBASIC string
  37. structure to another bwBASIC string
  38. structure.
  39. ***************************************************************/
  40. int
  41. str_btob(bstring * d, bstring * s)
  42. {
  43. bwx_DEBUG(__FUNCTION__);
  44. /* get memory for new buffer */
  45. /* Only one of these two conditions necessitates reallocation (JBV) */
  46. if ((d->sbuffer == NULL) || (d->rab == TRUE))
  47. {
  48. if ((d->sbuffer = (char *) CALLOC(BasicStringLengthMax + 1, 1, "str_btob")) == NULL)
  49. {
  50. bwb_error("in str_btob(): failed to get memory for new buffer");
  51. return FALSE;
  52. }
  53. }
  54. /* write the b string to the b string */
  55. if (s->length > BasicStringLengthMax)
  56. {
  57. /* INTERNAL ERROR, so truncate */
  58. s->length = BasicStringLengthMax;
  59. }
  60. d->length = s->length;
  61. memcpy(d->sbuffer, s->sbuffer, d->length);
  62. d->sbuffer[d->length] = '\0';
  63. d->rab = (char) FALSE; /* JBV */
  64. return TRUE;
  65. }
  66. /***************************************************************
  67. FUNCTION: str_ctob()
  68. DESCRIPTION: This C function assigns a null-terminated
  69. C string to a bwBASIC string structure.
  70. ***************************************************************/
  71. int
  72. str_ctob(bstring * s, char *buffer)
  73. {
  74. bwx_DEBUG(__FUNCTION__);
  75. /* get memory for new buffer */
  76. /* Only one of these two conditions necessitates reallocation (JBV) */
  77. if ((s->sbuffer == NULL) || (s->rab == TRUE))
  78. {
  79. if ((s->sbuffer = (char *) CALLOC(BasicStringLengthMax + 1, 1, "str_ctob")) == NULL)
  80. {
  81. bwb_error("in str_ctob(): failed to get memory for new buffer");
  82. return FALSE;
  83. }
  84. }
  85. /* write the c string to the b string */
  86. s->length = strlen(buffer);
  87. if (s->length > BasicStringLengthMax)
  88. {
  89. /* INTERNAL ERROR, so truncate */
  90. s->length = BasicStringLengthMax;
  91. }
  92. memcpy(s->sbuffer, buffer, s->length);
  93. s->sbuffer[s->length] = '\0';
  94. s->rab = (char) FALSE; /* JBV */
  95. return TRUE;
  96. }
  97. /***************************************************************
  98. FUNCTION: str_btoc()
  99. DESCRIPTION: This C function assigns a null-terminated
  100. C string to a bwBASIC string structure.
  101. ***************************************************************/
  102. int
  103. str_btoc(char *buffer, bstring * s)
  104. {
  105. bwx_DEBUG(__FUNCTION__);
  106. /* write the b string to the c string */
  107. if (s->length > BasicStringLengthMax)
  108. {
  109. /* INTERNAL ERROR, so truncate */
  110. s->length = BasicStringLengthMax;
  111. }
  112. memcpy(buffer, s->sbuffer, s->length);
  113. buffer[s->length] = '\0';
  114. return TRUE;
  115. }
  116. /***************************************************************
  117. FUNCTION: str_cat()
  118. DESCRIPTION: This C function performs the equivalent
  119. of the C strcat() function, using BASIC
  120. strings.
  121. ***************************************************************/
  122. bstring *
  123. str_cat(bstring * a, bstring * b)
  124. {
  125. int i;
  126. bwx_DEBUG(__FUNCTION__);
  127. if (a->length > BasicStringLengthMax)
  128. {
  129. /* INTERNAL ERROR, so truncate */
  130. a->length = BasicStringLengthMax;
  131. }
  132. if (b->length > BasicStringLengthMax)
  133. {
  134. /* INTERNAL ERROR, so truncate */
  135. b->length = BasicStringLengthMax;
  136. }
  137. i = b->length;
  138. if (a->length + i > BasicStringLengthMax)
  139. {
  140. /* INTERNAL ERROR, so truncate */
  141. i = BasicStringLengthMax - a->length;
  142. }
  143. if (i > 0)
  144. {
  145. char *t;
  146. t = a->sbuffer;
  147. t += a->length;
  148. memcpy(t, b->sbuffer, i);
  149. a->length += i;
  150. a->sbuffer[a->length] = '\0';
  151. }
  152. return a;
  153. }
  154. /***************************************************************
  155. FUNCTION: str_cmp()
  156. DESCRIPTION: This C function performs the equivalent
  157. of the C strcmp() function, using BASIC
  158. strings.
  159. ***************************************************************/
  160. int
  161. str_cmp(bstring * a, bstring * b)
  162. {
  163. bwx_DEBUG(__FUNCTION__);
  164. if (a->length > BasicStringLengthMax)
  165. {
  166. /* INTERNAL ERROR, so truncate */
  167. a->length = BasicStringLengthMax;
  168. }
  169. if (b->length > BasicStringLengthMax)
  170. {
  171. /* INTERNAL ERROR, so truncate */
  172. b->length = BasicStringLengthMax;
  173. }
  174. if (a->sbuffer == NULL)
  175. {
  176. if (b->sbuffer == NULL)
  177. {
  178. return 0;
  179. }
  180. if (b->length == 0)
  181. {
  182. return 0;
  183. }
  184. return 1;
  185. }
  186. else
  187. {
  188. a->sbuffer[a->length] = '\0';
  189. }
  190. if (b->sbuffer == NULL)
  191. {
  192. if (a->sbuffer == NULL)
  193. {
  194. return 0;
  195. }
  196. if (a->length == 0)
  197. {
  198. return 0;
  199. }
  200. return -1;
  201. }
  202. else
  203. {
  204. b->sbuffer[b->length] = '\0';
  205. }
  206. if (OptionFlags & OPTION_COMPARE_TEXT)
  207. {
  208. /* case insensitive */
  209. return strcasecmp(a->sbuffer, b->sbuffer);
  210. }
  211. else
  212. {
  213. /* case sensitive */
  214. return strcmp(a->sbuffer, b->sbuffer);
  215. }
  216. }
  217. /* EOF */