summaryrefslogtreecommitdiff
path: root/support/lib/Support/ProgramOption.cpp
blob: 2fa987941be33753b4d2ba8c67e818648deb7848 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// $Id$
//***************************************************************************
//
// File:
//    ProgramOption.C
//
// Purpose:
//    General representations for a program option.
//
// History:
//    08/08/95 - adve  - created in the dHPF compiler
//    11/26/96 - adve  - EvalOpt now returns #args consumed, or -1 for error
//    07/15/01 - vadve - Copied to LLVM system and modified
//
//**************************************************************************/

//************************** System Include Files **************************/

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>

//*************************** User Include Files ***************************/

#include "llvm/Support/ProgramOption.h"

//********************** Local Variable Definitions ************************/

//************************ Class Implementations ***************************/


//**************************************************************************/

StringOption::StringOption(const char* _argString,
			   const char* _helpMesg,
			   const char* _initValue, 
			   bool _append)
  : ProgramOption(_argString, _helpMesg),
    value(_initValue),
    append(_append) 
{}

int
StringOption::EvalOpt(const char* optarg)
{
  if (optarg == (char*) NULL)
    return -1;			// flag the error
  
  if (this->append)
    value += optarg;
  else
    value = optarg;
  
  optionSpecified = true;
  return 1;				// one additional argument consumed
}


//**************************************************************************/

FlagOption::FlagOption(const char* _argString,
		       const char* _helpMesg,
		       bool _initValue)
  : ProgramOption(_argString, _helpMesg, 0),
    value(_initValue)
{}

int
FlagOption::EvalOpt(const char* optarg)
{
  if (strcmp(optarg, "0") == 0) {
    value = false;
    return 1;				// one additional argument consumed
  }
  else {
    value = true; 
    return 0;				// zero ... consumed
  }
}

//**************************************************************************/

RealValuedOption::RealValuedOption(const char* _argString,
				   const char* _helpMesg,
				   double _initValue)
  : ProgramOption(_argString, _helpMesg),
    value(_initValue)
{}

int
RealValuedOption::EvalOpt(const char* optarg)
{
  if (optarg == (char*) NULL)
    return -1;
    
  char* lastCharScanned = NULL;
  value = strtod(optarg, &lastCharScanned);
  if (! (*lastCharScanned == '\0'))	// look for incorrect or partially
    return -1;				// correct numerical argument
  
  optionSpecified = true;
  return 1;
}

char*
RealValuedOption::GetTextValue() const
{
  char buffer[40];
  sprintf(buffer, "%f", value);
  return strdup(buffer);
}

//**************************************************************************/

IntegerValuedOption::IntegerValuedOption(const char* _argString,
					 const char* _helpMesg,
					 int _initValue)
  : RealValuedOption(_argString, _helpMesg, (double) _initValue)
{}

int
IntegerValuedOption::Value() const
{
  double realValue = RealValuedOption::Value();
  assert(realValue == (int) realValue);
  return (int) realValue;
}

char*
IntegerValuedOption::GetTextValue() const
{
  char buffer[40];
  sprintf(buffer, "%d", Value());
  return strdup(buffer);
}

//**************************************************************************/