Include cleanup

Also remove all uses of unistd.h by replacing unlink with std::rename
This commit is contained in:
Matthias Schiffer 2015-02-06 19:51:20 +01:00
parent fb81ea7d60
commit d73c8b930b
3 changed files with 32 additions and 34 deletions

View file

@ -30,7 +30,6 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <unistd.h>
namespace MinedMap { namespace MinedMap {
@ -38,64 +37,64 @@ namespace MinedMap {
void Info::writeJSON(const char *filename) const { void Info::writeJSON(const char *filename) const {
const std::string tmpfile = std::string(filename) + ".tmp"; const std::string tmpfile = std::string(filename) + ".tmp";
FILE *f = fopen(tmpfile.c_str(), "w"); FILE *f = std::fopen(tmpfile.c_str(), "w");
if (!f) { if (!f) {
std::fprintf(stderr, "Unable to open %s: %s\n", tmpfile.c_str(), std::strerror(errno)); std::fprintf(stderr, "Unable to open %s: %s\n", tmpfile.c_str(), std::strerror(errno));
return; return;
} }
fprintf(f, "{\n"); std::fprintf(f, "{\n");
fprintf(f, " \"mipmaps\" : [\n"); std::fprintf(f, " \"mipmaps\" : [\n");
for (size_t level = 0; level < regions.size(); level++) { for (size_t level = 0; level < regions.size(); level++) {
int minX, maxX, minZ, maxZ; int minX, maxX, minZ, maxZ;
std::tie(minX, maxX, minZ, maxZ) = getBounds(level); std::tie(minX, maxX, minZ, maxZ) = getBounds(level);
fprintf(f, " {\n"); std::fprintf(f, " {\n");
fprintf(f, " \"info\" : {\n"); std::fprintf(f, " \"info\" : {\n");
fprintf(f, " \"minX\" : %i,\n", minX); std::fprintf(f, " \"minX\" : %i,\n", minX);
fprintf(f, " \"maxX\" : %i,\n", maxX); std::fprintf(f, " \"maxX\" : %i,\n", maxX);
fprintf(f, " \"minZ\" : %i,\n", minZ); std::fprintf(f, " \"minZ\" : %i,\n", minZ);
fprintf(f, " \"maxZ\" : %i\n", maxZ); std::fprintf(f, " \"maxZ\" : %i\n", maxZ);
fprintf(f, " },\n"); std::fprintf(f, " },\n");
fprintf(f, " \"regions\" : [\n"); std::fprintf(f, " \"regions\" : [\n");
for (int z = minZ; z <= maxZ; z++) { for (int z = minZ; z <= maxZ; z++) {
fprintf(f, " ["); std::fprintf(f, " [");
for (int x = minX; x <= maxX; x++) { for (int x = minX; x <= maxX; x++) {
fprintf(f, "%s", regions[level].count(std::make_pair(x, z)) ? "true" : "false"); std::fprintf(f, "%s", regions[level].count(std::make_pair(x, z)) ? "true" : "false");
if (x < maxX) if (x < maxX)
fprintf(f, ", "); std::fprintf(f, ", ");
} }
if (z < maxZ) if (z < maxZ)
fprintf(f, "],\n"); std::fprintf(f, "],\n");
else else
fprintf(f, "]\n"); std::fprintf(f, "]\n");
} }
fprintf(f, " ]\n"); std::fprintf(f, " ]\n");
if (level < regions.size() - 1) if (level < regions.size() - 1)
fprintf(f, " },\n"); std::fprintf(f, " },\n");
else else
fprintf(f, " }\n"); std::fprintf(f, " }\n");
} }
fprintf(f, " ],\n"); std::fprintf(f, " ],\n");
fprintf(f, " \"spawn\" : {\n"); std::fprintf(f, " \"spawn\" : {\n");
fprintf(f, " \"x\" : %li,\n", (long)spawnX); std::fprintf(f, " \"x\" : %li,\n", (long)spawnX);
fprintf(f, " \"z\" : %li\n", (long)spawnZ); std::fprintf(f, " \"z\" : %li\n", (long)spawnZ);
fprintf(f, " }\n"); std::fprintf(f, " }\n");
fprintf(f, "}\n"); std::fprintf(f, "}\n");
fclose(f); std::fclose(f);
if (std::rename(tmpfile.c_str(), filename) < 0) { if (std::rename(tmpfile.c_str(), filename) < 0) {
std::fprintf(stderr, "Unable to save %s: %s\n", filename, std::strerror(errno)); std::fprintf(stderr, "Unable to save %s: %s\n", filename, std::strerror(errno));
unlink(tmpfile.c_str()); std::remove(tmpfile.c_str());
} }
} }

View file

@ -45,7 +45,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h>
namespace MinedMap { namespace MinedMap {
@ -100,11 +99,11 @@ static void writeImage(const std::string &output, const uint8_t *data, bool colo
if (std::rename(tmpfile.c_str(), output.c_str()) < 0) { if (std::rename(tmpfile.c_str(), output.c_str()) < 0) {
std::fprintf(stderr, "Unable to save %s: %s\n", output.c_str(), std::strerror(errno)); std::fprintf(stderr, "Unable to save %s: %s\n", output.c_str(), std::strerror(errno));
unlink(tmpfile.c_str()); std::remove(tmpfile.c_str());
} }
} }
catch (const std::exception& ex) { catch (const std::exception& ex) {
unlink(tmpfile.c_str()); std::remove(tmpfile.c_str());
throw; throw;
} }
} }
@ -223,11 +222,11 @@ static bool makeMipmap(const std::string &dir, size_t level, size_t x, size_t z,
if (std::rename(tmpfile.c_str(), output.c_str()) < 0) { if (std::rename(tmpfile.c_str(), output.c_str()) < 0) {
std::fprintf(stderr, "Unable to save %s: %s\n", output.c_str(), std::strerror(errno)); std::fprintf(stderr, "Unable to save %s: %s\n", output.c_str(), std::strerror(errno));
unlink(tmpfile.c_str()); std::remove(tmpfile.c_str());
} }
} }
catch (const std::exception& ex) { catch (const std::exception& ex) {
unlink(tmpfile.c_str()); std::remove(tmpfile.c_str());
throw; throw;
} }

View file

@ -26,7 +26,7 @@
#pragma once #pragma once
#include <stdint.h> #include <cstdint>
namespace MinedMap { namespace MinedMap {