chiark / gitweb /
some maskmap stuff;
[chiark-tcl.git] / base / parse.c
1 /*
2  */
3
4 #include "tables.h"
5
6 int pat_charfrom(Tcl_Interp *ip, Tcl_Obj *obj, int *val,
7                  const char *opts, const char *what) {
8   *val= enum1_lookup_cached_func(ip,obj,opts,what);
9   if (*val==-1) return TCL_ERROR;
10   return TCL_OK;
11 }
12
13 int pat_int(Tcl_Interp *ip, Tcl_Obj *obj, int *val) {
14   return Tcl_GetIntFromObj(ip, obj, val);
15 }
16   
17 int pat_long(Tcl_Interp *ip, Tcl_Obj *obj, long *val) {
18   return Tcl_GetLongFromObj(ip, obj, val);
19 }
20   
21 int pat_string(Tcl_Interp *ip, Tcl_Obj *obj, const char **val) {
22   *val= Tcl_GetString(obj);
23   return TCL_OK;
24 }
25
26 void init_somethingv(Something_Var *sth) {
27   sth->obj=0; sth->var=0; sth->copied=0;
28 }
29
30 int pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
31                    Something_Var *sth, Tcl_ObjType *type) {
32   int rc;
33   Tcl_Obj *val;
34
35   sth->var= var;
36
37   val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
38   if (!val) return TCL_ERROR;
39
40   rc= Tcl_ConvertToType(ip,val,type);
41   if (rc) return rc;
42
43   if (Tcl_IsShared(val)) {
44     val= Tcl_DuplicateObj(val);
45     sth->copied= 1;
46   }
47   Tcl_InvalidateStringRep(val);
48   sth->obj= val;
49
50   return TCL_OK;
51 }
52
53 int pat_hbv(Tcl_Interp *ip, Tcl_Obj *var, HBytes_Var *agg) {
54   int rc;
55   rc= pat_somethingv(ip,var,&agg->sth,&hbytes_type);  if (rc) return rc;
56   agg->hb= OBJ_HBYTES(agg->sth.obj);
57   return TCL_OK;
58 }
59
60 void fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth) {
61   Tcl_Obj *ro;
62   
63   if (!rc) {
64     assert(sth->obj);
65     ro= Tcl_ObjSetVar2(ip,sth->var,0,sth->obj,TCL_LEAVE_ERR_MSG);
66     if (!ro) rc= TCL_ERROR;
67   }
68   if (rc && sth->copied)
69     Tcl_DecrRefCount(sth->obj);
70 }
71
72 int pat_hb(Tcl_Interp *ip, Tcl_Obj *obj, HBytes_Value *val) {
73   int rc;
74   rc= Tcl_ConvertToType(ip,obj,&hbytes_type);  if (rc) return rc;
75   *val= *OBJ_HBYTES(obj);
76   return TCL_OK;
77 }
78
79 Tcl_Obj *ret_hb(Tcl_Interp *ip, HBytes_Value val) {
80   Tcl_Obj *obj;
81   obj= Tcl_NewObj();
82   Tcl_InvalidateStringRep(obj);
83   *OBJ_HBYTES(obj)= val;
84   obj->typePtr= &hbytes_type;
85   return obj;
86 }
87
88 Tcl_Obj *ret_long(Tcl_Interp *ip, long val) {
89   return Tcl_NewLongObj(val);
90 }
91
92 Tcl_Obj *ret_string(Tcl_Interp *ip, const char *val) {
93   return Tcl_NewStringObj(val,-1);
94 }