Factor out code for enumerating the tiles in a directory

This commit is contained in:
Eric Fischer
2017-11-30 15:37:46 -08:00
parent ad86d06cce
commit 1acd771743
5 changed files with 106 additions and 332 deletions
+34 -17
View File
@@ -2,6 +2,7 @@
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -9,8 +10,8 @@
#include <sys/stat.h>
#include "dirtiles.hpp"
std::string dir_read_tile(std::string pbfPath) {
std::ifstream pbfFile(pbfPath, std::ios::in | std::ios::binary);
std::string dir_read_tile(std::string base, struct zxy tile) {
std::ifstream pbfFile(base + "/" + tile.path(), std::ios::in | std::ios::binary);
std::ostringstream contents;
contents << pbfFile.rdbuf();
pbfFile.close();
@@ -39,7 +40,7 @@ void dir_write_tile(const char *outdir, int z, int tx, int ty, std::string const
pbfFile.close();
}
bool numeric(const char *s) {
static bool numeric(const char *s) {
if (*s == '\0') {
return false;
}
@@ -51,7 +52,7 @@ bool numeric(const char *s) {
return true;
}
bool pbfname(const char *s) {
static bool pbfname(const char *s) {
while (*s >= '0' && *s <= '9') {
s++;
}
@@ -79,12 +80,33 @@ void check_dir(const char *dir, bool force, bool forcetable) {
return;
}
DIR *d1 = opendir(dir);
std::vector<zxy> tiles = enumerate_dirtiles(dir);
for (size_t i = 0; i < tiles.size(); i++) {
std::string fn = std::string(dir) + "/" + tiles[i].path();
if (force) {
if (unlink(fn.c_str()) != 0) {
perror(fn.c_str());
exit(EXIT_FAILURE);
}
} else {
fprintf(stderr, "%s: file exists\n", fn.c_str());
exit(EXIT_FAILURE);
}
}
}
std::vector<zxy> enumerate_dirtiles(const char *fname) {
std::vector<zxy> tiles;
DIR *d1 = opendir(fname);
if (d1 != NULL) {
struct dirent *dp;
while ((dp = readdir(d1)) != NULL) {
if (numeric(dp->d_name)) {
std::string z = std::string(dir) + "/" + dp->d_name;
std::string z = std::string(fname) + "/" + dp->d_name;
int tz = atoi(dp->d_name);
DIR *d2 = opendir(z.c_str());
if (d2 == NULL) {
@@ -96,6 +118,7 @@ void check_dir(const char *dir, bool force, bool forcetable) {
while ((dp2 = readdir(d2)) != NULL) {
if (numeric(dp2->d_name)) {
std::string x = z + "/" + dp2->d_name;
int tx = atoi(dp2->d_name);
DIR *d3 = opendir(x.c_str());
if (d3 == NULL) {
@@ -106,17 +129,8 @@ void check_dir(const char *dir, bool force, bool forcetable) {
struct dirent *dp3;
while ((dp3 = readdir(d3)) != NULL) {
if (pbfname(dp3->d_name)) {
std::string y = x + "/" + dp3->d_name;
if (force) {
if (unlink(y.c_str()) != 0) {
perror(y.c_str());
exit(EXIT_FAILURE);
}
} else {
fprintf(stderr, "%s: file exists\n", y.c_str());
exit(EXIT_FAILURE);
}
int ty = atoi(dp3->d_name);
tiles.push_back(zxy(tz, tx, ty));
}
}
@@ -130,4 +144,7 @@ void check_dir(const char *dir, bool force, bool forcetable) {
closedir(d1);
}
std::sort(tiles.begin(), tiles.end());
return tiles;
}