-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTclCmdUtils.cc
76 lines (60 loc) · 1.48 KB
/
TclCmdUtils.cc
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
#include "plvGlobals.h"
#include "TclCmdUtils.h"
#include <cstring>
#include <cstdlib>
static bool
boolFromString (char* str)
{
struct { char* name; bool val; } table[] = {
{ "true", true },
{ "false", false },
{ "yes", true },
{ "no", false },
{ "on", true },
{ "off", false }
};
#define nTable (sizeof(table) / sizeof(table[0]))
for (int i = 0; i < nTable; i++) {
if (!strcmp (table[i].name, str))
return table[i].val;
}
return (atoi (str) > 0);
}
char* GetTclGlobal (char* name, char* def)
{
char* val = Tcl_GetVar (g_tclInterp, name, TCL_GLOBAL_ONLY);
if (!val)
return def;
return val;
}
bool GetTclGlobalBool (char* name, bool def)
{
char* val = GetTclGlobal (name);
if (!val)
return def; // == false
return boolFromString (val);
}
bool SetVarFromBoolString (char* str, bool& out)
{
// could have this error-check that given str is actually a bool value
out = boolFromString (str);
return true;
}
bool SetVarFromBoolArgListIndex (int argc, char** argv,
int i, bool& var)
{
if (i >= argc) {
char buffer[1000];
sprintf (buffer, "Missing argument to %s %s\n", argv[0], argv[i-1]);
Tcl_SetResult (g_tclInterp, buffer, TCL_VOLATILE);
return false;
}
if (!SetVarFromBoolString (argv[i], var)) {
char buffer[1000];
sprintf (buffer, "Bad argument '%s' to %s %s\n",
argv[i], argv[0], argv[i-1]);
Tcl_SetResult (g_tclInterp, buffer, TCL_VOLATILE);
return false;
}
return true;
}