summaryrefslogtreecommitdiff
path: root/src/options/argument_extender.cpp
blob: d5561059079d7ccd9a4f631db1729124e4796982 (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
63
64
65
66
67
68
69
70
71
72
73
74
/*********************                                                        */
/*! \file preempt_get_option.h
 ** \verbatim
 ** Original author: Tim King
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2014  New York University and The University of Iowa
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief Utility function for parsing commandline options.
 **
 ** Utility function for parsing commandline options.
 **/

#include "options/argument_extender.h"

#include <cstdlib>
#include <cstring>
#include <vector>

#include "base/cvc4_assert.h"
#include "base/output.h"

namespace CVC4 {
namespace options {

ArgumentExtender::ArgumentExtender(unsigned additional, size_t length)
    : d_additional(additional)
    , d_length(length)
{
  AlwaysAssert(d_additional >= 1);
  AlwaysAssert(d_length >= 1);
}

ArgumentExtender::~ArgumentExtender(){}

unsigned ArgumentExtender::getIncrease() const { return d_additional; }
size_t ArgumentExtender::getLength() const { return d_length; }

void ArgumentExtender::extend(int& argc, char**& argv, const char* opt,
                                std::vector<char*>& allocated)
{

  Debug("preemptGetopt") << "preempting getopt() with " << opt << std::endl;

  AlwaysAssert(opt != NULL && *opt != '\0');
  AlwaysAssert(strlen(opt) <= getLength());

  ++argc;
  unsigned i = 1;
  while(argv[i] != NULL && argv[i][0] != '\0') {
    ++i;
  }

  if(argv[i] == NULL) {
    unsigned newSize = i + getIncrease();
    argv = (char**) realloc(argv, newSize * sizeof(char*));
    for(unsigned j = i; j < newSize-1; ++j) {
      char* newString = (char*) malloc(sizeof(char) * getLength());
      newString[0] = '\0';
      argv[j] = newString;
      allocated.push_back(newString);
    }
    argv[newSize - 1] = NULL;
  }

  strncpy(argv[i], opt, getLength() - 1);
  argv[i][getLength() - 1] = '\0'; // ensure NULL-termination even on overflow
}

}/* CVC4::options namespace */
}/* CVC4 namespace */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback