Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor|FS: Added F_Dump() for writing data to a file
Utility for writing a data buffer to a file.
  • Loading branch information
skyjake committed Jul 13, 2012
1 parent 0d8f046 commit 8f3e12c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
11 changes: 11 additions & 0 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -220,6 +220,17 @@ DFile* F_OpenLump(lumpnum_t lumpNum);
*/
boolean F_DumpLump(lumpnum_t lumpNum, const char* fileName);

/**
* Write data into a file.
*
* @param data Data to write.
* @param size Size of the data in bytes.
* @param path Path of the file to create (existing file replaced).
*
* @return @c true if successful, otherwise @c false.
*/
boolean F_Dump(const void *data, size_t size, const char* path);

/**
* @return The time when the file was last modified, as seconds since
* the Epoch else zero if the file is not found.
Expand Down
47 changes: 30 additions & 17 deletions doomsday/engine/portable/src/fs_main.c
Expand Up @@ -1070,15 +1070,39 @@ void F_CacheChangeTag(abstractfile_t* fsObject, int lumpIdx, int tag)
}
}

boolean F_Dump(const void* data, size_t size, const char* path)
{
FILE* outFile;
AutoStr* nativePath = AutoStr_New();

if(!size) return false;

DENG_ASSERT(data != 0);
DENG_ASSERT(path != 0);

Str_Set(nativePath, path);
F_ToNativeSlashes(nativePath, nativePath);

outFile = fopen(Str_Text(nativePath), "wb");
if(!outFile)
{
Con_Message("Warning: Failed to open \"%s\" for writing (error: %s), aborting.\n",
F_PrettyPath(Str_Text(nativePath)), strerror(errno));
return false;
}
fwrite(data, 1, size, outFile);
fclose(outFile);
return true;
}

boolean F_DumpLump(lumpnum_t absoluteLumpNum, const char* path)
{
const LumpInfo* info;
abstractfile_t* fsObject;
ddstring_t nativePath;
const char* lumpName;
const char* fname;
int lumpIdx;
FILE* outFile;
boolean ok;

fsObject = F_FindFileForLumpNum2(absoluteLumpNum, &lumpIdx);
if(!fsObject) return false;
Expand All @@ -1096,23 +1120,12 @@ boolean F_DumpLump(lumpnum_t absoluteLumpNum, const char* path)
fname = lumpName;
}

Str_Init(&nativePath); Str_Set(&nativePath, fname);
F_ToNativeSlashes(&nativePath, &nativePath);

outFile = fopen(Str_Text(&nativePath), "wb");
if(!outFile)
{
Con_Printf("Warning: Failed to open \"%s\" for writing (error: %s), aborting.\n", F_PrettyPath(Str_Text(&nativePath)), strerror(errno));
Str_Free(&nativePath);
return false;
}

fwrite((void*)F_CacheLump(fsObject, lumpIdx, PU_APPSTATIC), 1, info->size, outFile);
fclose(outFile);
ok = F_Dump(F_CacheLump(fsObject, lumpIdx, PU_APPSTATIC), info->size, fname);
F_CacheChangeTag(fsObject, lumpIdx, PU_CACHE);
if(!ok) return false;

Con_Printf("%s dumped to \"%s\"\n", lumpName, F_PrettyPath(Str_Text(&nativePath)));
Str_Free(&nativePath);
LegacyCore_PrintfLogFragmentAtLevel(DE2_LOG_VERBOSE,
"%s dumped to \"%s\"\n", lumpName, F_PrettyPath(fname));
return true;
}

Expand Down

0 comments on commit 8f3e12c

Please sign in to comment.