Page MenuHomec4science

reader.hpp
No OneTemporary

File Metadata

Created
Tue, Jul 16, 13:25

reader.hpp

/*-------------------------------------------------------
- Module : database
- File : reader.hpp
- Author : Fabien Georget
Copyright (c) 2014, Fabien Georget <fabieng@princeton.edu>, Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Princeton University nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------*/
#ifndef DATABASE_READER_H
#define DATABASE_READER_H
//! \file reader.hpp Read the json database
#include <jsoncpp/json/value.h>
#include "module.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 DatabaseModule
{
public:
//! \brief Default constructor
//!
//! Be sure to set the path to the database using set_database_path
DataReader() {
reset_database();
}
//! \brief Constructor
//!
//! @param filepath string containing the path to the database
DataReader(std::string filepath):
m_filepath(filepath)
{
reset_database();
}
//! \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
RawDatabasePtr 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 Parse the gas section
void parse_gas();
//! \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
};
//! \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 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