parser.cpp 4.26 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
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
	else return defa;
}

bool parser::defined(std::string name)
{
	std::map<std::string,std::string>::iterator it;
	it=vars.find(name);
	if(it==vars.end()) return false;
	else return true;
}	

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";
		os<<"\n";
	}
}	

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
138
139
		if((*i2).first==sfai)
			os << (*i2).first.substr(1) << " = " << ((*i2).second)[fai] << "\n";
stefanwessel's avatar
stefanwessel committed
140
		else {
maximilianlohoefer's avatar
maximilianlohoefer committed
141
			int size=((*i2).second).size();
stefanwessel's avatar
stefanwessel committed
142
143
144
			for(int j=0;j<size;j++)
				os<< ((*i2).second)[j] <<"\n";
		}
maximilianlohoefer's avatar
maximilianlohoefer committed
145
146
147
148
149
150
151
		/*		os<<(*i2).first<<"\n";
				int size=((*i2).second).size();
			if((*i2).first==sfai) os<<((*i2).second)[fai]<<"\n";
			else {
				for(int j=0;j<size;j++)
					os<< ((*i2).second)[j] <<"\n";
			}*/
stefanwessel's avatar
stefanwessel committed
152
153
154
155
		os<<"\n";
	}
}

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