OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [start/] [insight/] [libgui/] [src/] [tkTableCmd.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/*
2
 * tkTableCmd.c --
3
 *
4
 *      This module implements command structure lookups.
5
 *
6
 * Copyright (c) 1997,1998 Jeffrey Hobbs
7
 *
8
 * See the file "license.terms" for information on usage and redistribution
9
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10
 *
11
 */
12
 
13
#include "tkTableCmd.h"
14
 
15
/*
16
 * Functions for handling custom options that use Cmd_Structs
17
 */
18
 
19
int
20
Cmd_OptionSet(ClientData clientData, Tcl_Interp *interp,
21
              Tk_Window unused, char *value, char *widgRec, int offset)
22
{
23
  Cmd_Struct *p = (Cmd_Struct *)clientData;
24
  int mode = Cmd_GetValue(p,value);
25
  if (!mode) {
26
    Cmd_GetError(interp,p,value);
27
    return TCL_ERROR;
28
  }
29
  *((int*)(widgRec+offset)) = mode;
30
  return TCL_OK;
31
}
32
 
33
char *
34
Cmd_OptionGet(ClientData clientData, Tk_Window unused,
35
              char *widgRec, int offset, Tcl_FreeProc **freeProcPtr)
36
{
37
  Cmd_Struct *p = (Cmd_Struct *)clientData;
38
  int mode = *((int*)(widgRec+offset));
39
  return Cmd_GetName(p,mode);
40
}
41
 
42
/*
43
 * Options for bits in an int
44
 * This will set/clear one bit in an int, the specific bit
45
 * being passed in clientData
46
 */
47
int
48
Cmd_BitSet(ClientData clientData, Tcl_Interp *interp,
49
           Tk_Window unused, char *value, char *widgRec, int offset)
50
{
51
  int mode;
52
  if (Tcl_GetBoolean(interp, value, &mode) != TCL_OK) {
53
    return TCL_ERROR;
54
  }
55
  if (mode) {
56
    *((int*)(widgRec+offset)) |= (int)clientData;
57
  } else {
58
    *((int*)(widgRec+offset)) &= ~((int)clientData);
59
  }
60
  return TCL_OK;
61
}
62
 
63
char *
64
Cmd_BitGet(ClientData clientData, Tk_Window unused,
65
           char *widgRec, int offset, Tcl_FreeProc **freeProcPtr)
66
{
67
  return (*((int*)(widgRec+offset)) & (int) clientData)?"1":"0";
68
}
69
 
70
/*
71
 * simple Cmd_Struct lookup functions
72
 */
73
 
74
char *
75
Cmd_GetName(const Cmd_Struct *cmds, int val)
76
{
77
  for(;cmds->name && cmds->name[0];cmds++) {
78
    if (cmds->value==val) return cmds->name;
79
  }
80
  return NULL;
81
}
82
 
83
int
84
Cmd_GetValue(const Cmd_Struct *cmds, const char *arg)
85
{
86
  int len=strlen(arg);
87
  for(;cmds->name && cmds->name[0];cmds++) {
88
    if (!strncmp(cmds->name,arg,len)) return cmds->value;
89
  }
90
  return 0;
91
}
92
 
93
void
94
Cmd_GetError(Tcl_Interp *interp, const Cmd_Struct *cmds, const char *arg)
95
{
96
  int i;
97
  Tcl_AppendResult(interp, "bad option \"", arg, "\" must be ", (char *) 0);
98
  for(i=0;cmds->name && cmds->name[0];cmds++,i++) {
99
    Tcl_AppendResult(interp, (i?", ":""), cmds->name, (char *) 0);
100
  }
101
}
102
 
103
/*
104
 * Parses a command string passed in an arg comparing it with all the
105
 * command strings in the command array. If it finds a string which is a
106
 * unique identifier of one of the commands, returns the index . If none of
107
 * the commands match, or the abbreviation is not unique, then it sets up
108
 * the message accordingly and returns 0
109
 */
110
 
111
int
112
Cmd_Parse (Tcl_Interp *interp, Cmd_Struct *cmds, const char *arg)
113
{
114
  int len = (int)strlen(arg);
115
  Cmd_Struct *matched = (Cmd_Struct *) 0;
116
  int err = 0;
117
  Cmd_Struct *next = cmds;
118
  while (*(next->name)) {
119
    if (strncmp (next->name, arg, len) == 0) {
120
      /* have we already matched this one if so make up an error message */
121
      if (matched) {
122
        if (!err) {
123
          Tcl_AppendResult(interp, "ambiguous option \"", arg,
124
                           "\" could be ", matched->name, (char *) 0);
125
          matched = next;
126
          err = 1;
127
        }
128
        Tcl_AppendResult(interp, ", ", next->name, (char *) 0);
129
      } else {
130
        matched = next;
131
        /* return on an exact match */
132
        if (len == (int)strlen(next->name))
133
          return matched->value;
134
      }
135
    }
136
    next++;
137
  }
138
  /* did we get multiple possibilities */
139
  if (err) return 0;
140
  /* did we match any at all */
141
  if (matched) {
142
    return matched->value;
143
  } else {
144
    Tcl_AppendResult(interp, "bad option \"", arg, "\" must be ",
145
                     (char *) NULL);
146
    next = cmds;
147
    while (1) {
148
      Tcl_AppendResult(interp, next->name, (char *) NULL);
149
      /* the end of them all ? */
150
      if (!*((++next)->name)) return 0;
151
      /* or the last one at least */
152
      if (*((next + 1)->name))
153
        Tcl_AppendResult(interp, ", ", (char *) NULL);
154
      else
155
        Tcl_AppendResult(interp, " or ", (char *) NULL);
156
    }
157
  }
158
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.