File: lineBasedFile.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (272 lines) | stat: -rw-r--r-- 5,638 bytes parent folder | download | duplicates (6)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/FORMAT/lineBasedFile.h>
#include <BALL/COMMON/exception.h>
#include <cstdio>

using namespace std;

namespace BALL
{

	LineBasedFile::LineBasedFile()
		:	File(),
			line_number_(0),
			trim_whitespaces_(false)
	{
	}

	LineBasedFile::LineBasedFile(const String& filename, File::OpenMode open_mode, bool trim_whitespaces)
		: File(),
			line_number_(0),
			trim_whitespaces_(trim_whitespaces)
	{
		File::open(filename, open_mode);
		if (!isAccessible())
		{
			throw Exception::FileNotFound(__FILE__, __LINE__, filename);
		}
	}

	const LineBasedFile& LineBasedFile::operator = (const LineBasedFile& f)
	{
		open(f.getName(), f.getOpenMode());
		line_number_ = 0;
		trim_whitespaces_ = f.trim_whitespaces_;
		skipLines(f.line_number_ - 1);
		return *this;
	}

	bool LineBasedFile::operator == (const LineBasedFile& f)  
	{
		return File::operator == (f);
	}

	bool LineBasedFile::operator != (const LineBasedFile& f)  
	{
		return !(File::operator == (f));
	}


	bool LineBasedFile::search(const String& text, bool return_to_start)
	{
		if (!isOpen() || getOpenMode() != MODE_IN)
		{
			throw Exception::ParseError(__FILE__, __LINE__, String("File '") + getName() + "' not open for reading" , 
																	"LineBasedFile::search");
		}

		Position start_point = line_number_;
		while (readLine())
		{
			if (startsWith(text))
			{ 
				return true;
			}
		}
		
		if (return_to_start)
		{
			gotoLine(start_point);
		}
		return false;
	}

	bool LineBasedFile::search(const String& text, const String& stop, bool return_to_start)
	{
		if (!isOpen() || getOpenMode() != MODE_IN)
		{
			throw Exception::ParseError(__FILE__, __LINE__, String("File '") + getName() + "' not open for reading" , 
																	"LineBasedFile::search");
		}

		Position start_point = line_number_;

		while (readLine())
		{
			if (startsWith(stop))
			{
				if (return_to_start)
				{
					gotoLine(start_point);
				}

				return false;
			}

			if (startsWith(text))
			{
				return true;
			}
		}

		if (return_to_start)
		{
			gotoLine(start_point);
		}

		return false;
	}

	bool LineBasedFile::readLine()
	{
		if (!isOpen() || getOpenMode() != MODE_IN)
		{
			throw Exception::ParseError(__FILE__, __LINE__, String("File '") + getName() + "' not open for reading" , 
																	"LineBasedFile::readLine");
		}
		line_.getline(getFileStream());
		if (trim_whitespaces_) line_.trim();
		++line_number_;
		return !eof();
	}

	bool LineBasedFile::skipLines(Size number)
	{
		for (Position i = 0; i < number +1; i++)
		{
			if (!readLine())
			{
				return false;
			}
		}

		return true;
	}

	void LineBasedFile::rewind()
	{
		if (!isOpen())
		{
			throw Exception::ParseError(__FILE__, __LINE__, String("File '") + getName() + "' not open" , 
																	"LineBasedFile::rewind");
		}
		File::reopen();
		line_number_ = 0;
		line_ = "";
	}

	bool LineBasedFile::gotoLine(Position line_number)
	{
		if (!isOpen())
		{
			throw Exception::ParseError(__FILE__, __LINE__, String("File '") + getName() + "' not open for reading" , 
																	"LineBasedFile::gotoLine");
		}

		if (line_number == line_number_)
		{
			return true;
		}

		if (line_number < line_number_)
		{
			rewind();
			if (line_number == 0)
			{
				return true;
			}

			return skipLines(line_number - 1);
		}

		return skipLines(line_number - line_number_ - 1);
	}

	void LineBasedFile::clear()
	{
		line_ = "";
		line_number_ = 0;
		File::clear();
		trim_whitespaces_ = false;
	}

	void LineBasedFile::test(const char* file, int line, bool condition, const String& msg) const
	{
		if (!condition)
		{
			throw Exception::ParseError(file, line, String("File '") + getName() + "' while parsing line " + String(getLineNumber()), msg);
		}
	}

	String LineBasedFile::getField(Index pos, const String& quotes, const String& delimiters) const
	{
		if (quotes == "")
		{
			return line_.getField(pos, delimiters.c_str());
		}

		return line_.getFieldQuoted(pos, delimiters.c_str(), quotes.c_str());
	}

	Index LineBasedFile::switchString(const vector<String>& data) const 
	{
		for (Index i = 0; i < (Index) data.size(); i++)
		{
			if (line_ == data[i])
			{
				return i;
			}
		}
		return (-1);
	}

	bool LineBasedFile::startsWith(const String& text) const 
	{
		return line_.hasPrefix(text);
	}

	bool LineBasedFile::has(const String& text) const 
	{
		return line_.hasSubstring(text);
	}

	bool LineBasedFile::parseColumnFormat(const char* format, Position start, Size length, void* ptr)
	{
		// the number of entries parsed
		int read = 0;

		// make sure the specified section of the string exists
		if (getLine().size() >= (start + length))
		{
			const Size max_len = 16384;

			if (col_buffer_.empty())
				col_buffer_.resize(max_len+1);

			length = std::min(length, max_len);

			// copy the specified string section into the buffer...
			strncpy(&(col_buffer_[0]), line_.c_str() + start, length);
			col_buffer_[length] = '\0';

			// ...and try to parse it.
			read = sscanf(&(col_buffer_[0]), format, ptr);
		}
		else
		{
			Log.warn() << "LineBasedFile::parseColumnFormat: undefined position while parsing line (" 
							   << start << "-" << start + length << " in line of length " << getLine().size() << ")" << std::endl;
		}

		// return true if exactly one entry was read
		return (read == 1);
	}

	void LineBasedFile::enableTrimWhitespaces(bool state)
	{
		trim_whitespaces_ = state;
	}

	bool LineBasedFile::trimWhiteSpacesEnabled() const
	{
		return trim_whitespaces_;
	}

# ifdef BALL_NO_INLINE_FUNCTIONS
#   include <BALL/FORMAT/lineBasedFile.iC>
# endif

} // namespace BALL