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