Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
First draft.
Able to convert basic METARs and TAFs, for example:
UKLL 042000Z 14003MPS CAVOK 13/11 Q1001 R13/D NOSIG
TAF UKLL 041706Z 0418/0518 17005G10MPS 9999 BKN020 TEMPO 0418/0509 2100 -SHRA BKN007 BKN017CB TEMPO 0509/0518 19009G14MPS 5000 -SHRA BKN010 BKN025CB
  • Loading branch information
nnaumenko committed Jun 4, 2020
0 parents commit 6cd7fdc
Show file tree
Hide file tree
Showing 33 changed files with 4,090 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Auto detect text files and perform LF normalization
* text=auto

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
misc/
build/

Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/
*.cab
*.msi
*.msm
*.msp
*.lnk

*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache
*.sublime-workspace
*.sublime-project

.vscode
*.code-workspace

CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
18 changes: 18 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[submodule "metaf"]
path = metaf
url = https://gitlab.com/nnaumenko/metaf
[submodule "json"]
path = json
url = https://github.com/nlohmann/json
[submodule "googletest"]
path = googletest
url = https://github.com/google/googletest
[submodule "cxxopts"]
path = cxxopts
url = https://github.com/jarro2783/cxxopts
[submodule "magic_enum"]
path = magic_enum
url = https://github.com/Neargye/magic_enum
[submodule "date"]
path = date
url = https://github.com/HowardHinnant/date
61 changes: 61 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
cmake_minimum_required(VERSION 3.11)

project(metafjson)

