-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first commit : add SpecList and Specification classes
issue #28
- Loading branch information
Valentin Noel
committed
Jul 19, 2013
1 parent
b74c1c0
commit 4ba75fd
Showing
6 changed files
with
277 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Import( 'project', 'libs' ) | ||
|
||
project.StaticLibrary( | ||
'specReader', | ||
dirs = ['src'], | ||
includes = ['src'], | ||
libraries = [ | ||
libs.common, | ||
], | ||
shared = True | ||
) |
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,97 @@ | ||
#include "SpecList.hpp" | ||
|
||
#include <boost/foreach.hpp> | ||
#include <boost/algorithm/string.hpp> | ||
#include <boost/filesystem.hpp> | ||
|
||
namespace bfs = boost::filesystem; | ||
|
||
SpecList::SpecList() | ||
{ | ||
} | ||
|
||
SpecList::~SpecList() | ||
{ | ||
} | ||
|
||
void SpecList::initDirectoryPaths() | ||
{ | ||
clearDirectories(); | ||
std::vector< std::string > dirList; | ||
if( const char* env_options = std::getenv( "QC_SPECS_DIR" ) ) | ||
boost::algorithm::split( dirList, env_options, boost::algorithm::is_any_of(" ") ); | ||
|
||
BOOST_FOREACH( std::string dirPath, dirList ) | ||
addDirectoryPath( dirPath ); | ||
} | ||
|
||
void SpecList::addDirectoryPath( const std::string& directoryPath ) | ||
{ | ||
bfs::path path( directoryPath ); | ||
try | ||
{ | ||
if( !exists( path ) ) | ||
throw std::runtime_error( " does not exist" ); | ||
|
||
if ( !is_directory( path ) ) | ||
throw std::runtime_error( " is not a directory" ); | ||
|
||
_directories.push_back( directoryPath ); | ||
} | ||
catch( const std::runtime_error& ex ) | ||
{ | ||
LOG_WARNING( "Init Directory Path: "<< path.string() << ex.what() ); | ||
} | ||
catch( const bfs::filesystem_error& ex ) | ||
{ | ||
LOG_ERROR( "Init Directory Path: " << ex.what() ); | ||
} | ||
} | ||
|
||
void SpecList::addSpecFromDirectories() | ||
{ | ||
BOOST_FOREACH( std::string filepath, _directories ) | ||
{ | ||
LOG_INFO( "Searching specs in path : " << filepath ); | ||
bfs::path path( filepath ); | ||
bfs::directory_iterator end_itr; | ||
for( bfs::directory_iterator itr( path ); itr != end_itr; ++itr ) | ||
{ | ||
Specification spec; | ||
if( spec.setFromFile( itr->path().string() ) ) | ||
addSpecification( spec ); | ||
} | ||
} | ||
LOG_TRACE( getSpecNumber() << " specification file(s) loaded" ); | ||
} | ||
|
||
void SpecList::addSpecification( const Specification& spec ) | ||
{ | ||
_specifications.push_back( spec ); | ||
} | ||
|
||
|
||
void SpecList::clearSpecifications() | ||
{ | ||
_specifications.clear(); | ||
} | ||
|
||
void SpecList::clearDirectories() | ||
{ | ||
_directories.clear(); | ||
} | ||
|
||
void SpecList::getSpec( Specification& spec, const size_t index ) const | ||
{ | ||
spec = _specifications.at( index ); | ||
} | ||
|
||
void SpecList::getSpecList( std::vector< Specification >& specs ) const | ||
{ | ||
specs = _specifications; | ||
} | ||
|
||
size_t SpecList::getSpecNumber() const | ||
{ | ||
return _specifications.size(); | ||
} |
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,34 @@ | ||
#ifndef _SPECREADER_SPECLIST_HPP_ | ||
#define _SPECREADER_SPECLIST_HPP_ | ||
|
||
#include <Specification/Specification.hpp> | ||
|
||
class SpecList | ||
{ | ||
public: | ||
SpecList(); | ||
~SpecList(); | ||
|
||
void initDirectoryPaths(); | ||
void addDirectoryPath( const std::string& directoryPath ); | ||
void addSpecFromDirectories(); | ||
void addSpecification( const Specification& spec ); | ||
|
||
void clearSpecifications(); | ||
void clearDirectories(); | ||
|
||
void getSpec( Specification& spec, const size_t index ) const; | ||
void getSpecList( std::vector< Specification >& specs ) const; | ||
|
||
size_t getSpecNumber() const; | ||
|
||
private: | ||
std::vector< Specification > _specifications; | ||
std::vector< std::string > _directories; | ||
}; | ||
|
||
|
||
|
||
|
||
|
||
#endif |
101 changes: 101 additions & 0 deletions
101
libraries/specReader/src/Specification/Specification.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,101 @@ | ||
#include "Specification.hpp" | ||
|
||
#include <boost/foreach.hpp> | ||
#include <boost/filesystem.hpp> | ||
|
||
namespace bfs = boost::filesystem; | ||
|
||
const std::string kExtension = "extension"; | ||
const std::string kFooter = "footer"; | ||
const std::string kHeader = "header"; | ||
const std::string kId = "id"; | ||
const std::string kLabel = "label"; | ||
const std::string kStandard = "standard"; | ||
const std::string kType = "type"; | ||
|
||
Specification::Specification() | ||
{ | ||
} | ||
|
||
Specification::~Specification() | ||
{ | ||
} | ||
|
||
void Specification::setFromTree( const bpt::ptree& spec ) | ||
{ | ||
_specTree = spec; | ||
} | ||
|
||
bool Specification::setFromFile( const std::string& filepath ) | ||
{ | ||
bfs::path path( filepath ); | ||
try | ||
{ | ||
if( !exists( path ) ) | ||
throw std::runtime_error( " does not exist" ); | ||
|
||
if ( !is_regular( path ) ) | ||
throw std::runtime_error( " is not a regular file" ); | ||
|
||
if ( path.extension() != ".json" ) | ||
throw std::runtime_error( " - Invalid extension: '.json' expected" ); | ||
|
||
LOG_TRACE( path.string() << ": Reading JSON..." ); | ||
bpt::read_json( path.string(), _specTree ); | ||
return true; | ||
} | ||
catch( const std::runtime_error& ex ) | ||
{ | ||
LOG_WARNING( "Specification from file: "<< path.string() << ex.what() ); | ||
} | ||
catch( const bfs::filesystem_error& ex ) | ||
{ | ||
LOG_ERROR( "Specification from file: " << ex.what() ); | ||
} | ||
return false; | ||
} | ||
|
||
void Specification::setFromString( const std::string& string ) | ||
{ | ||
std::istringstream isstream( string ); | ||
try | ||
{ | ||
LOG_TRACE( "Reading JSON..." ); | ||
bpt::read_json( isstream, _specTree ); | ||
} | ||
catch( const std::exception& ex ) | ||
{ | ||
LOG_ERROR( "Specification from String: " << ex.what() ); | ||
} | ||
} | ||
|
||
|
||
std::string Specification::getId( ) | ||
{ | ||
return _specTree.get< std::string >( kStandard + "." + kId ); | ||
} | ||
|
||
std::string Specification::getLabel( ) | ||
{ | ||
return _specTree.get< std::string >( kStandard + "." + kLabel ); | ||
} | ||
|
||
std::string Specification::getType( ) | ||
{ | ||
return _specTree.get< std::string >( kStandard + "." + kType ); | ||
} | ||
|
||
std::vector< std::string > Specification::getSupportedExtensions( ) | ||
{ | ||
std::vector< std::string > list; | ||
BOOST_FOREACH( bpt::ptree::value_type &node, _specTree.get_child( kStandard + "." + kExtension ) ) | ||
{ | ||
list.push_back( node.second.data() ); | ||
} | ||
return list; | ||
} | ||
|
||
bpt::ptree Specification::getBody( ) | ||
{ | ||
return _specTree.get_child( kHeader ); | ||
} |
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,33 @@ | ||
#ifndef _SPECREADER_SPECIFICATION_HPP_ | ||
#define _SPECREADER_SPECIFICATION_HPP_ | ||
|
||
#include <common/global.hpp> | ||
|
||
#include <boost/property_tree/ptree.hpp> | ||
#include <boost/property_tree/json_parser.hpp> | ||
#include <vector> | ||
|
||
namespace bpt = boost::property_tree; | ||
|
||
class Specification | ||
{ | ||
public: | ||
Specification(); | ||
~Specification(); | ||
|
||
void setFromTree ( const bpt::ptree& spec ); | ||
bool setFromFile ( const std::string& filepath ); | ||
void setFromString( const std::string& string ); | ||
|
||
std::string getId(); | ||
std::string getLabel(); | ||
std::string getType(); | ||
std::vector< std::string > getSupportedExtensions(); | ||
|
||
bpt::ptree getBody( ); | ||
|
||
private: | ||
bpt::ptree _specTree; | ||
}; | ||
|
||
#endif |