Page MenuHomec4science

reader.hpp
No OneTemporary

File Metadata

Created
Fri, Dec 27, 06:31

reader.hpp

/*-------------------------------------------------------
- Module : database
- File : reader.hpp
- Author : Fabien Georget
Copyright (c) 2014, Fabien Georget, Princeton University
---------------------------------------------------------*/
#ifndef DATABASE_READER_H
#define DATABASE_READER_H
//! \file reader.hpp Read the json database
#include <jsoncpp/json/value.h>
#include <memory>
#include "data_container.hpp"
namespace specmicp {
namespace database {
//! \brief Read a json file containing a database
//!
//! This is a reader class, it does not transform
//! (reduce to canonical form, swap the basis) the data
class DataReader
{
public:
//! Type of a smart pointer containing the database
using DataContainerPtr = std::shared_ptr<DataContainer>;
//! \brief Default constructor
//!
//! Be sure to set the path to the database using set_database_path
DataReader() {
data = std::make_shared<DataContainer>();
}
//! \brief Constructor
//!
//! @param filepath string containing the path to the database
DataReader(std::string filepath):
m_filepath(filepath)
{
data = std::make_shared<DataContainer>();
}
//! \brief Set the database path
void set_database_path(std::string filepath){
m_filepath = filepath;
}
//! \brief Parse database
//!
//! Throw db_invalid_syntax if a syntax error was detected.
//! Throws std::invalid_argument if the path to the database is not correct
void parse();
//! Return the databes
DataContainerPtr get_database() {return data;}
private:
//! \brief Parse the metadata section
//!
//! we don't do much with them for now....
void parse_metadata();
//! \brief Parse the basis section
//!
//! Contains the list of primary species
void parse_basis();
//! \brief Parse the aqueous section
//!
//! Contains the list of secondary species
void parse_aqueous();
//! \brief Parse the mineral section
//!
//! Contains the list of minerals
void parse_minerals();
//! \brief To be sure that the database is og
void check_database();
//! \brief Check the size of the basis
void parse_size_db();
std::string m_filepath; //! The path to the database
Json::Value m_root; //! Root of the json structure
DataContainerPtr data; //! The database to fill
};
//! \brief Parse an equation
void parse_equation(const std::string &equation,
std::map<std::string, float>& compo);
//! \brief Get the charge of a species by parsing the label
//!
//! Examples :
//! - neutral -> 0
//! - neutral[] -> 0
//! - charge[+] -> +1
//! - charge[-1] -> -1
//! - charge[+2] -> +2
//! - charge[2-] -> -2
double charge_from_label(const std::string& label);
//! \brief This error is thrown when a bad syntax is detected in the database
class db_invalid_syntax : public std::exception
{
std::string m_msg;
const char *m_msg_cstr;
public:
db_invalid_syntax (const std::string &msg):
m_msg("DB Invalid Syntax :" + msg), m_msg_cstr(m_msg.c_str())
{}
const char *what() throw()
{
return m_msg_cstr;
}
};
//! \brief Check if a mandatory section/attribute exists
//!
//! Throw db_invalid_syntax if not.
inline void check_for_mandatory_value(const Json::Value& root, const std::string& attribute)
{
if (not root.isMember(attribute))
{
throw db_invalid_syntax("Missing : mandatory parameter '" + attribute +"'.");
}
}
//! \brief Return a mandatory section
//!
//! Throw db_invalid_syntax if the section does not exist
inline Json::Value& get_mandatory_section(Json::Value& root, const std::string& section)
{
check_for_mandatory_value(root, section);
return root[section];
}
} // end namespace database
} // end namespace specmicp
#endif // DATABASE_READER_H

Event Timeline