home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / mststr.c < prev    next >
C/C++ Source or Header  |  1992-02-15  |  4KB  |  183 lines

  1.  
  2. /***********************************************************************
  3.  *
  4.  * String Functions
  5.  *
  6.  ***********************************************************************/
  7.  
  8. /***********************************************************************
  9.  *
  10.  * Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11.  * Written by Steve Byrne.
  12.  *
  13.  * This file is part of GNU Smalltalk.
  14.  *
  15.  * GNU Smalltalk is free software; you can redistribute it and/or modify it
  16.  * under the terms of the GNU General Public License as published by the Free
  17.  * Software Foundation; either version 1, or (at your option) any later 
  18.  * version.
  19.  * 
  20.  * GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  21.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  22.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  23.  * more details.
  24.  * 
  25.  * You should have received a copy of the GNU General Public License along with
  26.  * GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  27.  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  28.  *
  29.  ***********************************************************************/
  30.  
  31.  
  32. /*
  33.  *    Change Log
  34.  * ============================================================================
  35.  * Author      Date       Change 
  36.  *
  37.  * sbyrne    27 Dec 88    created.
  38.  */
  39.  
  40.  
  41. #include <stdio.h>
  42. #include "mststr.h"
  43.  
  44. #define    STRING_GRANULARITY    128
  45.  
  46. static void    reallocStrBase();
  47.  
  48. static char    *strBase = NULL, *strPtr;
  49. static long    maxStrLen;
  50.  
  51.  
  52. /*
  53.  *    void initStrBuf()
  54.  *
  55.  * Description
  56.  *
  57.  *    Initializes the string buffer to accumulate new characters.
  58.  *
  59.  */
  60. void initStrBuf()
  61. {
  62.   if (strBase == NULL) {
  63.     reallocStrBase(0L, STRING_GRANULARITY);
  64.   }
  65.   
  66.   strPtr = strBase;
  67. }
  68.  
  69. /*
  70.  *    char *curStrBuf()
  71.  *
  72.  * Description
  73.  *
  74.  *    Returns the currently accumulated string, as a C string.  Note that the
  75.  *    actual string returned is not a unique string, and should be copied as
  76.  *    soon as possible.
  77.  *
  78.  * Outputs
  79.  *
  80.  *    Pointer to NUL terminated C string that is the accumulated string.
  81.  */
  82. char *curStrBuf()
  83. {
  84.   addStrBufChar('\0');
  85.   return (strBase);
  86. }
  87.  
  88. /*
  89.  *    void addStrBufChar(c)
  90.  *
  91.  * Description
  92.  *
  93.  *    Adds a character "c" to the string being accumulated.  The character can
  94.  *    be any valid ASCII character.
  95.  *
  96.  * Inputs
  97.  *
  98.  *    c     : the character to add to the string.
  99.  *
  100.  */
  101. void addStrBufChar(c)
  102. char    c;
  103. {
  104.   if (strPtr - strBase  >= maxStrLen) {
  105.     reallocStrBase(maxStrLen, STRING_GRANULARITY);
  106.   }
  107.  
  108.   *strPtr++ = c;
  109. }
  110.  
  111.  
  112. /*
  113.  *    static void reallocStrBase(len, delta)
  114.  *
  115.  * Description
  116.  *
  117.  *    Called to allocate a new string to be accumulated.  If there is an
  118.  *    existing string, it is copied into the new string and then free;
  119.  *    otherwise the new string is created.
  120.  *
  121.  * Inputs
  122.  *
  123.  *    len   : current length of current string, in bytes
  124.  *    delta : increment to add to string length in bytes.  New string length
  125.  *        is len+delta.
  126.  *
  127.  * Outputs
  128.  *
  129.  *    strBase, maxStrLen, and strPtr are all globals that are affected by
  130.  *    this routine.
  131.  */
  132. static void reallocStrBase(len, delta)
  133. long    len;
  134. int    delta;
  135. {
  136.   char        *newStr;
  137.   long        l;
  138.  
  139.   maxStrLen = len + delta;
  140.   newStr = (char *)malloc(maxStrLen);
  141.  
  142.   if (strBase) {
  143.     l = strPtr - strBase;
  144.     strncpy(newStr, strBase, len);
  145.     free(strBase);
  146.   } else {
  147.     l = 0L;
  148.   }
  149.  
  150.   strBase = newStr;
  151.   strPtr = strBase + l;
  152. }
  153.  
  154.  
  155. /*
  156.  *    char *copyStr(str)
  157.  *
  158.  * Description
  159.  *
  160.  *    Returns a newly allocated copy of the string "str".
  161.  *
  162.  * Inputs
  163.  *
  164.  *    str   : NUL terminated C string to be copied.
  165.  *
  166.  * Outputs
  167.  *
  168.  *    Pointer to new NUL terminated C string that is a copy of "str".
  169.  */
  170. char *copyStr(str)
  171. char    *str;
  172. {
  173.   char        *newStr;
  174.   long        l;
  175.  
  176.   l = strlen(str) + 1;
  177.  
  178.   newStr = (char *)malloc(l);
  179.   strncpy(newStr, str, l);
  180.  
  181.   return (newStr);
  182. }
  183.