restructure code

This commit is contained in:
Steffen Vogel 2016-10-27 02:24:47 -04:00
parent 42f7c44e05
commit 112fd4cfd6
5 changed files with 42 additions and 166 deletions

View file

@ -1,6 +1,6 @@
bin_PROGRAMS = calcelestial
calcelestial_SOURCES = calcelestial.c objects.c helpers.c formatter.c
calcelestial_SOURCES = calcelestial.c objects.c formatter.c
calcelestial_LDADD = -lm
OBJS = sun moon mars neptune jupiter mercury uranus saturn venus pluto
@ -8,7 +8,7 @@ OBJS = sun moon mars neptune jupiter mercury uranus saturn venus pluto
if GEONAMES_SUPPORT
noinst_PROGRAMS = geonames
geonames_SOURCES = geonames_main.c geonames.c
geonames_SOURCES = geonames_main.c geonames.c formatter.c
geonames_LDADD = $(DEPS_GEONAMES_LIBS)
calcelestial_SOURCES += geonames.c

View file

@ -28,9 +28,43 @@
#include <string.h>
#include "objects.h"
#include "helpers.h"
#include "formatter.h"
#define PRECISION "3"
struct specifiers {
const char *token;
void *data;
enum { DOUBLE, STRING, INTEGER } format;
};
/** Replace parts of the string with a possibily long replacement */
char * strrepl(const char *subject, const char *search, const char *replace) {
int new_len = strlen(subject);
int search_len = strlen(search);
int replace_len = strlen(replace);
char *tmp;
for (tmp = strstr(subject, search); tmp != NULL; tmp = strstr(tmp + search_len, search))
new_len += replace_len - search_len;
const char *old = subject;
char *new = malloc(new_len);
new[0] = '\0'; /* empty string */
for (tmp = strstr(subject, search); tmp != NULL; tmp = strstr(tmp + search_len, search)) {
new_len = strlen(new);
strncpy(new + new_len, old, tmp - old);
strcpy(new + new_len + (tmp - old), replace);
old = tmp + search_len;
}
strcpy(new + strlen(new), old);
return new;
}
void format_result(const char *format, struct object_details *result)
{
char buffer[128];
@ -56,7 +90,6 @@ void format_result(const char *format, struct object_details *result)
{"§h", &result->hrz.alt, DOUBLE},
{"§d", &result->diameter, DOUBLE},
{"§e", &result->distance, DOUBLE},
{"§t", &result->tz, INTEGER},
{"§A", &result->obs.lat, DOUBLE},
{"§O", &result->obs.lng, DOUBLE},
{"§s", (void *) result->azidir, STRING},
@ -72,11 +105,11 @@ void format_result(const char *format, struct object_details *result)
case INTEGER: snprintf(buffer, sizeof(buffer), "%d", * (int *) specifiers[i].data); break;
}
local_format = strreplace(local_format, specifiers[i].token, buffer);
local_format = strrepl(local_format, specifiers[i].token, buffer);
}
}
strfjd(buffer, sizeof(buffer), local_format, result->jd, result->tz);
strftime(buffer, sizeof(buffer), local_format, &result->tm);
printf("%s\n", buffer);
free(local_format);

View file

@ -28,15 +28,10 @@
#include <libnova/libnova.h>
#define PRECISION "3"
/* Forward declaration */
struct object_details;
enum specifier_fmt { DOUBLE, STRING, INTEGER };
struct specifiers {
const char *token;
void *data;
enum specifier_fmt format;
};
char * strrepl(const char *subject, const char *search, const char *replace);
void format_result(const char *format, struct object_details *result);

View file

@ -1,117 +0,0 @@
/**
* Helper functions
*
* @copyright 2012 Steffen Vogel
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <post@steffenvogel.de>
* @link https://www.noteblok.net/2012/03/14/cron-jobs-fur-sonnenauf-untergang/
*/
/*
* This file is part of calcelestial
*
* calcelestial is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* calcelestial is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with calcelestial. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <libnova/libnova.h>
#include "../config.h"
#include "helpers.h"
char * strfjddur(char *s, size_t max, const char *format, double jd) {
struct tm tmd;
struct ln_date lnd;
char *local_format = strdup(format);
ln_get_date(jd + 0.5, &lnd);
if (strstr(format, "%s") != NULL) {
char timestamp_str[16];
unsigned long long seconds = round(jd * 86400);
snprintf(timestamp_str, sizeof(timestamp_str), "%llu", seconds);
format = strreplace(local_format, "%s", timestamp_str);
}
tmd.tm_year = -1900;
tmd.tm_mon = -1;
tmd.tm_mday = 0;
tmd.tm_hour = lnd.hours;
tmd.tm_min = lnd.minutes;
tmd.tm_sec = lnd.seconds;
strftime(s, max, local_format, &tmd);
free(local_format);
return s;
}
char * strfjd(char *s, size_t max, const char *format, double jd, int tz) {
struct tm tmd;
time_t t;
ln_get_timet_from_julian(jd + tz / 24.0, &t);
strftime(s, max, format, gmtime(&t));
return s;
}
char * strreplace(char *subject, const char *search, const char *replace) {
int new_len = strlen(subject);
int search_len = strlen(search);
int replace_len = strlen(replace);
char *tmp;
for (tmp = strstr(subject, search); tmp != NULL; tmp = strstr(tmp + search_len, search)) {
new_len += replace_len - search_len;
}
char *old = subject;
char *new = malloc(new_len);
new[0] = '\0'; /* empty string */
for (tmp = strstr(subject, search); tmp != NULL; tmp = strstr(tmp + search_len, search)) {
new_len = strlen(new);
strncpy(new + new_len, old, tmp - old);
strcpy(new + new_len + (tmp - old), replace);
old = tmp + search_len;
}
strcpy(new + strlen(new), old);
return new;
}
time_t mktimeutc(struct tm *date) {
time_t result;
char *tz;
tz = getenv("TZ");
setenv("TZ", "", 1);
tzset();
result = mktime(date);
if (tz) setenv("TZ", tz, 1);
else unsetenv("TZ");
tzset();
return result;
}

View file

@ -1,35 +0,0 @@
/**
* Helper functions
*
* @copyright 2012 Steffen Vogel
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <post@steffenvogel.de>
* @link https://www.noteblok.net/2012/03/14/cron-jobs-fur-sonnenauf-untergang/
*/
/*
* This file is part of calcelestial
*
* calcelestial is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* calcelestial is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with calcelestial. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _HELPERS_H_
#define _HELPERS_H_
char * strfjddur(char *s, size_t max, const char *format, double jd);
char * strfjd(char *s, size_t max, const char *format, double jd, int tz);
char * strreplace(char *subject, const char *search, const char *replace);
time_t mktimeutc(struct tm *date);
#endif /* _HELPERS_H_ */