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.
 
 
 
 
 
 

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