support compilation with json-c and son

This commit is contained in:
Steffen Vogel 2016-10-27 04:18:59 -04:00
parent 449fcf05b6
commit 04021a8e7b
6 changed files with 109 additions and 71 deletions

View file

@ -4,11 +4,17 @@ dist_doc_DATA = README
man_MANS = doc/calcelestial.1
if DEBUG
AM_CFLAGS = -g -Wall -Werror -Wno-uninitialized -O0 -fno-omit-frame-pointer
else
AM_CFLAGS = -O3
endif
TEST_OPTS = -p sun -m rise -t 1990-03-20 -q Baden,Switzerland
test: src/calcelestial
[ "$$(src/calcelestial ${TEST_OPTS} -l -f %H:%M:%S)" == "06:30:53" ]
[ "$$(src/calcelestial ${TEST_OPTS} -l -f §A:§O)" == "47.473:8.306" ]
[ "$$(src/calcelestial ${TEST_OPTS} -l -f %J)" == "2447970.730" ]
[ "$$(src/calcelestial ${TEST_OPTS} -l -f %Z)" == "CET" ]
[ "$$(src/calcelestial ${TEST_OPTS} -l -f %H:%M:%S)" = "06:30:53" ]
[ "$$(src/calcelestial ${TEST_OPTS} -l -f §A:§O)" = "47.473:8.306" ]
[ "$$(src/calcelestial ${TEST_OPTS} -l -f %J)" = "2447970.730" ]
[ "$$(src/calcelestial ${TEST_OPTS} -l -f %Z)" = "CET" ]
[ "$$(src/calcelestial ${TEST_OPTS} -z America/New_York -f %H:%M:%S)" == "00:30:53" ]

View file

