Skip to content

Commit

Permalink
Merge pull request #10 from LBNL-ETA/BaseLoaderRoutines
Browse files Browse the repository at this point in the history
Base loader routines
  • Loading branch information
vidanovic authored Sep 19, 2024
2 parents fb09720 + a2c0d12 commit 9ff1a3d
Show file tree
Hide file tree
Showing 12 changed files with 630 additions and 123 deletions.
17 changes: 10 additions & 7 deletions CMakeLists-xmlParser.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
cmake_minimum_required(VERSION 3.11)

include(FetchContent)
if(NOT TARGET xmlParser)

FetchContent_Declare(
XMLParser
GIT_REPOSITORY https://github.com/LBNL-ETA/XMLParser.git
GIT_TAG v1.0.0
)
include(FetchContent)

FetchContent_MakeAvailable(XMLParser)
FetchContent_Declare(
XMLParser
GIT_REPOSITORY https://github.com/LBNL-ETA/XMLParser.git
GIT_TAG v1.0.1
)

FetchContent_MakeAvailable(XMLParser)
endif()

# Assuming the target name is xmlParser
target_include_directories(xmlParser SYSTEM PUBLIC ${xmlParser_INCLUDE_DIRS})
60 changes: 35 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,50 +1,60 @@
cmake_minimum_required(VERSION 3.5)
project(FileParse CXX)

# Set the runtime library to dynamic for all configurations in MSVC
if (MSVC)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

set(LIB_MAJOR_VERSION "1")
set(LIB_MINOR_VERSION "1")
set(LIB_PATCH_VERSION "0")
set(LIB_VERSION_STRING "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_PATCH_VERSION}")

cmake_minimum_required(VERSION 3.5)

set(LIB_NAME ${PROJECT_NAME})

