Some more code modernization

also a few small performance improvements
This commit is contained in:
sfan5 2020-05-08 22:10:49 +02:00
parent 2979dc5b6b
commit 8e83ce6464
21 changed files with 149 additions and 199 deletions

View File

@ -11,7 +11,8 @@ static inline uint16_t readU16(const unsigned char *data)
return data[0] << 8 | data[1];
}
static int readBlockContent(const unsigned char *mapData, u8 contentWidth, unsigned int datapos)
static inline uint16_t readBlockContent(const unsigned char *mapData,
u8 contentWidth, unsigned int datapos)
{
if (contentWidth == 2) {
size_t index = datapos << 1;
@ -21,7 +22,7 @@ static int readBlockContent(const unsigned char *mapData, u8 contentWidth, unsig
if (param <= 0x7f)
return param;
else
return (int(param) << 4) | (int(mapData[datapos + 0x2000]) >> 4);
return (param << 4) | (mapData[datapos + 0x2000] >> 4);
}
}
@ -125,10 +126,10 @@ void BlockDecoder::decode(const ustring &datastr)
// Node timers
if (version >= 25) {
dataOffset++;
uint8_t timerLength = data[dataOffset++];
uint16_t numTimers = readU16(data + dataOffset);
dataOffset += 2;
dataOffset += numTimers * 10;
dataOffset += numTimers * timerLength;
}
}
@ -138,16 +139,18 @@ bool BlockDecoder::isEmpty() const
return m_nameMap.empty();
}
std::string BlockDecoder::getNode(u8 x, u8 y, u8 z) const
const static std::string empty;
const std::string &BlockDecoder::getNode(u8 x, u8 y, u8 z) const
{
unsigned int position = x + (y << 4) + (z << 8);
int content = readBlockContent(m_mapData.c_str(), m_contentWidth, position);
uint16_t content = readBlockContent(m_mapData.c_str(), m_contentWidth, position);
if (content == m_blockAirId || content == m_blockIgnoreId)
return "";
return empty;
NameMap::const_iterator it = m_nameMap.find(content);
if (it == m_nameMap.end()) {
std::cerr << "Skipping node with invalid ID." << std::endl;
return "";
return empty;
}
return it->second;
}

View File

@ -17,7 +17,7 @@
// ARGB but with inverted alpha
static inline int color2int(Color c)
static inline int color2int(const Color &c)
{
u8 a = (255 - c.a) * gdAlphaMax / 255;
return (a << 24) | (c.r << 16) | (c.g << 8) | c.b;
@ -35,6 +35,7 @@ static inline Color int2color(int c)
return c2;
}
#ifndef NDEBUG
static inline void check_bounds(int x, int y, int width, int height)
{
if(x < 0 || x >= width) {
@ -50,11 +51,13 @@ static inline void check_bounds(int x, int y, int width, int height)
throw std::out_of_range(oss.str());
}
}
#endif
Image::Image(int width, int height) :
m_width(width), m_height(height), m_image(NULL)
{
SIZECHECK(0, 0);
m_image = gdImageCreateTrueColor(m_width, m_height);
}

View File

@ -1,14 +1,6 @@
/*
* =====================================================================
* Version: 1.0
* Created: 25.08.2012 10:55:27
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#include <cstring>
#include "PixelAttributes.h"
#include <cstring>
PixelAttributes::PixelAttributes():
m_width(0)

View File

@ -9,11 +9,9 @@
#include "PlayerAttributes.h"
#include "util.h"
using namespace std;
PlayerAttributes::PlayerAttributes(const std::string &worldDir)
{
std::ifstream ifs((worldDir + "world.mt").c_str());
std::ifstream ifs(worldDir + "world.mt");
if (!ifs.good())
throw std::runtime_error("Failed to read world.mt");
std::string backend = read_setting_default("player_backend", ifs, "files");
@ -39,18 +37,18 @@ void PlayerAttributes::readFiles(const std::string &playersPath)
if (ent->d_name[0] == '.')
continue;
string path = playersPath + PATH_SEPARATOR + ent->d_name;
ifstream in(path.c_str());
std::string path = playersPath + PATH_SEPARATOR + ent->d_name;
std::ifstream in(path);
if(!in.good())
continue;
string name, position;
std::string name, position;
name = read_setting("name", in);
in.seekg(0);
position = read_setting("position", in);
Player player;
istringstream iss(position);
std::istringstream iss(position);
char tmp;
iss >> tmp; // '('
iss >> player.x;
@ -121,13 +119,13 @@ void PlayerAttributes::readSqlite(const std::string &db_name)
/**********/
PlayerAttributes::Players::iterator PlayerAttributes::begin()
PlayerAttributes::Players::const_iterator PlayerAttributes::begin() const
{
return m_players.begin();
return m_players.cbegin();
}
PlayerAttributes::Players::iterator PlayerAttributes::end()
PlayerAttributes::Players::const_iterator PlayerAttributes::end() const
{
return m_players.end();
return m_players.cend();
}

