~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/sys/lists/strings.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /** @(#)Tstring handling (Tstring is generic list).
  2  * @(#)Tstring is {int type; List *start; List *end; int count; Listprops;}
  3  * @(#)Tstring represents and manipulates connected data (eg derived from
  4  * @(#)edges) as a whole. Tstring handling functions are prefixed str. */
  5 
  6 #include <stdio.h>
  7 #include <tina/sys.h>
  8 #include <tina/sysfuncs.h>
  9 
 10 List *proplist_copy();
 11 List *dd_list_make();
 12 List *dd_get_end();
 13 
 14 Tstring *str_alloc(void)
 15 {
 16     /* allocate string structure and initialise pointers to nil */
 17     Tstring *str = ts_ralloc(Tstring);
 18     str->count = str->type = 0;
 19     str->start = str->end = (List *) NULL;
 20     str->props = NULL;
 21     return (str);
 22 }
 23 
 24 Tstring *str_make(int type, List * start, List * end)
 25 {
 26     Tstring *str = ts_ralloc(Tstring);
 27     str->type = type;
 28     str->start = start;
 29     str->end = end;
 30     if (type == LOOP)
 31     {
 32         start->last = end;
 33         end->next = start;
 34     }
 35     str->count = ddstr_count(start, end);
 36     str->props = NULL;
 37     return (str);
 38 }
 39 
 40 List *str_mid_point(Tstring * str)
 41 {
 42     List *ddstr_nth_point();
 43     if (str == NULL)
 44         return (NULL);
 45     return (ddstr_nth_point(str->start, str->end, str->count / 2));
 46 }
 47 
 48 /* copy of n evenly spaced points along a string */
 49 
 50 Tstring *str_divide(Tstring * str, int n)
 51 {
 52     Tstring *new = str_alloc();
 53     List *ddnew = (List *) dd_list_make(n, (int) NULL);
 54     List *dd1, *dd2;
 55     int i, j, l;
 56     double dl;
 57 
 58     new->type = str->type;
 59     new->start = ddnew;
 60     new->end = dd_get_end(ddnew);
 61     new->count = n;
 62 
 63     l = str->count;
 64     switch (str->type)
 65     {
 66       case STRING:
 67         dl = (l - 1) / (n - 1);
 68         break;
 69       case LOOP:
 70         dl = (l - 1) / n;
 71         new->end->next = new->start;
 72         new->start->last = new->end;
 73         break;
 74     }
 75 
 76     for (i = 0, j = 0, dd1 = str->start, dd2 = ddnew;
 77          j < n;
 78          i++, dd1 = dd1->next, dd2 = dd2->next)
 79     {
 80         if (i >= j * dl || (str->type == STRING && dd1 == str->end))
 81         {
 82             dd2->type = dd1->type;
 83             dd2->to = dd1->to;
 84             j++;
 85         }
 86     }
 87 
 88     return (new);
 89 }
 90 
 91 void str_free(Tstring * str, void (*freefunc) ( /* ??? */ ))
 92 {
 93     void ddstr_free();
 94 
 95     if (str == NULL)
 96         return;
 97 
 98     proplist_freelist(str->props);
 99     ddstr_free(str->start, str->end, freefunc);
100     rfree((void *) str);
101 }
102 
103 void str_rm_links(Tstring * str)
104 {
105     void ddstr_rm_links();
106 
107     if (str == NULL)
108         return;
109 
110     proplist_freelist(str->props);
111     ddstr_rm_links(str->start, str->end);
112     rfree((void *) str);
113 }
114 
115 void str_rm(Tstring * str, void (*freefunc) ( /* ??? */ ))
116 {
117     void ddstr_rm();
118 
119     if (str == NULL)
120         return;
121 
122     proplist_freelist(str->props);
123     ddstr_rm(str->start, str->end, freefunc);
124     rfree((void *) str);
125 }
126 
127 void str_rm_only_str(Tstring * str)
128 {
129     if (str == NULL)
130         return;
131 
132     proplist_freelist(str->props);
133     rfree((void *) str);
134 }
135 
136 Tstring *str_segment(Tstring * str, List * at)
137 {
138     List *start;
139     List *end;
140 
141     if (str == NULL)
142         return (NULL);
143 
144     start = str->start;
145     end = str->end;
146 
147     if (at == start || at == end)
148         return (NULL);
149 
150     str->end = at;
151     str->count = ddstr_count(start, at);
152     return (str_make(str->type, at->next, end));
153 }
154 
155 List *str_link_get_by_ref(Tstring * str, void *ptr)
156 {
157     List *ddstr_link_get_by_ref();
158 
159     if (str == NULL)
160         return (NULL);
161 
162     return (ddstr_link_get_by_ref(str->start, str->end, ptr));
163 }
164 
165 Tstring *str_list_get_by_ref(List * strings, void *ptr)
166 {
167     List *lptr;
168 
169     for (lptr = strings; lptr != NULL; lptr = lptr->next)
170         if (str_link_get_by_ref((Tstring *) lptr->to, ptr) != NULL)
171             return ((Tstring *) lptr->to);
172     return (NULL);
173 }
174 
175 void str_reverse(Tstring * str)
176 {
177     void ddstr_reverse();
178     if (str == NULL)
179         return;
180     ddstr_reverse(&(str->start), &(str->end));
181     if(str->type == LOOP)
182     {
183         str->start->last = str->end;
184         str->end->next = str->start;
185     }
186 }
187 
188 Tstring *str_combine(Tstring * s1, Tstring * s2)
189 {
190     if (s1 == NULL || s2 == NULL)
191         return (NULL);
192 
193     s1->end->next = s2->start;
194     s2->start->last = s1->end;
195     return (str_make(s1->type, s1->start, s2->end));
196 }
197 
198 Tstring *str_copy(Tstring * str, void *(*copyfunc) ( /* ??? */ ), void *data)
199 {
200     Tstring *copy;
201     List *start;
202     List *end;
203     void ddstr_copy();
204 
205     if (str == NULL)
206         return (NULL);
207 
208     start = str->start;
209     end = str->end;
210     ddstr_copy(&start, &end, copyfunc, data);
211     copy = str_make(str->type, start, end);
212     copy->props = proplist_copy(str->props);
213     return (copy);
214 }
215 
216 Tstring *str_clone(Tstring * str)       /* new string pointing at old
217                                          * data */
218 
219 {
220     Tstring *copy;
221     List *start;
222     List *end;
223 
224     if (str == NULL)
225         return (NULL);
226 
227     start = str->start;
228     end = str->end;
229 
230     ddstr_copy(&start, &end, (void *(*) ()) NULL, NULL);
231     if (str->start->last == str->end)
232     {
233         start->last = end;
234         end->next = start;
235     }
236     copy = str_make(str->type, start, end);
237     copy->props = proplist_copy(str->props);
238     return (copy);
239 }
240 
241 void str_apply_func(Tstring * str, void (*func) ( /* ??? */ ), void *data)
242 {
243     void ddstr_apply_func();
244 
245     if (str == NULL)
246         return;
247 
248     ddstr_apply_func(str->start, str->end, func, data);
249 }
250 
251 List *str_get_min(Tstring * str, Bool(*func) ( /* ??? */ ), void *data)
252 {
253     List *ddstr_get_min();
254     List *ddlist = NULL;
255 
256     if (str)
257     {
258         ddlist = ddstr_get_min(str->start, str->end, func, data);
259     }
260     return ddlist;
261 }
262 
263 Bool str_get_min_dist(Tstring * str, Bool(*func) ( /* ??? */ ), void *data, double *d)
264 {
265     List *min_ptr = str_get_min(str, func, data);
266 
267     return (func(min_ptr->to, min_ptr->type, data, d));
268 }
269 
270 Tstring *str_list_get_min(List * strings, Bool(*func) ( /* ??? */ ), void *data)
271 {
272     List *ptr;
273     Tstring *min_str = NULL;
274     double d, min_d = 0.0;
275 
276     for (ptr = strings; ptr != NULL; ptr = ptr->next)
277     {
278         if (str_get_min_dist((Tstring *) ptr->to, func, data, &d) == false)
279             continue;
280 
281         if (min_str == NULL || d < min_d)
282         {
283             min_str = ptr->to;
284             min_d = d;
285         }
286     }
287     return (min_str);
288 }
289 
290 void str_rm_list(List * strings)
291 {
292     list_rm(strings, str_rm_links);
293 }
294 
295 int str_length(Tstring * str)
296 {
297     int i;
298     List *dd;
299 
300     if (str == NULL)
301         return (-1);
302 
303     for (dd = str->start, i = 1;; dd = dd->next, i++)
304         if (dd == str->end)
305             break;
306 
307     return (i);
308 }
309 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.