OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
oofemtxtdatareader.C
Go to the documentation of this file.
1 /*
2  *
3  * ##### ##### ###### ###### ### ###
4  * ## ## ## ## ## ## ## ### ##
5  * ## ## ## ## #### #### ## # ##
6  * ## ## ## ## ## ## ## ##
7  * ## ## ## ## ## ## ## ##
8  * ##### ##### ## ###### ## ##
9  *
10  *
11  * OOFEM : Object Oriented Finite Element Code
12  *
13  * Copyright (C) 1993 - 2013 Borek Patzak
14  *
15  *
16  *
17  * Czech Technical University, Faculty of Civil Engineering,
18  * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33  */
34 
35 #include "oofemtxtdatareader.h"
36 #include "error.h"
37 
38 #include <string>
39 
40 namespace oofem {
41 OOFEMTXTDataReader :: OOFEMTXTDataReader(std :: string inputfilename) : DataReader(),
42  dataSourceName(std :: move(inputfilename)), recordList()
43 {
44  std :: list< std :: pair< int, std :: string > >lines;
45  // Read all the lines in the main input file:
46  {
47  std :: ifstream inputStream(dataSourceName);
48  if ( !inputStream.is_open() ) {
49  OOFEM_ERROR("Can't open input stream (%s)", dataSourceName.c_str());
50  }
51 
52  int lineNumber = 0;
53  std :: string line;
54 
55  this->giveRawLineFromInput(inputStream, lineNumber, outputFileName);
56  this->giveRawLineFromInput(inputStream, lineNumber, description);
57 
58  while (this->giveLineFromInput(inputStream, lineNumber, line)) {
59  lines.emplace_back(make_pair(lineNumber, line));
60  }
61  }
62  // Check for included files: @include "somefile"
63  for ( auto it = lines.begin(); it != lines.end(); ++it ) {
64  if ( it->second.compare(0, 8, "@include") == 0 ) {
65  std :: string fname = it->second.substr(10, it->second.length()-11);
66  OOFEM_LOG_INFO("Reading included file: %s\n", fname.c_str());
67 
68  // Remove the include line
69  lines.erase(it++);
70  // Add all the included lines:
71  int includedLine = 0;
72  std :: string line;
73  std :: ifstream includedStream(fname);
74  if ( !includedStream.is_open() ) {
75  OOFEM_ERROR("Can't open input stream (%s)", fname.c_str());
76  }
77  while (this->giveLineFromInput(includedStream, includedLine, line)) {
78  lines.emplace(it, make_pair(includedLine, line));
79  }
80  }
81  }
84  for ( auto &line: lines ) {
85  //printf("line: %s\n", line.second.c_str());
86  this->recordList.emplace_back(line.first, line.second);
87  }
88  this->it = this->recordList.begin();
89 }
90 
92 
94 {
95 }
96 
99 {
100  if ( this->it == this->recordList.end() ) {
101  OOFEM_ERROR("Out of input records, file contents must be missing");
102  }
103  return &(*this->it++);
104 }
105 
106 bool
107 OOFEMTXTDataReader :: peakNext(const std :: string &keyword)
108 {
109  std :: string nextKey;
110  this->it->giveRecordKeywordField(nextKey);
111  return keyword.compare( nextKey ) == 0;
112 }
113 
114 void
116 {
117  if ( this->it != this->recordList.end() ) {
118  OOFEM_WARNING("There are unread lines in the input file\n"
119  "The most common cause are missing entries in the domain record, e.g. 'nset'");
120  }
121  this->recordList.clear();
122 }
123 
124 bool
125 OOFEMTXTDataReader :: giveLineFromInput(std :: ifstream &stream, int &lineNum, std :: string &line)
126 {
127  bool flag = false; //0-tolower, 1-remain with capitals
128 
129  bool read = this->giveRawLineFromInput(stream, lineNum, line);
130  if ( !read ) {
131  return false;
132  }
133 
134  for ( auto &c: line ) {
135  if ( c == '"' ) { //do not change to lowercase inside quotation marks
136  flag = !flag; // switch flag
137  }
138 
139  if ( !flag ) {
140  c = (char)tolower(c); // convert line to lowercase
141  }
142  }
143  return true;
144 }
145 
146 bool
147 OOFEMTXTDataReader :: giveRawLineFromInput(std :: ifstream &stream, int &lineNum, std :: string &line)
148 {
149  do {
150  lineNum++;
151  std :: getline(stream, line);
152  if ( !stream ) {
153  return false;
154  } if ( line.length() > 0 ) {
155  if ( line.back() == '\\' ) {
156  std :: string continuedLine;
157  do {
158  lineNum++;
159  std :: getline(stream, continuedLine);
160  if ( !stream ) {
161  return false;
162  }
163  line.pop_back();
164  line += continuedLine;
165  } while ( continuedLine.back() == '\\' );
166  }
167  }
168  } while ( line.length() == 0 || line [ 0 ] == '#' ); // skip comments
169  return true;
170 }
171 } // end namespace oofem
bool giveRawLineFromInput(std::ifstream &stream, int &lineNum, std::string &line)
Reads one line from stream.
virtual bool peakNext(const std::string &keyword)
Peak in advance into the record list.
OOFEMTXTDataReader(std::string inputfilename)
Constructor.
virtual void finish()
Allows to detach all data connections.
Class representing the abstraction for input data source.
Definition: datareader.h:50
std::list< OOFEMTXTInputRecord >::iterator it
Keeps track of the current position in the list.
#define OOFEM_LOG_INFO(...)
Definition: logger.h:127
bool giveLineFromInput(std::ifstream &stream, int &lineNum, std::string &line)
Reads one line from inputStream Parts within quotations have case preserved.
#define OOFEM_ERROR(...)
Definition: error.h:61
std::string description
Description line (second line in OOFEM input files).
Definition: datareader.h:56
std::list< OOFEMTXTInputRecord > recordList
Class representing the general Input Record.
Definition: inputrecord.h:101
InputRecordType
Determines the type of input record.
Definition: datareader.h:60
std::string outputFileName
Output file name (first line in OOFEM input files).
Definition: datareader.h:54
Class representing the implementation of plain text date reader.
the oofem namespace is to define a context or scope in which all oofem names are defined.
virtual InputRecord * giveInputRecord(InputRecordType, int recordId)
Returns input record corresponding to given InputRecordType value and its record_id.
#define OOFEM_WARNING(...)
Definition: error.h:62

This page is part of the OOFEM documentation. Copyright (c) 2011 Borek Patzak
Project e-mail: info@oofem.org
Generated at Tue Jan 2 2018 20:07:30 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011