-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from njoy/feature/disco
Feature/disco
- Loading branch information
Showing
48 changed files
with
3,366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_cpp_test( disco.BaseField BaseField.test.cpp ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
55 changes: 55 additions & 0 deletions
55
src/tools/disco/BaseFixedWidthField/test/BaseFixedWidthField.test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_cpp_test( disco.BaseFixedWidthField BaseFixedWidthField.test.cpp ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_cpp_test( disco.Character Character.test.cpp ) |
Oops, something went wrong.