29 namespace ublas = boost::numeric::ublas;
31 FileParser::FileParser(std::string filename,
int prec)
32 : filename(filename), input(), output(){
37 FileParser::FileParser(
int prec){
41 FileParser::~FileParser(){
45 void FileParser::open(
bool readMode){
53 void FileParser::openOutput(){
55 output.open(filename.c_str());
57 void FileParser::openInput(){
59 input.open(filename.c_str());
61 void FileParser::close(){
67 bool FileParser::isReading(){
68 return input.is_open();
71 bool FileParser::isWriting(){
72 return output.is_open();
76 void FileParser::setPrecision(
int prec){
86 bool FileParser::fileExists(){
87 std::ifstream ifile(filename.c_str());
92 void FileParser::write(std::string name, std::string value){
93 output << name <<
"=" << value << std::endl;
95 void FileParser::read(std::string name, std::string &value){
98 if(!movePointer(name,value))
100 std::cerr <<
"Variable: " << name
101 <<
" does not exist in file: " << filename
105 std::string FileParser::read(std::string name){
112 void FileParser::write(std::string name,
113 const std::vector<std::string> &arr,
114 const std::vector<int> &dims){
116 output << name <<
"=[";
117 for(std::vector<int>::const_iterator it = dims.begin(); it != dims.end(); ++it) {
118 if(it != dims.begin()){
127 for(std::vector<std::string>::const_iterator it = arr.begin(); it != arr.end(); ++it) {
128 if(it != arr.begin()){
133 output <<
")" << std::endl;
135 void FileParser::read(std::string name, std::vector<std::string> &arr,
136 std::vector<int> &dims)
138 std::string contents;
139 if(movePointer(name,contents)){
140 parseArray(contents, arr, dims);
143 std::cerr <<
"Variable: " << name
144 <<
" does not exist in file: " << filename
152 void FileParser::write_chars(std::string name,
char* value){
153 std::string str(value);
156 void FileParser::read_chars(std::string name,
char* value){
159 strcpy(value, str.c_str());
162 void FileParser::readOrWrite(std::string name,
char* value){
164 read_chars(name, value);
166 else if(isWriting()){
167 write_chars(name,value);
171 void FileParser::write_ublas(std::string name,
172 ublas::vector<double> &values)
174 std::vector<int> dims;
175 dims.push_back(values.size());
177 std::vector<std::string> arr;
178 for(ublas::vector<double>::iterator it = values.begin();
179 it != values.end(); ++it)
181 arr.push_back(to_string(*it));
183 write(name, arr, dims);
185 void FileParser::read_ublas(std::string name, ublas::vector<double> &values){
186 std::vector<std::string> arr;
187 std::vector<int> dims;
188 read(name, arr, dims);
190 std::vector<double> doubles_arr;
191 for(std::vector<std::string>::iterator it = arr.begin(); it != arr.end(); ++it) {
192 doubles_arr.push_back(to_value<double>(*it));
195 values.resize(arr.size(),
false);
196 std::copy(doubles_arr.begin(), doubles_arr.end(), values.begin());
198 void FileParser::readOrWrite(std::string name, ublas::vector<double> &values){
200 read_ublas(name, values);
202 else if(isWriting()){
203 write_ublas(name,values);
207 void FileParser::write_vecOfvec(std::string name,
208 std::vector<ublas::vector<double> > &values){
209 std::vector<int> dims;
210 dims.push_back(values.size());
211 dims.push_back(values.at(0).size());
213 std::vector<std::string> arr;
214 for(
size_t i=0; i<values.size(); i++){
215 ublas::vector<double> current = values.at(i);
216 for(ublas::vector<double>::iterator it = current.begin();
217 it != current.end(); ++it) {
218 arr.push_back(to_string(*it));
221 write(name, arr, dims);
223 void FileParser::read_vecOfvec(std::string name, std::vector<ublas::vector<double> > &values){
224 std::vector<int> dims;
225 std::vector<std::string> arr;
226 read(name, arr, dims);
228 size_t sample_dim = dims.at(1);
230 values.resize(dims.at(0));
231 for(
size_t i=0; i<dims.at(0); i++){
232 values.at(i).resize(sample_dim);
233 for(
size_t j=0; j<sample_dim; j++){
234 values.at(i)[j] = to_value<double>(arr.at(i*sample_dim + j));
238 void FileParser::readOrWrite(std::string name, std::vector<ublas::vector<double> > &values){
240 read_vecOfvec(name, values);
242 else if(isWriting()){
243 write_vecOfvec(name,values);
247 void FileParser::write_double_array(std::string name,
double values[],
size_t length){
248 std::vector<std::string> arr;
249 std::vector<int> dims;
250 dims.push_back(length);
251 for(
size_t i=0; i<length; i++){
252 arr.push_back(to_string(values[i]));
254 write(name, arr, dims);
256 void FileParser::read_double_array(std::string name,
double values[],
size_t length){
257 std::vector<std::string> arr;
258 std::vector<int> dims;
259 read(name, arr, dims);
261 for(
size_t i=0; i<length; i++){
262 values[i] = to_value<double>(arr.at(i));
265 void FileParser::readOrWrite(std::string name,
double values[],
size_t length){
267 read_double_array(name, values, length);
269 else if(isWriting()){
270 write_double_array(name,values, length);
275 bool FileParser::movePointer(std::string name, std::string &contents){
276 if(currentLine.length() > 0 && startsWith(currentLine, name+
"=")){
277 contents = currentLine.substr(name.length()+1);
283 for(
int i=0; i<2; i++){
284 while (getline( input, currentLine)){
285 if(currentLine.length() > 0 && startsWith(currentLine, name+
"=")){
286 contents = currentLine.substr(name.length()+1);
291 input.seekg(0, std::ios::beg);
298 bool FileParser::startsWith(std::string all, std::string sub){
299 return all.rfind(sub, 0) == 0;
302 void FileParser::parseArray(std::string contents,
303 std::vector<std::string> &arr,
304 std::vector<int> &dims){
306 size_t init_arr = contents.find(
"(")+1;
307 size_t end_arr = contents.find(
")")-1;
308 std::string input = contents.substr(init_arr, end_arr - init_arr +1);
312 std::vector<std::string> dims_string;
313 size_t init_dims = contents.find(
"[")+1;
314 size_t end_dims = contents.find(
"]")-1;
315 input = contents.substr(init_dims, end_dims - init_dims +1);
318 for(std::vector<std::string>::iterator it = dims_string.begin();
319 it != dims_string.end(); ++it) {
320 dims.push_back(to_value<size_t>(*it));
Namespace of the library interface.
void split(std::string &input, char delim, std::vector< std::string > &elems)
Splits the input string with a delimiter to extract elements.
Functions to write and parse data files.