Sprinkle consts and casts to make it valid C++ as well as C

This commit is contained in:
Eric Fischer 2014-12-02 14:17:49 -08:00
parent 77c4ce4171
commit a1e7426956
7 changed files with 43 additions and 36 deletions

View File

@ -13,6 +13,7 @@
#include <limits.h>
#include <sqlite3.h>
#include <stdarg.h>
#include "jsonpull.h"
#include "tile.h"
#include "pool.h"
@ -30,7 +31,7 @@ int full_detail = -1;
#define GEOM_MULTIPOLYGON 5 /* array of arrays of arrays of arrays of positions */
#define GEOM_TYPES 6
char *geometry_names[GEOM_TYPES] = {
const char *geometry_names[GEOM_TYPES] = {
"Point",
"MultiPoint",
"LineString",
@ -58,8 +59,8 @@ int mb_geometry[GEOM_TYPES] = {
};
int indexcmp(const void *v1, const void *v2) {
const struct index *i1 = v1;
const struct index *i2 = v2;
const struct index *i1 = (const struct index *) v1;
const struct index *i2 = (const struct index *) v2;
if (i1->index < i2->index) {
return -1;
@ -82,7 +83,7 @@ int indexcmp(const void *v1, const void *v2) {
return 0;
}
size_t fwrite_check(const void *ptr, size_t size, size_t nitems, FILE *stream, char *fname, json_pull *source) {
size_t fwrite_check(const void *ptr, size_t size, size_t nitems, FILE *stream, const char *fname, json_pull *source) {
size_t w = fwrite(ptr, size, nitems, stream);
if (w != nitems) {
fprintf(stderr, "%s:%d: Write to temporary file failed: %s\n", fname, source->line, strerror(errno));
@ -91,22 +92,22 @@ size_t fwrite_check(const void *ptr, size_t size, size_t nitems, FILE *stream, c
return w;
}
void serialize_int(FILE *out, int n, long long *fpos, char *fname, json_pull *source) {
void serialize_int(FILE *out, int n, long long *fpos, const char *fname, json_pull *source) {
fwrite_check(&n, sizeof(int), 1, out, fname, source);
*fpos += sizeof(int);
}
void serialize_byte(FILE *out, signed char n, long long *fpos, char *fname, json_pull *source) {
void serialize_byte(FILE *out, signed char n, long long *fpos, const char *fname, json_pull *source) {
fwrite_check(&n, sizeof(signed char), 1, out, fname, source);
*fpos += sizeof(signed char);
}
void serialize_uint(FILE *out, unsigned n, long long *fpos, char *fname, json_pull *source) {
void serialize_uint(FILE *out, unsigned n, long long *fpos, const char *fname, json_pull *source) {
fwrite_check(&n, sizeof(unsigned), 1, out, fname, source);
*fpos += sizeof(unsigned);
}
void serialize_string(FILE *out, char *s, long long *fpos, char *fname, json_pull *source) {
void serialize_string(FILE *out, const char *s, long long *fpos, const char *fname, json_pull *source) {
int len = strlen(s);
serialize_int(out, len + 1, fpos, fname, source);
@ -115,7 +116,7 @@ void serialize_string(FILE *out, char *s, long long *fpos, char *fname, json_pul
*fpos += len + 1;
}
void parse_geometry(int t, json_object *j, unsigned *bbox, long long *fpos, FILE *out, int op, char *fname, json_pull *source) {
void parse_geometry(int t, json_object *j, unsigned *bbox, long long *fpos, FILE *out, int op, const char *fname, json_pull *source) {
if (j == NULL || j->type != JSON_ARRAY) {
fprintf(stderr, "%s:%d: expected array for type %d\n", fname, source->line, t);
return;
@ -194,7 +195,7 @@ struct pool_val *deserialize_string(char **f, struct pool *p, int type) {
return ret;
}
void check(struct index *ix, long long n, char *metabase, unsigned *file_bbox, struct pool *file_keys, unsigned *midx, unsigned *midy, char *layername, int maxzoom, int minzoom, sqlite3 *outdb, double droprate, int buffer) {
void check(struct index *ix, long long n, char *metabase, unsigned *file_bbox, struct pool *file_keys, unsigned *midx, unsigned *midy, const char *layername, int maxzoom, int minzoom, sqlite3 *outdb, double droprate, int buffer) {
fprintf(stderr, "\n");
long long most = 0;
@ -296,7 +297,7 @@ static void merge(struct merge *merges, int nmerges, unsigned char *map, FILE *f
}
}
void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom, sqlite3 *outdb, struct pool *exclude, struct pool *include, int exclude_all, double droprate, int buffer, char *tmpdir) {
void read_json(FILE *f, const char *fname, const char *layername, int maxzoom, int minzoom, sqlite3 *outdb, struct pool *exclude, struct pool *include, int exclude_all, double droprate, int buffer, const char *tmpdir) {
char metaname[strlen(tmpdir) + strlen("/meta.XXXXXXXX") + 1];
char indexname[strlen(tmpdir) + strlen("/index.XXXXXXXX") + 1];
@ -354,25 +355,29 @@ void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom,
json_object *geometry = json_hash_get(j, "geometry");
if (geometry == NULL) {
fprintf(stderr, "%s:%d: feature with no geometry\n", fname, jp->line);
goto next_feature;
json_free(j);
continue;
}
json_object *geometry_type = json_hash_get(geometry, "type");
if (geometry_type == NULL || geometry_type->type != JSON_STRING) {
fprintf(stderr, "%s:%d: geometry without type string\n", fname, jp->line);
goto next_feature;
json_free(j);
continue;
}
json_object *properties = json_hash_get(j, "properties");
if (properties == NULL || properties->type != JSON_HASH) {
fprintf(stderr, "%s:%d: feature without properties hash\n", fname, jp->line);
goto next_feature;
json_free(j);
continue;
}
json_object *coordinates = json_hash_get(geometry, "coordinates");
if (coordinates == NULL || coordinates->type != JSON_ARRAY) {
fprintf(stderr, "%s:%d: feature without coordinates array\n", fname, jp->line);
goto next_feature;
json_free(j);
continue;
}
int t;
@ -383,7 +388,8 @@ void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom,
}
if (t >= GEOM_TYPES) {
fprintf(stderr, "%s:%d: Can't handle geometry type %s\n", fname, jp->line, geometry_type->string);
goto next_feature;
json_free(j);
continue;
}
{
@ -428,7 +434,8 @@ void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom,
;
} else {
fprintf(stderr, "%s:%d: Unsupported property type for %s\n", fname, jp->line, properties->keys[i]->string);
goto next_feature;
json_free(j);
continue;
}
}
}
@ -552,7 +559,6 @@ void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom,
seq++;
}
next_feature:
json_free(j);
/* XXX check for any non-features in the outer object */
@ -581,7 +587,7 @@ next_feature:
exit(EXIT_FAILURE);
}
char *meta = mmap(NULL, metast.st_size, PROT_READ, MAP_PRIVATE, metafd, 0);
char *meta = (char *) mmap(NULL, metast.st_size, PROT_READ, MAP_PRIVATE, metafd, 0);
if (meta == MAP_FAILED) {
perror("mmap meta");
exit(EXIT_FAILURE);
@ -592,14 +598,15 @@ next_feature:
char trunc[strlen(fname) + 1];
if (layername == NULL) {
char *cp, *use = fname;
for (cp = fname; *cp; cp++) {
if (*cp == '/' && cp[1] != '\0') {
use = cp + 1;
const char *ocp, *use = fname;
for (ocp = fname; *ocp; ocp++) {
if (*ocp == '/' && ocp[1] != '\0') {
use = ocp + 1;
}
}
strcpy(trunc, use);
cp = strstr(trunc, ".json");
char *cp = strstr(trunc, ".json");
if (cp != NULL) {
*cp = '\0';
}
@ -691,7 +698,7 @@ next_feature:
exit(EXIT_FAILURE);
}
merge(merges, nmerges, map, f, bytes, indexst.st_size / bytes);
merge(merges, nmerges, (unsigned char *) map, f, bytes, indexst.st_size / bytes);
munmap(map, indexst.st_size);
fclose(f);
@ -708,7 +715,7 @@ next_feature:
exit(EXIT_FAILURE);
}
struct index *index = mmap(NULL, indexst.st_size, PROT_READ, MAP_PRIVATE, indexfd, 0);
struct index *index = (struct index *) mmap(NULL, indexst.st_size, PROT_READ, MAP_PRIVATE, indexfd, 0);
if (index == MAP_FAILED) {
perror("mmap index");
exit(EXIT_FAILURE);
@ -765,7 +772,7 @@ int main(int argc, char **argv) {
int force = 0;
double droprate = 2.5;
int buffer = 5;
char *tmpdir = "/tmp";
const char *tmpdir = "/tmp";
struct pool exclude, include;
pool_init(&exclude, 0);

View File

@ -161,7 +161,7 @@ static json_object *add_object(json_pull *j, json_type type) {
return o;
}
json_object *json_hash_get(json_object *o, char *s) {
json_object *json_hash_get(json_object *o, const char *s) {
if (o == NULL || o->type != JSON_HASH) {
return NULL;
}

View File

@ -54,4 +54,4 @@ json_object *json_read(json_pull *j);
json_object *json_read_separators(json_pull *j, json_separator_callback cb, void *state);
void json_free(json_object *j);
json_object *json_hash_get(json_object *o, char *s);
json_object *json_hash_get(json_object *o, const char *s);

View File

@ -70,7 +70,7 @@ void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data,
}
}
static void quote(char **buf, char *s) {
static void quote(char **buf, const char *s) {
char tmp[strlen(s) * 8 + 1];
char *out = tmp;
@ -107,7 +107,7 @@ static void aprintf(char **buf, const char *format, ...) {
free(tmp);
}
void mbtiles_write_metadata(sqlite3 *outdb, char *fname, char *layername, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, struct pool *fields) {
void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, const char *layername, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, struct pool *fields) {
char *sql, *err;
sql = sqlite3_mprintf("INSERT INTO metadata (name, value) VALUES ('name', %Q);", fname);

View File

@ -2,6 +2,6 @@ sqlite3 *mbtiles_open(char *dbname, char **argv);
void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data, int size);
void mbtiles_write_metadata(sqlite3 *outdb, char *fname, char *layername, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, struct pool *fields);
void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, const char *layername, int minzoom, int maxzoom, double minlat, double minlon, double maxlat, double maxlon, double midlat, double midlon, struct pool *fields);
void mbtiles_close(sqlite3 *outdb, char **argv);

View File

@ -214,7 +214,7 @@ void decode_meta(char **meta, struct pool *keys, struct pool *values, struct poo
}
}
mapnik::vector::tile create_tile(char *layername, int line_detail, std::vector<coalesce> &features, long long *count, struct pool *keys, struct pool *values) {
mapnik::vector::tile create_tile(const char *layername, int line_detail, std::vector<coalesce> &features, long long *count, struct pool *keys, struct pool *values) {
mapnik::vector::tile tile;
mapnik::vector::tile_layer *layer = tile.add_layers();
@ -284,7 +284,7 @@ struct sll {
}
};
void evaluate(std::vector<coalesce> &features, char *metabase, struct pool *file_keys, char *layername, int line_detail, long long orig) {
void evaluate(std::vector<coalesce> &features, char *metabase, struct pool *file_keys, const char *layername, int line_detail, long long orig) {
std::vector<sll> options;
struct pool_val *pv;
@ -342,7 +342,7 @@ void evaluate(std::vector<coalesce> &features, char *metabase, struct pool *file
pool_free(&keys);
}
long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned tx, unsigned ty, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb, double droprate, int buffer) {
long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned tx, unsigned ty, int detail, int basezoom, struct pool *file_keys, const char *layername, sqlite3 *outdb, double droprate, int buffer) {
int line_detail;
static bool evaluated = false;

2
tile.h
View File

@ -27,4 +27,4 @@ struct index {
int candup : 1;
};
long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned x, unsigned y, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb, double droprate, int buffer);
long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned x, unsigned y, int detail, int basezoom, struct pool *file_keys, const char *layername, sqlite3 *outdb, double droprate, int buffer);