File: aarg.h

package info (click to toggle)
gav 0.9.0-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, lenny, sid, squeeze, stretch, wheezy
  • size: 660 kB
  • ctags: 814
  • sloc: cpp: 4,401; makefile: 147; sh: 15
file content (180 lines) | stat: -rw-r--r-- 3,687 bytes parent folder | download
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
/* -*- C++ -*- */
#ifndef _AARG_H_
#define _AARG_H_

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <map>
#include <string>
#include <cstring>
#include <sstream>

class Aargh
{
public:
  Aargh ()
  {
  }

// questo costruttore serve per avere i parametri caricati
// prima dell'avvio di main, cosi' da poter chiamare i
// costruttori degli oggetti globali che dipendono da
// dei parametri
  Aargh (char *filename)
  {
    loadConf (filename);
  }

// questi altri due costruttori sono meno necessari ma
// sono comunque utili
  Aargh (int argc, char **argv)
  {
    parseArgs (argc, argv);
  }

// vedi sopra
  Aargh (int argc, char **argv, char *filename)
  {
    parseArgs (argc, argv);
    loadConf (filename);
  }

// parametri accettati
//  -qualcosa
//  -qualcosa valore
  bool parseArgs (int argc, char **argv)
  {
    bool allright = true;
    std::string base = "";
    for (int i = 1; i < argc; ++i)
      {
	if (argv[i][0] == '-')
	  {
	    base = std::string (argv[i]);
	    argmap[base] = "_Aargh";
	  }
	else
	  {
	    if (base != "")
	      {
		argmap[base] = std::string (argv[i]);
		base = "";
	      }
	    else
	      allright = false;
	  }
      }
    return allright;
  }

  bool loadConf (const char *filename)
  {
    bool allright = true;
    FILE *fp;
    char ln[128];

    if ((fp = fopen (filename, "r")) == NULL)
      return (false);

    while (fgets (ln, 128, fp))
      if (*ln != '#' && *ln != '\n')
	{
	  char *key = ln;
	  char *value = ln;
	  while ((*value != ' ') && (*value != '\t') && (*value != '\n'))
	    value++;		// finds the first space or tab
	  if (*value == '\n')
	    {
	      *value = '\0';
	      argmap[key] = "_Aargh";
	      continue;
	    }
	  // removes spaces and tabs
	  while ((*value == ' ') || (*value == '\t'))
	    {
	      *value = '\0';
	      value++;
	    }
	  char *end = value;
	  while ((*end != '\n') && (*end))
	    end++;
	  *end = '\0';		// null terminates value (fgets puts a '\n' at the end)
	  // now, key is a null terminated string holding the key, and value is everything which
	  // is found after the first series of spaces or tabs.
	  if (strcmp (key, "") && strcmp (value, ""))
	    argmap[key] = value;
	}

    return allright;
  }

  bool getArg (std::string name)
  {
    return (argmap.find (name) != argmap.end ());
  }

  bool getArg (std::string name, std::string & value)
  {
    if (argmap.find (name) != argmap.end ())
      {
	value = argmap[name];
	return true;
      }
    else
      return false;
  }

  bool getArg (std::string name, std::string & value, const char* def)
  {
    if ( getArg(name, value) )
      return true;
    else
      value = std::string(def);
    
    return false;
  }

  bool setArg (std::string name, std::string value)
  {
    bool ret = (argmap.find (name) != argmap.end ());
    // std::cerr << "Setting " << name << " to: " << value << std::endl;
    argmap[name] = value;
    return(ret);
  }

  bool setArg (std::string name, int intvalue)
  {
    std::ostringstream value;

    value << intvalue;

    bool ret = (argmap.find (value.str()) != argmap.end ());
    // std::cerr << "Setting " << name << " to: " << value.str() << std::endl;
    argmap[name] = value.str();
    return(ret);
  }

  void dump (std::string & out)
  {
    std::map < std::string, std::string >::const_iterator b = argmap.begin ();
    std::map < std::string, std::string >::const_iterator e = argmap.end ();
    out = "";
    while (b != e)
      {
	out += b->first + ' ' + b->second + '\n';
	++b;
      }
  }

  void reset() {
    argmap.clear();
  }

private:
  std::map < std::string, std::string > argmap;
};

extern Aargh aargh;

#endif // _AARG_H_