summaryrefslogtreecommitdiffstats
path: root/src/Info.cpp
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2015-02-02 17:56:43 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2015-02-02 17:56:43 +0100
commitdb59b7385621a271684c3be5f20618d74e631dda (patch)
tree90f9bee4359db4757574dcabeff0fec562826249 /src/Info.cpp
parent8bbc75d42f46c3a3525a5898235d0e1cd5d1677a (diff)
downloadMinedMap-db59b7385621a271684c3be5f20618d74e631dda.tar
MinedMap-db59b7385621a271684c3be5f20618d74e631dda.zip
Refactor info generation
Diffstat (limited to 'src/Info.cpp')
-rw-r--r--src/Info.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/Info.cpp b/src/Info.cpp
new file mode 100644
index 0000000..7b411f7
--- /dev/null
+++ b/src/Info.cpp
@@ -0,0 +1,82 @@
+/*
+ Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#include "Info.hpp"
+
+#include <cstdio>
+#include <cstring>
+#include <string>
+#include <unistd.h>
+
+
+namespace MinedMap {
+
+void Info::writeJSON(const char *filename) {
+ const std::string tmpfile = std::string(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) < 0) {
+ std::fprintf(stderr, "Unable to save %s: %s\n", filename, std::strerror(errno));
+ unlink(tmpfile.c_str());
+ }
+}
+
+}