-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathRifEclipseTextFileReader.cpp
More file actions
300 lines (243 loc) · 10.1 KB
/
Copy pathRifEclipseTextFileReader.cpp
File metadata and controls
300 lines (243 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2021 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RifEclipseTextFileReader.h"
#include "RifEclipseKeywordContent.h"
// Utility class for fast conversion from string to float
#include "fast_float/fast_float.h"
// Utility class memory mapping of files
#include "mio/mio.hpp"
#include "RiaPreferencesSystem.h"
#include "RiaStdStringTools.h"
#include <filesystem>
#include <fstream>
#include <iosfwd>
#include <iostream>
#include <sstream>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RifEclipseKeywordContent> RifEclipseTextFileReader::readKeywordAndValues( const std::string& filename )
{
if ( RiaPreferencesSystem::current()->eclipseTextFileReaderMode() == RiaPreferencesSystem::EclipseTextFileReaderMode::MEMORY_MAPPED_FILE )
{
return readKeywordAndValuesMemoryMappedFile( filename );
}
return readKeywordAndValuesFile( filename );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RifEclipseKeywordContent> RifEclipseTextFileReader::readKeywordAndValuesFile( const std::string& filename )
{
std::string stringData;
std::ifstream inFile;
inFile.open( filename );
std::stringstream strStream;
strStream << inFile.rdbuf();
stringData = strStream.str();
return parseStringData( stringData );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RifEclipseKeywordContent> RifEclipseTextFileReader::readKeywordAndValuesMemoryMappedFile( const std::string& filename )
{
if ( !std::filesystem::exists( filename ) ) return {};
mio::mmap_source mmap( filename );
std::error_code error;
mio::mmap_sink rw_mmap = mio::make_mmap_sink( filename, 0, mio::map_entire_file, error );
std::string_view stringData = rw_mmap.data();
return parseStringData( stringData );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<std::string, std::vector<float>>
RifEclipseTextFileReader::readKeywordAndValues( const std::string_view& stringData, const size_t offset, size_t& bytesRead )
{
std::vector<float> values;
auto isKeywordWithoutData = []( const std::string& keyword )
{
std::vector<std::string> keywords = { "ECHO", "NOECHO" };
return std::find( keywords.begin(), keywords.end(), keyword ) != keywords.end();
};
const auto commentChar = '-';
const auto commentString = "--";
std::string keywordName;
std::string_view line;
bool isEndTokenKeywordRead = false;
bytesRead = 0;
float value = 0.0f;
bool isFaultKeyword = false;
while ( !isEndTokenKeywordRead && ( offset + bytesRead < stringData.size() ) )
{
size_t bytesReadLine = 0;
line = readLine( stringData, offset + bytesRead, bytesReadLine );
bytesRead += bytesReadLine;
if ( isFaultKeyword )
{
// Read data until the FAULTS section is closed with a single / on one line
ltrim( line );
if ( !line.empty() && line[0] == '/' )
{
return std::make_pair( keywordName, values );
}
continue;
}
// Check for '--', used to define start of comments
if ( line.size() > 2 && line[0] == commentChar && line[1] == commentChar ) continue;
if ( keywordName.empty() )
{
auto found = line.find_first_of( commentString );
if ( found != std::string::npos )
{
line = line.substr( 0, found );
}
trim( line );
if ( !line.empty() )
{
keywordName = line;
if ( keywordName == "FAULTS" ) isFaultKeyword = true;
if ( isKeywordWithoutData( keywordName ) ) isEndTokenKeywordRead = true;
}
continue;
}
// End of keyword is defined by '/', parse values of line until '/' is found
auto positionOfEndToken = line.find_first_of( '/' );
if ( positionOfEndToken != std::string::npos )
{
line = line.substr( 0, positionOfEndToken );
}
if ( !isEndTokenKeywordRead )
{
size_t start = 0;
size_t end = 0;
// Index in token for the '*' character used to define a multiplier for the float value
//
size_t multiplierIndex = 0;
while ( ( start = line.find_first_not_of( RifEclipseTextFileReader::m_whiteSpace, end ) ) != std::string::npos )
{
end = line.find_first_of( RifEclipseTextFileReader::m_whiteSpace, start );
int multiplier = 1;
multiplierIndex = line.find_first_of( '*', start );
if ( multiplierIndex < end )
{
int multiplierCandidate = 1;
if ( RiaStdStringTools::toInt( line.substr( start, multiplierIndex - start ), multiplierCandidate ) )
{
multiplier = multiplierCandidate;
}
start = multiplierIndex + 1;
}
auto resultObject = fast_float::from_chars( line.data() + start, line.data() + end, value );
if ( resultObject.ec == std::errc() )
{
for ( size_t i = 0; i < static_cast<size_t>( multiplier ); i++ )
{
values.emplace_back( value );
}
}
}
}
if ( positionOfEndToken != std::string::npos )
{
isEndTokenKeywordRead = true;
continue;
}
}
return std::make_pair( keywordName, values );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RifEclipseKeywordContent> RifEclipseTextFileReader::parseStringData( const std::string_view& stringData )
{
size_t offset = 0;
size_t bytesRead = 0;
std::vector<RifEclipseKeywordContent> dataObjects;
while ( offset < stringData.size() )
{
auto [keyword, values] = RifEclipseTextFileReader::readKeywordAndValues( stringData, offset, bytesRead );
if ( !keyword.empty() )
{
RifEclipseKeywordContent content;
content.keyword = keyword;
content.offset = offset;
if ( values.empty() )
{
content.content = stringData.substr( offset, bytesRead );
}
else
{
content.values = values;
}
dataObjects.emplace_back( content );
}
offset += bytesRead;
}
return dataObjects;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::string_view RifEclipseTextFileReader::readLine( const std::string_view& source, const size_t offset, size_t& bytesRead )
{
if ( offset >= source.size() ) return {};
auto start = source.find_first_not_of( RifEclipseTextFileReader::m_whiteSpace, offset );
if ( start == std::string::npos )
{
bytesRead = source.size() - offset;
return source.substr( offset, bytesRead );
}
auto end = source.find_first_of( '\n', start );
if ( end != std::string::npos )
{
// Add 1 to skip the \n we have found
bytesRead = end - offset + 1;
return source.substr( start, end - start );
}
// No line shift found, reached end of string data
end = source.size();
bytesRead = end - offset;
return source.substr( start, bytesRead );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseTextFileReader::rtrim( std::string_view& s, const char* t /*= ws */ )
{
if ( s.empty() ) return;
s = s.substr( 0, s.find_last_not_of( t ) + 1 );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseTextFileReader::ltrim( std::string_view& s, const char* t /*= ws */ )
{
if ( s.empty() ) return;
s = s.substr( s.find_first_not_of( t ), s.size() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseTextFileReader::trim( std::string_view& s, const char* t /*= ws */ )
{
if ( s.empty() ) return;
rtrim( s, t );
ltrim( s, t );
}