home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap6 / 6_8 / newd_c / string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  4.7 KB  |  307 lines

  1.  
  2. #include "string.h"
  3.  
  4.  
  5. /* Creates a string with the initial size, and returns it */
  6.  
  7. String string_alloc(unsigned int size)
  8. {
  9.     String retVal;
  10.     
  11.     retVal = (String) malloc(sizeof(struct _string));
  12.     
  13.     /* set the size, default to 16 */
  14.     
  15.     retVal->mallocedSize = (size <= 0) ? 16 : size;
  16.     
  17.     retVal->string = (char *) malloc(sizeof(char) * retVal->mallocedSize);
  18.     
  19.     /* Initialize the string to empty */
  20.     
  21.     retVal->string[0] = '\0';
  22.     
  23.     return retVal;
  24. }
  25.  
  26. /* Frees the string and its memory */
  27.  
  28. void string_free(String str)
  29. {
  30.     if(str)
  31.     {
  32.         if(str->string) free(str->string);
  33.         
  34.         free(str);
  35.         
  36.         str = 0;
  37.     }
  38. }
  39.  
  40. int string_length(String str)
  41. {
  42.   int retVal = 0;
  43.  
  44.   if(str && str->string)
  45.     {
  46.       retVal = strlen(str->string);
  47.     }
  48.   return retVal;
  49. }
  50.  
  51. /* Sets the strings data to "", without freeing the space */
  52.  
  53. void string_empty(String str)
  54. {
  55.     if(str)
  56.     {
  57.         str->string[0] = '\0';
  58.     }
  59. }
  60.  
  61. /* Copys the data in str2 to str */
  62.  
  63. void string_setStringValue(String str,char *str2)
  64. {
  65.     string_setStringNValue(str,str2,-1);
  66. }
  67.  
  68. /* Copys the data in str2 to str , constrained by numChar*/
  69.  
  70. void string_setStringNValue(String str,char *str2, int numChar)
  71. {
  72.     if(str2){
  73.     
  74.         int newS, posL;
  75.         
  76.         /* determine the maximum possible char */
  77.         
  78.         posL = strlen(str2);
  79.         
  80.         /* Cant set more than are avaiable */
  81.         
  82.         if(numChar > posL) numChar = posL;
  83.         
  84.         /* Figure out the strings new size */
  85.             
  86.         if(numChar < 0)
  87.         {
  88.             newS = (posL+1)*sizeof(char);
  89.         }
  90.         else
  91.         {
  92.             newS = (numChar+1)*sizeof(char);
  93.         }
  94.         
  95.         /* If the string is not big enough, resize it */
  96.         
  97.         if(newS > str->mallocedSize)
  98.         {
  99.             string_setSize(str,newS,0);
  100.         }
  101.     
  102.         /* Make sure that the string is empty */
  103.         
  104.         string_empty(str);
  105.         
  106.         /* Copy the data */
  107.         
  108.         if(numChar < 0)
  109.         {
  110.             strcpy(str->string,str2);
  111.             str->string[newS] = '\0';
  112.         }
  113.         else
  114.         {
  115.             strncpy(str->string,str2,numChar);
  116.             str->string[numChar] = '\0';
  117.         }
  118.     }
  119.     else /* str2 is (char *)0 */
  120.     {
  121.     
  122.         if(str->string)
  123.         {
  124.             free(str->string);
  125.             str->string = (char *)0;
  126.         }
  127.         
  128.         str->mallocedSize = 0;
  129.     }
  130. }
  131.  
  132.  
  133. /* Copys the strings data and returns it*/
  134.  
  135. char * string_copyValue(String str)
  136. {
  137.     char *retVal = (char *) 0;
  138.     
  139.     if(str && str->string)
  140.     {
  141.         int len;
  142.         
  143.         len = strlen(str->string);
  144.         
  145.         retVal = (char *) malloc(sizeof(char) * (len +1));
  146.         
  147.         strcpy(retVal,str->string);
  148.     }
  149.     return retVal;
  150. }
  151.  
  152.  
  153. /* Resizes the String, will use realloc or malloc based on freeString */
  154.  
  155. void string_setSize(String str,unsigned int size,int freeString)
  156. {
  157.     if(size > 0){
  158.  
  159.         str->mallocedSize = size;
  160.     
  161.         if(freeString && str->string)
  162.         {
  163.             free(str->string);
  164.             str->string = (char *)0;
  165.         }
  166.         
  167.         if(str->string)
  168.         {
  169.             str->string = (char *) realloc(str->string, sizeof(char) * str->mallocedSize);
  170.         }
  171.         else
  172.         {
  173.             str->string = (char *)malloc(sizeof(char) * str->mallocedSize);
  174.         }
  175.     }
  176. }
  177.  
  178. void string_appendChar(String str,char c)
  179. {
  180.     char buffer[2];
  181.     
  182.     buffer[0] = c;
  183.     buffer[1] = '\0';
  184.     
  185.     string_appendString(str,buffer);
  186. }
  187.  
  188. void string_appendString(String str1,const char *str2)
  189. {
  190.     string_appendNString(str1,str2,-1);
  191. }
  192.  
  193. void string_appendNString(String str1,const char *str2,int numChar)
  194. {
  195.     if(str1->string && str2){
  196.         
  197.         int newS,posL;
  198.         int curSize;
  199.         
  200.         /* Determine the possible characters to add */
  201.         
  202.         posL = strlen(str2);
  203.         
  204.         /* Determine our current length */
  205.         
  206.         curSize = strlen(str1->string);
  207.         
  208.         /* Make sure that we are not trying to add more than possible */
  209.         
  210.         if(numChar > posL) numChar = posL;
  211.  
  212.         /*
  213.          * If numChar < 0, add the entire string
  214.          * Store the new size in newS, we need this to determine
  215.          * if we need to allocate more space.
  216.          */
  217.         
  218.         if(numChar < 0)
  219.         {
  220.             newS = ((posL+1+curSize)*sizeof(char));
  221.         }
  222.         else
  223.         {
  224.             newS = ((numChar+1+curSize)*sizeof(char));
  225.         }
  226.         
  227.         /* Determine if we need to resize, and do it */
  228.         
  229.         if(newS > str1->mallocedSize)
  230.         {
  231.             string_setSize(str1,newS,0);
  232.         }
  233.  
  234.         /* Append the characters */
  235.  
  236.         if(numChar < 0)
  237.         {
  238.             strcat(str1->string,str2);
  239.         }
  240.         else
  241.         {
  242.             strncat(str1->string,str2, numChar);
  243.         }
  244.         
  245.     }
  246. }
  247.  
  248. /* Convert the string to upper case */
  249.  
  250. void string_toUpper(String str)
  251. {
  252.     if(str && str->string)
  253.     {
  254.         int i,max;
  255.         
  256.         max = strlen(str->string);
  257.         
  258.         for(i=0;i<max;i++)
  259.         {
  260.             str->string[i] = (char)toupper(str->string[i]);
  261.         }
  262.     }
  263. }
  264.  
  265. /* Remove the first n characters */
  266.  
  267. void string_crop(String str, int n)
  268. {
  269.     if(str && str->string)
  270.     {
  271.         int curL;
  272.         char *stopStr = (char *)0;
  273.         char *iterStr = (char *)0;
  274.         
  275.         curL = strlen(str->string);
  276.             
  277.         if(str->string && (n>0) && (n <= curL))
  278.         {
  279.             iterStr = str->string;
  280.             stopStr = str->string + curL - n;
  281.             
  282.             while(iterStr <= stopStr)
  283.             {
  284.                 iterStr[0] = iterStr[n];
  285.                 iterStr++;
  286.             }
  287.         }
  288.     }
  289. }
  290.  
  291. /* Remove the last n characters */
  292.  
  293. void string_chop(String str, int n)
  294. {
  295.     if(str && str->string)
  296.     {
  297.         int len;
  298.     
  299.         len = strlen(str->string);
  300.         
  301.         if(n < len)
  302.         {
  303.             str->string[n] = '\0';
  304.         }
  305.     }
  306. }
  307.