a couple of coding style cleanups

This commit is contained in:
Steffen Vogel 2016-10-26 17:34:10 -04:00
parent c7e4179ec3
commit 83fafc09a3
3 changed files with 38 additions and 47 deletions

View file

@ -224,11 +224,11 @@ int main(int argc, char *argv[]) {
case 'v':
version();
return EXIT_SUCCESS;
return 0;
case 'h':
usage();
return EXIT_SUCCESS;
return 0;
case '?':
default:
@ -265,7 +265,7 @@ int main(int argc, char *argv[]) {
if (error) {
printf("\n");
usage();
return EXIT_FAILURE;
return -1;
}
/* calculate julian date */
@ -273,9 +273,9 @@ int main(int argc, char *argv[]) {
t = (utc) ? mktimeutc(date) : mktime(date);
free(date);
}
else {
else
t = time(NULL);
}
jd = ln_get_julian_from_timet(&t);
date = localtime(&t);
@ -294,8 +294,8 @@ int main(int argc, char *argv[]) {
/* calc rst date */
rst: if (object_rst(obj, jd - .5, horizon, &result.obs, &result.rst) == 1) {
if (moment != MOMENT_NOW) {
fprintf(stderr, "object is circumpolar\n");
return EXIT_CIRCUMPOLAR;
fprintf(stderr, "object is circumpolar\n");
return 2;
}
}
else {
@ -319,5 +319,5 @@ rst: if (object_rst(obj, jd - .5, horizon, &result.obs, &result.rst) == 1) {
/* format & output */
format_result(format, &result);
return EXIT_SUCCESS;
return 0;
}

View file

@ -72,11 +72,11 @@ static size_t json_parse_callback(void *contents, size_t size, size_t nmemb, voi
int geonames_lookup(const char *place, struct ln_lnlat_posn *result, char *name, int n) {
#ifdef GEONAMES_CACHE_SUPPORT
if (geonames_cache_lookup(place, result, name, n) == EXIT_SUCCESS) {
if (geonames_cache_lookup(place, result, name, n) == 0) {
#ifdef DEBUG
printf("using cached geonames entry\n");
#endif
return EXIT_SUCCESS;
return 0;
}
#endif
@ -87,14 +87,14 @@ int geonames_lookup(const char *place, struct ln_lnlat_posn *result, char *name,
/* setup curl */
ch = curl_easy_init();
if (!ch) return -1;
if (!ch)
return -1;
/* prepare url */
int len = strlen(place) + strlen(request_url_tpl) + 1;
char *request_url = malloc(len);
if (!request_url) {
if (!request_url)
return -2;
}
snprintf(request_url, len, request_url_tpl, place, username);
@ -115,12 +115,12 @@ int geonames_lookup(const char *place, struct ln_lnlat_posn *result, char *name,
if (res != CURLE_OK) {
fprintf(stderr, "request failed: %s\n", curl_easy_strerror(res));
return EXIT_FAILURE;
return -1;
}
if (jobj) {
int ret = geonames_parse(jobj, result, name, n);
if (ret == EXIT_SUCCESS) {
if (!ret) {
#ifdef GEONAMES_CACHE_SUPPORT
geonames_cache_store(place, result, name, n);
#ifdef DEBUG
@ -131,26 +131,23 @@ int geonames_lookup(const char *place, struct ln_lnlat_posn *result, char *name,
return ret;
}
else {
return EXIT_FAILURE;
}
else
return -1;
}
int geonames_parse(struct json_object *jobj, struct ln_lnlat_posn *result, char *name, int n) {
int results = json_object_get_int(json_object_object_get(jobj, "totalResultsCount"));
if (results == 0) {
return EXIT_FAILURE;
}
if (results == 0)
return -1;
struct json_object *jobj_place = json_object_array_get_idx(json_object_object_get(jobj, "geonames"), 0);
result->lat = json_object_get_double(json_object_object_get(jobj_place, "lat"));
result->lng = json_object_get_double(json_object_object_get(jobj_place, "lng"));
if (name && n > 0) {
if (name && n > 0)
strncpy(name, json_object_get_string(json_object_object_get(jobj_place, "name")), n);
}
return EXIT_SUCCESS;
return 0;
}
int geonames_cache_lookup(const char *place, struct ln_lnlat_posn *result, char *name, int n) {
@ -159,20 +156,18 @@ int geonames_cache_lookup(const char *place, struct ln_lnlat_posn *result, char
snprintf(filename, sizeof(filename), "%s/%s", getenv("HOME"), GEONAMES_CACHE_FILE);
FILE *file = fopen(filename, "r"); /* should check the result */
if (file == NULL) {
return EXIT_FAILURE;
}
if (file == NULL)
return -1;
char line[256];
while (fgets(line, sizeof(line), file)) {
/* replace newline at the end */
char *end = strchr(line, '\n');
if (end == NULL) {
return EXIT_FAILURE;
return -1;
}
else {
else
*end = '\0';
}
char *tok;
int col;
@ -195,7 +190,7 @@ int geonames_cache_lookup(const char *place, struct ln_lnlat_posn *result, char
case 3:
strncpy(name, tok, n);
fclose(file);
return EXIT_SUCCESS; /* found! */
return 0; /* found! */
}
col++;
}
@ -211,9 +206,8 @@ int geonames_cache_store(const char *place, struct ln_lnlat_posn *result, char *
snprintf(filename, sizeof(filename), "%s/%s", getenv("HOME"), GEONAMES_CACHE_FILE);
FILE* file = fopen(filename, "a+"); /* should check the result */
if (file == NULL) {
return EXIT_FAILURE;
}
if (file == NULL)
return -1;
/* build cache entry */
char line[256];
@ -221,9 +215,9 @@ int geonames_cache_store(const char *place, struct ln_lnlat_posn *result, char *
if (fputs(line, file) == EOF) {
fclose(file);
return EXIT_FAILURE;
return -1;
}
fclose(file);
return EXIT_SUCCESS;
return 0;
}

View file

@ -33,21 +33,18 @@
int main(int argc, char *argv[]) {
struct ln_lnlat_posn res;
char *result_name = malloc(32);
char *name = "Aachen";
char *result_name, *name;
if (result_name == NULL) {
return EXIT_FAILURE;
}
result_name = malloc(128);
if (result_name == NULL)
return -1;
if (argc == 2) {
name = argv[1];
}
if (argc != 2)
fprintf(stderr, "Usage: geonames LOCATION\n");
int ret = geonames_lookup(name, &res, result_name, 32);
if (ret == EXIT_SUCCESS) {
int ret = geonames_lookup(argv[1], &res, result_name, 32);
if (!ret)
printf("%s is at (%.4f, %.4f)\r\n", result_name, res.lat, res.lng);
}
free(result_name);