diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-02 04:47:40 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-02 04:47:40 +0100 |
commit | 8bdbfd3ec02f019a2e419441ee378d5efcb115c1 (patch) | |
tree | 49ea0aa98f3f63e8d4f013c127868703b51a3182 /src | |
parent | 213124ec57f388ad51a6fa114030d6302ee238a2 (diff) | |
download | MinedMap-8bdbfd3ec02f019a2e419441ee378d5efcb115c1.tar MinedMap-8bdbfd3ec02f019a2e419441ee378d5efcb115c1.zip |
Factor out filename check
Diffstat (limited to 'src')
-rw-r--r-- | src/MinedMap.cpp | 49 |
1 files changed, 28 insertions, 21 deletions
diff --git a/src/MinedMap.cpp b/src/MinedMap.cpp index 5c86a18..ff7b4e2 100644 --- a/src/MinedMap.cpp +++ b/src/MinedMap.cpp @@ -33,6 +33,7 @@ #include <cstring> #include <cstdlib> #include <iostream> +#include <set> #include <system_error> #include <sys/types.h> @@ -138,6 +139,20 @@ static void doRegion(const std::string &input, const std::string &output) { } } +static bool checkFilename(const char *name, int *x, int *z) { + if (std::sscanf(name, "r.%i.%i.mca", x, z) != 2) + return false; + + size_t l = strlen(name) + 1; + char buf[l]; + std::snprintf(buf, l, "r.%i.%i.mca", *x, *z); + if (std::memcmp(name, buf, l)) + return false; + + return true; + +} + int main(int argc, char *argv[]) { if (argc < 3) { std::fprintf(stderr, "Usage: %s <data directory> <output directory>\n", argv[0]); @@ -155,32 +170,24 @@ int main(int argc, char *argv[]) { return 1; } + std::set<std::pair<int, int>> regions; int minX = INT_MAX, maxX = INT_MIN, minZ = INT_MAX, maxZ = INT_MIN; struct dirent *entry; while ((entry = readdir(dir)) != nullptr) { int x, z; - if (std::sscanf(entry->d_name, "r.%i.%i.mca", &x, &z) == 2) { - size_t l = strlen(entry->d_name) + 1; - char buf[l]; - std::snprintf(buf, l, "r.%i.%i.mca", x, z); - if (std::memcmp(entry->d_name, buf, l)) - continue; - - if (x < minX) - minX = x; - if (x > maxX) - maxX = x; - - if (z < minZ) - minZ = z; - if (z > maxZ) - maxZ = z; - - std::string name(entry->d_name); - - doRegion(inputdir + "/" + name, outputdir + "/" + name.substr(0, name.length()-3) + "png"); - } + if (!checkFilename(entry->d_name, &x, &z)) + continue; + + if (x < minX) minX = x; + if (x > maxX) maxX = x; + if (z < minZ) minZ = z; + if (z > maxZ) maxZ = z; + + regions.insert(std::make_pair(x, z)); + + std::string name(entry->d_name); + doRegion(inputdir + "/" + name, outputdir + "/" + name.substr(0, name.length()-3) + "png"); } closedir(dir); |