enable_language(CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-pedantic -Wall -Wextra -fcxx-exceptions")

set(SOURCE src/main.cpp)

include_directories(
include
metaf/include
cxxopts/include
json/include
magic_enum/include
date/include
)

# Build program

add_executable(${PROJECT_NAME}
src/main.cpp
src/commandlineargs.cpp
src/datetimeformat.cpp
src/outputformat.cpp
src/outputformatbasic.cpp
src/settings.cpp
src/utility.cpp
src/valueformat.cpp
)

# Tests

add_executable(test
src/commandlineargs.cpp
src/datetimeformat.cpp
src/outputformat.cpp
src/outputformatbasic.cpp
src/settings.cpp
src/utility.cpp
src/valueformat.cpp
googletest/googletest/src/gtest-all.cc
test/main.cpp
test/test_commandlineargs.cpp
test/test_datetimeformat.cpp
test/test_valueformat.cpp
)

target_include_directories(test PRIVATE
googletest/googletest
googletest/googletest/include)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(TEST_LINK_FLAGS "-lpthread -lm")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(TEST_LINK_FLAGS "-pthread")
endif()

set_target_properties(test PROPERTIES
LINK_FLAGS ${TEST_LINK_FLAGS})
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2020 Nick Naumenko (https://gitlab.com/nnaumenko,
https:://github.com/nnaumenko)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions cxxopts
Submodule cxxopts added at 9a454c
1 change: 1 addition & 0 deletions date
Submodule date added at cac99d
1 change: 1 addition & 0 deletions googletest
Submodule googletest added at cb44c8
41 changes: 41 additions & 0 deletions include/commandlineargs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2020 Nick Naumenko (https://gitlab.com/nnaumenko,
* https:://github.com/nnaumenko)
* All rights reserved.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

#ifndef COMMANDLINEARGS_HPP
#define COMMANDLINEARGS_HPP

#include "settings.hpp"

// Processes command line args, checks for unknown options, if needed
// displays error messages (to stderr), help, and version (to stdout).
class CommandLineArgs : public Settings {
public:
// Process command line args and set parameters according to ones specified
// in the command line
CommandLineArgs(int argc, char *argv[]);

private:
// Print version to stdout
void displayVersion();
// Print help test to stdout
void displayHelp(std::string commandLineArgsHelp);
// Print error message to stderr
void displayError(const char * message);

// Process the value of --output arg
OutputFormat getOutputFormat(std::string format);
// Process the value of --datetime arg
DateTimeFormat getDateTimeFormat(std::string format);
// Process the value of --unit arg
UnitFormat getUnitFormat(std::string format);

// Set reference date from command line args
void setRefDate(std::string yyyymmdd);
};

#endif //#ifndef COMMANDLINEARGS_HPP
74 changes: 74 additions & 0 deletions include/datetimeformat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2020 Nick Naumenko (https://gitlab.com/nnaumenko,
* https:://github.com/nnaumenko)
* All rights reserved.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

#ifndef DATETIMEFORMAT_HPP
#define DATETIMEFORMAT_HPP

#include "nlohmann/json_fwd.hpp"

namespace metaf
{
class MetafTime;
}

class DateTimeFormat
{
public:
struct DateTime
{
DateTime() = default;
DateTime(const metaf::MetafTime &reportTime,
int refYear,
unsigned refMonth,
unsigned refDay);
DateTime(const DateTime &reportTime,
const metaf::MetafTime &time,
bool forecast = false);

// Converts date/time into Unix time
time_t toUnixTime();
bool isEmpty() { return !year && !month && !day && !hour && !minute && !metafTime; }

int year = 0;
unsigned month = 0;
unsigned day = 0;
unsigned hour = 0;
unsigned minute = 0;
std::unique_ptr<metaf::MetafTime> metafTime;

private:
// decrease month (and year if jumping from January to December),
// leave day, hour, minute intact
void previousMonth();
// increase month (and year if jumping from December to January),
// leave day, hour, minute intact
void nextMonth();
// increase day (also month if last day of month and year if last
// day of year), leave hour and minute intact
void nextDay();
};

DateTimeFormat() = default;
virtual ~DateTimeFormat() {}
virtual nlohmann::json format(const DateTime &dateTime) const = 0;
nlohmann::json format(const metaf::MetafTime &time,
const DateTime &reportTime,
bool forecast = false) const;

protected:
};

class DateTimeFormatBasic : public DateTimeFormat
{
public:
DateTimeFormatBasic() = default;
virtual ~DateTimeFormatBasic() {}
virtual nlohmann::json format(const DateTime &dateTime) const;
};

#endif // #ifndef DATETIMEFORMAT_HPP
60 changes: 60 additions & 0 deletions include/metafvisitor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2020 Nick Naumenko (https://gitlab.com/nnaumenko,
* https:://github.com/nnaumenko)
* All rights reserved.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

#ifndef METAFVISITOR_HPP
#define METAFVISITOR_HPP

#include <stdexcept>
#include "metaf.hpp"
#include "datetimeformat.hpp"

class MetafVisitor : public metaf::Visitor<nlohmann::json>
{
public:
MetafVisitor() = delete;
MetafVisitor(const metaf::ParseResult &result,
const DateTimeFormat *dtFormat,
const ValueFormat *valFormat,
bool rawStrings,
int refYear,
unsigned refMonth,
unsigned refDay)
: dateTimeFormat(dtFormat),
valueFormat(valFormat),
includeRawStrings(rawStrings),
referenceYear(refYear),
referenceMonth(refMonth),
referenceDay(refDay)
{
if (!dtFormat)
throw(std::runtime_error("dateTimeFormat is null when creating MetafVisitor"));
if (!valFormat)
throw(std::runtime_error("valueFormat is null when creating MetafVisitor"));

reportDateTime.year = refYear;
reportDateTime.month = refMonth;
reportDateTime.day = refDay;
if (const auto rt = result.reportMetadata.reportTime; rt.has_value()) {
reportDateTime = DateTimeFormat::DateTime(*rt, refYear, refMonth, refDay);
}
}

protected:

const DateTimeFormat *dateTimeFormat;
const ValueFormat *valueFormat;
bool includeRawStrings = false;

int referenceYear = 0;
int referenceMonth = 0;
int referenceDay = 0;

DateTimeFormat::DateTime reportDateTime;
};

#endif //#ifndef METAFVISITOR_HPP
Loading

0 comments on commit 6cd7fdc

Please sign in to comment.