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.
 
 
 
 
 
 

355 lines
7.2 KiB

  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. #include <stdio.h>
  18. #include "bwbasic.h"
  19. #include "bwb_mes.h"
  20. #if INTENSIVE_DEBUG || TEST_BSTRING
  21. static char tbuf[ MAXSTRINGSIZE + 1 ];
  22. #endif
  23. /***************************************************************
  24. FUNCTION: str_btob()
  25. DESCRIPTION: This C function assigns a bwBASIC string
  26. structure to another bwBASIC string
  27. structure.
  28. ***************************************************************/
  29. #if ANSI_C
  30. int
  31. str_btob( bstring *d, bstring *s )
  32. #else
  33. int
  34. str_btob( d, s )
  35. bstring *d;
  36. bstring *s;
  37. #endif
  38. {
  39. char *t;
  40. register int i;
  41. #if TEST_BSTRING
  42. sprintf( tbuf, "in str_btob(): entry, source b string name is <%s>", s->name );
  43. bwb_debug( tbuf );
  44. sprintf( tbuf, "in str_btob(): entry, destination b string name is <%s>", d->name );
  45. bwb_debug( tbuf );
  46. #endif
  47. /* get memory for new buffer */
  48. if ( ( t = (char *) calloc( s->length + 1, 1 )) == NULL )
  49. {
  50. #if PROG_ERRORS
  51. bwb_error( "in str_btob(): failed to get memory for new buffer" );
  52. #else
  53. bwb_error( err_getmem );
  54. #endif
  55. return FALSE;
  56. }
  57. /* write the c string to the b string */
  58. t[ 0 ] = '\0';
  59. for ( i = 0; i < (int) s->length; ++i )
  60. {
  61. t[ i ] = s->sbuffer[ i ];
  62. #if INTENSIVE_DEBUG
  63. tbuf[ i ] = s->sbuffer[ i ];
  64. tbuf[ i + 1 ] = '\0';
  65. #endif
  66. }
  67. /* deallocate old memory */
  68. #if INTENSIVE_DEBUG
  69. if ( d->rab == TRUE )
  70. {
  71. sprintf( bwb_ebuf, "in str_btob(): reallocating RAB" );
  72. bwb_debug( bwb_ebuf );
  73. }
  74. #endif
  75. if (( d->rab != TRUE ) && ( d->sbuffer != NULL ))
  76. {
  77. #if INTENSIVE_DEBUG
  78. sprintf( tbuf, "in str_btob(): deallocating string memory" );
  79. bwb_debug ( tbuf );
  80. #endif
  81. free( d->sbuffer );
  82. }
  83. else
  84. {
  85. d->rab = (char) FALSE;
  86. }
  87. /* reassign buffer */
  88. d->sbuffer = t;
  89. /* reassign length */
  90. d->length = s->length;
  91. #if INTENSIVE_DEBUG
  92. sprintf( bwb_ebuf, "in str_btob(): exit length <%d> string <%s>",
  93. d->length, tbuf );
  94. bwb_debug( bwb_ebuf );
  95. #endif
  96. /* return */
  97. return TRUE;
  98. }
  99. /***************************************************************
  100. FUNCTION: str_ctob()
  101. DESCRIPTION: This C function assigns a null-terminated
  102. C string to a bwBASIC string structure.
  103. ***************************************************************/
  104. #if ANSI_C
  105. int
  106. str_ctob( bstring *s, char *buffer )
  107. #else
  108. int
  109. str_ctob( s, buffer )
  110. bstring *s;
  111. char *buffer;
  112. #endif
  113. {
  114. char *t;
  115. register int i;
  116. #if INTENSIVE_DEBUG
  117. sprintf( tbuf, "in str_ctob(): entry, c string is <%s>", buffer );
  118. bwb_debug( tbuf );
  119. #endif
  120. #if TEST_BSTRING
  121. sprintf( tbuf, "in str_ctob(): entry, b string name is <%s>", s->name );
  122. bwb_debug( tbuf );
  123. #endif
  124. /* get memory for new buffer */
  125. if ( ( t = (char *) calloc( strlen( buffer ) + 1, 1 )) == NULL )
  126. {
  127. #if PROG_ERRORS
  128. bwb_error( "in str_ctob(): failed to get memory for new buffer" );
  129. #else
  130. bwb_error( err_getmem );
  131. #endif
  132. return FALSE;
  133. }
  134. /* write the c string to the b string */
  135. t[ 0 ] = '\0';
  136. for ( i = 0; i < (int) strlen( buffer ); ++i )
  137. {
  138. t[ i ] = buffer[ i ];
  139. #if INTENSIVE_DEBUG
  140. tbuf[ i ] = buffer[ i ];
  141. tbuf[ i + 1 ] = '\0';
  142. #endif
  143. }
  144. /* deallocate old memory */
  145. #if INTENSIVE_DEBUG
  146. if ( s->rab == TRUE )
  147. {
  148. sprintf( bwb_ebuf, "in str_ctob(): reallocating RAB" );
  149. bwb_debug( bwb_ebuf );
  150. }
  151. #endif
  152. if (( s->rab != TRUE ) && ( s->sbuffer != NULL ))
  153. {
  154. free( s->sbuffer );
  155. }
  156. else
  157. {
  158. s->rab = (char) FALSE;
  159. }
  160. /* reassign buffer */
  161. s->sbuffer = t;
  162. /* reassign length */
  163. s->length = (unsigned char) strlen( buffer );
  164. #if INTENSIVE_DEBUG
  165. sprintf( bwb_ebuf, "in str_ctob(): exit length <%d> string <%s>",
  166. s->length, tbuf );
  167. bwb_debug( bwb_ebuf );
  168. #endif
  169. /* return */
  170. return TRUE;
  171. }
  172. /***************************************************************
  173. FUNCTION: str_btoc()
  174. DESCRIPTION: This C function assigns a null-terminated
  175. C string to a bwBASIC string structure.
  176. ***************************************************************/
  177. #if ANSI_C
  178. int
  179. str_btoc( char *buffer, bstring *s )
  180. #else
  181. int
  182. str_btoc( buffer, s )
  183. char *buffer;
  184. bstring *s;
  185. #endif
  186. {
  187. register int i;
  188. #if INTENSIVE_DEBUG
  189. sprintf( tbuf, "in str_btoc(): entry, b string length is <%d>",
  190. s->length );
  191. bwb_debug( tbuf );
  192. #endif
  193. #if TEST_BSTRING
  194. sprintf( tbuf, "in str_btoc(): entry, b string name is <%s>", s->name );
  195. bwb_debug( tbuf );
  196. #endif
  197. /* write the b string to the c string */
  198. buffer[ 0 ] = '\0';
  199. for ( i = 0; i < (int) s->length; ++i )
  200. {
  201. buffer[ i ] = s->sbuffer[ i ];
  202. buffer[ i + 1 ] = '\0';
  203. if ( i >= MAXSTRINGSIZE )
  204. {
  205. i = s->length + 1;
  206. }
  207. }
  208. #if INTENSIVE_DEBUG
  209. sprintf( tbuf, "in str_btoc(): exit, c string is <%s>", buffer );
  210. bwb_debug( tbuf );
  211. #endif
  212. /* return */
  213. return TRUE;
  214. }
  215. /***************************************************************
  216. FUNCTION: str_cat()
  217. DESCRIPTION: This C function performs the equivalent
  218. of the C strcat() function, using BASIC
  219. strings.
  220. ***************************************************************/
  221. #if ANSI_C
  222. char *
  223. str_cat( bstring *a, bstring *b )
  224. #else
  225. char *
  226. str_cat( a, b )
  227. bstring *a;
  228. bstring *b;
  229. #endif
  230. {
  231. char abuf[ MAXSTRINGSIZE + 1 ];
  232. char bbuf[ MAXSTRINGSIZE + 1 ];
  233. char *r;
  234. str_btoc( abuf, a );
  235. str_btoc( bbuf, b );
  236. #if INTENSIVE_DEBUG
  237. sprintf( bwb_ebuf, "in str_cat(): a <%s> b <%s>", abuf, bbuf );
  238. bwb_debug( bwb_ebuf );
  239. #endif
  240. strcat( abuf, bbuf );
  241. str_ctob( a, abuf );
  242. #if INTENSIVE_DEBUG
  243. sprintf( bwb_ebuf, "in str_cat(): returns <%s>", abuf );
  244. bwb_debug( bwb_ebuf );
  245. #endif
  246. return r;
  247. }
  248. /***************************************************************
  249. FUNCTION: str_cmp()
  250. DESCRIPTION: This C function performs the equivalent
  251. of the C strcmp() function, using BASIC
  252. strings.
  253. ***************************************************************/
  254. #if ANSI_C
  255. int
  256. str_cmp( bstring *a, bstring *b )
  257. #else
  258. int
  259. str_cmp( a, b )
  260. bstring *a;
  261. bstring *b;
  262. #endif
  263. {
  264. char abuf[ MAXSTRINGSIZE + 1 ];
  265. char bbuf[ MAXSTRINGSIZE + 1 ];
  266. str_btoc( abuf, a );
  267. str_btoc( bbuf, b );
  268. return strcmp( abuf, bbuf );
  269. }
  270.