]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/zwgc/string_dictionary_aux.c
r4275@bucket (orig r265): kcr | 2008-01-21 02:57:32 -0500
[1ts-debian.git] / zephyr / zwgc / string_dictionary_aux.c
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
3  * client.
4  *
5  *      Created by:     Marc Horowitz <marc@athena.mit.edu>
6  *
7  *      $Id$
8  *
9  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h".
12  */
13
14 #if (!defined(lint) && !defined(SABER))
15 static const char rcsid_string_dictionary_aux_c[] = "$Id$";
16 #endif
17
18 /*
19  * string_dictionary_aux - a module implementing convenience routines for use
20  *                         with string_dictionarys
21  *
22  * Overview:
23  *
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.
32  */
33
34 #include <sysdep.h>
35 #include "new_memory.h"
36 #include "string_dictionary.h"
37
38 /*
39  *    void string_dictionary_Set(string_dictionary d, string key,string value):
40  *        Modifies: d
41  *        Effects: Binds key to value in d.  Automatically free's the
42  *                 previous value of key, if any.  Value is copied on the
43  *                 heap.
44  */
45
46 void
47 string__dictionary_Set(string_dictionary d,
48                        string key,
49                        string value)
50 {
51     string_dictionary_binding *binding;
52     int already_exists;
53
54     binding = string_dictionary_Define(d, key, &already_exists);
55     if (already_exists)
56       free(binding->value);
57
58     binding->value = string_Copy(value);
59 }
60
61 /*
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.
68  */
69
70 char *
71 string_dictionary_Fetch(string_dictionary d,
72                         string key)
73 {
74     string_dictionary_binding *binding;
75
76     binding = string_dictionary_Lookup(d, key);
77     if (!binding)
78       return(0);
79
80     return(binding->value);
81 }
82
83 /*
84  *    void string_dictionary_SafeDestroy(string_dictionary d)
85  *        Modifies: d
86  *        Effects: Like string_dictionary_Destroy except first frees
87  *                 all value's in the dictionary.
88  */
89
90 static void
91 free_value_of_binding(string_dictionary_binding *b)
92 {
93     free(b->value);
94 }
95
96 void
97 string_dictionary_SafeDestroy(string_dictionary d)
98 {
99     string_dictionary_Enumerate(d, free_value_of_binding);
100     string_dictionary_Destroy(d);
101 }