G:/ScriptBasic/source/options.c

Go to the documentation of this file.
00001 /*
00002 FILE:   options.c
00003 HEADER: options.h
00004 
00005 --GNU LGPL
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 This library is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public
00017 License along with this library; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 Handling options
00021 
00022 TO_HEADER:
00023 
00024 */
00025 
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 
00030 #include "sym.h"
00031 #include "errcodes.h"
00032 #include "report.h"
00033 #include "lexer.h"
00034 #include "expression.h"
00035 #include "builder.h"
00036 #include "memory.h"
00037 #include "syntax.h"
00038 #include "execute.h"
00039 #include "myalloc.h"
00040 #include "command.h"
00041 
00042 /*POD
00043 @c Setting and getting option values
00044 
00045 Each BASIC interpreter maintains a symbol table holding option values.
00046 These option values can be set using the BASIC command T<OPTION> and an
00047 option value can be retrieved using the function T<OPTION()>.
00048 
00049 An option has an integer value (T<long>). Options are usually used to
00050 alter the behaviour of some commands or modules, altough BASIC programs
00051 are free to use any string to name an option. For example the option
00052 T<compare> may alter the behavior of the string comparision function
00053 to be case sensitive or insensitive:
00054 
00055 =verbatim
00056 OPTION compare 1
00057 =noverbatim
00058 
00059 Unitialized options are treated as being zero. There is no special option value for uninitialized 
00060 options. In other words BASIC programs can not distinguish between unitialized options and
00061 options having the value zero.
00062 
00063 This file contains the functions that handle the option symbol table. The option
00064 symbol tableis pointed by the field T<OptionsTable> of the execution object. This
00065 pointer is initialized to be T<NULL>, which means no options are available, or in other
00066 words all options are zero.
00067 CUT*/
00068 
00069 /*POD
00070 =H options_Reset
00071 @c Clear an option data
00072 
00073 Calling this function resets an option. This means that the memory holding
00074 the T<long> value is released and the pointer that was pointing to it is set T<NULL>.
00075 
00076 /*FUNCTION*/
00077 int options_Reset(pExecuteObject pEo,
00078                   char *name
00079   ){
00080 /*noverbatim
00081 CUT*/
00082   void **p;
00083 
00084   /* If there is no any option then the actual option need not be reset. */
00085   if( pEo->OptionsTable == NULL )return 0;
00086   p = sym_LookupSymbol(name,pEo->OptionsTable,0,alloc_Alloc,alloc_Free,pEo->pMemorySegment);
00087   /* If the option is not in the symbol table it need not be reset. */
00088   if( p == NULL )return 0;
00089   /* If the option is in the symbol table with NULL value then it IS already reset. */
00090   if( *p == NULL )return 0;
00091   /* release the memory (a long) */
00092   alloc_Free(*p,pEo->pMemorySegment);
00093   /* do not point to the released long */
00094   *p = NULL;
00095   return 0;
00096   }
00097 
00098 /*POD
00099 =H options_Set
00100 @c Set option data
00101 
00102 This function sets a long value for an option. If the option
00103 did not exist before in the symbol table it is inserted. If the
00104 symbol table was empty (aka T<OptionsTable> pointed T<NULL>) the
00105 symbol table is also created.
00106 
00107 If the symbol already existed with some T<long> value then the new value
00108 is stored in the already allocated place and thus the caller
00109 may store the pointer to the long returned by R<GetR> and access 
00110 possibly updated data without searching the table again and again.
00111 
00112 /*FUNCTION*/
00113 int options_Set(pExecuteObject pEo,
00114                 char *name,
00115                 long value
00116   ){
00117 /*noverbatim
00118 The function returns zero if the option was set or T<1> if there was a memory failure.
00119 CUT*/
00120   void **p;
00121 
00122   /* create the symbol table if it does not exist */
00123   if( pEo->OptionsTable == NULL )
00124     pEo->OptionsTable = sym_NewSymbolTable(alloc_Alloc,pEo->pMemorySegment);
00125   if( pEo->OptionsTable == NULL )return 1; /* In case of memory failure. */
00126 
00127   /* lookup the option and insert it into the table in case it is not there yet */
00128   p = sym_LookupSymbol(name,pEo->OptionsTable,1,alloc_Alloc,alloc_Free,pEo->pMemorySegment);
00129   if( p == NULL )return 1; /* In case of memory failure. */
00130 
00131   /* If the option was not defined before. */
00132   if( *p == NULL ){
00133     *p = alloc_Alloc(sizeof(long),pEo->pMemorySegment);
00134     if( *p == NULL )return 1; /* In case of memory failure. */
00135     }
00136   /* store the value */
00137   *((long *)*p) = value;
00138   return 0;/*OK*/
00139   }
00140 
00141 /*POD
00142 =H options_Get
00143 @c Get option data
00144 
00145 This function retrieves and returns the value of an option data.
00146 
00147 /*FUNCTION*/
00148 long options_Get(pExecuteObject pEo,
00149                  char *name
00150   ){
00151 /*noverbatim
00152 The return value is the option value or zero in case the option is not set.
00153 CUT*/
00154   void **p;
00155 
00156   if( pEo->OptionsTable == NULL )return 0L;
00157   p = sym_LookupSymbol(name,pEo->OptionsTable,0,alloc_Alloc,alloc_Free,pEo->pMemorySegment);
00158   if( p == NULL )return 0L;
00159   if( *p == NULL )return 0L;
00160 
00161   return *((long *)*p);
00162   }
00163 
00164 /*POD
00165 =H options_GetR
00166 @c Get option data
00167 
00168 This function retrieves and returns the value of an option data.
00169 
00170 /*FUNCTION*/
00171 long *options_GetR(pExecuteObject pEo,
00172                  char *name
00173   ){
00174 /*noverbatim
00175 The return value is a T<long *> pointer to the option value or T<NULL> if the option is
00176 not set. If the caller sets the T<long> variable pointed by the returned pointer the value of the
00177 option is changed directly.
00178 CUT*/
00179   void **p;
00180 
00181   if( pEo->OptionsTable == NULL )return NULL;
00182   p = sym_LookupSymbol(name,pEo->OptionsTable,0,alloc_Alloc,alloc_Free,pEo->pMemorySegment);
00183   if( p == NULL )return NULL;
00184   if( *p == NULL )return NULL;
00185 
00186   return ((long *)*p);
00187   }

Generated on Sun Mar 12 23:56:31 2006 for ScriptBasic by  doxygen 1.4.6-NO