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