00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "mit-copyright.h"
00010
00011
00012
00013
00014
00015
00016
00017 #include "notice.h"
00018 #include "string_dictionary_aux.h"
00019 #include "variables.h"
00020
00021
00022
00023
00024
00025
00026
00027 static char *fields_data;
00028 static int fields_data_length = 0;
00029
00030
00031
00032
00033
00034
00035
00036
00037 static string_dictionary non_number_variable_dict = NULL;
00038 static string_dictionary number_variable_dict = NULL;
00039
00040
00041
00042
00043
00044
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
00060
00061
00062
00063
00064
00065 #define is_number_variable(x) (isdigit(*(x)) && is_digits((x)))
00066
00067
00068
00069
00070
00071
00072
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
00090
00091
00092
00093
00094
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
00111
00112 while (*name == '0')
00113 name++;
00114 if (strlen(name) > 12)
00115 field_to_get = 0;
00116
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
00138
00139
00140
00141
00142
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
00156
00157
00158
00159
00160
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
00176
00177
00178
00179
00180
00181
00182
00183
00184
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
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
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 }