Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include(cmake/prelude.cmake)

project(
lob
VERSION 0.6.5
VERSION 0.7.0
DESCRIPTION "an exterior balistics calculation library"
HOMEPAGE_URL "https://github.com/joelbenway/lob"
LANGUAGES CXX)
Expand Down
2 changes: 1 addition & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.14)

project(
lobExamples
VERSION 1.0.2
VERSION 1.0.3
DESCRIPTION "Examples using the lob library"
HOMEPAGE_URL "https://github.com/joelbenway/lob"
LANGUAGES CXX)
Expand Down
62 changes: 47 additions & 15 deletions example/lobber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <nlohmann/json.hpp>
#include <sstream>
#include <string>
#include <vector>

#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif

#include "lob/lob.hpp"
#include "version.hpp"

Expand Down Expand Up @@ -42,7 +49,7 @@ void PrintVersion() {
}

void PrintHelp() {
std::cout << "Usage: lobber [options]\n"
std::cout << "Usage: lobber [options] [< input.json]\n"
<< "Options:\n"
<< " --h, --help Show this help message\n"
<< " --v, --version Show version information\n"
Expand All @@ -51,6 +58,9 @@ void PrintHelp() {
"instead of user prompts\n"
<< " --of=FILE Output json file where data is saved for "
"future use as an input file\n"
<< "\n"
<< "Note: When run interactively, a wizard prompts for input.\n"
<< " When stdin is redirected, JSON data is read from stdin.\n"
<< "Example:\n"
<< colors::kYellow << " lobber --of=my_rifle_load.json\n\n"
<< colors::kReset;
Expand All @@ -63,6 +73,15 @@ void PrintGreeting() {
"ballistics library. Follow the prompts to enter data.\n\n";
}

// Returns true if the program is being run in an interactive terminal.
bool IsInteractive() {
#ifdef _WIN32
return _isatty(_fileno(stdin)) != 0;
#else
return isatty(STDIN_FILENO) != 0;
#endif
}

lob::DragFunctionT ConvertDF(double input) {
switch (static_cast<int>(std::round(input))) {
case 2: // NOLINT(cppcoreguidelines-avoid-magic-numbers,
Expand Down Expand Up @@ -509,14 +528,18 @@ void PrintExtraInfo(const lob::Input& input) {
const auto kSFw = static_cast<int>(kSF.length() + kExtra);
const std::string kSS("Speed of Sound");
const auto kSSw = static_cast<int>(kSS.length() + kExtra);
const std::string kE("Error");
const auto kEw = static_cast<int>(kE.length() + kExtra);

std::cout << colors::kYellow << std::left << std::setw(kZAw) << kZA
<< std::setw(kSFw) << kSF << std::setw(kSSw) << kSS
<< colors::kReset << "\n";
<< std::setw(kEw) << kE << colors::kReset << "\n";
std::cout << std::left << std::setw(kZAw) << std::fixed
<< std::setprecision(2) << input.zero_angle << std::setw(kSFw)
<< input.stability_factor << std::setw(kSSw) << input.speed_of_sound
<< "\n\n";
<< std::setw(kEw) << std::hex << std::showbase
<< static_cast<unsigned int>(input.error) << std::dec
<< std::noshowbase << "\n\n";
}

void PrintSolutionTable(const lob::Output* psolutions, size_t size) {
Expand Down Expand Up @@ -618,22 +641,31 @@ int main(int argc, char* argv[]) {
}

if (json.empty()) {
try {
std::cin >> json;
} catch (const nlohmann::json::parse_error& e) {
std::cerr << colors::kRed
<< "Error parsing JSON from stdin: " << colors::kReset
<< e.what() << "\n";
return 1;
if (example::IsInteractive()) {
for (const auto& pair : example::GetStateKeys()) {
json[pair.second] = "nan";
}
example::PrintGreeting();
example::PromptWizard(&json);
} else {
if (std::cin.peek() != std::char_traits<char>::eof()) {
try {
std::cin >> json;
} catch (const nlohmann::json::parse_error& e) {
std::cerr << colors::kRed
<< "Error parsing JSON from stdin: " << colors::kReset
<< e.what() << "\n";
return 1;
}
}
}
}

if (json.empty()) {
for (const auto& pair : example::GetStateKeys()) {
json[pair.second] = "nan";
}
example::PrintGreeting();
example::PromptWizard(&json);
std::cerr << colors::kRed << "Error: No input data provided."
<< colors::kReset << "\n\n";
example::PrintHelp();
return 1;
}

using example::StateType;
Expand Down
55 changes: 48 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@
cmake --install build --prefix $out
'';
};
lobber = pkgs.stdenv.mkDerivation {
name = "lobber";
src = self;
nativeBuildInputs = with pkgs; [
cmake
nlohmann_json
];
configurePhase = ''
cmake -S . -B build \
-D CMAKE_BUILD_TYPE=Release \
-D LOB_DEVELOPER_MODE=OFF \
-D BUILD_EXAMPLES=ON \
-D BUILD_BENCHMARKS=OFF
'';
buildPhase = ''
cmake --build build --parallel $NIX_BUILD_CORES
'';
installPhase = ''
mkdir -p $out/bin
if [ ! -f build/example/lobber ]; then
echo "Error: lobber binary not found at build/example/lobber"
exit 1
fi
cp build/example/lobber $out/bin/
'';
};
});
devShells = forEachSupportedSystem ({pkgs}: let
baseShell =
Expand Down Expand Up @@ -101,13 +127,15 @@
buildInputs = oldAttrs.buildInputs ++ extraDevPackages;
shellHook = let
inherit (pkgs) stdenv;
filename = "CMakeUserPresets.json";
clangdFile = ".clangd";
CMakeUserPresetsFile = "CMakeUserPresets.json";
os =
if stdenv.isLinux
then "linux"
else if stdenv.isDarwin
then "darwin"
else "<os>";
sourceDir = "\\\${sourceDir}";
in
''
json=$(cat <<-EOF
Expand All @@ -121,7 +149,7 @@
"configurePresets": [
{
"name": "dev",
"binaryDir": "/build/dev",
"binaryDir": "${sourceDir}/build/dev",
"inherits": ["dev-mode", "ci-${os}"],
"generator": "Ninja",
"environment": {
Expand All @@ -130,7 +158,7 @@
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"CMAKE_CXX_FLAGS": "$env{CXX_FLAGS_DEV_LINUX} $env{LOB_CXX_FLAGS_COMMON}",
"CMAKE_CXX_FLAGS": "\$env{CXX_FLAGS_DEV_LINUX} \$env{LOB_CXX_FLAGS_COMMON}",
"CMAKE_LINKER_TYPE": "MOLD"
}
}
Expand Down Expand Up @@ -161,11 +189,24 @@
EOF
)

if [ ! -f ${filename} ]; then
echo "$json" > ${filename}
echo "${filename} created successfully"
clangd=$(cat <<-EOF
CompileFlags:
CompilationDatabase: build/dev
EOF
)

if [ ! -f ${CMakeUserPresetsFile} ]; then
echo "$json" > ${CMakeUserPresetsFile}
echo "${CMakeUserPresetsFile} created successfully"
else
echo "${CMakeUserPresetsFile} already exists"
fi

if [ ! -f ${clangdFile} ]; then
echo "$clangd" > ${clangdFile}
echo "${clangdFile} created successfully"
else
echo "${filename} already exists"
echo "${clangdFile} already exists"
fi
''
+ oldAttrs.shellHook;
Expand Down
52 changes: 25 additions & 27 deletions include/lob/lob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,31 @@ enum class LOB_EXPORT ClockAngleT : uint8_t {

enum class LOB_EXPORT ErrorT : uint8_t {
kNone,
kAirPressure,
kAltitude,
kAzimuth,
kBallisticCoefficient,
kBaseDiameter,
kDiameter,
kHumidity,
kInitialVelocity,
kLatitude,
kLength,
kMachDragTable,
kMass,
kMaximumTime,
kMeplatDiameter,
kNoseLength,
kOgiveRtR,
kRangeAngle,
kTailLength,
kWindHeading,
kZeroAngle,
kZeroDistance,
kAirPressureOOR,
kAltitudeOfBarometerOOR,
kAltitudeOfFiringSiteOOR,
kAltitudeOfThermometerOOR,
kAzimuthOOR,
kBallisticCoefficientOOR,
kBallisticCoefficientRequired,
kBaseDiameterOOR,
kDiameterOOR,
kHumidityOOR,
kInitialVelocityRequired,
kInternalError,
kLatitudeOOR,
kLengthOOR,
kMassOOR,
kMaximumTimeOOR,
kMeplatDiameterOOR,
kNoseLengthOOR,
kOgiveRtROOR,
kRangeAngleOOR,
kTailLengthOOR,
kWindHeadingOOR,
kZeroAngleOOR,
kZeroDataRequired,
kZeroDistanceOOR,
kNotFormed
}; // enum class ErrorT

Expand Down Expand Up @@ -437,12 +441,6 @@ class LOB_EXPORT Builder {
*/
Builder& Reset() noexcept;

/**
* @brief Checks if the current builder state is well-formed.
* @return True if state is valid, false otherwise.
*/
bool IsValid() const;

/**
* @brief Builds the `Input` object with the configured parameters.
* @return The constructed `Input` object.
Expand Down
Loading