Skip to content

Commit

Permalink
Cleanup|libdoomsday: Removed the obsolete F_FileName(), moved F_Match…
Browse files Browse the repository at this point in the history
…FileName()
  • Loading branch information
danij-deng committed Jun 17, 2014
1 parent 192a262 commit 776c891
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 106 deletions.
12 changes: 0 additions & 12 deletions doomsday/api/api_filesys.h
Expand Up @@ -41,23 +41,13 @@ DENG_API_TYPEDEF(F)
*/
int (*FileExists)(const char* path);

/**
* @return The time when the file/directory was last modified, as seconds since
* the Epoch else zero if @a path is not found.
*
* @attention This only works on native paths.
*/
unsigned int (*GetLastModified)(const char* path);

/**
* Check that the given directory exists. If it doesn't, create it.
*
* @return @c true if successful.
*/
dd_bool (*MakePath)(const char* path);

void (*FileName)(Str* dst, const char* src);

/**
* @warning Not thread-safe!
* @return A prettier copy of the original path.
Expand All @@ -83,9 +73,7 @@ DENG_API_T(F);
#ifndef DENG_NO_API_MACROS_FILESYS
#define F_Access _api_F.Access
#define F_FileExists _api_F.FileExists
#define F_GetLastModified _api_F.GetLastModified
#define F_MakePath _api_F.MakePath
#define F_FileName _api_F.FileName
#define F_PrettyPath _api_F.PrettyPath
#define M_ReadFile _api_F.ReadFile
#define M_ReadFileIntoString _api_F.ReadFileIntoString
Expand Down
4 changes: 0 additions & 4 deletions doomsday/client/src/api_filesys.cpp
Expand Up @@ -24,9 +24,7 @@

/*
extern int F_FileExists(char const *path);
extern uint F_GetLastModified(char const *path);
extern dd_bool F_MakePath(char const *path);
extern void F_FileName(ddstring_t *dst, char const *src);
extern const char* F_PrettyPath(char const *path);
*/

Expand All @@ -41,9 +39,7 @@ DENG_DECLARE_API(F) =

