6 * Utility functions, I need to program fgis
7 * Copyright (c) Softweyr, 1997
10 * If passed condition is true, puts standard error message, appended
11 * by msg argument to interp->result.
15 int Fgis_CkArgs(Tcl_Interp *interp,int cond,char *cmd, char *msg)
16 { static char buffer[256]="Wrong # args. Should be ";
18 Tcl_ResetResult(interp);
19 Tcl_AppendResult(interp,buffer,cmd," ",msg,NULL);
24 * Get integer value from command argument and checks it for valid range.
26 * interp: Tcl interpreter to leave error message
27 * argc, argv - command arguments
28 * index - position of argument in question in argv
29 * min, max - range limits
30 * name - string name of parameter to report errors more clearly
32 * TCL_OK on success, TCL_ERROR on failure
34 * Places read integer in location, specified by result
37 int Fgis_GetInt(Tcl_Interp *interp,int argc,char **argv,int index,
38 int min, int max,int *result, char *name)
40 int tmp;/* Temporary place to hold result */
41 char msg[1024]; /* place to format error messages */
44 Tcl_SetResult(interp, "Integer value expected",TCL_STATIC);
48 if (Tcl_GetInt(interp,argv[index],&tmp)!=TCL_OK)
50 if (tmp<=min||tmp>=max) {
51 sprintf(msg,"Invalid %s: %d. Should be between %d and %d",
53 Tcl_SetResult(interp,msg,TCL_VOLATILE);
59 char *inttostr(long src,char *dest)
60 { sprintf(dest,"%ld",src);
65 * Searches for free command name with given prefix and creates command
66 * with it. Places name of command into interp->result
67 * Returns TCL_OK on success, TCL_ERROR otherwise
69 int Fgis_CreateObjectCommand(Tcl_Interp *interp,char *prefix,
70 Tcl_CmdProc *proc,ClientData data,Tcl_CmdDeleteProc deleteProc)
73 static char token[30],*numstart;
76 numstart=token+strlen(prefix);
78 inttostr(num,numstart);
79 if (Tcl_GetCommandInfo(interp,token,&info)) num++; else break;
81 Tcl_SetResult(interp,token,TCL_VOLATILE);
82 if (!Tcl_CreateCommand(interp,token,proc,data,deleteProc)) {
83 (*deleteProc)(data); return TCL_ERROR;
91 * Standard TclDeleteProc for commads, which have no ClientData
92 * Does nothing, successifully
94 EXPORT(void, Fgis_DefDeleteProc)(ClientData clientdata)
99 * Fgis_GetLimits - parses four-element list of doubles, putting them
100 * into four specified global variables
101 * ARGUMENTS: interp - Tcl interpreter to return error
102 * list - string to parse
103 * X1,Y1,X2,Y2 - double variables to put results
104 * RETURNS: TCL_OK on success, TCL_ERROR otherwise
105 * SIDE EFFECTS: Fills four double variables
107 int Fgis_GetLimits(Tcl_Interp *interp,char *list,double *X1,double *Y1,
108 double *X2,double *Y2)
110 int no_error;/* Temporary flag, to pass success around Tcl_Free */
111 int listc; char **listv; /* for TclSplitList */
112 double x1,y1,x2,y2; /* Temporary place for results to avoid overriding if
115 if(Tcl_SplitList(interp,list,&listc,&listv)==TCL_ERROR)
118 Tcl_SetResult(interp,"Invalid limits list",TCL_STATIC);
119 Tcl_Free((char *)listv);
122 no_error = (Tcl_GetDouble(interp,listv[0],&x1)==TCL_OK) &&
123 (Tcl_GetDouble(interp,listv[1],&y1)==TCL_OK) &&
124 (Tcl_GetDouble(interp,listv[2],&x2)==TCL_OK) &&
125 (Tcl_GetDouble(interp,listv[3],&y2)==TCL_OK);
126 Tcl_Free((char*)listv);
128 *X1=x1; *Y1=y1; *X2=x2; *Y2=y2;