2 * base code for various Tcl extensions
3 * Copyright 2006 Ian Jackson
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.
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.
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
21 #include "chiark-tcl-base.h"
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;
30 int cht_pat_int(Tcl_Interp *ip, Tcl_Obj *obj, int *val) {
31 return Tcl_GetIntFromObj(ip, obj, val);
34 int cht_pat_long(Tcl_Interp *ip, Tcl_Obj *obj, long *val) {
35 return Tcl_GetLongFromObj(ip, obj, val);
38 int cht_pat_string(Tcl_Interp *ip, Tcl_Obj *obj, const char **val) {
39 *val= Tcl_GetString(obj);
43 int cht_pat_constv(Tcl_Interp *ip, Tcl_Obj *var,
44 Tcl_Obj **val_r, Tcl_ObjType *type) {
48 val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
49 if (!val) return TCL_ERROR;
52 rc= Tcl_ConvertToType(ip,val,type);
60 void cht_init_somethingv(Something_Var *sth) {
61 sth->obj=0; sth->var=0; sth->copied=0;
64 int cht_pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
65 Something_Var *sth, Tcl_ObjType *type) {
71 val= Tcl_ObjGetVar2(ip,var,0,TCL_LEAVE_ERR_MSG);
72 if (!val) return TCL_ERROR;
74 rc= Tcl_ConvertToType(ip,val,type);
77 if (Tcl_IsShared(val)) {
78 val= Tcl_DuplicateObj(val);
81 Tcl_InvalidateStringRep(val);
87 void cht_fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth) {
92 ro= Tcl_ObjSetVar2(ip,sth->var,0,sth->obj,TCL_LEAVE_ERR_MSG);
93 if (!ro) rc= TCL_ERROR;
95 if (rc && sth->copied)
96 Tcl_DecrRefCount(sth->obj);
99 Tcl_Obj *cht_ret_long(Tcl_Interp *ip, long val) {
100 return Tcl_NewLongObj(val);
103 Tcl_Obj *cht_ret_string(Tcl_Interp *ip, const char *val) {
104 return Tcl_NewStringObj(val,-1);