From 5f0b7428ed1d5c1733e42cc30e3478b5b9d8703e Mon Sep 17 00:00:00 2001 From: "xanthos@durruti" Date: Wed, 16 Oct 2024 15:07:29 +0300 Subject: [PATCH] added bins --- CMakeLists.txt | 10 ++- src/CMakeLists.txt | 13 ---- src/bin/mjd2ydoy.cpp | 87 ++++++++++++++++++++++++++ src/bin/mjd2ymd.cpp | 87 ++++++++++++++++++++++++++ src/bin/ymd2mjd.cpp | 88 +++++++++++++++++++++++++++ src/lib/CMakeLists.txt | 13 ++++ src/{ => lib}/dat.cpp | 0 src/{ => lib}/datetime_io_core.cpp | 0 src/{ => lib}/modified_julian_day.cpp | 0 src/{ => lib}/month.cpp | 0 src/{ => lib}/strmonth.cpp | 0 src/{ => lib}/tpdateutc.cpp | 0 src/{ => lib}/twopartdates.cpp | 0 src/{ => lib}/utc2tai.cpp | 0 src/{ => lib}/ydoy_date.cpp | 0 src/{ => lib}/ymd_date.cpp | 0 16 files changed, 284 insertions(+), 14 deletions(-) delete mode 100644 src/CMakeLists.txt create mode 100644 src/bin/mjd2ydoy.cpp create mode 100644 src/bin/mjd2ymd.cpp create mode 100644 src/bin/ymd2mjd.cpp create mode 100644 src/lib/CMakeLists.txt rename src/{ => lib}/dat.cpp (100%) rename src/{ => lib}/datetime_io_core.cpp (100%) rename src/{ => lib}/modified_julian_day.cpp (100%) rename src/{ => lib}/month.cpp (100%) rename src/{ => lib}/strmonth.cpp (100%) rename src/{ => lib}/tpdateutc.cpp (100%) rename src/{ => lib}/twopartdates.cpp (100%) rename src/{ => lib}/utc2tai.cpp (100%) rename src/{ => lib}/ydoy_date.cpp (100%) rename src/{ => lib}/ymd_date.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5e4eb1..d1396d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,7 +52,15 @@ target_include_directories(datetime PUBLIC $ ) -add_subdirectory(src) +add_subdirectory(src/lib) + +# add executables +add_executable(ymd2mjd src/bin/ymd2mjd.cpp) +target_link_libraries(ymd2mjd PRIVATE datetime) +add_executable(mjd2ymd src/bin/mjd2ymd.cpp) +target_link_libraries(mjd2ymd PRIVATE datetime) +add_executable(mjd2ydoy src/bin/mjd2ydoy.cpp) +target_link_libraries(mjd2ydoy PRIVATE datetime) # disable clang-tidy (targets that follow will not be checked) set(CMAKE_CXX_CLANG_TIDY "") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index 5bd8431..0000000 --- a/src/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -target_sources(datetime - PRIVATE - ${CMAKE_SOURCE_DIR}/src/dat.cpp - ${CMAKE_SOURCE_DIR}/src/datetime_io_core.cpp - ${CMAKE_SOURCE_DIR}/src/modified_julian_day.cpp - ${CMAKE_SOURCE_DIR}/src/month.cpp - ${CMAKE_SOURCE_DIR}/src/strmonth.cpp - ${CMAKE_SOURCE_DIR}/src/tpdateutc.cpp - ${CMAKE_SOURCE_DIR}/src/twopartdates.cpp - ${CMAKE_SOURCE_DIR}/src/utc2tai.cpp - ${CMAKE_SOURCE_DIR}/src/ydoy_date.cpp - ${CMAKE_SOURCE_DIR}/src/ymd_date.cpp -) diff --git a/src/bin/mjd2ydoy.cpp b/src/bin/mjd2ydoy.cpp new file mode 100644 index 0000000..f65f21e --- /dev/null +++ b/src/bin/mjd2ydoy.cpp @@ -0,0 +1,87 @@ +#include "calendar.hpp" +#include "datetime_write.hpp" +#include +#include +#include +#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) +#include +#endif + +constexpr const int MAX_ERRORS_ALLOWED = 10; + +/* help message */ +void prhelp() { + printf( + "jd2ydoy: Transform a date from Modified Julian Day to calendar date, " + "i.e.\n\"YYYYdDDD\". The program expects the read a Modified Julian " + "Day string\n(actually an integral value) from STDIN (or multiple MJDs, " + "seperated by\nnewlines) and will print results on STDOUT. The MJD string" + " can be followed by\nany number of remaining characters that will be " + "ignored.\n\nOptions:\n[-h] " + "help message\n\tprint (this) message and exit.\n[-e] " + "MAX_ERRORS_ALLOWED\n\tMaximum number of errors allowed (i.e. date " + "strings that where not\n\tparsed correctly). Default values is %d\n\n" + "\n\nWarnings:\n\t* Command line options are only available on POSIX " + "systems.\n\nDionysos Satellite Observatory\nNational Technical " + "University of " + "Athens\nhttps://github.com/DSOlab/ggdatetime\n", + MAX_ERRORS_ALLOWED); + return; +} + +int main(int argc, char *argv[]) { + char line[124]; + char buf[64]; + int mjd; + char c; + int error = 0, cer = 0; + int max_errors_allowed = MAX_ERRORS_ALLOWED; + +#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) + /* parse command line arguments */ + while ((c = getopt(argc, argv, "he:")) != -1) { + switch (c) { + case 'h': + prhelp(); + return 0; + case 'e': + max_errors_allowed = atoi(optarg); + break; + default: + fprintf(stderr, "Usage: %s [-e MAX_ERRORS_ALLOWED]\n", argv[0]); + return 1; + } /* switch c */ + } +#endif + + while (fgets(line, sizeof(line), stdin) && (error::spit(ymd, buf) != + dso::SpitDate::numChars) { + ++error; + } else { + printf("%s\n", buf); + } + } catch (std::exception &) { + ++error; + } + } else { + ++error; + } + + if (error > cer) { + fprintf(stderr, "ERROR. Failed parsing/transforming line: %s\n", line); + ++cer; + } + } + + if (error>=max_errors_allowed) { + fprintf(stderr, "Too many errors, giving up!\n"); + return 1; + } + + return 0; +} diff --git a/src/bin/mjd2ymd.cpp b/src/bin/mjd2ymd.cpp new file mode 100644 index 0000000..9199874 --- /dev/null +++ b/src/bin/mjd2ymd.cpp @@ -0,0 +1,87 @@ +#include "calendar.hpp" +#include "datetime_write.hpp" +#include +#include +#include +#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) +#include +#endif + +constexpr const int MAX_ERRORS_ALLOWED = 10; + +/* help message */ +void prhelp() { + printf( + "jd2ymd: Transform a date from Modified Julian Day to calendar date, " + "i.e.\n\"YYYYdMMdDD\". The program expects the read a Modified Julian " + "Day string\n(actually an integral value) from STDIN (or multiple MJDs, " + "seperated by\nnewlines) and will print results on STDOUT. The MJD string" + " can be followed by\nany number of remaining characters that will be " + "ignored.\n\nOptions:\n[-h] " + "help message\n\tprint (this) message and exit.\n[-e] " + "MAX_ERRORS_ALLOWED\n\tMaximum number of errors allowed (i.e. date " + "strings that where not\n\tparsed correctly). Default values is %d\n\n" + "\n\nWarnings:\n\t* Command line options are only available on POSIX " + "systems.\n\nDionysos Satellite Observatory\nNational Technical " + "University of " + "Athens\nhttps://github.com/DSOlab/ggdatetime\n", + MAX_ERRORS_ALLOWED); + return; +} + +int main(int argc, char *argv[]) { + char line[124]; + char buf[64]; + int mjd; + char c; + int error = 0, cer = 0; + int max_errors_allowed = MAX_ERRORS_ALLOWED; + +#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) + /* parse command line arguments */ + while ((c = getopt(argc, argv, "he:")) != -1) { + switch (c) { + case 'h': + prhelp(); + return 0; + case 'e': + max_errors_allowed = atoi(optarg); + break; + default: + fprintf(stderr, "Usage: %s [-e MAX_ERRORS_ALLOWED]\n", argv[0]); + return 1; + } /* switch c */ + } +#endif + + while (fgets(line, sizeof(line), stdin) && (error::spit(ymd, buf) != + dso::SpitDate::numChars) { + ++error; + } else { + printf("%s\n", buf); + } + } catch (std::exception &) { + ++error; + } + } else { + ++error; + } + + if (error > cer) { + fprintf(stderr, "ERROR. Failed parsing/transforming line: %s\n", line); + ++cer; + } + } + + if (error>=max_errors_allowed) { + fprintf(stderr, "Too many errors, giving up!\n"); + return 1; + } + + return 0; +} diff --git a/src/bin/ymd2mjd.cpp b/src/bin/ymd2mjd.cpp new file mode 100644 index 0000000..0dd838a --- /dev/null +++ b/src/bin/ymd2mjd.cpp @@ -0,0 +1,88 @@ +#include "calendar.hpp" +#include +#include +#include +#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) +#include +#endif + +constexpr const int MAX_ERRORS_ALLOWED = 10; + +/* help message */ +void prhelp() { + printf( + "ymd2mjd: Transform a date given as \"YYYYdMMdDD\" to Modified Julian " + "Day. The\ncharacter \"d\" in the date string can be any character you " + "want,\nexcept from a numeric value. The program expects the read a date " + "string\nfrom STDIN (or multuple date strings, seperated by newlines) " + "and " + "will\nprint results on STDOUT.\nThe date string can be followed by any " + "numberof remaining characters\nthat will be ignored.\n\nExample " + "Usage:\n$>cat " + "dates\n2014:01:09\n2014:01:9\n2014:1:09\n2014:01:08\n2014:01:07\n2014:" + "01:0 // ERROR\n2014:01:1\n2014T01:1\n2014TT01:1 //ERROR\n2014:01:1with " + "some string\n2014/01/1with some string\n$>cat dates | " + "ymd2mjd\n56666\n56666\n56666\n56665\n56664\nERROR. Failed " + "parsing/transforming line: 2014:01:0\n\n56658\n56658\nERROR. Failed " + "parsing/transforming line: 2014TT01:1\n\n56658\n56658\n\nOptions:\n[-h] " + "help message\n\tprint (this) message and exit.\n[-e] " + "MAX_ERRORS_ALLOWED\n\tMaximum number of errors allowed (i.e. date " + "strings that where not\n\tparsed correctly). Default values is %d\n\n" + "\n\nWarnings:\n\t* Command line options are only available on POSIX " + "systems.\n\nDionysos Satellite " + "Observatory\nNational Technical University of " + "Athens\nhttps://github.com/DSOlab/ggdatetime\n", + MAX_ERRORS_ALLOWED); + return; +} + +int main(int argc, char *argv[]) { + char line[124]; + int yr, mn, dm; + char c; + int error = 0, cer = 0; + int max_errors_allowed = MAX_ERRORS_ALLOWED; + +#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) + /* parse command line arguments */ + while ((c = getopt(argc, argv, "he:")) != -1) { + switch (c) { + case 'h': + prhelp(); + return 0; + case 'e': + max_errors_allowed = atoi(optarg); + break; + default: + fprintf(stderr, "Usage: %s [-e MAX_ERRORS_ALLOWED]\n", argv[0]); + return 1; + } /* switch c */ + } +#endif + + while (fgets(line, sizeof(line), stdin) && (error cer) { + fprintf(stderr, "ERROR. Failed parsing/transforming line: %s\n", line); + ++cer; + } + } + + if (error>=max_errors_allowed) { + fprintf(stderr, "Too many errors, giving up!\n"); + return 1; + } + + return 0; +} diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt new file mode 100644 index 0000000..c320a9e --- /dev/null +++ b/src/lib/CMakeLists.txt @@ -0,0 +1,13 @@ +target_sources(datetime + PRIVATE + ${CMAKE_SOURCE_DIR}/src/lib/dat.cpp + ${CMAKE_SOURCE_DIR}/src/lib/datetime_io_core.cpp + ${CMAKE_SOURCE_DIR}/src/lib/modified_julian_day.cpp + ${CMAKE_SOURCE_DIR}/src/lib/month.cpp + ${CMAKE_SOURCE_DIR}/src/lib/strmonth.cpp + ${CMAKE_SOURCE_DIR}/src/lib/tpdateutc.cpp + ${CMAKE_SOURCE_DIR}/src/lib/twopartdates.cpp + ${CMAKE_SOURCE_DIR}/src/lib/utc2tai.cpp + ${CMAKE_SOURCE_DIR}/src/lib/ydoy_date.cpp + ${CMAKE_SOURCE_DIR}/src/lib/ymd_date.cpp +) diff --git a/src/dat.cpp b/src/lib/dat.cpp similarity index 100% rename from src/dat.cpp rename to src/lib/dat.cpp diff --git a/src/datetime_io_core.cpp b/src/lib/datetime_io_core.cpp similarity index 100% rename from src/datetime_io_core.cpp rename to src/lib/datetime_io_core.cpp diff --git a/src/modified_julian_day.cpp b/src/lib/modified_julian_day.cpp similarity index 100% rename from src/modified_julian_day.cpp rename to src/lib/modified_julian_day.cpp diff --git a/src/month.cpp b/src/lib/month.cpp similarity index 100% rename from src/month.cpp rename to src/lib/month.cpp diff --git a/src/strmonth.cpp b/src/lib/strmonth.cpp similarity index 100% rename from src/strmonth.cpp rename to src/lib/strmonth.cpp diff --git a/src/tpdateutc.cpp b/src/lib/tpdateutc.cpp similarity index 100% rename from src/tpdateutc.cpp rename to src/lib/tpdateutc.cpp diff --git a/src/twopartdates.cpp b/src/lib/twopartdates.cpp similarity index 100% rename from src/twopartdates.cpp rename to src/lib/twopartdates.cpp diff --git a/src/utc2tai.cpp b/src/lib/utc2tai.cpp similarity index 100% rename from src/utc2tai.cpp rename to src/lib/utc2tai.cpp diff --git a/src/ydoy_date.cpp b/src/lib/ydoy_date.cpp similarity index 100% rename from src/ydoy_date.cpp rename to src/lib/ydoy_date.cpp diff --git a/src/ymd_date.cpp b/src/lib/ymd_date.cpp similarity index 100% rename from src/ymd_date.cpp rename to src/lib/ymd_date.cpp