F_Access,
F_FileExists,
F_GetLastModified,
F_MakePath,
F_FileName,
F_PrettyPath,
M_ReadFile,
M_ReadFileIntoString,
Expand Down
38 changes: 10 additions & 28 deletions doomsday/libdoomsday/include/doomsday/filesys/fs_util.h
Expand Up @@ -38,33 +38,31 @@
extern "C" {
#endif

LIBDOOMSDAY_PUBLIC void F_FileName(ddstring_t* dst, const char* src);

LIBDOOMSDAY_PUBLIC int F_FileExists(const char *path);
LIBDOOMSDAY_PUBLIC int F_FileExists(char const *path);

/**
* Converts directory slashes to our internal '/'.
* @return @c true iff the path was modified.
*/
LIBDOOMSDAY_PUBLIC dd_bool F_FixSlashes(ddstring_t* dst, const ddstring_t* src);
LIBDOOMSDAY_PUBLIC dd_bool F_FixSlashes(ddstring_t *dst, ddstring_t const *src);

/**
* Appends a slash at the end of @a path if there isn't one.
* @return @c true if a slash was appended, @c false otherwise.
*/
LIBDOOMSDAY_PUBLIC dd_bool F_AppendMissingSlashCString(char* path, size_t maxLen);
LIBDOOMSDAY_PUBLIC dd_bool F_AppendMissingSlashCString(char *path, size_t maxLen);

/**
* Converts directory slashes to tha used by the host file system.
* @return @c true iff the path was modified.
*/
LIBDOOMSDAY_PUBLIC dd_bool F_ToNativeSlashes(ddstring_t* dst, const ddstring_t* src);
LIBDOOMSDAY_PUBLIC dd_bool F_ToNativeSlashes(ddstring_t *dst, ddstring_t const *src);

/**
* @return @c true, if the given path is absolute (starts with \ or / or the
* second character is a ':' (drive).
*/
LIBDOOMSDAY_PUBLIC dd_bool F_IsAbsolute(const ddstring_t* path);
LIBDOOMSDAY_PUBLIC dd_bool F_IsAbsolute(ddstring_t const *path);

/**
* Attempt to prepend the base path. If @a src is already absolute do nothing.
Expand All @@ -75,8 +73,8 @@ LIBDOOMSDAY_PUBLIC dd_bool F_IsAbsolute(const ddstring_t* path);
*
* @return @c true iff the path was prepended.
*/
LIBDOOMSDAY_PUBLIC dd_bool F_PrependBasePath2(ddstring_t* dst, const ddstring_t* src, const ddstring_t* base);
LIBDOOMSDAY_PUBLIC dd_bool F_PrependBasePath(ddstring_t* dst, const ddstring_t* src /*, const ddstring_t* base=ddBasePath*/);
LIBDOOMSDAY_PUBLIC dd_bool F_PrependBasePath2(ddstring_t *dst, ddstring_t const *src, ddstring_t const *base);
LIBDOOMSDAY_PUBLIC dd_bool F_PrependBasePath(ddstring_t *dst, ddstring_t const *src /*, ddstring_t const *base = ddBasePath*/);

/**
* Expands relative path directives like '>'.
Expand All @@ -92,7 +90,7 @@ LIBDOOMSDAY_PUBLIC dd_bool F_PrependBasePath(ddstring_t* dst, const ddstring_t*
*
* @return @c true iff the path was expanded.
*/
LIBDOOMSDAY_PUBLIC dd_bool F_ExpandBasePath(ddstring_t* dst, const ddstring_t* src);
LIBDOOMSDAY_PUBLIC dd_bool F_ExpandBasePath(ddstring_t *dst, ddstring_t const *src);

/**
* Write the data associated with the specified lump index to @a outputPath.
Expand All @@ -117,28 +115,12 @@ LIBDOOMSDAY_PUBLIC dd_bool F_DumpFile(de::File1 &file, char const *fileName);
*/
LIBDOOMSDAY_PUBLIC dd_bool F_Dump(void const *data, size_t size, char const *path);

LIBDOOMSDAY_PUBLIC const char* F_PrettyPath(const char* path);

LIBDOOMSDAY_PUBLIC uint F_GetLastModified(const char* path);
LIBDOOMSDAY_PUBLIC char const *F_PrettyPath(char const *path);

LIBDOOMSDAY_PUBLIC dd_bool F_MakePath(const char* path);
LIBDOOMSDAY_PUBLIC dd_bool F_MakePath(char const *path);

#ifdef __cplusplus
} // extern "C"

#include <de/String>

/**
* Performs a case-insensitive pattern match. The pattern can contain
* wildcards.
*
* @param filePath Path to match.
* @param pattern Pattern with * and ? as wildcards.
*
* @return @c true, if @a filePath matches the pattern.
*/
LIBDOOMSDAY_PUBLIC bool F_MatchFileName(de::String const &filePath, de::String const &pattern);

#endif

#endif // LIBDENG_FILESYS_UTIL_H
55 changes: 52 additions & 3 deletions doomsday/libdoomsday/src/filesys/fs_main.cpp
Expand Up @@ -45,6 +45,8 @@

using namespace de;

extern uint F_GetLastModified(char const *path);

D_CMD(Dir);
D_CMD(DumpLump);
D_CMD(ListFiles);
Expand Down Expand Up @@ -77,6 +79,53 @@ typedef QList<PathMapping> PathMappings;

static bool applyPathMapping(ddstring_t* path, PathMapping const& vdm);

/**
* Performs a case-insensitive pattern match. The pattern can contain
* wildcards.
*
* @param filePath Path to match.
* @param pattern Pattern with * and ? as wildcards.
*
* @return @c true, if @a filePath matches the pattern.
*/
static bool matchFileName(String const &string, String const &pattern)
{
static QChar const ASTERISK('*');
static QChar const QUESTION_MARK('?');

QChar const *in = string.constData(), *st = pattern.constData();

while(!in->isNull())
{
if(*st == ASTERISK)
{
st++;
continue;
}

if(*st != QUESTION_MARK && st->toLower() != in->toLower())
{
// A mismatch. Hmm. Go back to a previous '*'.
while(st >= pattern && *st != ASTERISK) { st--; }

if(st < pattern)
return false; // No match!
// The asterisk lets us continue.
}

// This character of the pattern is OK.
st++;
in++;
}

// Match is good if the end of the pattern was reached.

// Skip remaining asterisks.
while(*st == ASTERISK) { st++; }

return st->isNull();
}