View File

@ -4,6 +4,8 @@ Minetest Mapper C++
.. image:: https://travis-ci.org/minetest/minetestmapper.svg?branch=master
:target: https://travis-ci.org/minetest/minetestmapper
Minetestmapper generates an overview image from a Minetest map.
A port of minetestmapper.py to C++ from https://github.com/minetest/minetest/tree/master/util.
This version is both faster and provides more features than the now deprecated Python script.
@ -12,14 +14,14 @@ Requirements
* libgd
* sqlite3
* LevelDB (optional, set ENABLE_LEVELDB=1 in CMake to enable)
* hiredis library (optional, set ENABLE_REDIS=1 in CMake to enable)
* Postgres libraries (optional, set ENABLE_POSTGRES=1 in CMake to enable)
* LevelDB (optional)
* hiredis (optional)
* Postgres libraries (optional)
e.g. on Debian:
^^^^^^^^^^^^^^^
sudo apt-get install libgd-dev libsqlite3-dev libleveldb-dev libhiredis-dev libpq-dev
sudo apt install libgd-dev libsqlite3-dev libleveldb-dev libhiredis-dev libpq-dev
Windows
^^^^^^^
@ -37,7 +39,7 @@ Compilation
::
cmake . -DENABLE_LEVELDB=1
make -j2
make -j4
Usage
-----

View File

