Skip to content

Commit

Permalink
Merge pull request #20 from njoy/feature/disco
Browse files Browse the repository at this point in the history
Feature/disco
  • Loading branch information
whaeck authored Apr 23, 2024
2 parents 76af2ad + d96f5f3 commit 8ee21f4
Show file tree
Hide file tree
Showing 48 changed files with 3,366 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ target_include_directories( tools
target_link_libraries( tools
INTERFACE
spdlog::spdlog
FastFloat::fast_float
)

target_compile_definitions( tools INTERFACE -DNANORANGE_NO_STD_FORWARD_DECLARATIONS )
Expand Down
7 changes: 7 additions & 0 deletions cmake/develop_dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ FetchContent_Declare( pybind11
GIT_SHALLOW TRUE
)

FetchContent_Declare( fast_float
GIT_REPOSITORY https://github.com/fastfloat/fast_float
GIT_TAG v6.1.1
GIT_SHALLOW TRUE
)

#######################################################################
# Load dependencies
#######################################################################

FetchContent_MakeAvailable(
spdlog
fast_float
)
6 changes: 6 additions & 0 deletions cmake/release_dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ FetchContent_Declare( Catch2
GIT_TAG 3f0283de7a9c43200033da996ff9093be3ac84dc # tag: v3.3.2
)

FetchContent_Declare( fast_float
GIT_REPOSITORY https://github.com/fastfloat/fast_float
GIT_TAG f476bc713fda06fbd34dc621b466745a574b3d4c # tag: v6.1.1
)

FetchContent_Declare( pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG 5b0a6fc2017fcc176545afe3e09c9f9885283242 # tag: v2.10.4
Expand All @@ -26,5 +31,6 @@ set( SPDLOG_BUILD_PIC CACHE INTERNAL BOOL ON )
#######################################################################

FetchContent_MakeAvailable(
fast_float
spdlog
)
15 changes: 15 additions & 0 deletions cmake/unit_testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ endfunction()

add_subdirectory( src/tools/Log/test )

add_subdirectory( src/tools/disco/functions/test )
add_subdirectory( src/tools/disco/BaseField/test )
add_subdirectory( src/tools/disco/FreeFormatCharacter/test )
add_subdirectory( src/tools/disco/FreeFormatInteger/test )
add_subdirectory( src/tools/disco/FreeFormatReal/test )
add_subdirectory( src/tools/disco/BaseFixedWidthField/test )
add_subdirectory( src/tools/disco/Column/test )
add_subdirectory( src/tools/disco/Character/test )
add_subdirectory( src/tools/disco/Integer/test )
add_subdirectory( src/tools/disco/Real/test )
add_subdirectory( src/tools/disco/FixedPoint/test )
add_subdirectory( src/tools/disco/Scientific/test )
add_subdirectory( src/tools/disco/ENDF/test )
add_subdirectory( src/tools/disco/Record/test )

add_subdirectory( src/tools/std20/algorithm/test )
add_subdirectory( src/tools/std20/concepts/test )
add_subdirectory( src/tools/std20/functional/test )
Expand Down
3 changes: 3 additions & 0 deletions src/tools.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// disco
#include "tools/disco.hpp"

// the logging tool
#include "tools/Log.hpp"

Expand Down
14 changes: 14 additions & 0 deletions src/tools/disco.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "tools/disco/functions.hpp"
#include "tools/disco/BaseField.hpp"
#include "tools/disco/FreeFormatCharacter.hpp"
#include "tools/disco/FreeFormatInteger.hpp"
#include "tools/disco/FreeFormatReal.hpp"
#include "tools/disco/BaseFixedWidthField.hpp"
#include "tools/disco/Column.hpp"
#include "tools/disco/Character.hpp"
#include "tools/disco/Integer.hpp"
#include "tools/disco/Real.hpp"
#include "tools/disco/FixedPoint.hpp"
#include "tools/disco/Scientific.hpp"
#include "tools/disco/ENDF.hpp"
#include "tools/disco/Record.hpp"
51 changes: 51 additions & 0 deletions src/tools/disco/BaseField.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef NJOY_TOOLS_DISCO_BASEFIELD
#define NJOY_TOOLS_DISCO_BASEFIELD

// system includes
#include <cctype>

// other includes
#include "tools/disco/functions.hpp"

namespace njoy {
namespace tools {
namespace disco {

/**
* @brief A base class for reading data fields (either fixed width or free
* format).
*/
class BaseField {

/* fields */

protected:

/* auxiliary functions */

/**
* @brief Skip the '+' character
*
* Note: when a plus sign is encountered, the position is incremented.
* In the case of a fixed width field, this may put the position over
* the width so this must be checked prior to calling this function.
*
* @param[in,out] iter an iterator to a character in a range
* @param[in,out] position the current position in the field
*/
template < typename Iterator >
static void skipPlusSign( Iterator& iter, unsigned int& position ) {

if ( *iter == '+' ) {

++iter;
++position;
}
}
};

} // disco namespace
} // tools namespace
} // njoy namespace

#endif
39 changes: 39 additions & 0 deletions src/tools/disco/BaseField/test/BaseField.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// include Catch2
#include <catch2/catch_test_macros.hpp>

// what we are testing
#include "tools/disco/BaseField.hpp"

// other includes

// convenience typedefs
using namespace njoy::tools::disco;

class TestBaseField : protected BaseField {

public:

using BaseField::skipPlusSign;
};

SCENARIO( "BaseField" ) {

std::string string = " a\t\n\r\n\f";
string += char{ std::char_traits<char>::eof() };
auto iter = string.begin();
unsigned int position = 0;

string = "+abc";
position = 0;
iter = string.begin();
TestBaseField::skipPlusSign( iter, position );
CHECK( iter == string.begin() + 1 );
CHECK( position == 1 );

string = "abc";
position = 0;
iter = string.begin();
TestBaseField::skipPlusSign( iter, position );
CHECK( iter == string.begin() );
CHECK( position == 0 );
} // SCENARIO
1 change: 1 addition & 0 deletions src/tools/disco/BaseField/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_cpp_test( disco.BaseField BaseField.test.cpp )
48 changes: 48 additions & 0 deletions src/tools/disco/BaseFixedWidthField.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef NJOY_TOOLS_DISCO_BASEFIXEDWIDTHFIELD
#define NJOY_TOOLS_DISCO_BASEFIXEDWIDTHFIELD

// system includes
#include <cctype>

// other includes
#include "tools/disco/BaseField.hpp"

namespace njoy {
namespace tools {
namespace disco {

/**
* @brief A base class for reading fixed width data fields
*/
template < unsigned int Width >
class BaseFixedWidthField : public BaseField {

/* fields */

protected:

using BaseField::skipPlusSign;

/**
* @brief Skip over spaces until the end of the field or until
* a non-space character is encountered
*
* @param[in,out] iter an iterator to a character in a range
* @param[in,out] position the current position in the field
*/
template < typename Iterator >
static void skipSpaces( Iterator& iter, unsigned int& position ) {

while( position < Width && isSpace( iter ) ) {

++position;
++iter;
}
}
};

} // disco namespace
} // tools namespace
} // njoy namespace

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// include Catch2
#include <catch2/catch_test_macros.hpp>

// what we are testing
#include "tools/disco/BaseFixedWidthField.hpp"

// other includes

// convenience typedefs
using namespace njoy::tools::disco;

template < unsigned int Width >
class TestBaseFixedWidthField : protected BaseFixedWidthField< Width > {

public:

using BaseFixedWidthField< Width >::skipPlusSign;
using BaseFixedWidthField< Width >::skipSpaces;
};

SCENARIO( "BaseFixedWidthField" ) {

std::string string = " a\t\n\r\n\f";
string += char{ std::char_traits<char>::eof() };
auto iter = string.begin();
unsigned int position = 0;

string = "+abc";
position = 0;
iter = string.begin();
TestBaseFixedWidthField< 6 >::skipPlusSign( iter, position );
CHECK( iter == string.begin() + 1 );
CHECK( position == 1 );

string = "abc";
position = 0;
iter = string.begin();
TestBaseFixedWidthField< 6 >::skipPlusSign( iter, position );
CHECK( iter == string.begin() );
CHECK( position == 0 );

string = " ";
position = 0;
iter = string.begin();
TestBaseFixedWidthField< 6 >::skipSpaces( iter, position );
CHECK( iter == string.end() );
CHECK( position == 6 );

string = " ";
position = 0;
iter = string.begin();
TestBaseFixedWidthField< 6 >::skipSpaces( iter, position );
CHECK( iter == string.end() - 1 );
CHECK( position == 6 );
} // SCENARIO
1 change: 1 addition & 0 deletions src/tools/disco/BaseFixedWidthField/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_cpp_test( disco.BaseFixedWidthField BaseFixedWidthField.test.cpp )
90 changes: 90 additions & 0 deletions src/tools/disco/Character.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef NJOY_TOOLS_DISCO_CHARACTER
#define NJOY_TOOLS_DISCO_CHARACTER

// system includes
#include <sstream>
#include <iomanip>

// other includes
#include "tools/disco/BaseFixedWidthField.hpp"

namespace njoy {
namespace tools {
namespace disco {

/**
* @brief A class for reading and writing fixed width data fields containing
* characters.
*/
template< unsigned int Width >
class Character : public BaseFixedWidthField< Width > {

/* fields */

protected:

using BaseFixedWidthField< Width >::skipSpaces;
using BaseFixedWidthField< Width >::skipPlusSign;

public:

/**
* @brief Read character data from the fixed width field
*
* The resulting character data has a length equal to the width of the field.
* Spaces are appended to the character data if a new line character or if the
* end of file character is encountered.
*
* @param[in,out] iter an iterator to a character in a range
*/
template < typename Representation, typename Iterator >
static Representation read( Iterator& iter, const Iterator& ) {

Representation value;
value.reserve( Width );

unsigned int position = 0;
while( position < Width && ! ( isNewLine( iter ) || isEndOfFile( iter ) ) ) {

++position;
value.push_back( *iter++ );
}
while ( position < Width ) {

++position;
value.push_back( ' ' );
}

return value;
}

template < typename Iterator >
static std::string read( Iterator& iter, const Iterator& end ) {

return read< std::string >( iter, end );
}

template< typename Representation, typename Iterator >
static void write( const Representation& value, Iterator& iter ) {

unsigned int position = 0;
const unsigned int difference = value.size() >= Width ? 0 : Width - value.size();
while ( position < difference ) {

++position;
*iter++ = ' ';
}
auto viter = value.begin();
while ( position < Width ) {

++position;
*iter++ = *viter++;
}
}
};

} // disco namespace
} // tools namespace
} // njoy namespace

#endif
1 change: 1 addition & 0 deletions src/tools/disco/Character/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_cpp_test( disco.Character Character.test.cpp )
Loading

0 comments on commit 8ee21f4

Please sign in to comment.