37static const int kOK = TCL_OK;
38static const int kERR = TCL_ERROR;
45 Tcl_CreateObjCommand(interp,
"bvi",
DoCmd,
46 reinterpret_cast<ClientData
>(
kStr2Int),
nullptr);
47 Tcl_CreateObjCommand(interp,
"pbvi",
DoCmd,
48 reinterpret_cast<ClientData
>(
kInt2Str),
nullptr);
56 Tcl_Obj*
const objv[])
68 Tcl_Obj** lobjv =
nullptr;
69 if (Tcl_ListObjGetElements(interp, objv[2], &lobjc, &lobjv) !=
kOK) {
73 RtclOPtr rlist(Tcl_NewListObj(0,
nullptr));
75 for (
int i=0; i<lobjc; i++) {
77 if (!rval)
return kERR;
78 if (Tcl_ListObjAppendElement(interp, rlist, rval) !=
kOK)
return kERR;
81 Tcl_SetObjResult(interp, rlist);
84 Tcl_Obj* rval =
DoConv(interp, mode, objv[2], form, nbit);
85 if (rval==0)
return kERR;
86 Tcl_SetObjResult(interp, rval);
99 const char* pval = Tcl_GetString(val);
100 int lval = strlen(pval);
103 while (pval[0]!=0 && ::isblank(pval[0])) {
108 while (lval>0 && ::isblank(pval[lval-1])) {
113 if (lval>3 && pval[1]==
'"' && pval[lval-1]==
'"') {
114 if (strchr(
"bBoOdDxX", pval[0]) == 0) {
115 Tcl_AppendResult(interp,
"-E: bad prefix in c'dddd' format string",
123 }
else if (lval>2 && pval[0]==
'0' && (pval[1]==
'x' || pval[1]==
'X')) {
131 case 'b':
case 'B': base = 2;
break;
132 case 'o':
case 'O': base = 8;
break;
133 case 'd':
case 'D': base = 10;
break;
134 case 'x':
case 'X': base = 16;
break;
137 unsigned long lres=0;
140 if (base==10 && pval[0]==
'-') {
141 lres =
static_cast<unsigned long>(::strtol(pval, &eptr, base));
142 if (nbit<32) lres &= (1ul<<nbit)-1;
144 lres = ::strtoul(pval, &eptr, base);
147 if (eptr != pval+lval) {
148 Tcl_AppendResult(interp,
"-E: conversion error in '",
149 Tcl_GetString(val),
"'",
nullptr);
153 if (lres > (1ul<<nbit)-1) {
154 Tcl_AppendResult(interp,
"-E: too many bits defined in '",
155 Tcl_GetString(val),
"'",
nullptr);
159 return Tcl_NewIntObj(
int(lres));
163 if (Tcl_GetIntFromObj(interp, val, &val_int) !=
kOK)
return nullptr;
164 unsigned int val_uint = val_int;
167 if (form==
'o' || form==
'O') nwidth = 3;
168 if (form==
'x' || form==
'X') nwidth = 4;
169 unsigned int nmask = (1<<nwidth)-1;
173 if (form==
'B' || form==
'O' || form==
'X') {
174 *pbuf++ = tolower(form);
178 int ndig = (nbit+nwidth-1)/nwidth;
179 for (
int i=ndig-1; i>=0; i--) {
180 unsigned int nibble = ((val_uint)>>(i*nwidth)) & nmask;
181 nibble += (nibble <= 9) ?
'0' : (
'a'-10);
182 *pbuf++ = char(nibble);
185 if (form==
'B' || form==
'O' || form==
'X') {
189 return Tcl_NewStringObj(buf, pbuf-buf);
192 Tcl_AppendResult(interp,
"-E: BUG! bad cdata in RtclBvi::DoConv() call",
202 bool& list,
char& form,
int& nbit)
209 Tcl_WrongNumArgs(interp, 1, objv,
"form arg");
213 const char* opt = Tcl_GetString(objv[1]);
231 if (*opt>=
'0' && *opt<=
'9') {
232 nbit = 10*nbit + ((*opt) -
'0');
234 Tcl_AppendResult(interp,
"-E: invalid bvi format '", opt,
"'",
235 " bit count > 32",
nullptr);
239 Tcl_AppendResult(interp,
"-E: invalid bvi format '", opt,
"'",
240 " allowed: [bBoOxXl][0-9]*",
nullptr);
static int DoCmd(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
FIXME_docs.
static bool CheckFormat(Tcl_Interp *interp, int objc, Tcl_Obj *const objv[], bool &list, char &form, int &nbit)
FIXME_docs.
static Tcl_Obj * DoConv(Tcl_Interp *interp, ConvMode mode, Tcl_Obj *val, char form, int nbit)
FIXME_docs.
static void CreateCmds(Tcl_Interp *interp)
FIXME_docs.
Implemenation (inline) of RtclOPtr.
Declaration of class ReventLoop.