/home/dko/projects/mobilec/trunk/src/dynstring.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include "include/dynstring.h"
00005 dynstring_t* dynstring_New(void)
00006 {
00007   dynstring_t* message;
00008   message = (dynstring_t*)malloc(sizeof(dynstring_t));
00009   message->len = 0;
00010   message->size = COMPOSE_BLOCKSIZE;
00011   message->message = (char*)malloc(sizeof(char) * message->size);
00012   if (message->message == NULL) {
00013     fprintf(stderr, "Memory error. %s:%d\n", __FILE__, __LINE__);
00014     exit(0);
00015   }
00016   memset(message->message, 0, message->size);
00017   
00018   return message;
00019 }
00020 
00021 int dynstring_Append(dynstring_t* msg, char* str)
00022 {
00023   char* tmp;
00024   /* Check to see if we need to reallocate the message buffer */
00025   while ( (strlen(str)+4) > (msg->size - msg->len) ) {
00026     /* Increase the size of the buffer by COMPOSE_BLOCKSIZE */
00027     tmp = (char*)malloc(
00028         sizeof(char) * 
00029         (msg->size + COMPOSE_BLOCKSIZE)
00030         );
00031     if (tmp == NULL) {
00032       fprintf(stderr, "Memory Error. %s:%d\n", __FILE__, __LINE__);
00033       exit(0);
00034     }
00035     msg->size = msg->size + COMPOSE_BLOCKSIZE;
00036     strcpy(tmp, msg->message);
00037     free(msg->message);
00038     msg->message = tmp;
00039   }
00040   strcat(msg->message, str);
00041   msg->len += strlen(str);
00042 
00043   return 0;
00044 }
00045 
00046 int dynstring_Destroy(dynstring_t* dynstring)
00047 {
00048   free(dynstring->message);
00049   free(dynstring);
00050   return 0;
00051 }

Generated on Fri May 16 14:49:54 2008 for Mobile-C by  doxygen 1.5.4