if(NOT "${CMAKE_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD 17)
endif()

if(NOT "${CMAKE_CXX_EXTENSIONS}")
set(CMAKE_CXX_EXTENSIONS OFF)
endif()
# Set C++ standard and extensions
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include(GNUInstallDirs)
if(NOT "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
endif()

if(NOT "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
endif()

if(NOT "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
endif()
# Set output directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})

# Include additional CMake files
include(CMakeLists-xmlParser.txt)

add_subdirectory(include/fileParse)

# Include directories for targets
target_include_directories(${LIB_NAME}
PUBLIC
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)

option(BUILD_FileParse_tests "Build FileParse tests." ON)

if(BUILD_FileParse_tests)
enable_testing()
add_subdirectory(test)
endif()
enable_testing()
add_subdirectory(test)
endif()

# Explicitly set the runtime library for each target
if (MSVC)
set_target_properties(${LIB_NAME} PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

# Set runtime for tests if enabled
if(TARGET FileParse-test)
set_target_properties(FileParse-test PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

# Repeat for other targets, including any third-party or external targets
endif()
1 change: 1 addition & 0 deletions include/fileParse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_library( ${LIB_NAME}
Array.hxx
Attributes.hxx
Base.hxx
FileDataHandler.hxx
Formatter.hxx
Formatter.cxx
Common.hxx
Expand Down
88 changes: 88 additions & 0 deletions include/fileParse/FileDataHandler.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#pragma once

#include <string>
#include <fstream>
#include <sstream>
#include <optional>

#include "XMLNodeAdapter.hxx"

namespace Common
{
namespace
{
void createFileFromString(std::string_view fileName, std::string_view fileContent)
{
std::ofstream out(fileName.data());
out << fileContent;
out.close();
}
} // namespace

template<typename T>
std::optional<T> loadFromXMLString(const std::string & data, const std::string & nodeTypeName)
{
// Attempt to load the top node for the given type
const auto xmlNode = getTopNodeFromString(data, nodeTypeName);

// Create an instance of the type
if(xmlNode.has_value())
{
T model;
// Assume that `operator>>` is overloaded for T and xmlNode type
xmlNode.value() >> model;
return model;
}

return std::nullopt;
}

template<typename T>
std::optional<T> loadFromXMLFile(std::string_view fileName, const std::string & nodeTypeName)
{
// Convert std::string_view to std::string for file operations
std::string fileNameStr(fileName);

// Check if file exists and is accessible; if not, create it with default content
if(std::ifstream f(fileNameStr.c_str()); !f.good())
{
const std::string fileContent = "<" + nodeTypeName + ">\n</" + nodeTypeName + ">";
createFileFromString(fileNameStr, fileContent);
}

// Attempt to load the top node for the given type
const auto xmlNode = getTopNodeFromFile(fileNameStr, nodeTypeName);


if(xmlNode.has_value())
{
T model;
// Assume that `operator>>` is overloaded for T and xmlNode type
xmlNode.value() >> model;
return model;
}

return std::nullopt;
}

template<typename T>
int saveToXMLFile(const T & object, std::string_view fileName, const std::string & nodeName)

{
auto node = createTopNode(nodeName);

node << object;

return node.writeToFile(fileName.data());
}

template<typename T>
std::string saveToXMLString(const T & object, const std::string & nodeName)
{
auto node = createTopNode(nodeName);

node << object;

return node.getContent();
}
} // namespace Common
81 changes: 81 additions & 0 deletions include/fileParse/StringConversion.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#pragma once

#include <string>
#include <array>
#include <map>

namespace FileParse
{
Expand Down Expand Up @@ -60,4 +62,83 @@ namespace FileParse
{
return from_string<T>(str);
}

/// Enumerator to string conversion routines.

/// Converts an enumerator value to a string.
/// @tparam EnumType The type of the enumerator.
/// @tparam N The number of enumerator values.
/// @param value The enumerator value to convert to a string.
/// @param values An array of strings representing the enumerator values.
/// @return The string representation of the enumerator value.
template<typename EnumType, std::size_t N>
EnumType enumFromString(std::string_view name,
const std::array<std::string, N> & values,
EnumType defaultValue)
{
for(std::size_t i = 0; i < values.size(); ++i)
{
if(values[i] == name)
{
return static_cast<EnumType>(i);
}
}
return defaultValue;
}

/// Converts an enumerator value to a string.
/// @tparam EnumType The type of the enumerator.
/// @tparam N The number of enumerator values.
/// @param value The enumerator value to convert to a string.
/// @param values An array of strings representing the enumerator values.
/// @return The string representation of the enumerator value.
template<typename EnumType, std::size_t N>
std::string enumToString(EnumType value, const std::array<std::string, N> & values)
{
const auto index{static_cast<std::size_t>(value)};
if(index < values.size())
{
return values[index];
}
return "Unknown";
}

/// Converts an enumerator value to a string.
/// @tparam T The type of the enumerator.
/// @param enumType The enumerator value to convert to a string.
/// @param enumMap A map of enumerator values to string representations.
/// @return The string representation of the enumerator value.
template<typename T>
std::string enumToString(const T enumType, const std::map<T, std::string> & enumMap)
{
if(enumMap.count(enumType) > 0)
{
return enumMap.at(enumType);
}
return "Unknown";
}

/// Converts a string to an enumerator value.
/// @tparam T The type of the enumerator.
/// @param enumString The string to convert to an enumerator value.
/// @param enumMap A map of enumerator values to string representations.
/// @return The enumerator value corresponding to the input string.
template<typename T>
T enumFromString(std::string_view enumString, const std::map<T, std::string> & enumMap)
{
if(!enumString.empty())
{
auto it
= std::find_if(enumMap.begin(), enumMap.end(), [&enumString](const auto & pair) {
return pair.second == enumString;
});

if(it != enumMap.end())
{
return it->first;
}
}

return enumMap.begin()->first;
}
} // namespace FileParse
Loading

0 comments on commit 9ff1a3d

Please sign in to comment.