1 | #include <string.h> |
---|
2 | #include "RpChainHelper.h" |
---|
3 | |
---|
4 | #ifdef __cplusplus |
---|
5 | extern "C" { |
---|
6 | #endif |
---|
7 | |
---|
8 | Rp_Chain * |
---|
9 | Rp_ChainJoin ( |
---|
10 | Rp_Chain *c1, |
---|
11 | Rp_Chain *c2) |
---|
12 | { |
---|
13 | Rp_ChainLink *l = NULL; |
---|
14 | |
---|
15 | if (c1 == NULL) { |
---|
16 | return c2; |
---|
17 | } |
---|
18 | |
---|
19 | if (c2 == NULL) { |
---|
20 | return c1; |
---|
21 | } |
---|
22 | |
---|
23 | l = Rp_ChainLastLink(c2); |
---|
24 | while (l != NULL) { |
---|
25 | Rp_ChainUnlinkLink(c2,l); |
---|
26 | Rp_ChainPrependLink(c1,l); |
---|
27 | l = Rp_ChainLastLink(c2); |
---|
28 | } |
---|
29 | |
---|
30 | if (Rp_ChainGetLength(c2) != 0) { |
---|
31 | // FIXME: add error message |
---|
32 | } |
---|
33 | |
---|
34 | Rp_ChainDestroy(c2); |
---|
35 | |
---|
36 | return c1; |
---|
37 | } |
---|
38 | |
---|
39 | Rp_Chain * |
---|
40 | Rp_ChainConcat ( |
---|
41 | Rp_Chain *c1, |
---|
42 | Rp_Chain *c2) |
---|
43 | { |
---|
44 | Rp_ChainLink *l = NULL; |
---|
45 | |
---|
46 | if (c1 == NULL) { |
---|
47 | return c2; |
---|
48 | } |
---|
49 | |
---|
50 | if (c2 == NULL) { |
---|
51 | return c1; |
---|
52 | } |
---|
53 | |
---|
54 | l = Rp_ChainFirstLink(c2); |
---|
55 | while (l != NULL) { |
---|
56 | Rp_ChainUnlinkLink(c2,l); |
---|
57 | Rp_ChainAppendLink(c1,l); |
---|
58 | l = Rp_ChainFirstLink(c2); |
---|
59 | } |
---|
60 | |
---|
61 | return c1; |
---|
62 | } |
---|
63 | |
---|
64 | Rp_Chain * |
---|
65 | Rp_ChainInsertChainAfter ( |
---|
66 | Rp_Chain *c1, |
---|
67 | Rp_Chain *c2, |
---|
68 | Rp_ChainLink *at) |
---|
69 | { |
---|
70 | Rp_ChainLink *l = NULL; |
---|
71 | |
---|
72 | if (c1 == NULL) { |
---|
73 | return c2; |
---|
74 | } |
---|
75 | |
---|
76 | if (c2 == NULL) { |
---|
77 | return c1; |
---|
78 | } |
---|
79 | |
---|
80 | l = Rp_ChainLastLink(c2); |
---|
81 | while (l != NULL) { |
---|
82 | Rp_ChainUnlinkLink(c2,l); |
---|
83 | Rp_ChainLinkAfter(c1,l,at); |
---|
84 | l = Rp_ChainLastLink(c2); |
---|
85 | } |
---|
86 | |
---|
87 | return c1; |
---|
88 | } |
---|
89 | |
---|
90 | Rp_Chain * |
---|
91 | Rp_ChainInsertChainBefore ( |
---|
92 | Rp_Chain *c1, |
---|
93 | Rp_Chain *c2, |
---|
94 | Rp_ChainLink *at) |
---|
95 | { |
---|
96 | Rp_ChainLink *l = NULL; |
---|
97 | |
---|
98 | if (c1 == NULL) { |
---|
99 | return c2; |
---|
100 | } |
---|
101 | |
---|
102 | if (c2 == NULL) { |
---|
103 | return c1; |
---|
104 | } |
---|
105 | |
---|
106 | l = Rp_ChainFirstLink(c2); |
---|
107 | while (l != NULL) { |
---|
108 | Rp_ChainUnlinkLink(c2,l); |
---|
109 | Rp_ChainLinkBefore(c1,l,at); |
---|
110 | l = Rp_ChainFirstLink(c2); |
---|
111 | } |
---|
112 | |
---|
113 | return c1; |
---|
114 | } |
---|
115 | |
---|
116 | int |
---|
117 | Rp_ChainCopy ( |
---|
118 | Rp_Chain *c1, |
---|
119 | Rp_Chain *c2, |
---|
120 | int (*cpyFxn)(void **to,void *from)) |
---|
121 | { |
---|
122 | void *origVal = NULL; |
---|
123 | void *copyVal = NULL; |
---|
124 | Rp_ChainLink *lorig = NULL; |
---|
125 | |
---|
126 | if ( ( (c1 == NULL) && (c2 == NULL) ) |
---|
127 | || (cpyFxn == NULL) ) { |
---|
128 | // raise error, improper function call |
---|
129 | return -1; |
---|
130 | } |
---|
131 | |
---|
132 | if (c1 == NULL) { |
---|
133 | // nothing to copy to |
---|
134 | return 0; |
---|
135 | } |
---|
136 | |
---|
137 | if (c2 == NULL) { |
---|
138 | // nothing to copy from |
---|
139 | return 0; |
---|
140 | } |
---|
141 | |
---|
142 | lorig = Rp_ChainFirstLink(c2); |
---|
143 | while (lorig != NULL) { |
---|
144 | origVal = (void*) Rp_ChainGetValue(lorig); |
---|
145 | if (cpyFxn(©Val,origVal) != 0) { |
---|
146 | // error while copying |
---|
147 | return -1; |
---|
148 | } |
---|
149 | Rp_ChainAppend(c1,copyVal); |
---|
150 | lorig = Rp_ChainNextLink(lorig); |
---|
151 | } |
---|
152 | |
---|
153 | return 0; |
---|
154 | } |
---|
155 | |
---|
156 | int |
---|
157 | Rp_ChainCharCpyFxn ( |
---|
158 | void **to, |
---|
159 | void *from) |
---|
160 | { |
---|
161 | size_t len = 0; |
---|
162 | |
---|
163 | len = strlen((char*)from); |
---|
164 | *to = (void*) malloc(len*sizeof(char)+1); |
---|
165 | strncpy((char*)(*to),(char*)from,len+1); |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | int |
---|
170 | Rp_ChainCharCmpFxn ( |
---|
171 | void *to, |
---|
172 | void *from) |
---|
173 | { |
---|
174 | return strcmp(to,from); |
---|
175 | } |
---|
176 | |
---|
177 | #ifdef __cplusplus |
---|
178 | } |
---|
179 | #endif |
---|
180 | |
---|