File: commandline_configuration.cpp

package info (click to toggle)
epiphany 0.7.0+0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 7,532 kB
  • sloc: cpp: 6,404; ansic: 141; makefile: 78; sh: 7
file content (129 lines) | stat: -rw-r--r-- 2,574 bytes parent folder | download | duplicates (6)
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
#include "commandline_configuration.h"
#include <map>
#include <string>
#include <cassert>
#include <cstring>
#include <vector>


CommandlineConfiguration::CommandlineConfiguration(Sint32 argc, char* argv[])
{
	std::vector<std::string> arguments;
	
	for(Sint32 i = 1; i<argc; i++)
	{
		arguments.push_back(std::string(argv[i]));
	}
	
	parse_arguments(arguments);
	
}

void CommandlineConfiguration::parse_arguments(const std::vector<std::string>& arguments)
{
	
	Uint32 it = 0;

	
	std::string curr_arg("");
	
	std::string prev_arg("");
	
	while (  it < arguments.size())
	{
    	curr_arg = arguments[it];
		if(curr_arg.substr(0, 2) == "--")
		{
			//DEBWARN("Argument: "<<curr_arg.substr(2)<<"\n");
			if(prev_arg.substr(0, 2) == "--")
			{
				std::string object_name(prev_arg.substr(2)); 
				m_arguments[object_name] = std::string("true");
				DEBOUT("Setting "<<object_name<<" = true\n");
			}
			
			if(it == arguments.size()-1)
			{
				std::string object_name(curr_arg.substr(2)); 
				m_arguments[object_name] = std::string("true");
				DEBOUT("Setting "<<object_name<<" = true\n");
			}
			prev_arg = curr_arg;
		}
		else
		{
			if(prev_arg.substr(0, 2) == "--")
			{
				std::string object_name(prev_arg.substr(2)); 
				m_arguments[object_name] = curr_arg;
				DEBOUT("Setting "<<object_name<<" = "<<curr_arg<<"\n");
				
			}
			else if(curr_arg.substr(0, 2) == "--")
			{
				DEBWARN("Invalid option: "<<curr_arg<<"\n");
				
			}
			prev_arg = curr_arg;
			//assert(0);
		}
		it++;	    		
	}
	
	
}

bool CommandlineConfiguration::get_string(const std::string& section_name, const std::string& object_name, std::string& value) const
{
	if(m_arguments.count(object_name)==0)
		return false;

	std::map<std::string, std::string>::const_iterator it;
	
	it = m_arguments.find(object_name);
	
	value = it->second;
	return true;
	
}
		
bool CommandlineConfiguration::get_int(const std::string& section_name, const std::string& object_name, Uint32& value) const
{
	if(m_arguments.count(object_name)==0)
		return false;

	std::map<std::string, std::string>::const_iterator it;
	
	it = m_arguments.find(object_name);
	
	value = atoi(it->second.c_str());
	return true;
	
}
	
bool CommandlineConfiguration::get_bool(const std::string& section_name, const std::string& object_name, bool& value) const
{

	if(m_arguments.find(object_name)==m_arguments.end())
		return false;

	std::map<std::string, std::string>::const_iterator it;
	
	it = m_arguments.find(object_name);
	
	
	if(it->second=="true")
	{
		value = true;
	}
	else
	{
		value = false;
	}
	
	return true;
	
}