Skip to content

Commit

Permalink
Refactor: Switched abstractresource.c to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Nov 2, 2012
1 parent 0c22faf commit cd82892
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion doomsday/engine/engine.pro
Expand Up @@ -436,7 +436,7 @@ deng_nodisplaymode {

# Platform-independent sources.
SOURCES += \
portable/src/abstractresource.c \
portable/src/abstractresource.cpp \
portable/src/animator.c \
portable/src/audiodriver.cpp \
portable/src/audiodriver_music.c \
Expand Down
@@ -1,11 +1,11 @@
/**\file abstractresource.c
/**\file abstractresource.cpp
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2010-2012 Daniel Swanson <danij@dengine.net>
*
* This program is free software; you can redistribute it and/or modify
* This program is M_Free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
Expand Down Expand Up @@ -51,7 +51,7 @@ struct AbstractResource_s {
ddstring_t foundPath;
};

static __inline size_t countElements(ddstring_t** list)
static inline size_t countElements(ddstring_t** list)
{
size_t n = 0;
if(list)
Expand All @@ -66,12 +66,14 @@ static ddstring_t* buildNameStringList(AbstractResource* r, char delimiter)
int i, requiredLength = 0;
ddstring_t* list;

assert(r);
DENG_ASSERT(r);

if(!r->namesCount) return 0;

for(i = 0; i < r->namesCount; ++i)
{
requiredLength += Str_Length(r->names[i]);
}
requiredLength += r->namesCount - 1;

if(!requiredLength) return 0;
Expand All @@ -84,15 +86,17 @@ static ddstring_t* buildNameStringList(AbstractResource* r, char delimiter)
i = r->namesCount-1;
Str_Set(list, Str_Text(r->names[i--]));
for(; i >= 0; i--)
{
Str_Appendf(list, "%c%s", delimiter, Str_Text(r->names[i]));
}

return list;
}

AbstractResource* AbstractResource_NewWithName(resourceclass_t rclass, int flags,
const ddstring_t* name)
ddstring_t const* name)
{
AbstractResource* r = (AbstractResource*)malloc(sizeof(*r));
AbstractResource* r = (AbstractResource*) M_Malloc(sizeof(*r));
if(!r) Con_Error("AbstractResource::NewWithName: Failed on allocation of %lu bytes.", (unsigned long) sizeof(*r));

r->rclass = rclass;
Expand All @@ -115,32 +119,32 @@ AbstractResource* AbstractResource_New(resourceclass_t rclass, int rflags)

void AbstractResource_Delete(AbstractResource* r)
{
assert(r);
DENG_ASSERT(r);
if(r->namesCount != 0)
{
int i;
for(i = 0; i < r->namesCount; ++i)
Str_Delete(r->names[i]);
free(r->names);
M_Free(r->names);
}

if(r->identityKeys)
{
size_t i;
for(i = 0; r->identityKeys[i]; ++i)
Str_Delete(r->identityKeys[i]);
free(r->identityKeys);
M_Free(r->identityKeys);
}

F_DestroyUriList(r->searchPaths);
Str_Free(&r->foundPath);
free(r);
M_Free(r);
}

void AbstractResource_AddName(AbstractResource* r, const ddstring_t* name)
void AbstractResource_AddName(AbstractResource* r, ddstring_t const* name)
{
int i;
assert(r);
DENG_ASSERT(r);

if(name == 0 || Str_IsEmpty(name)) return;

Expand All @@ -152,7 +156,7 @@ void AbstractResource_AddName(AbstractResource* r, const ddstring_t* name)
}

// Add the new name.
r->names = (ddstring_t**)realloc(r->names, sizeof *r->names * ++r->namesCount);
r->names = (ddstring_t**) M_Realloc(r->names, sizeof *r->names * ++r->namesCount);
if(!r->names)
Con_Error("AbstractResource::AddName: Failed on (re)allocation of %lu bytes for names list.", (unsigned long) sizeof *r->names * r->namesCount);

Expand All @@ -167,13 +171,13 @@ void AbstractResource_AddName(AbstractResource* r, const ddstring_t* name)
Str_Free(&r->foundPath);
}

void AbstractResource_AddIdentityKey(AbstractResource* r, const ddstring_t* identityKey)
void AbstractResource_AddIdentityKey(AbstractResource* r, ddstring_t const* identityKey)
{
size_t num;
assert(r && identityKey);
DENG_ASSERT(r && identityKey);

num = countElements(r->identityKeys);
r->identityKeys = (ddstring_t**)realloc(r->identityKeys, sizeof *r->identityKeys * MAX_OF(num+1, 2));
r->identityKeys = (ddstring_t**)M_Realloc(r->identityKeys, sizeof *r->identityKeys * MAX_OF(num+1, 2));
if(!r->identityKeys)
Con_Error("AbstractResource::AddIdentityKey: Failed on (re)allocation of %lu bytes for identitykey list.", (unsigned long) sizeof *r->identityKeys * MAX_OF(num+1, 2));

Expand All @@ -185,7 +189,7 @@ void AbstractResource_AddIdentityKey(AbstractResource* r, const ddstring_t* iden

Uri* const* AbstractResource_SearchPaths(AbstractResource* r)
{
assert(r);
DENG_ASSERT(r);
if(!r->searchPaths)
{
ddstring_t* searchPaths = AbstractResource_NameStringList(r);
Expand All @@ -200,9 +204,9 @@ ddstring_t* AbstractResource_NameStringList(AbstractResource* r)
return buildNameStringList(r, ';');
}

const ddstring_t* AbstractResource_ResolvedPath(AbstractResource* r, boolean canLocate)
ddstring_t const* AbstractResource_ResolvedPath(AbstractResource* r, boolean canLocate)
{
assert(r);
DENG_ASSERT(r);
if(r->searchPathUsed == 0 && canLocate)
{
r->searchPathUsed = F_FindResourceForRecord(r, &r->foundPath);
Expand All @@ -215,11 +219,11 @@ const ddstring_t* AbstractResource_ResolvedPath(AbstractResource* r, boolean can
return 0;
}

const ddstring_t* AbstractResource_ResolvedPathWithIndex(AbstractResource* r, int searchPathIndex, boolean canLocate)
ddstring_t const* AbstractResource_ResolvedPathWithIndex(AbstractResource* r, int searchPathIndex, boolean canLocate)
{
const Uri* list[2] = { AbstractResource_SearchPaths(r)[searchPathIndex], NULL };
uri_s const* list[2] = { AbstractResource_SearchPaths(r)[searchPathIndex], NULL };

assert(r);
DENG_ASSERT(r);
if(canLocate)
{
int found = F_FindResourceForRecord2(r, &r->foundPath, list);
Expand All @@ -235,34 +239,34 @@ const ddstring_t* AbstractResource_ResolvedPathWithIndex(AbstractResource* r, in

resourceclass_t AbstractResource_ResourceClass(AbstractResource* r)
{
assert(r);
DENG_ASSERT(r);
return r->rclass;
}

int AbstractResource_ResourceFlags(AbstractResource* r)
{
assert(r);
DENG_ASSERT(r);
return r->flags;
}

AbstractResource* AbstractResource_MarkAsFound(AbstractResource* r, boolean yes)
{
assert(r);
DENG_ASSERT(r);
if(yes) r->flags |= RF_FOUND;
else r->flags &= ~RF_FOUND;
return r;
}

ddstring_t* const* AbstractResource_IdentityKeys(AbstractResource* r)
{
assert(r);
DENG_ASSERT(r);
return (ddstring_t* const*) r->identityKeys;
}

void AbstractResource_Print(AbstractResource* r, boolean printStatus)
{
ddstring_t* searchPaths = AbstractResource_NameStringList(r);
const boolean markedFound = !!(r->flags & RF_FOUND);
bool const markedFound = !!(r->flags & RF_FOUND);

if(printStatus)
Con_Printf("%s", !markedFound? " ! ":" ");
Expand Down

0 comments on commit cd82892

Please sign in to comment.