]>
Commit | Line | Data |
---|---|---|
885824d3 | 1 | # include <stdlib.h> |
2 | ||
3 | # include "erc.h" | |
4 | ||
5 | static size_t int_toSize (int x) /*@*/ | |
6 | { | |
7 | size_t res = (size_t) x; | |
8 | ||
9 | if ((int) res != x) | |
10 | { | |
11 | fprintf (stderr, "Error: int_toSize failure: %d", x); | |
12 | return 0; | |
13 | } | |
14 | else | |
15 | { | |
16 | return res; | |
17 | } | |
18 | } | |
19 | ||
20 | erc erc_create (void) | |
21 | { | |
22 | erc c; | |
23 | ||
24 | c = (erc) malloc (sizeof (*c)); | |
25 | ||
26 | if (c == 0) | |
27 | { | |
28 | printf ("Malloc returned null in erc_create\n"); | |
29 | exit (EXIT_FAILURE); | |
30 | } | |
31 | ||
32 | c->vals = 0; | |
33 | c->size = 0; | |
34 | return c; | |
35 | } | |
36 | ||
37 | void erc_clear (erc c) | |
38 | { | |
39 | ercList elem; | |
40 | ercList next; | |
41 | ||
42 | for (elem = c->vals; elem != 0; elem = next) | |
43 | { | |
44 | next = elem->next; | |
45 | free (elem); | |
46 | } | |
47 | ||
48 | c->vals = 0; | |
49 | c->size = 0; | |
50 | } | |
51 | ||
52 | void erc_final (erc c) | |
53 | { | |
54 | erc_clear (c); | |
55 | free (c); | |
56 | } | |
57 | ||
58 | bool erc_member (eref er, erc c) | |
59 | { | |
60 | ercList tmpc; | |
61 | ||
62 | for (tmpc = c->vals; tmpc != 0; tmpc = tmpc->next) | |
63 | { | |
64 | if (eref_equal (tmpc->val, er)) | |
65 | { | |
66 | return TRUE; | |
67 | } | |
68 | } | |
69 | ||
70 | return FALSE; | |
71 | } | |
72 | ||
73 | void erc_insert (erc c, eref er) | |
74 | { | |
75 | ercList newElem; | |
76 | newElem = (ercElem *) malloc (sizeof (*newElem)); | |
77 | ||
78 | if (newElem == 0) | |
79 | { | |
80 | printf ("Malloc returned null in erc_insert\n"); | |
81 | exit (EXIT_FAILURE); | |
82 | } | |
83 | ||
84 | newElem->val = er; | |
85 | newElem->next = c->vals; | |
86 | c->vals = newElem; | |
87 | c->size++; | |
88 | } | |
89 | ||
90 | bool erc_delete (erc c, eref er) | |
91 | { | |
92 | ercList elem = c->vals; | |
93 | ||
94 | if (elem != NULL) | |
95 | { | |
96 | if (eref_equal (elem->val, er)) | |
97 | { | |
98 | c->vals = elem->next; | |
99 | free (elem); | |
100 | c->size--; | |
101 | return TRUE; | |
102 | } | |
103 | else | |
104 | { | |
105 | ercList prev = elem; | |
106 | elem = elem->next; | |
107 | ||
108 | for (; elem != 0; prev = elem, elem = elem->next) | |
109 | { | |
110 | if (eref_equal (elem->val, er)) | |
111 | { | |
112 | assert (prev != NULL); | |
113 | prev->next = elem->next; | |
114 | free (elem); | |
115 | c->size--; | |
116 | return TRUE; | |
117 | } | |
118 | } | |
119 | } | |
120 | } | |
121 | ||
122 | return FALSE; | |
123 | } | |
124 | ||
125 | void erc_join (erc c1, erc c2) | |
126 | { | |
127 | ercList tmpc; | |
128 | ||
129 | for (tmpc = c2->vals; tmpc != 0; tmpc = tmpc->next) | |
130 | { | |
131 | erc_insert (c1, tmpc->val); | |
132 | } | |
133 | } | |
134 | ||
135 | char *erc_sprint (erc c) | |
136 | { | |
137 | int len; | |
138 | char *result; | |
139 | ||
140 | result = (char *) | |
141 | malloc (int_toSize (erc_size (c) * (employeePrintSize + 1) + 1)); | |
142 | ||
143 | if (result == 0) | |
144 | { | |
145 | printf ("Malloc returned null in erc_sprint\n"); | |
146 | exit (EXIT_FAILURE); | |
147 | } | |
148 | ||
149 | len = 0; | |
150 | ||
151 | erc_elements(c, er) | |
152 | { | |
153 | employee_sprint(&(result[len]), eref_get(er)); | |
154 | len += employeePrintSize; | |
155 | result[len++] = '\n'; | |
156 | } end_erc_elements; | |
157 | ||
158 | result[len] = '\0'; | |
159 | return result; | |
160 | } | |
161 | ||
162 | eref erc_choose (erc c) | |
163 | { | |
164 | assert (c->vals != NULL); | |
165 | return c->vals->val; | |
166 | } |