chiark / gitweb /
settling on interface to cdb binding
[chiark-tcl.git] / base / parse.c
1 /*
2  */
3
4 #include "chiark-tcl-base.h"
5
6 int cht_pat_charfrom(Tcl_Interp *ip, Tcl_Obj *obj, int *val,
7                  const char *opts, const char *what) {
8   *val= cht_enum1_lookup_cached_func(ip,obj,opts,what);
9   if (*val==-1) return TCL_ERROR;
10   return TCL_OK;
11 }
12
13 int cht_pat_int(Tcl_Interp *ip, Tcl_Obj *obj, int *val) {
14   return Tcl_GetIntFromObj(ip, obj, val);
15 }
16   
17 int cht_pat_long(Tcl_Interp *ip, Tcl_Obj *obj, long *val) {
18   return Tcl_GetLongFromObj(ip, obj, val);
19 }
20   
21 int cht_pat_string(Tcl_Interp *ip, Tcl_Obj *obj, const char **val) {
22   *val= Tcl_GetString(obj);
23   return TCL_OK;
24 }
25
26 int cht_pat_constv(Tcl_Interp *ip, Tcl_Obj *var,
27                Tcl_Obj **val_r, Tcl_ObjType *type) {
28   int rc;
29   Tcl_Obj *val;
30   
31   val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
32   if (!val) return TCL_ERROR;
33
34   if (type) {
35     rc= Tcl_ConvertToType(ip,val,type);
36     if (rc) return rc;
37   }
38
39   *val_r= val;
40   return TCL_OK;
41 }
42
43 void cht_init_somethingv(Something_Var *sth) {
44   sth->obj=0; sth->var=0; sth->copied=0;
45 }
46
47 int cht_pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
48                    Something_Var *sth, Tcl_ObjType *type) {
49   int rc;
50   Tcl_Obj *val;
51
52   sth->var= var;
53
54   val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
55   if (!val) return TCL_ERROR;
56
57   rc= Tcl_ConvertToType(ip,val,type);
58   if (rc) return rc;
59
60   if (Tcl_IsShared(val)) {
61     val= Tcl_DuplicateObj(val);
62     sth->copied= 1;
63   }
64   Tcl_InvalidateStringRep(val);
65   sth->obj= val;
66
67   return TCL_OK;
68 }
69
70 void cht_fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth) {
71   Tcl_Obj *ro;
72   
73   if (!rc) {
74     assert(sth->obj);
75     ro= Tcl_ObjSetVar2(ip,sth->var,0,sth->obj,TCL_LEAVE_ERR_MSG);
76     if (!ro) rc= TCL_ERROR;
77   }
78   if (rc && sth->copied)
79     Tcl_DecrRefCount(sth->obj);
80 }
81
82 Tcl_Obj *cht_ret_long(Tcl_Interp *ip, long val) {
83   return Tcl_NewLongObj(val);
84 }
85
86 Tcl_Obj *cht_ret_string(Tcl_Interp *ip, const char *val) {
87   return Tcl_NewStringObj(val,-1);
88 }