123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
-
-
-
-
-
-
- #include "ASCIIParser.h"
-
- namespace Lemma {
-
-
-
-
- std::ostream &operator << (std::ostream &stream, const ASCIIParser &ob) {
- stream << ob.Serialize() << "\n---\n";
- return stream;
- }
-
-
-
-
-
-
-
-
- ASCIIParser::ASCIIParser ( const ctor_cookie& ) : LemmaObject( ), input(),
- CommentString("//"), BufferSize(255) {
-
- }
-
-
-
-
-
-
-
- std::shared_ptr< ASCIIParser > ASCIIParser::NewSP() {
-
-
- return std::make_shared<ASCIIParser>( ctor_cookie() );
- }
-
-
-
-
-
- YAML::Node ASCIIParser::Serialize ( ) const {
- YAML::Node node = LemmaObject::Serialize();;
- node.SetTag( GetName() );
- node["CommentString"] = CommentString;
- node["BufferSize"] = BufferSize;
- return node;
- }
-
-
-
-
-
- std::shared_ptr<ASCIIParser> ASCIIParser::DeSerialize ( const YAML::Node& node ) {
- if (node.Tag() != "ASCIIParser") {
- throw DeSerializeTypeMismatch( "ASCIIParser", node.Tag());
- }
-
- return std::make_shared< ASCIIParser >( node, ctor_cookie() );
- }
-
-
-
-
-
-
- ASCIIParser::~ASCIIParser () {
-
- }
-
-
-
-
-
-
-
- ASCIIParser::ASCIIParser (const YAML::Node& node, const ctor_cookie& ) : LemmaObject(node) {
- this->CommentString = node["CommentString"].as<std::string>();
- this->BufferSize = node["BufferSize"].as<int>();
- }
-
-
-
-
-
- void ASCIIParser::Open ( const std::string& fname ) {
- input.open(fname.c_str(), std::ios::in);
- if (input.fail()) {
- throw GenericFileIOError(this, fname);
- }
- return ;
- }
-
-
-
-
-
-
- void ASCIIParser::Close ( ) {
- input.close();
- return ;
- }
-
-
-
-
-
-
- std::vector<Real> ASCIIParser::ReadReals ( const int& nr ) {
- std::string buf;
- char *dump = new char[BufferSize];
- std::vector<Real> vals(0);
- while (input >> buf) {
- if (buf.substr(0, CommentString.size()) == CommentString) {
- input.getline(dump, BufferSize);
- } else {
- vals.push_back( atof(buf.c_str() ));
- }
- if (static_cast<int>(vals.size()) == nr) {
- delete [] dump;
- return vals;
- }
- }
- delete [] dump;
- return vals;
- }
-
-
-
-
-
- std::vector<int> ASCIIParser::ReadInts ( const int& nr ) {
- std::string buf;
- char *dump = new char[BufferSize];
- std::vector<int> vals(0);
- while (input >> buf) {
- if (buf.substr(0, CommentString.size()) == CommentString) {
- input.getline(dump, BufferSize);
- } else {
- vals.push_back( atoi(buf.c_str() ));
- }
- if (static_cast<int>(vals.size()) == nr) {
- delete [] dump;
- return vals;
- }
-
- }
- delete [] dump;
- return vals;
- }
-
-
-
-
-
- std::vector<std::string> ASCIIParser::ReadStrings ( const int& nr ) {
- std::string buf;
- char *dump = new char[BufferSize];
- std::vector<std::string> vals(0);
- while (input >> buf) {
- if (buf.substr(0, CommentString.size()) == CommentString) {
- input.getline(dump, BufferSize);
- } else {
- vals.push_back( buf );
- }
- if (static_cast<int>(vals.size()) == nr) {
- delete [] dump;
- return vals;
- }
-
- }
- delete [] dump;
- return vals;
- }
-
-
-
-
-
-
- void ASCIIParser::SetCommentString ( const std::string& key ) {
- CommentString = key;
- }
-
-
-
-
-
- void ASCIIParser::SetBufferSize ( const int& size ) {
- BufferSize = size;
- }
-
-
-
-
-
- int ASCIIParser::GetFileLocation ( ) {
- return input.tellg();
- }
-
-
-
-
-
- void ASCIIParser::JumpToLocation ( const int& loc ) {
- input.seekg( loc );
- return ;
- }
-
-
- }
-
|