GPUMLib  0.2.2
GPU Machine Learning Library
Settings.h
1 /*
2 Joao Goncalves is a MSc Student at the University of Coimbra, Portugal
3 Copyright (C) 2012 Joao Goncalves
4 
5 This file is part of GPUMLib.
6 
7 GPUMLib is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 /*
22 * Settings.h
23 *
24 * Created on: Jan 5, 2012
25 * Author: Joao Carlos Ferreira Goncalves
26 */
27 
28 #ifndef SETTINGS_H_
29 #define SETTINGS_H_
30 
31 #include <vector>
32 
33 namespace GPUMLib {
34 
36  class Argument {
37 
38  public:
40  char* argument;
42  char* value; // this could be updated (in the future) to have multiple values
43 
46  argument = NULL;
47  value = NULL;
48  }
49  };
50 
52  class Settings {
53 
54  private:
55  vector<Argument*> *argument_list;
56  int argc;
57  char **argv;
58 
60  void createSettings() {
61  //first argument is the executable, so ignore it
62  int argc_m1 = argc - 1;
63  for (int i = 1; i < argc; i++) {
64  // cout << argv[i] << endl;
65  //arguments must be on the form
66  //-arg <val>
67  //where val can be non-existent
68  char* cur_arg = argv[i];
69  //if it is an argument, it begins with a "-"
70  if (cur_arg[0] == '-') {
71  Argument *a = new Argument();
72  a->argument = cur_arg;
73  argument_list->push_back(a);
74  //check if next string is the value
75  if (i < argc_m1) { //obviously if there are more arguments
76  char* next_arg = argv[i + 1];
77  if (next_arg[0] != '-') {
78  a->value = next_arg;
79  i++;
80  }
81  }
82  }
83  // cout << argument_list->size() << endl;
84  }
85  }
86 
87  public:
91  Settings(int argc, char **argv) {
92  argument_list = new vector<Argument*>();
93  this->argc = argc;
94  this->argv = argv;
95  this->createSettings();
96  }
97 
100  for (size_t i = 0; i < argument_list->size(); i++) {
101  delete argument_list->at(i);
102  }
103  delete argument_list;
104  }
105 
109  Argument* getArgument(size_t pos) {
110  return this->argument_list->at(pos);
111  }
112 
116  return this->argument_list->size();
117  }
118  };
119 
120 } // namespace GPUMLib
121 
122 #endif /* SETTINGS_H_ */
Settings(int argc, char **argv)
Definition: Settings.h:91
~Settings()
Call this to free the internal structures created after using the constructor.
Definition: Settings.h:99
size_t getAmountArguments()
Definition: Settings.h:115
Argument()
Creates an empty argument, without parameters.
Definition: Settings.h:45
char * argument
An parameter/argument.
Definition: Settings.h:40
Argument * getArgument(size_t pos)
Definition: Settings.h:109
This class acts like a placeholder for an argument, composed of a parameter and a attribute/value...
Definition: Settings.h:36
char * value
A value associated with a given argument/parameter.
Definition: Settings.h:42
Utility class to parse main()'s arguments, store them in a convenient list and access when needed...
Definition: Settings.h:52