Skip to content

Commit

Permalink
[DataFileReader] Add sanity checks for files
Browse files Browse the repository at this point in the history
  • Loading branch information
rico23rico committed Feb 28, 2021
1 parent 4656129 commit 66a4565
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
shell: bash
working-directory: ${{github.workspace}}/src/build
if: matrix.os == 'windows-latest'
run: cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -G "MSYS Makefiles"
run: cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -G "MinGW Makefiles"

- name: Run cmake (Linux)
shell: bash
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[![Building](https://github.com/rico23rico/xpfiles/actions/workflows/cmake.yml/badge.svg)](https://github.com/rico23rico/xpfiles/actions)


This is a support library for this project: https://github.com/JonathanOrr/A321Neo-FXPL
This is a C++ support library for X-Plane 11 Lua plugins (working via FFI).

The library is in the early stage of development and it will offer:
- The asynchronous reading of X-Plane files (e.g., apt.dat and other files)
- Graphics-related support functions (such as polygon triangulation)


The library is currently developed to support the open source [A321Neo project](https://github.com/JonathanOrr/A321Neo-FXPL),
and the development is currently focused on the needs and integration with this project.
59 changes: 53 additions & 6 deletions src/data_file_reader.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,69 @@
#include "data_file_reader.hpp"
#include "plugin.hpp"

#include <filesystem>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdexcept>

#define LOG *this->logger

#define NAV_FILE_PATH "Resources/default data/earth_nav.dat"
#define FIX_FILE_PATH "Resources/default data/earth_fix.dat"
#define ARPT_FILE_PATH "Resources/default scenery/default apt dat/Earth nav data/apt.dat"


namespace xpfiles {

static bool is_a_directory(const std::string& name) noexcept {
struct stat info;
if( stat( name.c_str(), &info ) != 0 ) {
return false;
} else if( info.st_mode & S_IFDIR ) {
return true;
} else {
return false;
}
}

static bool is_a_file (const std::string& name) noexcept {
struct stat info;
if( stat( name.c_str(), &info ) != 0 ) {
return false;
} else if( info.st_mode & S_IFREG ) {
return true;
} else {
return false;
}
}

void DataFileReader::perform_init_checks() {
if (!is_a_directory(xplane_directory)) {
throw std::runtime_error("Directory " + xplane_directory + " is not a directory or is not accessible.");
}

std::string filename = xplane_directory + "/" + NAV_FILE_PATH;
if (!is_a_file(filename)) {
throw std::runtime_error("File " + filename + " is not accessible.");
}

filename = xplane_directory + "/" + FIX_FILE_PATH;
if (!is_a_file(filename)) {
throw std::runtime_error("File " + filename + " is not accessible.");
}

filename = xplane_directory + "/" + ARPT_FILE_PATH;
if (!is_a_file(filename)) {
throw std::runtime_error("File " + filename + " is not accessible.");
}

}

DataFileReader::DataFileReader(const std::string &xplane_directory) : xplane_directory(xplane_directory) {
this->logger = get_logger();

LOG << logger_level_t::DEBUG << "Initializing DataFileReader..." << ENDL;

std::filesystem::path dir_path(xplane_directory);

if (!std::filesystem::is_directory(xplane_directory)) {
throw std::runtime_error("Directory " + xplane_directory + "is not a directory");
}
perform_init_checks();

this->my_thread = std::thread(&DataFileReader::worker, this);
this->my_thread.detach();
Expand Down
4 changes: 4 additions & 0 deletions src/data_file_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class DataFileReader {
std::string xplane_directory;

std::thread my_thread;



void perform_init_checks();
};

}
Expand Down

0 comments on commit 66a4565

Please sign in to comment.