From 3f0904cce89026eab8b5503fc6cd328b3fae33fa Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Thu, 28 Apr 2016 11:56:30 -0700 Subject: [PATCH] Use std::string instead of malloc strings to make mbtiles metadata --- mbtiles.cpp | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/mbtiles.cpp b/mbtiles.cpp index 80153340..4b685458 100644 --- a/mbtiles.cpp +++ b/mbtiles.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "pool.hpp" #include "mbtiles.hpp" #include "geometry.hpp" @@ -82,7 +83,7 @@ void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data, } } -static void quote(char **buf, const char *s) { +static void quote(std::string *buf, const char *s) { char tmp[strlen(s) * 8 + 1]; char *out = tmp; @@ -101,15 +102,10 @@ static void quote(char **buf, const char *s) { } *out = '\0'; - *buf = (char *) realloc(*buf, strlen(*buf) + strlen(tmp) + 1); - if (*buf == NULL) { - perror("realloc"); - exit(EXIT_FAILURE); - } - strcat(*buf, tmp); + buf->append(std::string(tmp)); } -static void aprintf(char **buf, const char *format, ...) { +static void aprintf(std::string *buf, const char *format, ...) { va_list ap; char *tmp; @@ -120,12 +116,7 @@ static void aprintf(char **buf, const char *format, ...) { } va_end(ap); - *buf = (char *) realloc(*buf, strlen(*buf) + strlen(tmp) + 1); - if (*buf == NULL) { - perror("Out of memory"); - exit(EXIT_FAILURE); - } - strcat(*buf, tmp); + buf->append(std::string(tmp)); free(tmp); } @@ -236,11 +227,7 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername, } sqlite3_free(sql); - char *buf = strdup("{"); - if (buf == NULL) { - perror("Out of memory"); - exit(EXIT_FAILURE); - } + std::string buf("{"); aprintf(&buf, "\"vector_layers\": [ "); int i; @@ -291,7 +278,7 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername, aprintf(&buf, " ] }"); - sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('json', %Q);", buf); + sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('json', %Q);", buf.c_str()); if (sqlite3_exec(outdb, sql, NULL, NULL, &err) != SQLITE_OK) { fprintf(stderr, "set json: %s\n", err); if (!forcetable) { @@ -299,7 +286,6 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername, } } sqlite3_free(sql); - free(buf); } void mbtiles_close(sqlite3 *outdb, char **argv) {