struct FS1::Instance
{
FS1& self;
Expand Down Expand Up @@ -815,7 +864,7 @@ int FS1::findAllPaths(Path searchPattern, int flags, FS1::PathList& found)
if(!(flags & SearchPath::NoDescend))
{
filePath = lump.composePath();
patternMatched = F_MatchFileName(filePath, searchPattern);
patternMatched = matchFileName(filePath, searchPattern);
}
else
{
Expand All @@ -840,7 +889,7 @@ int FS1::findAllPaths(Path searchPattern, int flags, FS1::PathList& found)
{
DENG2_FOR_EACH_CONST(LumpMappings, i, d->lumpMappings)
{
if(!F_MatchFileName(i->first, searchPattern)) continue;
if(!matchFileName(i->first, searchPattern)) continue;

found.push_back(PathListItem(i->first, 0 /*only filepaths (i.e., leaves) can be mapped to lumps*/));
}
Expand Down Expand Up @@ -881,7 +930,7 @@ int FS1::findAllPaths(Path searchPattern, int flags, FS1::PathList& found)
if(Str_Compare(&fd.name, ".") && Str_Compare(&fd.name, ".."))
{
String foundPath = searchDirectory / NativePath(Str_Text(&fd.name)).withSeparators('/');
if(!F_MatchFileName(foundPath, searchPattern)) continue;
if(!matchFileName(foundPath, searchPattern)) continue;

nativeFilePaths.push_back(PathListItem(foundPath, fd.attrib));
}
Expand Down
65 changes: 6 additions & 59 deletions doomsday/libdoomsday/src/filesys/fs_util.cpp
Expand Up @@ -52,22 +52,7 @@

using namespace de;

void F_FileName(ddstring_t* dst, const char* src)
{
#ifdef WIN32
char name[_MAX_FNAME];
#else
char name[NAME_MAX];
#endif

if(!dst) return;
Str_Clear(dst);
if(!src) return;
_splitpath(src, 0, 0, name, 0);
Str_Set(dst, name);
}

int F_FileExists(const char* path)
int F_FileExists(char const *path)
{
int result = -1;
if(path && path[0])
Expand All @@ -86,7 +71,7 @@ int F_FileExists(const char* path)
return result;
}

uint F_GetLastModified(const char* path)
uint F_GetLastModified(char const *path)
{
#ifdef UNIX
struct stat s;
Expand All @@ -101,7 +86,7 @@ uint F_GetLastModified(const char* path)
#endif
}

dd_bool F_MakePath(const char* path)
dd_bool F_MakePath(char const *path)
{
#if !defined(WIN32) && !defined(UNIX)
# error F_MakePath has no implementation for this platform.
Expand Down Expand Up @@ -495,55 +480,17 @@ const char* F_PrettyPath(const char* path)
#undef NUM_BUFS
}

bool F_MatchFileName(de::String const &string, de::String const &pattern)
{
static QChar const ASTERISK('*');
static QChar const QUESTION_MARK('?');

QChar const *in = string.constData(), *st = pattern.constData();

while(!in->isNull())
{
if(*st == ASTERISK)
{
st++;
continue;
}

if(*st != QUESTION_MARK && st->toLower() != in->toLower())
{
// A mismatch. Hmm. Go back to a previous '*'.
while(st >= pattern && *st != ASTERISK)
st--;
if(st < pattern)
return false; // No match!
// The asterisk lets us continue.
}

// This character of the pattern is OK.
st++;
in++;
}

// Match is good if the end of the pattern was reached.
while(*st == ASTERISK)
st++; // Skip remaining asterisks.

return st->isNull();
}

dd_bool F_Dump(void const* data, size_t size, char const* path)
dd_bool F_Dump(void const *data, size_t size, char const *path)
{
DENG_ASSERT(data);
DENG_ASSERT(path);
DENG2_ASSERT(data != 0 && path != 0);

if(!size) return false;

AutoStr* nativePath = AutoStr_NewStd();
Str_Set(nativePath, path);
F_ToNativeSlashes(nativePath, nativePath);

FILE* outFile = fopen(Str_Text(nativePath), "wb");
FILE *outFile = fopen(Str_Text(nativePath), "wb");
if(!outFile)
{
LOG_RES_WARNING("Failed to open \"%s\" for writing: %s")
Expand Down

0 comments on commit 776c891

Please sign in to comment.