|
|
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 * string_dictionary_aux - a module implementing convenience routines for use 00013 * with string_dictionarys 00014 * 00015 * Overview: 00016 * 00017 * This module implements Fetch and Set operations on 00018 * string_dictionaries which take the place of Define and Lookup for 00019 * most uses. The importance difference between them and Define and 00020 * Lookup is that they maintain the invariant that all the value strings 00021 * in a string_dictionary are on the heap. In particular, they do 00022 * free's and string_Copy's whenever needed. Also implemented is 00023 * SafeDestroy which does a Destroy after freeing all the value strings 00024 * in a string_dictionary. 00025 */ 00026 00027 #include "string_dictionary.h" 00028 00029 /* 00030 * void string_dictionary_Set(string_dictionary d, string key,string value): 00031 * Modifies: d 00032 * Effects: Binds key to value in d. Automatically free's the 00033 * previous value of key, if any. Value is copied on the 00034 * heap. 00035 */ 00036 00037 void 00038 string__dictionary_Set(d, key, value) 00039 string_dictionary d; 00040 string key; 00041 string value; 00042 { 00043 string_dictionary_binding *binding; 00044 int already_exists; 00045 00046 binding = string_dictionary_Define(d, key, &already_exists); 00047 if (already_exists) 00048 free(binding->value); 00049 00050 binding->value = string_Copy(value); 00051 } 00052 00053 /* 00054 * char *string_dictionary_Fetch(string_dictionary d, string key) 00055 * Effects: If key is not bound in d, returns 0. Otherwise, 00056 * returns the value that key is bound to. 00057 * Note that the returned string if any should not be 00058 * freed or modified in any way. Note also that it may 00059 * disappear later if key is rebound. 00060 */ 00061 00062 char * 00063 string_dictionary_Fetch(d, key) 00064 string_dictionary d; 00065 string key; 00066 { 00067 string_dictionary_binding *binding; 00068 00069 binding = string_dictionary_Lookup(d, key); 00070 if (!binding) 00071 return (0); 00072 00073 return (binding->value); 00074 } 00075 00076 /* 00077 * void string_dictionary_SafeDestroy(string_dictionary d) 00078 * Modifies: d 00079 * Effects: Like string_dictionary_Destroy except first frees 00080 * all value's in the dictionary. 00081 */ 00082 00083 static void 00084 free_value_of_binding(b) 00085 string_dictionary_binding *b; 00086 { 00087 free(b->value); 00088 } 00089 00090 void 00091 string_dictionary_SafeDestroy(d) 00092 string_dictionary d; 00093 { 00094 string_dictionary_Enumerate(d, free_value_of_binding); 00095 string_dictionary_Destroy(d); 00096 }
Last updated at Tue Dec 18 21:07:42 PST 2007. | This site and project hosted by... |