home *** CD-ROM | disk | FTP | other *** search
-
- #include "string.h"
-
-
- /* Creates a string with the initial size, and returns it */
-
- String string_alloc(unsigned int size)
- {
- String retVal;
-
- retVal = (String) malloc(sizeof(struct _string));
-
- /* set the size, default to 16 */
-
- retVal->mallocedSize = (size <= 0) ? 16 : size;
-
- retVal->string = (char *) malloc(sizeof(char) * retVal->mallocedSize);
-
- /* Initialize the string to empty */
-
- retVal->string[0] = '\0';
-
- return retVal;
- }
-
- /* Frees the string and its memory */
-
- void string_free(String str)
- {
- if(str)
- {
- if(str->string) free(str->string);
-
- free(str);
-
- str = 0;
- }
- }
-
- int string_length(String str)
- {
- int retVal = 0;
-
- if(str && str->string)
- {
- retVal = strlen(str->string);
- }
- return retVal;
- }
-
- /* Sets the strings data to "", without freeing the space */
-
- void string_empty(String str)
- {
- if(str)
- {
- str->string[0] = '\0';
- }
- }
-
- /* Copys the data in str2 to str */
-
- void string_setStringValue(String str,char *str2)
- {
- string_setStringNValue(str,str2,-1);
- }
-
- /* Copys the data in str2 to str , constrained by numChar*/
-
- void string_setStringNValue(String str,char *str2, int numChar)
- {
- if(str2){
-
- int newS, posL;
-
- /* determine the maximum possible char */
-
- posL = strlen(str2);
-
- /* Cant set more than are avaiable */
-
- if(numChar > posL) numChar = posL;
-
- /* Figure out the strings new size */
-
- if(numChar < 0)
- {
- newS = (posL+1)*sizeof(char);
- }
- else
- {
- newS = (numChar+1)*sizeof(char);
- }
-
- /* If the string is not big enough, resize it */
-
- if(newS > str->mallocedSize)
- {
- string_setSize(str,newS,0);
- }
-
- /* Make sure that the string is empty */
-
- string_empty(str);
-
- /* Copy the data */
-
- if(numChar < 0)
- {
- strcpy(str->string,str2);
- str->string[newS] = '\0';
- }
- else
- {
- strncpy(str->string,str2,numChar);
- str->string[numChar] = '\0';
- }
- }
- else /* str2 is (char *)0 */
- {
-
- if(str->string)
- {
- free(str->string);
- str->string = (char *)0;
- }
-
- str->mallocedSize = 0;
- }
- }
-
-
- /* Copys the strings data and returns it*/
-
- char * string_copyValue(String str)
- {
- char *retVal = (char *) 0;
-
- if(str && str->string)
- {
- int len;
-
- len = strlen(str->string);
-
- retVal = (char *) malloc(sizeof(char) * (len +1));
-
- strcpy(retVal,str->string);
- }
- return retVal;
- }
-
-
- /* Resizes the String, will use realloc or malloc based on freeString */
-
- void string_setSize(String str,unsigned int size,int freeString)
- {
- if(size > 0){
-
- str->mallocedSize = size;
-
- if(freeString && str->string)
- {
- free(str->string);
- str->string = (char *)0;
- }
-
- if(str->string)
- {
- str->string = (char *) realloc(str->string, sizeof(char) * str->mallocedSize);
- }
- else
- {
- str->string = (char *)malloc(sizeof(char) * str->mallocedSize);
- }
- }
- }
-
- void string_appendChar(String str,char c)
- {
- char buffer[2];
-
- buffer[0] = c;
- buffer[1] = '\0';
-
- string_appendString(str,buffer);
- }
-
- void string_appendString(String str1,const char *str2)
- {
- string_appendNString(str1,str2,-1);
- }
-
- void string_appendNString(String str1,const char *str2,int numChar)
- {
- if(str1->string && str2){
-
- int newS,posL;
- int curSize;
-
- /* Determine the possible characters to add */
-
- posL = strlen(str2);
-
- /* Determine our current length */
-
- curSize = strlen(str1->string);
-
- /* Make sure that we are not trying to add more than possible */
-
- if(numChar > posL) numChar = posL;
-
- /*
- * If numChar < 0, add the entire string
- * Store the new size in newS, we need this to determine
- * if we need to allocate more space.
- */
-
- if(numChar < 0)
- {
- newS = ((posL+1+curSize)*sizeof(char));
- }
- else
- {
- newS = ((numChar+1+curSize)*sizeof(char));
- }
-
- /* Determine if we need to resize, and do it */
-
- if(newS > str1->mallocedSize)
- {
- string_setSize(str1,newS,0);
- }
-
- /* Append the characters */
-
- if(numChar < 0)
- {
- strcat(str1->string,str2);
- }
- else
- {
- strncat(str1->string,str2, numChar);
- }
-
- }
- }
-
- /* Convert the string to upper case */
-
- void string_toUpper(String str)
- {
- if(str && str->string)
- {
- int i,max;
-
- max = strlen(str->string);
-
- for(i=0;i<max;i++)
- {
- str->string[i] = (char)toupper(str->string[i]);
- }
- }
- }
-
- /* Remove the first n characters */
-
- void string_crop(String str, int n)
- {
- if(str && str->string)
- {
- int curL;
- char *stopStr = (char *)0;
- char *iterStr = (char *)0;
-
- curL = strlen(str->string);
-
- if(str->string && (n>0) && (n <= curL))
- {
- iterStr = str->string;
- stopStr = str->string + curL - n;
-
- while(iterStr <= stopStr)
- {
- iterStr[0] = iterStr[n];
- iterStr++;
- }
- }
- }
- }
-
- /* Remove the last n characters */
-
- void string_chop(String str, int n)
- {
- if(str && str->string)
- {
- int len;
-
- len = strlen(str->string);
-
- if(n < len)
- {
- str->string[n] = '\0';
- }
- }
- }
-