chiark / gitweb /
-Wno-strict-aliasing
[chiark-tcl.git] / base / parse.c
1 /*
2  * base code for various Tcl extensions
3  * Copyright 2006 Ian Jackson
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  */
20
21 #include "chiark-tcl-base.h"
22
23 int cht_pat_charfrom(Tcl_Interp *ip, Tcl_Obj *obj, int *val,
24                  const char *opts, const char *what) {
25   *val= cht_enum1_lookup_cached_func(ip,obj,opts,what);
26   if (*val==-1) return TCL_ERROR;
27   return TCL_OK;
28 }
29
30 int cht_pat_int(Tcl_Interp *ip, Tcl_Obj *obj, int *val) {
31   return Tcl_GetIntFromObj(ip, obj, val);
32 }
33   
34 int cht_pat_long(Tcl_Interp *ip, Tcl_Obj *obj, long *val) {
35   return Tcl_GetLongFromObj(ip, obj, val);
36 }
37   
38 int cht_pat_string(Tcl_Interp *ip, Tcl_Obj *obj, const char **val) {
39   *val= Tcl_GetString(obj);
40   return TCL_OK;
41 }
42
43 int cht_pat_constv(Tcl_Interp *ip, Tcl_Obj *var,
44                Tcl_Obj **val_r, Tcl_ObjType *type) {
45   int rc;
46   Tcl_Obj *val;
47   
48   val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
49   if (!val) return TCL_ERROR;
50
51   if (type) {
52     rc= Tcl_ConvertToType(ip,val,type);
53     if (rc) return rc;
54   }
55
56   *val_r= val;
57   return TCL_OK;
58 }
59
60 void cht_init_somethingv(Something_Var *sth) {
61   sth->obj=0; sth->var=0; sth->copied=0;
62 }
63
64 int cht_pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
65                    Something_Var *sth, Tcl_ObjType *type) {
66   int rc;
67   Tcl_Obj *val;
68
69   sth->var= var;
70
71   val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
72   if (!val) return TCL_ERROR;
73
74   rc= Tcl_ConvertToType(ip,val,type);
75   if (rc) return rc;
76
77   if (Tcl_IsShared(val)) {
78     val= Tcl_DuplicateObj(val);
79     sth->copied= 1;
80   }
81   Tcl_InvalidateStringRep(val);
82   sth->obj= val;
83
84   return TCL_OK;
85 }
86
87 void cht_fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth) {
88   Tcl_Obj *ro;
89   
90   if (!rc) {
91     assert(sth->obj);
92     ro= Tcl_ObjSetVar2(ip,sth->var,0,sth->obj,TCL_LEAVE_ERR_MSG);
93     if (!ro) rc= TCL_ERROR;
94   }
95   if (rc && sth->copied)
96     Tcl_DecrRefCount(sth->obj);
97 }
98
99 Tcl_Obj *cht_ret_long(Tcl_Interp *ip, long val) {
100   return Tcl_NewLongObj(val);
101 }
102
103 Tcl_Obj *cht_ret_string(Tcl_Interp *ip, const char *val) {
104   return Tcl_NewStringObj(val,-1);
105 }