OOFEM 3.0
Loading...
Searching...
No Matches
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 - 2025 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#include <sstream>
40
41namespace oofem {
42OOFEMTXTDataReader :: OOFEMTXTDataReader(std :: string inputfilename) : DataReader(),
43 dataSourceName(std :: move(inputfilename)), recordList()
44{
45 std :: list< std :: pair< int, std :: string > >lines;
46 // Read all the lines in the main input file:
47 {
48 std :: ifstream inputStream(dataSourceName);
49 if ( !inputStream.is_open() ) {
50 OOFEM_ERROR("Can't open input stream (%s)", dataSourceName.c_str());
51 }
52
53 int lineNumber = 0;
54 std :: string line;
55
56 this->giveRawLineFromInput(inputStream, lineNumber, outputFileName);
57 this->giveRawLineFromInput(inputStream, lineNumber, description);
58
59 while (this->giveLineFromInput(inputStream, lineNumber, line)) {
60 lines.emplace_back(make_pair(lineNumber, line));
61 }
62 }
63 // Check for included files: @include "somefile"
64 for ( auto it = lines.begin(); it != lines.end(); ++it ) {
65 if ( it->second.compare(0, 8, "@include") == 0 ) {
66 std :: string fname = it->second.substr(10, it->second.length()-11);
67 OOFEM_LOG_INFO("Reading included file: %s\n", fname.c_str());
68
69 // Remove the include line
70 lines.erase(it++);
71 // Add all the included lines:
72 int includedLine = 0;
73 std :: string line;
74 std :: ifstream includedStream(fname);
75 if ( !includedStream.is_open() ) {
76 OOFEM_ERROR("Can't open input stream (%s)", fname.c_str());
77 }
78 while (this->giveLineFromInput(includedStream, includedLine, line)) {
79 lines.emplace(it, make_pair(includedLine, line));
80 }
81 }
82 }
85 for ( auto &line: lines ) {
86 //printf("line: %s\n", line.second.c_str());
87 this->recordList.emplace_back(line.first, line.second);
88 }
89 this->it = this->recordList.begin();
90}
91
92OOFEMTXTDataReader :: OOFEMTXTDataReader(const OOFEMTXTDataReader &x) : OOFEMTXTDataReader(x.dataSourceName) {}
93
94OOFEMTXTDataReader :: ~OOFEMTXTDataReader()
95{
96}
97
99OOFEMTXTDataReader :: giveInputRecord(InputRecordType typeId, int recordId)
100{
101 if ( this->it == this->recordList.end() ) {
102 OOFEM_ERROR("Out of input records, file contents must be missing");
103 }
104 this->it->setInputRecordType(typeId);
105 return *this->it++;
106}
107
108bool
109OOFEMTXTDataReader :: peekNext(const std :: string &keyword)
110{
111 std :: string nextKey;
112 this->it->giveRecordKeywordField(nextKey);
113 return keyword.compare( nextKey ) == 0;
114}
115
116void
117OOFEMTXTDataReader :: finish()
118{
119 if ( this->it != this->recordList.end() ) {
120 std::ostringstream oss;
121 int i=0;
122 for(; it!=recordList.end(); it++){
123 oss<<" "<<it->giveLineNumber()<<": "<<it->giveRecordAsString()<<std::endl;
124 if(i++>10) { oss<<" ...\n"; break; }
125 }
126 OOFEM_WARNING("There are unread lines in the input file (the most common cause are missing entries in the domain record, e.g. 'nset'):\n%s",oss.str().c_str());
127 }
128 this->recordList.clear();
129}
130
131bool
132OOFEMTXTDataReader :: giveLineFromInput(std :: ifstream &stream, int &lineNum, std :: string &line)
133{
134 bool flag = false; //0-tolower, 1-remain with capitals
135
136 bool read = this->giveRawLineFromInput(stream, lineNum, line);
137 if ( !read ) {
138 return false;
139 }
140
141 for ( auto &c: line ) {
142 if ( c == '"' ) { //do not change to lowercase inside quotation marks
143 flag = !flag; // switch flag
144 }
145
146 if ( !flag ) {
147 c = (char)tolower(c); // convert line to lowercase
148 }
149 }
150 return true;
151}
152
153bool
154OOFEMTXTDataReader :: giveRawLineFromInput(std :: ifstream &stream, int &lineNum, std :: string &line)
155{
156 do {
157 lineNum++;
158 std :: getline(stream, line);
159 if ( !stream ) {
160 return false;
161 } if ( line.length() > 0 ) {
162 if ( line.back() == '\\' ) {
163 std :: string continuedLine;
164 do {
165 lineNum++;
166 std :: getline(stream, continuedLine);
167 if ( !stream ) {
168 return false;
169 }
170 line.pop_back();
171 line += continuedLine;
172 } while ( continuedLine.back() == '\\' );
173 }
174 }
175 } while ( line.length() == 0 || line [ 0 ] == '#' ); // skip comments
176 return true;
177}
178} // end namespace oofem
std::string outputFileName
Output file name (first line in OOFEM input files).
Definition datareader.h:58
InputRecordType
Determines the type of input record.
Definition datareader.h:64
std::string description
Description line (second line in OOFEM input files).
Definition datareader.h:60
OOFEMTXTDataReader(std ::string inputfilename)
Constructor.
std ::list< OOFEMTXTInputRecord > recordList
bool giveLineFromInput(std ::ifstream &stream, int &lineNum, std ::string &line)
std::list< OOFEMTXTInputRecord >::iterator it
Keeps track of the current position in the list.
bool giveRawLineFromInput(std ::ifstream &stream, int &lineNum, std ::string &line)
Reads one line from stream.
#define OOFEM_WARNING(...)
Definition error.h:80
#define OOFEM_ERROR(...)
Definition error.h:79

This page is part of the OOFEM-3.0 documentation. Copyright Copyright (C) 1994-2025 Borek Patzak Bořek Patzák
Project e-mail: oofem@fsv.cvut.cz
Generated at for OOFEM by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2011