GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/FileParser/src/ConfigParser.cpp Lines: 49 49 100.0 %
Date: 2023-10-11 10:52:07 Branches: 82 101 81.2 %

Line Branch Exec Source
1
/***************************************
2
	Auteur : Pierre Aubert
3
	Mail : pierre.aubert@lapp.in2p3.fr
4
	Licence : CeCILL-C
5
****************************************/
6
7
8
#include "ConfigParser.h"
9
10
///Default constructor of ConfigParser
11
3
ConfigParser::ConfigParser(){
12
13
3
}
14
15
///Destructor of ConfigParser
16
6
ConfigParser::~ConfigParser(){
17
6
	clear();
18
}
19
20
///Clear the vector of variable to be parsed
21
3
void ConfigParser::clear(){
22
6
	for(std::vector<ConfigBase*>::iterator it(p_vecVar.begin()); it != p_vecVar.end(); ++it){
23
3
		if(*it != NULL){
24
3
			delete (*it);
25
		}
26
	}
27
3
	p_vecVar.clear();
28
3
}
29
30
///Load the given file
31
/**	@param fileName : name of the file to be loaded
32
 * 	@return true on success, false otherwise
33
*/
34
10
bool ConfigParser::load(const std::string & fileName){
35
10
	clearIsParsed();
36
20
	PFileParser parser;
37
10
	if(!parser.open(fileName)){
38

3
		std::cerr << "ConfigParser::load : cannot load file '"<<fileName<<"'" << std::endl;
39
3
		return false;
40
	}
41

7
	if(!parser.isMatch("{")){
42

1
		std::cerr << "ConfigParser::load : expect '{' at the begning of the file '"<<fileName<<"'" << std::endl;
43
1
		return false;
44
	}
45




10
	while(!parser.isEndOfFile() && !parser.isMatch("}")){
46

8
		if(parser.isMatch("#")){
47
1
			parser.getUntilKeyWithoutPatern("\n");	//Skip comment
48
1
			continue;
49
		}
50
7
		bool isParsed(false);
51
7
		std::vector<ConfigBase*>::iterator it(p_vecVar.begin());
52





11
		while(!parser.isEndOfFile() && it != p_vecVar.end() && !isParsed && !parser.isMatchRewind("}")){
53
7
			if((*it) != NULL){
54
7
				isParsed = (*it)->parseVar(parser);
55
			}
56
4
			++it;
57
		}
58
4
		if(!isParsed){
59

1
			std::cerr << "ConfigParser::load : error at " << parser.getLocation() << std::endl;
60

1
			std::cerr << "\tunexpected token '"<<parser.getNextToken()<<"'" << std::endl;
61
1
			return false;
62
		}
63
	}
64
2
	return true;
65
}
66
67
///Save the given file
68
/**	@param fileName : name of the file to be saved
69
 * 	@return true on success, false otherwise
70
*/
71
4
bool ConfigParser::save(const std::string & fileName) const{
72
8
	std::string body("{\n");
73
8
	for(std::vector<ConfigBase*>::const_iterator it(p_vecVar.begin()); it != p_vecVar.end(); ++it){
74
4
		if((*it) != NULL){
75
4
			body += (*it)->saveToString();
76
		}
77
	}
78
4
	body += "}\n\n";
79
4
	if(!saveFileContent(fileName, body)){
80

2
		std::cerr << "ConfigParser::save : cannot save file '"<<fileName<<"'" << std::endl;
81
2
		return false;
82
	}
83
2
	return true;
84
}
85
86
///Clear the is parsed attribute of the variables
87
10
void ConfigParser::clearIsParsed(){
88
20
	for(std::vector<ConfigBase*>::iterator it(p_vecVar.begin()); it != p_vecVar.end(); ++it){
89
10
		if(*it != NULL){
90
10
			(*it)->setIsParsed(false);
91
		}
92
	}
93
10
}
94
95
96