summaryrefslogtreecommitdiff
path: root/TAO/tao/arg_shifter.cpp
blob: 0b9d1f8170dc535f171e686cb3727b5a8f67bbef (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
115
116
117
// $Id$

#include "arg_shifter.h"

Arg_Shifter::Arg_Shifter (int &argc, char **argv, char **temp)
  : argc_ (argc),
    total_size_ (argc),
    temp_ (temp),
    argv_ (argv),
    current_index_ (0),
    back_ (argc - 1),
    front_ (0)
{
  // If not provided with one, allocate a temporary array.
  if (this->temp_ == 0)
    this->temp_ = new char *[this->total_size_];
  
  if (this->temp_ != 0)
    {
      // Fill the temporary array.
      this->argc_ = 0;
      for (int i = 0; i < this->total_size_; i++)
	{
	  this->temp_[i] = this->argv_[i];
	  this->argv_[i] = 0;
	}
    }
  else
    {
      // Allocation failed, prohibit iteration.
      this->current_index_ = this->argc_;
      this->front_ = this->argc_;
    }
}

Arg_Shifter::~Arg_Shifter (void)
{
  // Delete the temporary vector.
  delete [] temp_;
}

char * 
Arg_Shifter::get_current (void) const
{
  char *return_value = 0;
  
  if (this->is_anything_left ())
    return_value =  this->temp_[current_index_];

  return return_value;
}

int
Arg_Shifter::consume_arg (int number)
{
  int return_value = 0;

  // Stick knowns at the end of the vector (consumed).
  if (this->is_anything_left() >= number)
    {
      for (int i = 0, j = this->back_ - (number - 1);
	   i < number;
	   i++, j++, this->current_index_++)
	this->argv_[j] = this->temp_[this->current_index_];
      
      this->back_ -= number;
      return_value = 1;
    }

  return return_value;
}

int
Arg_Shifter::ignore_arg (int number)
{
  int return_value = 0;

  // Keep unknowns at the head of the vector.
  if (this->is_anything_left () >= number)
    {
      for (int i = 0;
	   i < number;
	   i++, this->current_index_++, this->front_++)
	this->argv_[this->front_] = this->temp_[this->current_index_];

      return_value = 1;
      this->argc_ += number;
    }

  return return_value;
}

int
Arg_Shifter::is_anything_left (void) const
{
  return this->total_size_ - this->current_index_;
}

int
Arg_Shifter::is_option_next (void) const
{
  return this->is_anything_left () &&
    this->temp_[this->current_index_][0] == '-';
}

int
Arg_Shifter::is_parameter_next (void) const
{
  return this->is_anything_left () &&
    this->temp_[this->current_index_][0] != '-';
}

int
Arg_Shifter::num_ignored_args (void) const
{
  return this->front_;
}