parser.cpp 4.25 KB
Newer Older
stefanwessel's avatar
stefanwessel committed
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
#include "parser.h"

parser :: parser()
{
	notfine = " =\t";
	comment = "#";	
	array = "@";
}

parser :: parser(std::string input)
{
	notfine = " =\t";
	comment = "#";
	array = "@";
	read_file(input);
}

bool parser :: read_file(std::string input)
{
	std::string  buffer;
	std::fstream file(input.c_str(), std::ios::in);
	if (!file.good()) {
		std::cerr << "File not found: " << input << std::endl; 
		exit(1);
		return false;
	}
	while(getline(file,buffer))
	{
		std::string::size_type where_is_comment = buffer.find_first_of(comment);
		if(where_is_comment != buffer.npos)
		{
			buffer.erase(where_is_comment, buffer.size());
		}
		
		std::string::size_type is_array = buffer.find_first_of(array);
		if(is_array != buffer.npos)
		{	
			// we have to read an array
			//std::string::size_type start = buffer.find_first_not_of(array); not used?
			std::string::size_type end = buffer.find_first_of(array);
			buffer.erase(0, end);
			std::vector< std::string > dumpy;
			std::string line2;
			while(getline(file, line2) && line2.find(array) == line2.npos)
			{
				dumpy.push_back(line2);
			}
			arrs[buffer]=dumpy;
		} else
		{	// normal variable
			std::string _name, value;
			std::string::size_type start = buffer.find_first_not_of(notfine);
			std::string::size_type end = buffer.find_first_of(notfine);
			if(end != buffer.npos)
			{
				_name.assign(buffer, start, end);
				buffer.erase(start, end);
				end = buffer.find_first_not_of(notfine);
				buffer.erase(0, end);
				vars[_name]=buffer;
			}
		}
	}
64
65
	file.close();
	readVals.clear();
stefanwessel's avatar
stefanwessel committed
66
67
68
69
70
71
72
	return true;
}

std::string parser::return_value_of(std::string name)
{
	std::map<std::string,std::string>::iterator it;
	it=vars.find(name);
73
74
75
76
77
	if(it!=vars.end())
	{
		readVals.push_back(name);
		return (*it).second;
	}
stefanwessel's avatar
stefanwessel committed
78
79
80
81
82
83
84
85
86
87
	else {
		std::cerr << "#PARSER: NO VARIABLE WITH NAME " << name << " FOUND!" << std::endl;
		return "";
	}
}

std::string parser::value_or_default(std::string name, std::string defa)
{
	std::map<std::string,std::string>::iterator it;
	it=vars.find(name);
88
89
90
91
	if(it!=vars.end()) {
		readVals.push_back(name);
		return (*it).second;
	}
stefanwessel's avatar
stefanwessel committed
92
93
94
95
96
97
98
	else return defa;
}

bool parser::defined(std::string name)
{
	std::map<std::string,std::string>::iterator it;
	it=vars.find(name);
99
100
101
102
	std::map<std::string,std::vector<std::string> >::iterator arrit = arrs.find(name);
	return it != vars.end() || arrit != arrs.end();
//	if(it==vars.end()) return false;
//	else return true;
stefanwessel's avatar
stefanwessel committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
}	

void parser :: add_comment(std::string new_comm)
{
	comment += new_comm;
}

void parser :: add_delim(std::string new_del)
{
	notfine += new_del;
}

void parser :: get_all(std::ostream& os)
{
	std::map<std::string,std::string>::iterator i;
	std::map<std::string,std::vector<std::string> >::iterator i2;
	for(i=vars.begin(); i!=vars.end(); i++)
		os<<(*i).first<<" = "<<(*i).second<< "\n";

	for(i2=arrs.begin(); i2!=arrs.end();i2++) {
		os<<(*i2).first<<"\n";
		int size=((*i2).second).size();
		for(int j=0;j<size;j++)
			os<< ((*i2).second)[j] <<"\n";
stefanwessel's avatar
   
stefanwessel committed
127
		os<<(*i2).first<<"\n";
stefanwessel's avatar
stefanwessel committed
128
129
130
131
132
133
134
135
136
137
138
139
	}
}	

void parser :: get_all_with_one_from_specified_array(std::string sfai, int fai, std::ostream& os)
{
	std::map<std::string,std::string>::iterator i;
	std::map<std::string,std::vector<std::string> >::iterator i2;
	
	for(i=vars.begin(); i!=vars.end(); i++)
		os<<(*i).first<<" = "<<(*i).second<<"\n";

	for(i2=arrs.begin(); i2!=arrs.end();i2++) {
maximilianlohoefer's avatar
maximilianlohoefer committed
140
141
		if((*i2).first==sfai)
			os << (*i2).first.substr(1) << " = " << ((*i2).second)[fai] << "\n";
stefanwessel's avatar
stefanwessel committed
142
		else {
stefanwessel's avatar
   
stefanwessel committed
143
			os<<(*i2).first<<"\n";
maximilianlohoefer's avatar
maximilianlohoefer committed
144
			int size=((*i2).second).size();
stefanwessel's avatar
stefanwessel committed
145
146
			for(int j=0;j<size;j++)
				os<< ((*i2).second)[j] <<"\n";
stefanwessel's avatar
   
stefanwessel committed
147
148
			os<<(*i2).first<<"\n";

stefanwessel's avatar
stefanwessel committed
149
150
151
152
		}
	}
}

153
154
155
std::vector<std::string> parser::unused_parameters() {
	std::vector<std::string> unused;
	std::vector<std::string>::iterator re;
156
	bool in;
157
	for (std::map<std::string, std::string>::iterator va = vars.begin(); va != vars.end(); ++va) {
158
		in = false;
159
		for (re = readVals.begin(); re != readVals.end(); ++re) {
160
161
			if (va->first == (*re)) {
				in = true;
162
				break;
163
			}
164
		}
165
166
		if (!in)
			unused.push_back(va->first);
167
168
	}
	for (std::map<std::string, std::vector<std::string> >::iterator va = arrs.begin(); va != arrs.end(); ++va) {
169
		in = false;
170
		for (re = readVals.begin(); re != readVals.end(); ++re) {
171
172
			if (va->first == (*re)) {
				in = true;
173
				break;
174
			}
175
		}
176
177
		if (!in)
			unused.push_back(va->first);
178
179
180
181
	}
	return unused;
}