+prvm_stringbuffer_t *BufStr_FindCreateReplace (prvm_prog_t *prog, int bufindex, int flags, const char *format)
+{
+ prvm_stringbuffer_t *stringbuffer;
+ int i;
+
+ if (bufindex < 0)
+ return NULL;
+
+ // find buffer with wanted index
+ if (bufindex < (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray))
+ {
+ if ( (stringbuffer = (prvm_stringbuffer_t*) Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, bufindex)) )
+ {
+ if (stringbuffer->flags & STRINGBUFFER_TEMP)
+ stringbuffer->flags = flags; // created but has not been used yet
+ return stringbuffer;
+ }
+ return NULL;
+ }
+
+ // allocate new buffer with wanted index
+ while(1)
+ {
+ stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
+ stringbuffer->flags = STRINGBUFFER_TEMP;
+ for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
+ if (i == bufindex)
+ {
+ stringbuffer->flags = flags; // mark as used
+ break;
+ }
+ }
+ return stringbuffer;
+}
+
+void BufStr_Set(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex, const char *str)
+{
+ size_t alloclen;
+
+ if (!stringbuffer || strindex < 0)
+ return;
+
+ BufStr_Expand(prog, stringbuffer, strindex);
+ stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
+ if (stringbuffer->strings[strindex])
+ Mem_Free(stringbuffer->strings[strindex]);
+ stringbuffer->strings[strindex] = NULL;
+
+ if (str)
+ {
+ // not the NULL string!
+ alloclen = strlen(str) + 1;
+ stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
+ memcpy(stringbuffer->strings[strindex], str, alloclen);
+ }
+
+ BufStr_Shrink(prog, stringbuffer);
+}
+
+void BufStr_Del(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer)
+{
+ int i;
+
+ if (!stringbuffer)
+ return;
+
+ for (i = 0;i < stringbuffer->num_strings;i++)
+ if (stringbuffer->strings[i])
+ Mem_Free(stringbuffer->strings[i]);
+ if (stringbuffer->strings)
+ Mem_Free(stringbuffer->strings);
+ if(stringbuffer->origin)
+ PRVM_Free((char *)stringbuffer->origin);
+ Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
+}
+
+void BufStr_Flush(prvm_prog_t *prog)
+{
+ prvm_stringbuffer_t *stringbuffer;
+ int i, numbuffers;
+
+ numbuffers = (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray);
+ for (i = 0; i < numbuffers; i++)
+ if ( (stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i)) )
+ BufStr_Del(prog, stringbuffer);
+ Mem_ExpandableArray_NewArray(&prog->stringbuffersarray, prog->progs_mempool, sizeof(prvm_stringbuffer_t), 64);
+}
+