1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
14 #if (!defined(lint) && !defined(SABER))
15 static char rcsid_string_dictionary_aux_c[] = "$Id$";
19 * string_dictionary_aux - a module implementing convenience routines for use
20 * with string_dictionarys
24 * This module implements Fetch and Set operations on
25 * string_dictionaries which take the place of Define and Lookup for
26 * most uses. The importance difference between them and Define and
27 * Lookup is that they maintain the invariant that all the value strings
28 * in a string_dictionary are on the heap. In particular, they do
29 * free's and string_Copy's whenever needed. Also implemented is
30 * SafeDestroy which does a Destroy after freeing all the value strings
31 * in a string_dictionary.
35 #include "new_memory.h"
36 #include "string_dictionary.h"
39 * void string_dictionary_Set(string_dictionary d, string key,string value):
41 * Effects: Binds key to value in d. Automatically free's the
42 * previous value of key, if any. Value is copied on the
47 string__dictionary_Set(string_dictionary d,
51 string_dictionary_binding *binding;
54 binding = string_dictionary_Define(d, key, &already_exists);
58 binding->value = string_Copy(value);
62 * char *string_dictionary_Fetch(string_dictionary d, string key)
63 * Effects: If key is not bound in d, returns 0. Otherwise,
64 * returns the value that key is bound to.
65 * Note that the returned string if any should not be
66 * freed or modified in any way. Note also that it may
67 * disappear later if key is rebound.
71 string_dictionary_Fetch(string_dictionary d,
74 string_dictionary_binding *binding;
76 binding = string_dictionary_Lookup(d, key);
80 return(binding->value);
84 * void string_dictionary_SafeDestroy(string_dictionary d)
86 * Effects: Like string_dictionary_Destroy except first frees
87 * all value's in the dictionary.
91 free_value_of_binding(string_dictionary_binding *b)
97 string_dictionary_SafeDestroy(string_dictionary d)
99 string_dictionary_Enumerate(d, free_value_of_binding);
100 string_dictionary_Destroy(d);