1 /*
2    Named data record definition and routines.
3 
4    Simple storage of a memory block with a name in a node,
5    which may be stored in a linear list of nodes.
6 
7    clib v1.1
8 
9    Copyright (C) 1998 Per Kraulis
10     19-Jan-1998  first attempt
11      3-Apr-1998  added data clone procedure
12 */
13 
14 #include "named_data.h"
15 
16 /* public ====================
17 #include <stdlib.h>
18 
19 #include <boolean.h>
20 
21 typedef struct _named_data named_data;
22 
23 struct _named_data {
24   char *name;
25   void *data;
26   boolean name_copy, data_copy;
27   named_data *next;
28 };
29 ==================== public */
30 
31 #include <string.h>
32 #include <assert.h>
33 
34 #include <str_utils.h>
35 
36 
37 /*------------------------------------------------------------*/
38 named_data *
nd_create(char * name,boolean copy)39 nd_create (char *name, boolean copy)
40 {
41   named_data *new = malloc (sizeof (named_data));
42 
43   /* pre */
44   assert (name);
45 
46   new->name = name;
47   new->name_copy = copy;
48   new->data = NULL;
49   new->data_copy = FALSE;
50   new->next = NULL;
51 
52   return new;
53 }
54 
55 
56 /*------------------------------------------------------------*/
57 named_data *
nd_set_data(named_data * nd,void * data,boolean copy)58 nd_set_data (named_data *nd, void *data, boolean copy)
59 {
60   /* pre */
61   assert (nd);
62 
63   if (nd->data && nd->data_copy) free (nd->data);
64   nd->data = data;
65   nd->data_copy = copy;
66 
67   return nd;
68 }
69 
70 
71 /*------------------------------------------------------------*/
72 named_data *
nd_set_data_clone(named_data * nd,void * data,size_t size)73 nd_set_data_clone (named_data *nd, void *data, size_t size)
74 {
75   /* pre */
76   assert (nd);
77 
78   if (nd->data && nd->data_copy) free (nd->data);
79   nd->data = memcpy (malloc (size), data, size);
80   nd->data_copy = TRUE;
81 
82   return nd;
83 }
84 
85 
86 /*------------------------------------------------------------*/
87 void
nd_delete(named_data * nd)88 nd_delete (named_data *nd)
89 {
90   /* pre */
91   assert (nd);
92 
93   if (nd->next) nd_delete (nd->next);
94 
95   if (nd->name_copy) free (nd->name);
96   if (nd->data && nd->data_copy) free (nd->data);
97   free (nd);
98 }
99 
100 
101 /*------------------------------------------------------------*/
102 named_data *
nd_append(named_data * nd,named_data * new)103 nd_append (named_data *nd, named_data *new)
104 {
105   /* pre */
106   assert (nd);
107   assert (new);
108 
109   while (nd->next) nd = nd->next;
110   nd->next = new;
111 
112   return new;
113 }
114 
115 
116 /*------------------------------------------------------------*/
117 named_data *
nd_search(named_data * nd,const char * name)118 nd_search (named_data *nd, const char *name)
119 {
120   /* pre */
121   assert (name);
122 
123   for ( ; nd; nd = nd->next) {
124     assert (nd->name);
125     if (str_eq (nd->name, name)) return nd;
126   }
127 
128   return NULL;
129 }
130 
131 
132 /*------------------------------------------------------------*/
133 void *
nd_search_data(named_data * nd,const char * name)134 nd_search_data (named_data *nd, const char *name)
135 {
136   /* pre */
137   assert (name);
138 
139   for ( ; nd; nd = nd->next) {
140     assert (nd->name);
141     if (str_eq (nd->name, name)) return nd->data;
142   }
143 
144   return NULL;
145 }
146