summaryrefslogtreecommitdiffstats
path: root/src/utils/record_file.hpp
blob: 7d4b1db59e3a9f51ba003c4272d19400c43695b1 (plain)
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
/*
    Copyright (C) 2005-2007  Michel de Boer <michel@twinklephone.com>

    This program 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 2 of the License, or
    (at your option) any later version.

    This program 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 for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#define COMMENT_SYMBOL	'#'

template< class R >
void t_record_file<R>::split_record(const string &record, vector<string> &v) const {
	v = split_escaped(record, field_separator);
	
	for (vector<string>::iterator it = v.begin(); it != v.end(); ++it) {
		*it = unescape(*it);
	}
}

template< class R >
string t_record_file<R>::join_fields(const vector<string> &v) const {
	string s;
	
	for (vector<string>::const_iterator it = v.begin(); it != v.end(); ++it) {
		if (it != v.begin()) s += field_separator;
		
		// Escape comment symbol.
		if (!it->empty() && it->at(0) == COMMENT_SYMBOL) s += '\\';
		
		s += escape(*it, field_separator);
	}
	
	return s;
}

template< class R >
void t_record_file<R>::add_record(const R &record) {
	records.push_back(record);
}

template< class R >
t_record_file<R>::t_record_file(): field_separator('|')
{}

template< class R >
t_record_file<R>::t_record_file(const string &_header, char _field_separator, 
		const string &_filename) :
	header(_header),
	field_separator(_field_separator),
	filename(_filename)
{}

template< class R >
void t_record_file<R>::set_header(const string &_header) {
	header = _header;
}

template< class R >
void t_record_file<R>::set_separator(char _separator) {
	field_separator = _separator;
}

template< class R >
void t_record_file<R>::set_filename(const string &_filename) {
	filename = _filename;
}

template< class R >
list<R> *t_record_file<R>::get_records() {
	return &records;
}

template< class R >
bool t_record_file<R>::load(string &error_msg) {
	struct stat stat_buf;
	
	mtx_records.lock();
	
	records.clear();
	
	// Check if file exists
	if (filename.empty() || stat(filename.c_str(), &stat_buf) != 0) {
		// There is no file.
		mtx_records.unlock();
		return true;
	}
	
	// Open call ile
	ifstream file(filename.c_str());
	if (!file) {
		error_msg = TRANSLATE("Cannot open file for reading: %1");
		error_msg = replace_first(error_msg, "%1", filename);
		mtx_records.unlock();
		return false;
	}
	
	// Read and parse history file.
	while (!file.eof()) {
		string line;
		
		getline(file, line);

		// Check if read operation succeeded
		if (!file.good() && !file.eof()) {
			error_msg = TRANSLATE("File system error while reading file %1 .");
			error_msg = replace_first(error_msg, "%1", filename);
			mtx_records.unlock();
			return false;
		}

		line = trim(line);

		// Skip empty lines
		if (line.size() == 0) continue;

		// Skip comment lines
		if (line[0] == COMMENT_SYMBOL) continue;
		
		// Add record. Skip records that cannot be parsed.
		R record;
		vector<string> v;
		split_record(line, v);
		if (record.populate_from_file_record(v)) {
			add_record(record);
		}
	}
	
	mtx_records.unlock();
	return true;
}

template< class R >
bool t_record_file<R>::save(string &error_msg) const {	
	if (filename.empty()) return false;
	
	mtx_records.lock();
	
	// Open file
	ofstream file(filename.c_str());
	if (!file) {
		error_msg = TRANSLATE("Cannot open file for writing: %1");
		error_msg = replace_first(error_msg, "%1", filename);
		mtx_records.unlock();
		return false;
	}
	
	// Write file header
	file << "# " << header << endl;
	      
	// Write records
	for (record_const_iterator i = records.begin(); i != records.end(); ++i) {
		vector<string> v;
		if (i->create_file_record(v)) {
			file << join_fields(v);
			file << endl;
		}
	}
	
	mtx_records.unlock();
	 
	if (!file.good()) {
		error_msg = TRANSLATE("File system error while writing file %1 .");
		error_msg = replace_first(error_msg, "%1", filename);
		return false;
	}
	
	return true;
}