@ -13,7 +13,9 @@
#include "config.h"
#include "PlayerAttributes.h"
#include "BlockDecoder.h"
#include "Image.h"
#include "util.h"
#include "db-sqlite3.h"
#if USE_POSTGRESQL
#include "db-postgresql.h"
@ -25,8 +27,6 @@
#include "db-redis.h"
#endif
using namespace std;
template<typename T>
static inline T mymax(T a, T b)
{
@ -56,6 +56,20 @@ static inline unsigned int colorSafeBounds (int channel)
return mymin(mymax(channel, 0), 255);
}
static Color parseColor(const std::string &color)
{
if (color.length() != 7)
throw std::runtime_error("Color needs to be 7 characters long");
if (color[0] != '#')
throw std::runtime_error("Color needs to begin with #");
unsigned long col = strtoul(color.c_str() + 1, NULL, 16);
u8 b, g, r;
b = col & 0xff;
g = (col >> 8) & 0xff;
r = (col >> 16) & 0xff;
return Color(r, g, b);
}
static Color mixColors(Color a, Color b)
{
Color result;
@ -139,21 +153,6 @@ void TileGenerator::setScales(uint flags)
m_scales = flags;
}
Color TileGenerator::parseColor(const std::string &color)
{
Color parsed;
if (color.length() != 7)
throw std::runtime_error("Color needs to be 7 characters long");
if (color[0] != '#')
throw std::runtime_error("Color needs to begin with #");
unsigned long col = strtoul(color.c_str() + 1, NULL, 16);
parsed.b = col & 0xff;
parsed.g = (col >> 8) & 0xff;
parsed.r = (col >> 16) & 0xff;
parsed.a = 255;
return parsed;
}
void TileGenerator::setDrawOrigin(bool drawOrigin)
{
m_drawOrigin = drawOrigin;
@ -214,16 +213,15 @@ void TileGenerator::setExhaustiveSearch(int mode)
void TileGenerator::parseColorsFile(const std::string &fileName)
{
ifstream in;
in.open(fileName.c_str(), ifstream::in);
if (!in.is_open())
std::ifstream in(fileName);
if (!in.good())
throw std::runtime_error("Specified colors file could not be found");
parseColorsStream(in);
}
void TileGenerator::printGeometry(const std::string &input)
{
string input_path = input;
std::string input_path = input;
if (input_path[input.length() - 1] != PATH_SEPARATOR) {
input_path += PATH_SEPARATOR;
}
@ -249,7 +247,7 @@ void TileGenerator::setDontWriteEmpty(bool f)
void TileGenerator::generate(const std::string &input, const std::string &output)
{
string input_path = input;
std::string input_path = input;
if (input_path[input.length() - 1] != PATH_SEPARATOR) {
input_path += PATH_SEPARATOR;
}
@ -305,9 +303,8 @@ void TileGenerator::parseColorsStream(std::istream &in)
std::cerr << "Failed to parse color entry '" << line << "'" << std::endl;
continue;
}
ColorEntry color(r, g, b, a, t);
m_colorMap[name] = color;
m_colorMap[name] = ColorEntry(r, g, b, a, t);
}
}
@ -586,39 +583,43 @@ void TileGenerator::renderMapBlock(const BlockDecoder &blk, const BlockPos &pos)
if (m_readPixels.get(x, z))
continue;
int imageX = xBegin + x;
auto &attr = m_blockPixelAttributes.attribute(15 - z, xBegin + x);
for (int y = maxY; y >= minY; --y) {
string name = blk.getNode(x, y, z);
if (name == "")
const std::string &name = blk.getNode(x, y, z);
if (name.empty())
continue;
ColorMap::const_iterator it = m_colorMap.find(name);
if (it == m_colorMap.end()) {
m_unknownNodes.insert(name);
continue;
}
const Color c = it->second.to_color();
Color c = it->second.toColor();
if (c.a == 0)
continue; // node is fully invisible
if (m_drawAlpha) {
if (m_color[z][x].a == 0)
m_color[z][x] = c; // first visible time, no color mixing
else
m_color[z][x] = mixColors(m_color[z][x], c);
if(m_color[z][x].a < 0xff) {
// near thickness value to thickness of current node
m_thickness[z][x] = (m_thickness[z][x] + it->second.t) / 2.0;
if (m_color[z][x].a != 0)
c = mixColors(m_color[z][x], c);
if (c.a < 255) {
// remember color and near thickness value
m_color[z][x] = c;
m_thickness[z][x] = (m_thickness[z][x] + it->second.t) / 2;
continue;
}
// color became opaque, draw it
setZoomed(imageX, imageY, m_color[z][x]);
m_blockPixelAttributes.attribute(15 - z, xBegin + x).thickness = m_thickness[z][x];
setZoomed(imageX, imageY, c);
attr.thickness = m_thickness[z][x];
} else {
setZoomed(imageX, imageY, c.noAlpha());
c.a = 255;
setZoomed(imageX, imageY, c);
}
m_readPixels.set(x, z);
// do this afterwards so we can record height values
// inside transparent nodes (water) too
if (!m_readInfo.get(x, z)) {
m_blockPixelAttributes.attribute(15 - z, xBegin + x).height = pos.y * 16 + y;
attr.height = pos.y * 16 + y;
m_readInfo.set(x, z);
}
break;
@ -640,17 +641,19 @@ void TileGenerator::renderMapBlockBottom(const BlockPos &pos)
if (m_readPixels.get(x, z))
continue;
int imageX = xBegin + x;
auto &attr = m_blockPixelAttributes.attribute(15 - z, xBegin + x);
// set color since it wasn't done in renderMapBlock()
setZoomed(imageX, imageY, m_color[z][x]);
m_readPixels.set(x, z);
m_blockPixelAttributes.attribute(15 - z, xBegin + x).thickness = m_thickness[z][x];
attr.thickness = m_thickness[z][x];
}
}
}
void TileGenerator::renderShading(int zPos)
{
auto &a = m_blockPixelAttributes;
int zBegin = (m_zMax - zPos) * 16;
for (int z = 0; z < 16; ++z) {
int imageY = zBegin + z;
@ -658,23 +661,27 @@ void TileGenerator::renderShading(int zPos)
continue;
for (int x = 0; x < m_mapWidth; ++x) {
if(
!m_blockPixelAttributes.attribute(z, x).valid_height() ||
!m_blockPixelAttributes.attribute(z, x - 1).valid_height() ||
!m_blockPixelAttributes.attribute(z - 1, x).valid_height()
!a.attribute(z, x).valid_height() ||
!a.attribute(z, x - 1).valid_height() ||
!a.attribute(z - 1, x).valid_height()
)
continue;
// calculate shadow to apply
int y = m_blockPixelAttributes.attribute(z, x).height;
int y1 = m_blockPixelAttributes.attribute(z, x - 1).height;
int y2 = m_blockPixelAttributes.attribute(z - 1, x).height;
int y = a.attribute(z, x).height;
int y1 = a.attribute(z, x - 1).height;
int y2 = a.attribute(z - 1, x).height;
int d = ((y - y1) + (y - y2)) * 12;
if (m_drawAlpha) { // less visible shadow with increasing "thickness"
double t = m_blockPixelAttributes.attribute(z, x).thickness * 1.2;
d *= 1.0 - mymin(t, 255.0) / 255.0;
float t = a.attribute(z, x).thickness * 1.2f;
t = mymin(t, 255.0f);
d *= 1.0f - t / 255.0f;
}
d = mymin(d, 36);
// apply shadow/light by just adding to it pixel values
Color c = m_image->getPixel(getImageX(x), getImageY(imageY));
c.r = colorSafeBounds(c.r + d);
c.g = colorSafeBounds(c.g + d);
@ -682,7 +689,7 @@ void TileGenerator::renderShading(int zPos)
setZoomed(x, imageY, c);
}
}
m_blockPixelAttributes.scroll();
a.scroll();
}
void TileGenerator::renderScale()

