Mercurial > hg > index.cgi
annotate lwlib/lw_dict.c @ 540:98d68c27f610
Added tag lwtools-4.20 for changeset 0b0f4d5339f9
author | William Astle <lost@l-w.ca> |
---|---|
date | Wed, 17 Aug 2022 16:50:53 -0600 |
parents | 724bcc4508bc |
children |
rev | line source |
---|---|
519
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
1 /* |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
2 lw_dict.c |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
3 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
4 Copyright © 2021 William Astle |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
5 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
6 This file is part of LWTOOLS. |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
7 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
8 LWTOOLS is free software: you can redistribute it and/or modify it under the |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
9 terms of the GNU General Public License as published by the Free Software |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
10 Foundation, either version 3 of the License, or (at your option) any later |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
11 version. |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
12 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, but WITHOUT |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
16 more details. |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
17 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License along with |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
19 this program. If not, see <http://www.gnu.org/licenses/>. |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
20 */ |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
21 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
22 #include <stdlib.h> |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
23 #include <string.h> |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
24 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
25 #define ___lw_dict_c_seen___ |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
26 #include "lw_dict.h" |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
27 #include "lw_string.h" |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
28 #include "lw_alloc.h" |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
29 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
30 lw_dict_t lw_dict_create(void) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
31 { |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
32 lw_dict_t s; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
33 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
34 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
35 s = lw_alloc(sizeof(struct lw_dict_priv)); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
36 s -> head = NULL; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
37 return s; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
38 } |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
39 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
40 void lw_dict_unset(lw_dict_t S, char *key) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
41 { |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
42 struct lw_dict_ent *e1, *e2; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
43 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
44 for (e2 = NULL, e1 = S -> head; e1; ) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
45 { |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
46 if (strcmp(key, e1 -> key) == 0) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
47 break; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
48 e2 = e1; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
49 e1 = e1 -> next; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
50 } |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
51 if (!e1) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
52 return; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
53 if (!e2) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
54 S -> head = e1 -> next; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
55 else |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
56 e2 -> next = e1 -> next; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
57 lw_free(e1 -> key); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
58 lw_free(e1 -> value); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
59 lw_free(e1); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
60 } |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
61 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
62 void lw_dict_destroy(lw_dict_t S) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
63 { |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
64 if (S) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
65 { |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
66 while (S -> head) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
67 lw_dict_unset(S, S -> head -> key); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
68 lw_free(S); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
69 } |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
70 } |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
71 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
72 char *lw_dict_get(lw_dict_t S, char *key) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
73 { |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
74 struct lw_dict_ent *e1; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
75 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
76 for (e1 = S -> head; e1; e1 = e1 -> next) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
77 if (strcmp(key, e1 -> key) == 0) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
78 break; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
79 if (e1) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
80 return e1 -> value; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
81 return ""; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
82 } |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
83 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
84 void lw_dict_set(lw_dict_t S, char *key, char *value) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
85 { |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
86 struct lw_dict_ent *e1; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
87 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
88 for (e1 = S -> head; e1; e1 = e1 -> next) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
89 { |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
90 if (strcmp(key, e1 -> key) == 0) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
91 { |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
92 lw_free(e1 -> value); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
93 e1 -> value = lw_strdup(value); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
94 return; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
95 } |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
96 } |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
97 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
98 e1 = lw_alloc(sizeof(struct lw_dict_ent)); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
99 e1 -> next = S -> head; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
100 S -> head = e1; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
101 e1 -> key = lw_strdup(key); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
102 e1 -> value = lw_strdup(value); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
103 } |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
104 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
105 lw_dict_t lw_dict_copy(lw_dict_t S) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
106 { |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
107 lw_dict_t R; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
108 struct lw_dict_ent *e1; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
109 |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
110 R = lw_dict_create(); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
111 for (e1 = S -> head; e1; e1 = e1 -> next) |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
112 lw_dict_set(R, e1 -> key, e1 -> value); |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
113 return R; |
724bcc4508bc
Add SETSTR/INCLUDESTR for some basic code building
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
114 } |