summaryrefslogtreecommitdiff
path: root/src/options/argument_extender_implementation.cpp
blob: 9c38b348bb64f7048fcb5473bb86a49cd24d562a (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
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
/*********************                                                        */
/*! \file argument_extender_implementation.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Paul Meng
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Utility class for parsing commandline options.
 **
 ** Utility class for parsing commandline options.
 **/

#include "options/argument_extender_implementation.h"

#include <cstdlib>
#include <cstring>
#include <list>

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

namespace CVC4 {
namespace options {

ArgumentExtenderImplementation::ArgumentExtenderImplementation()
  : d_allocated()
  , d_preemptions()
  , d_arguments()
{
}

ArgumentExtenderImplementation::~ArgumentExtenderImplementation(){
  for(CharPointerList::iterator i = d_allocated.begin(),
        iend = d_allocated.end(); i != iend; ++i) {
    char* current = *i;
    Debug("options") << "~ArgumentExtenderImplementation " << current
    << std::endl;
    free(current);
  }
  d_allocated.clear();
}

size_t ArgumentExtenderImplementation::numArguments() const {
  return d_arguments.size();
}

char* ArgumentExtenderImplementation::allocateCopy(const char* element) {
  Assert(element != NULL);

  char* duplicate = strdup(element);
  Assert(duplicate != NULL);
  d_allocated.push_back(duplicate);
  return duplicate;
}

bool ArgumentExtenderImplementation::hasPreemptions() const {
  return !d_preemptions.empty();
}

void ArgumentExtenderImplementation::pushBackPreemption(const char* element) {
  d_preemptions.push_back(allocateCopy(element));
}

void ArgumentExtenderImplementation::movePreemptionsToArguments() {
  d_arguments.splice(d_arguments.begin(), d_preemptions);
}

void ArgumentExtenderImplementation::popFrontArgument() {
  Assert(!d_arguments.empty());
  Debug("options") << "ArgumentExtenderImplementation::popFrontArgument "
                   << d_arguments.front() << std::endl;
  d_arguments.pop_front();
}

void ArgumentExtenderImplementation::pushFrontArgument(const char* element) {
  d_arguments.push_front(allocateCopy(element));
}

void ArgumentExtenderImplementation::pushBackArgument(const char* element) {
  d_arguments.push_back(allocateCopy(element));
}

void ArgumentExtenderImplementation::getArguments(int* argc, char*** argv)
  const {
  Assert(argc != NULL);
  Assert(argv != NULL);

  *argc = numArguments();
  *argv = copyArguments();
}

char** ArgumentExtenderImplementation::copyArguments() const {
  int size = numArguments();
  Assert(size >= 0);

  char** array = (char**) malloc( sizeof(char*) * size );
  Assert(array != NULL);
  int position = 0;
  for(std::list< char* >::const_iterator i = d_arguments.begin(),
        iend = d_arguments.end(); i != iend; ++i, ++position) {
    char* at_position = *i;
    array[position] = at_position;
  }

  return array;
}

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