View File

@ -1,12 +1,3 @@
/*
* =====================================================================
* Version: 1.0
* Created: 18.09.2012 10:20:47
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#include <zlib.h>
#include <stdint.h>
#include "ZlibDecompressor.h"
@ -38,8 +29,8 @@ ustring ZlibDecompressor::decompress()
const std::size_t size = m_size - m_seekPos;
ustring buffer;
const size_t BUFSIZE = 128 * 1024;
uint8_t temp_buffer[BUFSIZE];
constexpr size_t BUFSIZE = 128 * 1024;
unsigned char temp_buffer[BUFSIZE];
z_stream strm;
strm.zalloc = Z_NULL;
@ -48,9 +39,8 @@ ustring ZlibDecompressor::decompress()
strm.next_in = Z_NULL;
strm.avail_in = size;
if (inflateInit(&strm) != Z_OK) {
if (inflateInit(&strm) != Z_OK)
throw DecompressError();
}
strm.next_in = const_cast<unsigned char *>(data);
int ret = 0;
@ -58,11 +48,11 @@ ustring ZlibDecompressor::decompress()
strm.avail_out = BUFSIZE;
strm.next_out = temp_buffer;
ret = inflate(&strm, Z_NO_FLUSH);
buffer += ustring(reinterpret_cast<unsigned char *>(temp_buffer), BUFSIZE - strm.avail_out);
buffer.append(temp_buffer, BUFSIZE - strm.avail_out);
} while (ret == Z_OK);
if (ret != Z_STREAM_END) {
if (ret != Z_STREAM_END)
throw DecompressError();
}
m_seekPos += strm.next_in - data;
(void)inflateEnd(&strm);

View File

@ -1,5 +1,4 @@
#ifndef BLOCKDECODER_H
#define BLOCKDECODER_H
#pragma once
#include <unordered_map>
@ -12,10 +11,11 @@ public:
void reset();
void decode(const ustring &data);
bool isEmpty() const;
std::string getNode(u8 x, u8 y, u8 z) const; // returns "" for air, ignore and invalid nodes
// returns "" for air, ignore and invalid nodes
const std::string &getNode(u8 x, u8 y, u8 z) const;
private:
typedef std::unordered_map<int, std::string> NameMap;
typedef std::unordered_map<uint16_t, std::string> NameMap;
NameMap m_nameMap;
int m_blockAirId;
int m_blockIgnoreId;
@ -23,5 +23,3 @@ private:
u8 m_version, m_contentWidth;
ustring m_mapData;
};
#endif // BLOCKDECODER_H

View File

@ -1,5 +1,4 @@
#ifndef IMAGE_HEADER
#define IMAGE_HEADER
#pragma once
#include "types.h"
#include <string>
@ -9,7 +8,6 @@ struct Color {
Color() : r(0), g(0), b(0), a(0) {};
Color(u8 r, u8 g, u8 b) : r(r), g(g), b(b), a(255) {};
Color(u8 r, u8 g, u8 b, u8 a) : r(r), g(g), b(b), a(a) {};
inline Color noAlpha() const { return Color(r, g, b); }
u8 r, g, b, a;
};
@ -19,6 +17,9 @@ public:
Image(int width, int height);
~Image();
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
void setPixel(int x, int y, const Color &c);
Color getPixel(int x, int y);
void drawLine(int x1, int y1, int x2, int y2, const Color &c);
@ -28,10 +29,6 @@ public:
void save(const std::string &filename);
private:
Image(const Image&);
int m_width, m_height;
gdImagePtr m_image;
};
#endif // IMAGE_HEADER

View File

@ -1,25 +1,15 @@
/*
* =====================================================================
* Version: 1.0
* Created: 25.08.2012 10:55:29
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#pragma once
#ifndef PIXELATTRIBUTES_H_ADZ35GYF
#define PIXELATTRIBUTES_H_ADZ35GYF
#include <limits>
#include <stdint.h>
#include <climits>
#include <cstdint>
#include "config.h"
struct PixelAttribute {
PixelAttribute(): height(std::numeric_limits<int>::min()), thickness(0) {};
int height;
PixelAttribute(): height(INT16_MIN), thickness(0) {};
int16_t height;
uint8_t thickness;
inline bool valid_height() {
return height != std::numeric_limits<int>::min();
return height != INT16_MIN;
}
};
@ -30,7 +20,9 @@ public:
virtual ~PixelAttributes();
void setWidth(int width);
void scroll();
inline PixelAttribute &attribute(int z, int x) { return m_pixelAttributes[z + 1][x + 1]; };
inline PixelAttribute &attribute(int z, int x) {
return m_pixelAttributes[z + 1][x + 1];
};
private:
void freeAttributes();
@ -45,6 +37,3 @@ private:
PixelAttribute *m_pixelAttributes[BLOCK_SIZE + 2]; // 1px gradient + empty
int m_width;
};
#endif /* end of include guard: PIXELATTRIBUTES_H_ADZ35GYF */

