Back to index

extremetuxracer  0.5beta
tcl_util.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  *
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "tcl_util.h"
00023 
00024 /* Parse an n-tuple of doubles specified as a tcl-list.
00025  * Used for grabbing point or vector coordinates, colors, and other things.
00026  * Puts results into an array of doubles.
00027  */
00028 int get_tcl_tuple ( Tcl_Interp *ip, const char *inList, double *p, int n ) 
00029 {
00030     CONST84 char **indices;
00031     double tmp;
00032     int num_doubles;
00033     int rtn;
00034     char s[100];
00035     int i;
00036 
00037     rtn = Tcl_SplitList(ip, inList, &num_doubles, &indices);
00038 
00039     if ((TCL_OK != rtn) || (n != num_doubles)) {
00040        sprintf(s,"%d",n);
00041        Tcl_AppendResult(ip, 
00042                       "Expected a tuple of ", s, " doubles.\n",
00043                       (char *) 0
00044            );
00045        Tcl_Free((char *)indices);
00046        return TCL_ERROR;
00047     }
00048 
00049     for (i = 0; i < n; i++) {
00050        if (TCL_OK != Tcl_GetDouble(ip, indices[i], &tmp)) {
00051            Tcl_Free((char *)indices);
00052            sprintf(s,"%d",n);
00053            Tcl_AppendResult(ip, 
00054                           "Expected a tuple of ", s, " doubles.\n",
00055                           (char *) 0
00056               );
00057            return TCL_ERROR;
00058        }
00059        p[i] = tmp;
00060     }
00061     Tcl_Free((char *)indices);
00062     return TCL_OK;
00063 
00064 }
00065 
00066 /* Parse an n-tuple of ints specified as a tcl-list. */
00067 int get_tcl_int_tuple( Tcl_Interp *ip, const char *inList, int *p, int n ) 
00068 {
00069     CONST84 char **indices;
00070     int tmp;
00071     int num_ints;
00072     int rtn;
00073     char s[100];
00074     int i;
00075 
00076     rtn = Tcl_SplitList(ip, inList, &num_ints, &indices);
00077 
00078     if ((TCL_OK != rtn) || (n != num_ints)) {
00079        sprintf(s,"%d",n);
00080        Tcl_AppendResult(ip, 
00081                       "Expected a tuple of ", s, " integers.\n",
00082                       (char *) 0
00083            );
00084        Tcl_Free((char *)indices);
00085        return TCL_ERROR;
00086     }
00087 
00088     for (i = 0; i < n; i++) {
00089        if (TCL_OK != Tcl_GetInt(ip, indices[i], &tmp)) {
00090            Tcl_Free((char *)indices);
00091            sprintf(s,"%d",n);
00092            Tcl_AppendResult(ip, 
00093                           "Expected a tuple of ", s, " integers.\n",
00094                           (char *) 0
00095               );
00096            return TCL_ERROR;
00097        }
00098        p[i] = tmp;
00099     }
00100     Tcl_Free((char *)indices);
00101     return TCL_OK;
00102 }
00103 
00104 
00105 /*---------------------------------------------------------------------------*/
00112 void setup_tcl_std_channels()
00113 {
00114     /* Only need to do this under Win32 */
00115 #if defined( NATIVE_WIN32_COMPILER )
00116     Tcl_Channel stdout_chnl, stderr_chnl;
00117 
00118     /* I'm not sure why the _dup is necessary under Windows.  
00119 
00120        See the Tcl_SetStdChannel manpage for more info.
00121     */
00122     
00123     /* Create new stdout channel */
00124     Tcl_SetStdChannel( NULL, TCL_STDOUT );
00125     
00126     stdout_chnl = Tcl_MakeFileChannel( 
00127        (ClientData) _get_osfhandle( _dup( _fileno(stdout) ) ),
00128        TCL_WRITABLE );
00129     
00130     check_assertion( stdout_chnl, "Couldn't create new stdout channel" );
00131     
00132     
00133     /* Create a new stderr channel */
00134     Tcl_SetStdChannel( NULL, TCL_STDERR );
00135     
00136     stderr_chnl = Tcl_MakeFileChannel( 
00137        (ClientData) _get_osfhandle( _dup( _fileno(stderr) ) ),
00138        TCL_WRITABLE );
00139     
00140     check_assertion( stderr_chnl, "Couldn't create new stderr channel" );
00141 #endif
00142 }