@ -2,33 +2,15 @@
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.68])
AC_INIT(calcelestial, 0.1, post@steffenvogel.de)
AC_INIT(calcelestial, 0.4, post@steffenvogel.de)
AC_CONFIG_SRCDIR([src/calcelestial.c])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CC
AC_PROG_LN_S
# Checks for libraries.
AC_CHECK_LIB([nova],[ln_get_version],[],[AC_MSG_ERROR([Couldn't find libnova])])
# Checks for header files.
AC_CHECK_HEADERS([libnova/libnova.h],[],[AC_MSG_ERROR([Couldn't find or include libnova headers.h])])
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
# Automake
AM_INIT_AUTOMAKE
AM_PROG_CC_C_O
AC_CONFIG_FILES([
Makefile
src/Makefile
])
# Custom switches
# geonames.org support
AC_ARG_ENABLE(
[geonames],
@ -37,25 +19,59 @@ AC_ARG_ENABLE(
[enable_geonames=yes]
)
AM_CONDITIONAL([GEONAMES_SUPPORT], [test x"$enable_geonames" = x"yes"])
if test x"$enable_geonames" = x"yes"; then
AC_DEFINE([GEONAMES_SUPPORT], [1], [compile with geonames.org lookup capabilities])
PKG_CHECK_MODULES([DEPS_GEONAMES], [libcurl >= 7.21, json-c >= 0.11])
fi
# debug compilation support
AC_ARG_ENABLE(
[debug],
[AS_HELP_STRING([--enable-debug], [enable debug data generation (def=no)])],
[debug=$enableval],
[debug=no]
[enable_debug=$enableval],
[enable_debug=no]
)
if test x"$debug" = x"yes"; then
AC_DEFINE([DEBUG], [], [enable debugging])
AM_CFLAGS="$AM_CFLAGS -g -Wall -Werror -Wno-uninitialized -O0 -fno-omit-frame-pointer"
else
AM_CFLAGS="$AM_CFLAGS -O3"
# Defines
if test x"$enable_geonames" = x"yes"; then
AC_DEFINE([WITH_GEONAMES], [1], [compile with geonames.org lookup capabilities])
fi
# Checks for programs.
AC_PROG_CC
AC_PROG_LN_S
# Checks for libraries.
AC_CHECK_LIB([nova],[ln_get_version],[],[AC_MSG_ERROR([Couldn't find libnova])])
if test x"$enable_geonames" = x"yes"; then
AC_CHECK_LIB([curl],[curl_version],[],[AC_MSG_ERROR([Couldn't find libcurl])])
AC_CHECK_LIB([json-c],[json_c_version],[has_lib_jsonc=yes])
AC_CHECK_LIB([json],[json_object_get],[has_lib_json=yes])
if test x"$has_lib_json" != x"yes" && test x"$has_lib_jsonc" != x"yes"; then
AC_MSG_ERROR([Couldn't find libjson0 or libjson-c library])
fi
fi
# Checks for header files.
AC_CHECK_HEADERS([db.h],[],[AC_MSG_ERROR([Couldn't find or include Berkeley DB header])])
AC_CHECK_HEADERS([libnova/libnova.h],[],[AC_MSG_ERROR([Couldn't find or include libnova headers])])
if test x"$enable_geonames" = x"yes"; then
AC_CHECK_HEADERS([curl/curl.h],[],[AC_MSG_ERROR([Couldn't find or include libcurl headers])])
AC_CHECK_HEADERS([json/json.h],[has_include_json=yes])
AC_CHECK_HEADERS([json-c/json.h],[has_include_jsonc=yes])
if test x"$has_include_json" != x"yes" && test x"$has_include_jsonc" != x"yes"; then
AC_MSG_ERROR([Couldn't find libjson0 or libjson-c headers])
fi
fi
# Conditionals
AM_CONDITIONAL([WITH_GEONAMES], [test x"$enable_geonames" = x"yes"])
AM_CONDITIONAL([DEBUG], [test x"$enable_debug" = x"yes"])
AM_CONDITIONAL([HAS_JSONC], [test x"$has_lib_jsonc" = x"yes"])
AC_CONFIG_FILES([
Makefile
src/Makefile
])
AC_OUTPUT

View file

@ -5,18 +5,23 @@ calcelestial_LDADD = -lm
OBJS = sun moon mars neptune jupiter mercury uranus saturn venus pluto
if GEONAMES_SUPPORT
if WITH_GEONAMES
noinst_PROGRAMS = geonames
geonames_SOURCES = geonames_main.c geonames.c formatter.c
geonames_LDADD = $(DEPS_GEONAMES_LIBS)
calcelestial_SOURCES += geonames.c
calcelestial_LDADD += $(DEPS_GEONAMES_LIBS)
AM_CFLAGS = $(DEPS_GEONAMES_CFLAGS)
endif
if HAS_JSONC
calcelestial_LDADD += -ljson-c
geonames_LDADD = -ljson-c
else
calcelestial_LDADD += -ljson
geonames_LDADD = -ljson
endif
links:
for OBJ in $(OBJS); do \
ln -s calcelestial $$OBJ; \

View file

@ -56,7 +56,7 @@ static struct option long_options[] = {
{"format", required_argument, 0, 'f'},
{"lat", required_argument, 0, 'a'},
{"lon", required_argument, 0, 'o'},
#ifdef GEONAMES_SUPPORT
#ifdef WITH_GEONAMES
{"query", required_argument, 0, 'q'},
{"local", no_argument, 0, 'l'},
#endif
@ -76,7 +76,7 @@ static const char *long_options_descs[] = {
"output format: see strftime (3) and calcelestial (1) for more details",
"geographical latitude of observer: -90° to 90°",
"geographical longitude of oberserver: -180° to 180°",
#ifdef GEONAMES_SUPPORT
#ifdef WITH_GEONAMES
"query coordinates using the geonames.org geolocation service",
"query local timezone using the geonames.org geolocation service",
#endif
@ -215,7 +215,7 @@ int main(int argc, char *argv[])
case 'o':
obs.lng = strtod(optarg, NULL);
break;
#ifdef GEONAMES_SUPPORT
#ifdef WITH_GEONAMES
case 'q':
query = strdup(optarg);
break;
@ -255,7 +255,7 @@ int main(int argc, char *argv[])
if (!obj)
usage_error("invalid or missing object, use --object");
#ifdef GEONAMES_SUPPORT
#ifdef WITH_GEONAMES
/* Lookup place at http://geonames.org */
if (query) {
ret = geonames_lookup_latlng(query, &obs, NULL, 0);

View file

@ -34,9 +34,16 @@
#include <errno.h>
#include <curl/curl.h>
#include <json-c/json.h>
#include <libnova/libnova.h>
#include "config.h"
#ifdef HAVE_JSON_C_JSON_H
#include <json-c/json.h>
#elif defined(HAVE_JSON_JSON_H)
#include <json/json.h>
#endif
#include "../config.h"
#include "geonames.h"
#include "formatter.h"
@ -156,7 +163,6 @@ static int request_json(const char *url, int (*parser)(struct json_object *jobj,
CURLcode res;
struct json_object *jobj;
enum json_tokener_error error;
struct string s = { 0 };
@ -196,10 +202,10 @@ static int request_json(const char *url, int (*parser)(struct json_object *jobj,
#endif /* DEBUG */
cached:
jobj = json_tokener_parse_verbose(s.ptr, &error);
jobj = json_tokener_parse(s.ptr);
if (!jobj) {
#ifdef DEBUG
printf("Debug: failed to parse json: %s\r\n", json_tokener_error_desc(error));
printf("Debug: failed to parse json: %s\r\n", s.ptr);
#endif /* DEBUG */
}
@ -226,14 +232,18 @@ static int parser_tz(struct json_object *jobj, void *userp)
json_bool exists;
struct json_object *jobj_offset, *jobj_tzid;
exists = json_object_object_get_ex(jobj, "gmtOffset", &jobj_offset);
if (!exists)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
jobj_offset = json_object_object_get(jobj, "gmtOffset");
if (!jobj_offset)
return -1;
exists = json_object_object_get_ex(jobj, "timezoneId", &jobj_tzid);
if (!exists)
return -1;
jobj_tzid = json_object_object_get(jobj, "timezoneId");
if (!jobj_tzid)
return -2;
#pragma GCC diagnostic pop
*ctx->gmt_offset = json_object_get_int(jobj_offset);
@ -250,14 +260,16 @@ static int parser_latlng(struct json_object *jobj, void *userp)
json_bool exists;
struct json_object *jobj_count, *jobj_geonames, *jobj_place, *jobj_lat, *jobj_lng, *jobj_name;
int results;
exists = json_object_object_get_ex(jobj, "totalResultsCount", &jobj_count);
if (!exists)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
jobj_count = json_object_object_get(jobj, "totalResultsCount");
if (!jobj_count)
return -1;
exists = json_object_object_get_ex(jobj, "geonames", &jobj_geonames);
if (!exists)
return -2;
jobj_geonames = json_object_object_get(jobj, "geonames");
if (!jobj_geonames)
return -2;
results = json_object_get_int(jobj_count);
if (results == 0)
@ -266,18 +278,19 @@ static int parser_latlng(struct json_object *jobj, void *userp)
jobj_place = json_object_array_get_idx(jobj_geonames, 0);
if (!jobj_place)
return -4;
exists = json_object_object_get_ex(jobj_place, "lat", &jobj_lat);
if (!exists)
jobj_lat = json_object_object_get(jobj_place, "lat");
if (!jobj_lat)
return -5;
exists = json_object_object_get_ex(jobj_place, "lng", &jobj_lng);
if (!exists)
jobj_lng = json_object_object_get(jobj_place, "lng");
if (!jobj_lng)
return -6;
exists = json_object_object_get_ex(jobj_place, "name", &jobj_name);
if (!exists)
jobj_name = json_object_object_get(jobj_place, "name");
if (!jobj_name)
return -7;
#pragma GCC diagnostic pop
ctx->coords->lat = json_object_get_double(jobj_lat);
ctx->coords->lng = json_object_get_double(jobj_lng);

View file

@ -26,8 +26,6 @@
#ifndef _GEONAMES_H_
#define _GEONAMES_H_
#include <json-c/json.h>
/* Forward declaration */
struct ln_lnlat_posn;