From fd4ce45885db15374dd3f39ac424fb3475a06f4a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 2 Feb 2015 05:13:07 +0100 Subject: Generate info file --- src/MinedMap.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src') diff --git a/src/MinedMap.cpp b/src/MinedMap.cpp index ff7b4e2..ddc6e0d 100644 --- a/src/MinedMap.cpp +++ b/src/MinedMap.cpp @@ -153,6 +153,52 @@ static bool checkFilename(const char *name, int *x, int *z) { } +static void writeInfo(const std::string &filename, const std::set> ®ions, int minX, int maxX, int minZ, int maxZ) { + const std::string tmpfile = filename + ".tmp"; + + FILE *f = fopen(tmpfile.c_str(), "w"); + if (!f) { + std::fprintf(stderr, "Unable to open %s: %s\n", tmpfile.c_str(), std::strerror(errno)); + return; + } + + fprintf(f, "{\n"); + fprintf(f, " \"info\" : {\n"); + fprintf(f, " \"minX\" : %i,\n", minX); + fprintf(f, " \"maxX\" : %i,\n", maxX); + fprintf(f, " \"minZ\" : %i,\n", minZ); + fprintf(f, " \"maxZ\" : %i\n", maxZ); + fprintf(f, " },\n"); + fprintf(f, " \"regions\" : [\n"); + + for (int z = minZ; z <= maxZ; z++) { + fprintf(f, " ["); + + for (int x = minX; x <= maxX; x++) { + fprintf(f, "%s", regions.count(std::make_pair(x, z)) ? "true" : "false"); + + if (x < maxX) + fprintf(f, ", "); + } + + if (z < maxZ) + fprintf(f, "],\n"); + else + fprintf(f, "]\n"); + } + + fprintf(f, " ]\n"); + fprintf(f, "}\n"); + + fclose(f); + + if (std::rename(tmpfile.c_str(), filename.c_str()) < 0) { + std::fprintf(stderr, "Unable to save %s: %s\n", filename.c_str(), std::strerror(errno)); + unlink(tmpfile.c_str()); + } + +} + int main(int argc, char *argv[]) { if (argc < 3) { std::fprintf(stderr, "Usage: %s \n", argv[0]); @@ -192,5 +238,7 @@ int main(int argc, char *argv[]) { closedir(dir); + writeInfo(outputdir + "/info.json", regions, minX, maxX, minZ, maxZ); + return 0; } -- cgit v1.2.3