View File

@ -1,5 +1,4 @@
#ifndef PLAYERATTRIBUTES_H_D7THWFVV
#define PLAYERATTRIBUTES_H_D7THWFVV
#pragma once
#include <list>
#include <string>
@ -16,8 +15,8 @@ public:
typedef std::list<Player> Players;
PlayerAttributes(const std::string &worldDir);
Players::iterator begin();
Players::iterator end();
Players::const_iterator begin() const;
Players::const_iterator end() const;
private:
void readFiles(const std::string &playersPath);
@ -25,6 +24,3 @@ private:
Players m_players;
};
#endif /* end of include guard: PLAYERATTRIBUTES_H_D7THWFVV */

View File

@ -1,21 +1,22 @@
#ifndef TILEGENERATOR_HEADER
#define TILEGENERATOR_HEADER
#include <iosfwd>
#include <iostream>
#include <map>
#include <set>
#include <config.h>
#include <unordered_map>
#include <unordered_set>
#include <stdint.h>
#include <cstdint>
#include <string>
#include "PixelAttributes.h"
#include "BlockDecoder.h"
#include "Image.h"
#include "db.h"
#include "types.h"
class BlockDecoder;
class Image;
enum {
SCALE_TOP = (1 << 0),
SCALE_BOTTOM = (1 << 1),
@ -31,10 +32,12 @@ enum {
};
struct ColorEntry {
ColorEntry(): r(0), g(0), b(0), a(0), t(0) {};
ColorEntry(uint8_t r, uint8_t g, uint8_t b, uint8_t a, uint8_t t): r(r), g(g), b(b), a(a), t(t) {};
inline Color to_color() const { return Color(r, g, b, a); }
uint8_t r, g, b, a, t;
ColorEntry() : r(0), g(0), b(0), a(0), t(0) {};
ColorEntry(uint8_t r, uint8_t g, uint8_t b, uint8_t a, uint8_t t) :
r(r), g(g), b(b), a(a), t(t) {};
inline Color toColor() const { return Color(r, g, b, a); }
uint8_t r, g, b, a; // Red, Green, Blue, Alpha
uint8_t t; // "thickness" value
};
struct BitmapThing { // 16x16 bitmap
@ -73,7 +76,6 @@ public:
void setScaleColor(const std::string &scaleColor);
void setOriginColor(const std::string &originColor);
void setPlayerColor(const std::string &playerColor);
Color parseColor(const std::string &color);
void setDrawOrigin(bool drawOrigin);
void setDrawPlayers(bool drawPlayers);
void setDrawScale(bool drawScale);

View File

@ -1,14 +1,4 @@
/*
* =====================================================================
* Version: 1.0
* Created: 18.09.2012 10:20:51
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#ifndef ZLIBDECOMPRESSOR_H_ZQL1PN8Q
#define ZLIBDECOMPRESSOR_H_ZQL1PN8Q
#pragma once
#include <cstdlib>
#include <string>
@ -31,7 +21,4 @@ private:
const unsigned char *m_data;
std::size_t m_seekPos;
std::size_t m_size;
}; /* ----- end of class ZlibDecompressor ----- */
#endif /* end of include guard: ZLIBDECOMPRESSOR_H_ZQL1PN8Q */
};

View File

@ -1,5 +1,4 @@
#ifndef DB_LEVELDB_HEADER
#define DB_LEVELDB_HEADER
#pragma once
#include "db.h"
#include <unordered_map>
@ -27,5 +26,3 @@ private:
std::unordered_map<int16_t, std::vector<pos2d>> posCache;
leveldb::DB *db;
};
#endif // DB_LEVELDB_HEADER

