TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Robin Sheppard
from: Kurt Kuzba
date: 1998-07-06 23:34:16
subject: (Im)proper use of string

RS> GW>   it is not doing the same as a BASIC LEFT$[], which
RS> GW>   extracts a substring without modifying the original.
RS>   If writing his own memory-copy routine, he could always
RS>   include a check for 0, so he wouldn't copy more beyond
RS>   the end of the string.
   If it were an actual basic type string, it would employ a
   table of strings and a fixed string buffer. The table would
   hold the string location and size in the buffer, with any
   temporary dynamic string being the last thing in the fixed
   buffer, ready to be used or assigned as a variable.
   deleting a string would shift the entire contents of the
   buffer for immediate garbage collection, and change the
   string buffer pointers for all the strings whose pointer
   was greater than that of the string deleted by the size of
   the string deleted in a negative direction. Given such a
   construct, then, your string delete function might appear
   something like this:
/*   begin define global pointer for string data table   */
typedef struct {
   char *label;
   int size, loc;
}  STRING_DATA;
STRING_DATA *Str_Arr = NULL;
static char Dynamic_String_Buffer[32768], tempstring = "$temp$";
int gHigh_string, gString_table_size;
/*   end define global pointer for string data table   */
int Init_string_array(void)
{
   int element;
   Str_Arr = malloc(16 * sizeof(STRING_DATA));
   if(Str_Arr)
   {
      Str_Arr[0]->loc = Str_Arr[0]->size = 0;;
      Str_Arr[0]->name = NULL;
      gHigh_string = 0;
      gString_table_size = 16;
      for(element = 1; element < gString_table_size; element++
         Str_Arr[element] = Str_Arr[0];
      Str_Arr[0]->name = tempstring;
   }
   return Str_Arr;
}
void Del_String(int x)
{
   int element;
   memmove(Dynamic_String_Buffer + Str_Arr[x]->loc,
      Dynamic_String_Buffer + Str_Arr[x]->loc + Str_Arr[x]->size,
      Str_Arr[x]->size );
   for(element = 0; element < gHigh_string; element++)
      if(Str_Arr[element]->loc > Str_Arr[x]->loc)
         Str_Arr[element]->loc -= Str_Arr[x]->size;
   Str_Arr[x]->size = Str_Arr[x]->loc = 0;
   free(Str_Arr[x]->name);
   Str_Arr[x]->name = NULL;
}
   Your MID$ function would only assign the temp string a length
   equal to the size of the resulting string and move the data
   to the end of the queue. Temporary strings require no garbage
   collection routines whatsoever in this method. Reassignment
   of an existing string requires moving the data to the temp
   string, deleting the original, editing the data, reassigning
   the string's pointer to the temp buffer and assigning the
   new string size, and moving the temp to the new buffer end.
   Your string table would use realloc() in blocks of 16, so
   that you would not need to realloc() your string table for
   every new string created. Of course, this being C, you could
   use unsigned for ->size and ->loc and increase your dynamic
   string buffer to nearly 64K instead of the 32K allowed by
   such interpreters as QBasic.

> ] It's never funny when you have to explain it. [ Joe Friday ]

---
* Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)
SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728
SEEN-BY: 639/252 670/213 218
@PATH: 154/750 222 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.