Skip to content

Commit

Permalink
String: Improved interface to allow chaining of init functions
Browse files Browse the repository at this point in the history
For example; Str_Set(Str_Init(&str), "sometext");
  • Loading branch information
danij-deng committed Feb 22, 2012
1 parent b687a8d commit d573039
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions doomsday/engine/api/dd_string.h
Expand Up @@ -104,20 +104,20 @@ ddstring_t* Str_NewFromReader(Reader* reader);
* Call this for uninitialized strings. Global variables are
* automatically cleared, so they don't need initialization.
*/
void Str_Init(ddstring_t* ds);
ddstring_t* Str_Init(ddstring_t* ds);

/**
* Call this for uninitialized strings. Makes the string use standard
* malloc for memory allocations.
*/
void Str_InitStd(ddstring_t* ds);
ddstring_t* Str_InitStd(ddstring_t* ds);

/**
* Initializes @a ds with a static const C string. No memory allocation
* model is selected; use this for strings that remain constant.
* If the string is never modified calling Str_Free() is not needed.
*/
void Str_InitStatic(ddstring_t* ds, const char* staticConstStr);
ddstring_t* Str_InitStatic(ddstring_t* ds, const char* staticConstStr);

/**
* Empty an existing string. After this the string is in the same
Expand Down
14 changes: 8 additions & 6 deletions doomsday/engine/portable/src/m_string.c
Expand Up @@ -99,19 +99,18 @@ static void allocateString(ddstring_t *str, size_t for_length, int preserve)
* automatically cleared, so they don't need initialization.
* The string will use the memory zone.
*/
void Str_Init(ddstring_t *str)
ddstring_t* Str_Init(ddstring_t* str)
{
if(!str)
{
Con_Error("Attempted String::Init with invalid reference (this==0).");
return; // Unreachable.
exit(1); // Unreachable.
}

if(!Z_IsInited())
{
// The memory zone is not available at the moment.
Str_InitStd(str);
return;
return Str_InitStd(str);
}

memset(str, 0, sizeof(*str));
Expand All @@ -120,25 +119,28 @@ void Str_Init(ddstring_t *str)
str->memFree = Z_Free;
str->memAlloc = zoneAlloc;
str->memCalloc = zoneCalloc;
return str;
}

/**
* The string will use standard memory allocation.
*/
void Str_InitStd(ddstring_t *str)
ddstring_t* Str_InitStd(ddstring_t* str)
{
memset(str, 0, sizeof(*str));

// Init the memory management.
str->memFree = free;
str->memAlloc = malloc;
str->memCalloc = stdCalloc;
return str;
}

void Str_InitStatic(ddstring_t* str, const char* staticConstStr)
ddstring_t* Str_InitStatic(ddstring_t* str, const char* staticConstStr)
{
memset(str, 0, sizeof(*str));
str->str = (char*) staticConstStr;
return str;
}

void Str_Free(ddstring_t* str)
Expand Down

0 comments on commit d573039

Please sign in to comment.