View File

@ -1,5 +1,4 @@
#ifndef _DB_POSTGRESQL_H
#define _DB_POSTGRESQL_H
#pragma once
#include "db.h"
#include <libpq-fe.h>
@ -31,5 +30,3 @@ protected:
private:
PGconn *db;
};
#endif // _DB_POSTGRESQL_H

View File

@ -1,5 +1,4 @@
#ifndef DB_REDIS_HEADER
#define DB_REDIS_HEADER
#pragma once
#include "db.h"
#include <unordered_map>
@ -33,5 +32,3 @@ private:
redisContext *ctx;
std::string hash;
};
#endif // DB_REDIS_HEADER

View File

@ -1,5 +1,4 @@
#ifndef _DB_SQLITE3_H
#define _DB_SQLITE3_H
#pragma once
#include "db.h"
#include <unordered_map>
@ -32,5 +31,3 @@ private:
int16_t blockCachedZ = -10000;
std::unordered_map<int16_t, BlockList> blockCache; // indexed by X
};
#endif // _DB_SQLITE3_H

View File

@ -1,8 +1,6 @@
#ifndef DB_HEADER
#define DB_HEADER
#pragma once
#include <stdint.h>
#include <map>
#include <cstdint>
#include <list>
#include <vector>
#include <utility>
@ -122,4 +120,3 @@ inline BlockPos DB::decodeBlockPos(int64_t hash) const
* End black magic *
*******************/
#endif // DB_HEADER

View File

@ -1,18 +1,9 @@
#ifndef UTIL_H
#define UTIL_H
#pragma once
#include <string>
#include <fstream>
std::string read_setting(const std::string &name, std::istream &is);
inline std::string read_setting_default(const std::string &name, std::istream &is, const std::string &def)
{
try {
return read_setting(name, is);
} catch(const std::runtime_error &e) {
return def;
}
}
#endif // UTIL_H
std::string read_setting_default(const std::string &name, std::istream &is,
const std::string &def);

View File

@ -8,7 +8,7 @@
#include <string>
#include <sstream>
#include <stdexcept>
#include "cmake_config.h"
#include "config.h"
#include "TileGenerator.h"
static void usage()
@ -58,7 +58,7 @@ static void usage()
static bool file_exists(const std::string &path)
{
std::ifstream ifs(path.c_str());
std::ifstream ifs(path);
return ifs.is_open();
}
@ -256,7 +256,7 @@ int main(int argc, char *argv[])
generator.parseColorsFile(colors);
generator.generate(input, output);
} catch(std::runtime_error &e) {
} catch (const std::runtime_error &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}

View File

@ -43,3 +43,13 @@ std::string read_setting(const std::string &name, std::istream &is)
oss << "Setting '" << name << "' not found";
throw std::runtime_error(oss.str());
}
std::string read_setting_default(const std::string &name, std::istream &is,
const std::string &def)
{
try {
return read_setting(name, is);
} catch(const std::runtime_error &e) {
return def;
}
}