string - format of BASIC strings


#include <basic/string.h>


BASIC strings require a header as follows:
/*	Basmark BASIC Compiler
 * Copyright 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992
 *	by Basmark Corporation
 *	Cleveland, Ohio, USA	*/

struct string {	/* BASIC string */
	short	str_len, str_flag;      /* length, deallocatable? */
	char	*str_text;		/* pointer to text */
BASIC strings may contain null characters and must be capable of pointing into a random file buffer (so no terminator may be present).  Therefore the length of the string is maintained explicitly in str_len

Str_text points to the text of the string.  If dynamically allocated, this space is obtained through malloc(3) and ought to be freed when the space is no longer needed.  There are three circumstances in which the space is not allocated in this way: (1) if the string is a constant or has not yet been used, (2) if it points into a random file buffer, (3) if it is a null string.  Thus str_flag is maintained to signal if malloc(3) was used and therefore if free(3) may be used to deallocate the space. 

Strings are passed to functions only by address since the function will need to manipulate the header.  Generally, functions which return strings return the whole header structure so that a “static” structure needn’t be declared.  This is mainly for convenience: on most systems we have seen, a “static” structure is created automatically and a pointer is returned and the calling code must collect the structure using the pointer in the same way. 




cstring(str), string(str)
malloc(3), free(3) in the UNIX Programmer’s Manual

from The Basmark QuickBASIC Programmer’s Manual by Lawrence Leinweber