File: parser.cpp

package info (click to toggle)
kmc 2.3%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,412 kB
  • sloc: cpp: 17,316; perl: 178; makefile: 90; sh: 16
file content (184 lines) | stat: -rw-r--r-- 5,135 bytes parent folder | download | duplicates (2)
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
178
179
180
181
182
183
184
/*
  This file is a part of KMC software distributed under GNU GPL 3 licence.
  The homepage of the KMC project is http://sun.aei.polsl.pl/kmc
  
  Authors: Marek Kokot
  
  Version: 2.3.0
  Date   : 2015-08-21
*/

#include "stdafx.h"
#include "parser.h"
#include "tokenizer.h"
#include "output_parser.h"
#include "config.h"

/*****************************************************************************************************************************/
/******************************************************** CONSTRUCTOR ********************************************************/
/*****************************************************************************************************************************/

CParser::CParser(const std::string& src):
	config(CConfig::GetInstance())
{
	line_no = 0;
	file.open(src);
	if (!file.is_open())
	{
		std::cout << "Cannot open file: " << src << "\n";
		exit(1);
	}
	//input_line_pattern = "\\s*(\\w*)\\s*=\\s*(.*)$";
	input_line_pattern = "^\\s*([\\w-+]*)\\s*=\\s*(.*)$"; //TODO: consider valid file name	
	empty_line_pattern = "^\\s*$";
}

/*****************************************************************************************************************************/
/********************************************************** PUBLIC ***********************************************************/
/*****************************************************************************************************************************/

void CParser::ParseInputs()
{
	std::string line;
	while (true)
	{
		if (!nextLine(line))
		{
			std::cout << "Error: 'INPUT:' missing\n";
			exit(1);
		}
		if (line.find("INPUT:") != std::string::npos)
			break;
	}

	if (!nextLine(line) || line.find("OUTPUT:") != std::string::npos)
	{
		std::cout << "Error: None input was defined\n";
		exit(1);
	}

	while (true)
	{
		parseInputLine(line);
		if (!nextLine(line))
		{
			std::cout << "Error: 'OUTPUT:' missing\n";
			exit(1);
		}
		if (line.find("OUTPUT:") != std::string::npos)
			break;
	}
}


/*****************************************************************************************************************************/
/********************************************************** PRIVATE **********************************************************/
/*****************************************************************************************************************************/

/*****************************************************************************************************************************/
void CParser::parseInputLine(const std::string& line)
{
	std::smatch match;
	if (std::regex_search(line, match, input_line_pattern))
	{
#ifdef ENABLE_DEBUG
		std::cout << "\ninput name: " << match[1];
		std::cout << "\nafter = " << match[2];
#endif
		if (input.find(match[1]) != input.end())
		{
			std::cout << "Error: Name redefinition(" << match[1] << ")" << " line: " << line_no << "\n";
			exit(1);
		}
		else
		{
			std::string file_name;
			std::istringstream stream(match[2]);
			
			CInputDesc desc;

			if (!(stream >> desc.file_src))
			{
				std::cout << "Error: file name for " << match[1] << " was not specified, line: "<< line_no <<"\n";
				exit(1);
			}
			std::string tmp;
			while (stream >> tmp)
			{
				if (strncmp(tmp.c_str(), "-ci", 3) == 0)
				{
					desc.cutoff_min = atoi(tmp.c_str() + 3);
					continue;
				}
				else if (strncmp(tmp.c_str(), "-cx", 3) == 0)
				{
					desc.cutoff_max = atoi(tmp.c_str() + 3);
					continue;
				}
				std::cout << "Error: Unknown parameter " << tmp << " for variable " << match[1] << ", line: "<< line_no <<"\n";
				exit(1);
			}

			config.input_desc.push_back(desc);
			input[match[1]] = (uint32)(config.input_desc.size() - 1);				
		}
	}
	else
	{
		std::cout << "Error: wrong line format, line: " << line_no << "\n";
		exit(1);
	}
}

/*****************************************************************************************************************************/
void CParser::parseOtuputParamsLine()
{
	std::string line;

	if (!nextLine(line))
	{
		std::cout << "Warning: OUTPUT_PARAMS exists, but no parameters are defined\n";
	}
	else
	{
		std::istringstream stream(line);
		std::string tmp;
		while (stream >> tmp)
		{
			if (strncmp(tmp.c_str(), "-ci", 3) == 0)
			{				
				config.output_desc.cutoff_min = atoi(tmp.c_str() + 3);
				continue;
			}
			else if (strncmp(tmp.c_str(), "-cx", 3) == 0)
			{			
				config.output_desc.cutoff_max = atoi(tmp.c_str() + 3);
				continue;
			}
			else if ((strncmp(tmp.c_str(), "-cs", 3) == 0))
			{
				config.output_desc.counter_max = atoi(tmp.c_str() + 3);
				continue;
			}
			std::cout << "Error: Unknown parameter " << tmp << " for variable " << tmp << ", line: " << line_no << "\n";
			exit(1);
		}
	}
}

/*****************************************************************************************************************************/
bool CParser::nextLine(std::string& line)
{
	while (true)
	{
		if (file.eof())
			return false;
		std::getline(file, line);
		++line_no;
		std::smatch match;
		if (!std::regex_search(line, match, empty_line_pattern))
			return true;
	}
}

// ***** EOF