Skip to content

Commit

Permalink
Resources|libdoomsday: Utility methods for file name manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Oct 28, 2017
1 parent 001570e commit c7524f1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "lumpdirectory.h"
#include <de/filesys/IInterpreter>
#include <de/Folder>
#include <de/Version>

/**
* Abstract base class for classic data files: PK3, WAD, LMP, DED, DEH.
Expand Down Expand Up @@ -172,6 +173,10 @@ class LIBDOOMSDAY_PUBLIC DataBundle : public de::IByteArray

static de::StringList gameTags();
static de::String anyGameTagPattern();
static de::String cleanIdentifier(de::String const &text);
static de::String stripVersion(de::String const &text, de::Version *version = nullptr);
static de::String stripRedundantParts(de::String const &id);
static de::String versionFromTimestamp(de::Time const &timestamp);

protected:
void setFormat(Format format);
Expand Down
93 changes: 49 additions & 44 deletions doomsday/apps/libdoomsday/src/resource/databundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,50 +97,6 @@ DENG2_PIMPL(DataBundle), public Lockable
return App::rootFolder().locate<Folder>(QStringLiteral("/sys/bundles"));
}

static String cleanIdentifier(String const &text)
{
// Periods and underscores have special meaning in packages IDs.
// Whitespace is used as separator in package ID lists (see PackageLoader).
// Info syntax has ambiguous quote/double-quote escaping in strings, so
// we'll also get rid of single quotes. (For example, Info converts a string
// containing ['"] to ['''].)
String cleaned = text.toLower();
cleaned.replace(QRegExp("[._'\\s]"), "-");
return cleaned;
}

static String stripVersion(String const &text, Version *version = nullptr)
{
QRegExp re(".*([-_. ][0-9._-]+)$");
if (re.exactMatch(text))
{
if (version)
{
String str = re.cap(1).mid(1);
str.replace("_", ".");
version->parseVersionString(str);
}
return text.mid(0, text.size() - re.cap(1).size());
}
return text;
}

static String stripRedundantParts(String const &id)
{
DotPath const path(id);
String stripped = path.segment(0);
for (int i = 1; i < path.segmentCount(); ++i)
{
String seg = path.segment(i);
if (seg.startsWith(path.segment(i - 1) + "-"))
{
seg = seg.mid(path.segment(i - 1).size() + 1);
}
stripped = stripped.concatenateMember(seg);
}
return stripped;
}

/**
* Identifies the data bundle and sets up a package link under "/sys/bundles" with
* the appropriate metadata.
Expand Down Expand Up @@ -1386,3 +1342,52 @@ String DataBundle::anyGameTagPattern()
{
return String("\\b(%1)\\b").arg(String::join(gameTags(), "|"));
}

String DataBundle::cleanIdentifier(String const &text)
{
// Periods and underscores have special meaning in packages IDs.
// Whitespace is used as separator in package ID lists (see PackageLoader).
// Info syntax has ambiguous quote/double-quote escaping in strings, so
// we'll also get rid of single quotes. (For example, Info converts a string
// containing ['"] to ['''].)
String cleaned = text.toLower();
cleaned.replace(QRegExp("[._'\\s]"), "-");
return cleaned;
}

String DataBundle::stripVersion(String const &text, Version *version)
{
QRegExp re(".*([-_. ][0-9._-]+)$");
if (re.exactMatch(text))
{
if (version)
{
String str = re.cap(1).mid(1);
str.replace("_", ".");
version->parseVersionString(str);
}
return text.mid(0, text.size() - re.cap(1).size());
}
return text;
}

String DataBundle::stripRedundantParts(String const &id)
{
DotPath const path(id);
String stripped = path.segment(0);
for (int i = 1; i < path.segmentCount(); ++i)
{
String seg = path.segment(i);
if (seg.startsWith(path.segment(i - 1) + "-"))
{
seg = seg.mid(path.segment(i - 1).size() + 1);
}
stripped = stripped.concatenateMember(seg);
}
return stripped;
}

de::String DataBundle::versionFromTimestamp(const Time &timestamp)
{
return timestamp.asDateTime().toString(QStringLiteral("0.yyyy.MMdd.hhmm"));
}

0 comments on commit c7524f1

Please sign in to comment.