diff --git a/golded3/gedoit.cpp b/golded3/gedoit.cpp index 9f92638..ada192d 100644 --- a/golded3/gedoit.cpp +++ b/golded3/gedoit.cpp @@ -87,9 +87,8 @@ void SaveLines(int mode, const char* savefile, GMsg* msg, int margin, bool clip) while(line) { uint lineisctrl = line->type & (GLINE_TEAR|GLINE_ORIG|GLINE_KLUDGE); if(not ((mode == MODE_SAVENOCTRL) and lineisctrl)) { - std::string::iterator p = line->txt.begin(); - while(p != line->txt.end()) { - if(mode == MODE_WRITE) { + if(mode == MODE_WRITE) { + for(std::string::iterator p = line->txt.begin(); p != line->txt.end(); p++) { // Replace control codes, except the ANSI escape code if(iscntrl(*p)) { // only allow ESC in file write @@ -98,7 +97,6 @@ void SaveLines(int mode, const char* savefile, GMsg* msg, int margin, bool clip) } } } - p++; } const char *ptr = line->txt.c_str(); fwrite(ptr, strlen(ptr), 1, prnfp); diff --git a/golded3/getpls.cpp b/golded3/getpls.cpp index 3aeb144..66fc086 100644 --- a/golded3/getpls.cpp +++ b/golded3/getpls.cpp @@ -33,6 +33,7 @@ extern GMsg* reader_msg; extern bool reader_gen_confirm; +const int REALLOC_CACHE_SIZE = 4096; // ------------------------------------------------------------------ @@ -307,6 +308,9 @@ int TemplateToText(int mode, GMsg* msg, GMsg* oldmsg, const char* tpl, int origa msg->txt = throw_strdup(buf); len = size = pos = 0; + size_t oldmsg_size = oldmsg->txt ? strlen(oldmsg->txt) : REALLOC_CACHE_SIZE; + size_t msg_txt_realloc_cache = 0; + while(fgets(buf, sizeof(buf), fp)) { ptr = strskip_wht(buf); if(*ptr != ';') { @@ -583,7 +587,13 @@ int TemplateToText(int mode, GMsg* msg, GMsg* oldmsg, const char* tpl, int origa strcat(buf, "\r"); len = strlen(buf); size += len; - msg->txt = (char*)throw_realloc(msg->txt, size+10); + if(msg_txt_realloc_cache >= len) { + msg_txt_realloc_cache -= len; + } + else { + msg_txt_realloc_cache += REALLOC_CACHE_SIZE; + msg->txt = (char*)throw_realloc(msg->txt, size+10+msg_txt_realloc_cache); + } strcpy(&(msg->txt[pos]), buf); pos += len; } @@ -611,7 +621,13 @@ int TemplateToText(int mode, GMsg* msg, GMsg* oldmsg, const char* tpl, int origa strcat(buf, "\r"); len = strlen(buf); size += len; - msg->txt = (char*)throw_realloc(msg->txt, size+10); + if(msg_txt_realloc_cache >= len) { + msg_txt_realloc_cache -= len; + } + else { + msg_txt_realloc_cache += REALLOC_CACHE_SIZE; + msg->txt = (char*)throw_realloc(msg->txt, size+10+msg_txt_realloc_cache); + } strcpy(&(msg->txt[pos]), buf); pos += len; } @@ -727,7 +743,13 @@ int TemplateToText(int mode, GMsg* msg, GMsg* oldmsg, const char* tpl, int origa strcat(buf, "\r"); len = strlen(buf); size += len; - msg->txt = (char*)throw_realloc(msg->txt, size+10); + if(msg_txt_realloc_cache >= len) { + msg_txt_realloc_cache -= len; + } + else { + msg_txt_realloc_cache += REALLOC_CACHE_SIZE; + msg->txt = (char*)throw_realloc(msg->txt, size+10+msg_txt_realloc_cache); + } strcpy(&(msg->txt[pos]), buf); pos += len; } @@ -765,7 +787,13 @@ int TemplateToText(int mode, GMsg* msg, GMsg* oldmsg, const char* tpl, int origa } len = strlen(buf); size += len; - msg->txt = (char*)throw_realloc(msg->txt, size+10); + if(msg_txt_realloc_cache >= (len+1)) { + msg_txt_realloc_cache -= (len+1); + } + else { + msg_txt_realloc_cache += (size <= oldmsg_size) ? oldmsg_size : REALLOC_CACHE_SIZE; + msg->txt = (char*)throw_realloc(msg->txt, size+10+msg_txt_realloc_cache); + } strcpy(&(msg->txt[pos]), buf); pos += len; if(oldmsg->line[n]->type & GLINE_HARD) { @@ -796,7 +824,13 @@ int TemplateToText(int mode, GMsg* msg, GMsg* oldmsg, const char* tpl, int origa TokenXlat(mode, buf, msg, oldmsg, origarea); len = strlen(buf); size += len; - msg->txt = (char*)throw_realloc(msg->txt, size+10); + if(msg_txt_realloc_cache >= len) { + msg_txt_realloc_cache -= len; + } + else { + msg_txt_realloc_cache += REALLOC_CACHE_SIZE; + msg->txt = (char*)throw_realloc(msg->txt, size+10+msg_txt_realloc_cache); + } strcpy(&(msg->txt[pos]), buf); pos += len; } @@ -841,7 +875,13 @@ int TemplateToText(int mode, GMsg* msg, GMsg* oldmsg, const char* tpl, int origa TokenXlat(mode, buf, msg, oldmsg, origarea); len = strlen(buf); size += len; - msg->txt = (char*)throw_realloc(msg->txt, size+10); + if(msg_txt_realloc_cache >= len) { + msg_txt_realloc_cache -= len; + } + else { + msg_txt_realloc_cache += REALLOC_CACHE_SIZE; + msg->txt = (char*)throw_realloc(msg->txt, size+10+msg_txt_realloc_cache); + } strcpy(&(msg->txt[pos]), buf); pos += len; }