summaryrefslogtreecommitdiff
path: root/vendor/bandit/specs/util/argv_helper.h
blob: 4e92e725731477f13c27ff518f9c212f598e4ff4 (plain)
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
#ifndef BANDIT_SPECS_ARGV_HELPER_H
#define BANDIT_SPECS_ARGV_HELPER_H

#include <string.h>

namespace bandit { namespace specs { namespace util {

  //
  // main() is supposed to receive its arguments as a non const 'char* argv[]'.
  // This is a pain to create for each test. It's a whole lot easier to create
  // a 'const char* argv[]' construct.
  //
  // This class helps copy from 'const char**' to 'char**' and handle cleanup
  // automatically.
  //
  struct argv_helper
  {
    argv_helper(int argc, const char* argv[])
      : argc_(argc) 
    {
      non_const_argv_ = new char*[argc];
      for(int i=0; i < argc; i++)
      {
		std::string s(argv[i]);
        non_const_argv_[i] = new char[s.size() + 1];
        for(size_t c=0;c<s.size();c++)
		{
			non_const_argv_[i][c] = s[c];
		}
		non_const_argv_[i][s.size()] = 0;
      }
    }



    ~argv_helper()
    {
      for(int i=0; i < argc_; i++)
      {
        delete[] non_const_argv_[i];
      }

      delete[] non_const_argv_;
    }

    char** argv()
    {
      return non_const_argv_;
    }

    int argc()
    {
      return argc_;
    }

    private:
    int argc_;
    char** non_const_argv_;
  };

}}}
#endif