Jabber WindowGram Client (JWGC)

Introduction Screenshots Installation Downloads
Documentation Browse Source Resources Project Site

Stable Version
-none-

Latest Version
beta5



Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

variables.c

Go to the documentation of this file.
00001 /*
00002  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
00003  *      For copying and distribution information, see the file
00004  *      "mit-copyright.h".
00005  *
00006  *      Modified for jwgc by Daniel Henninger.
00007  */
00008 
00009 #include "mit-copyright.h"
00010 
00011 /****************************************************************************/
00012 /* */
00013 /* Module containing code to deal with description langauge variables:    */
00014 /* */
00015 /****************************************************************************/
00016 
00017 #include "notice.h"
00018 #include "string_dictionary_aux.h"
00019 #include "variables.h"
00020 
00021 /*
00022  * fields_data[_length] - these point to the field data that the number
00023  *                        variables were last set to using
00024  *                        var_set_number_variables_to_fields.
00025  */
00026 
00027 static char *fields_data;
00028 static int fields_data_length = 0;
00029 
00030 /*
00031  * [non_]number_variable_dict - contains the values of all the [non-]number
00032  *                              variables that have been set since the last
00033  *                              var_clear_all_variables call or (for numbers
00034  *                              only) var_set_number_variables_to_fields call.
00035  */
00036 
00037 static string_dictionary non_number_variable_dict = NULL;
00038 static string_dictionary number_variable_dict = NULL;
00039 
00040 /*
00041  *  Internal Routine:
00042  *
00043  *    int is_digits(string text)
00044  *        Effects: Returns true iff text matches [0-9]*.  ("" matches...)
00045  */
00046 
00047 static int 
00048 is_digits(text)
00049         string text;
00050 {
00051         for (; *text; text++)
00052                 if (!isdigit(*text))
00053                         return (0);
00054 
00055         return (1);
00056 }
00057 
00058 /*
00059  *  Internal Routine:
00060  *
00061  *    int is_number_variable(string text)
00062  *        Effects: Returns true iff text matches [0-9]+.
00063  */
00064 
00065 #define  is_number_variable(x)      (isdigit(*(x)) && is_digits((x)))
00066 
00067 /*
00068  *    void var_clear_all_variables()
00069  *        Requires: This routine must be called before any other
00070  *                  var module routine is called.
00071  *        Modifies: All description language variables
00072  *        Effects: Sets all description langauge variables to "".
00073  */
00074 
00075 void 
00076 var_clear_all_variables()
00077 {
00078         if (non_number_variable_dict) {
00079                 string_dictionary_SafeDestroy(non_number_variable_dict);
00080                 string_dictionary_SafeDestroy(number_variable_dict);
00081         }
00082 
00083         non_number_variable_dict = string_dictionary_Create(101);
00084         number_variable_dict = string_dictionary_Create(11);
00085         fields_data_length = 0;
00086 }
00087 
00088 /*
00089  *    string var_get_variable(string name)
00090  *        Requires: var_clear_all_variables has been called
00091  *        Effects: Returns the value of the description langauge variable
00092  *                 named name.  The returned string is read-only and is
00093  *                 guarenteed to last only until the next var module
00094  *                 call.  DO NOT FREE THIS STRING.
00095  */
00096 
00097 string 
00098 var_get_variable(name)
00099         string name;
00100 {
00101         char *result;
00102         int field_to_get;
00103         static string last_get_field_call_result = NULL;
00104 
00105         if (is_number_variable(name)) {
00106                 if ((result = string_dictionary_Fetch(number_variable_dict, name)))
00107                         return (result);
00108 
00109                 /*
00110                  * Convert name to an integer avoiding overflow:
00111                  */
00112                 while (*name == '0')
00113                         name++;
00114                 if (strlen(name) > 12)
00115                         field_to_get = 0;       /* no way we'll have > 1
00116                                                  * trillian fields... */
00117                 else
00118                         field_to_get = atoi(name);
00119 
00120                 if (!field_to_get)
00121                         return ("");
00122                 if (last_get_field_call_result)
00123                         free(last_get_field_call_result);
00124                 last_get_field_call_result = get_field(fields_data,
00125                                                        fields_data_length,
00126                                                        field_to_get);
00127                 return (last_get_field_call_result);
00128         }
00129 
00130         if (!(result = string_dictionary_Fetch(non_number_variable_dict, name)))
00131                 result = "";
00132 
00133         return (result);
00134 }
00135 
00136 /*
00137  *    void var_set_variable(string name, value)
00138  *        Requires: var_clear_all_variables has been called
00139  *        Modifies: The value of description langauge variable
00140  *                  named name.
00141  *        Effects: Sets the description langauge variable named name
00142  *                 to have the value value.
00143  */
00144 
00145 void 
00146 var_set_variable(name, value)
00147         string name;
00148         string value;
00149 {
00150         string_dictionary_Set(is_number_variable(name) ? number_variable_dict
00151                               : non_number_variable_dict, name, value);
00152 }
00153 
00154 /*
00155  *    void var_set_variable_to_number(string name; int number)
00156  *        Requires: var_clear_all_variables has been called
00157  *        Modifies: The value of description langauge variable
00158  *                  named name.
00159  *        Effects: Sets the description langauge variable named name
00160  *                 to have as its value number's ascii representation.
00161  */
00162 
00163 void 
00164 var_set_variable_to_number(name, number)
00165         string name;
00166         int number;
00167 {
00168         char buffer[20];
00169 
00170         sprintf(buffer, "%d", number);
00171         var_set_variable(name, buffer);
00172 }
00173 
00174 /*
00175  *    void var_set_variable_then_free_value(string name, value)
00176  *        Requires: var_clear_all_variables has been called, value is
00177  *                  on the heap.
00178  *        Modifies: The value of description langauge variable
00179  *                  named name, value
00180  *        Effects: Sets the description langauge variable named name
00181  *                 to have the value value then frees value.  This
00182  *                 routine is slightly faster than calling var_set_variable
00183  *                 then freeing value.  It is provided mainly for
00184  *                 convenience reasons.
00185  */
00186 
00187 void 
00188 var_set_variable_then_free_value(name, value)
00189         string name;
00190         string value;
00191 {
00192         string_dictionary_binding *binding;
00193         int exists;
00194 
00195         if (is_number_variable(name)) {
00196                 var_set_variable(name, value);
00197                 free(value);
00198                 return;
00199         }
00200 
00201         binding = string_dictionary_Define(non_number_variable_dict, name,
00202                                            &exists);
00203         if (exists)
00204                 free(binding->value);
00205         binding->value = value;
00206 }
00207 
00208 /*
00209  *    void var_set_number_variables_to_fields(char *data, int length)
00210  *        Requires: var_clear_all_variables has been called
00211  *        Modifies: All numeric description language variables
00212  *        Effects: Treats data[0]..data[length-1] as a series of
00213  *                 null-seperated fields.  Sets $<number> (<number>
00214  *                 here means [0-9]+ to field # <number> in data.
00215  *                 Field 0 is defined to be "" as are all field #'s
00216  *                 greater than the number of fields in data.
00217  *                 Data[0]..data[length-1] must not be changed (or freed)
00218  *                 until either this call is made again with different
00219  *                 data or var_clear_all_variables is called.
00220  */
00221 
00222 void 
00223 var_set_number_variables_to_fields(data, length)
00224         char *data;
00225         int length;
00226 {
00227         fields_data = data;
00228         fields_data_length = length;
00229 
00230         string_dictionary_SafeDestroy(number_variable_dict);
00231         number_variable_dict = string_dictionary_Create(11);
00232 }


Last updated at Tue Dec 18 21:07:42 PST 2007. This site and project hosted by...SourceForge.net Logo
Source Perspective by Fisheye