ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/recipe.C
Revision: 1.18
Committed: Fri Feb 16 19:43:41 2007 UTC (17 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_0
Changes since 1.17: +25 -46 lines
Log Message:
- identified random memory corrutpion bug
- fixed most likely cause for bug above
- rewrote object loader etc. into a simple one-line lookahead
  parser.
- rewrote/cleaned up archetype, treasure, artifact, formula parser.
- some optimisations / cleanups

File Contents

# User Rev Content
1 elmex 1.1 /* Basic stuff for use with the alchemy code. Clearly some of this stuff
2     * could go into server/alchemy, but I left it here just in case it proves
3     * more generally useful.
4     *
5     * Nov 1995 - file created by b.t. thomas@astro.psu.edu
6     */
7    
8    
9     /* Our definition of 'formula' is any product of an alchemical process.
10     * Ingredients are just comma delimited list of archetype (or object)
11     * names.
12     */
13    
14     /* Example 'formula' entry in libdir/formulae:
15 root 1.18 * object transparency
16 elmex 1.1 * chance 10
17     * ingred dust of beholdereye,gem
18     * arch potion_generic
19     */
20    
21 root 1.11 #include <cctype>
22    
23 elmex 1.1 #include <global.h>
24     #include <object.h>
25    
26 root 1.5 static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
27 elmex 1.1
28     static recipelist *formulalist;
29    
30 root 1.5 static recipelist *
31     init_recipelist (void)
32     {
33 root 1.3 recipelist *tl = new recipelist;
34    
35 root 1.5 tl->total_chance = 0;
36     tl->number = 0;
37     tl->items = NULL;
38     tl->next = NULL;
39 elmex 1.1 return tl;
40     }
41    
42 root 1.5 static recipe *
43     get_empty_formula (void)
44     {
45 root 1.3 recipe *t = new recipe;
46    
47 elmex 1.1 t->chance = 0;
48     t->index = 0;
49     t->transmute = 0;
50 root 1.5 t->yield = 0;
51     t->diff = 0;
52     t->exp = 0;
53 elmex 1.1 t->keycode = 0;
54     t->title = NULL;
55     t->arch_names = 0;
56     t->arch_name = NULL;
57     t->skill = NULL;
58     t->cauldron = NULL;
59     t->ingred = NULL;
60 root 1.5 t->next = NULL;
61 elmex 1.1 return t;
62     }
63 root 1.5
64 elmex 1.1 /* get_formulalist() - returns pointer to the formula list */
65 root 1.5 recipelist *
66     get_formulalist (int i)
67     {
68     recipelist *fl = formulalist;
69     int number = i;
70    
71     while (fl && number > 1)
72     {
73     if (!(fl = fl->next))
74     break;
75     number--;
76     }
77 root 1.18
78 elmex 1.1 return fl;
79     }
80    
81     /* check_recipe() - makes sure we actually have the requested artifact
82     * and archetype. */
83 root 1.5 static int
84     check_recipe (const recipe *rp)
85     {
86     size_t i;
87 root 1.18 int result = 1;
88 root 1.5
89     for (i = 0; i < rp->arch_names; i++)
90     {
91 root 1.6 if (archetype::find (rp->arch_name[i]) != NULL)
92 root 1.5 {
93     artifact *art = locate_recipe_artifact (rp, i);
94    
95     if (!art && strcmp (rp->title, "NONE") != 0)
96     {
97 pippijn 1.12 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
98 root 1.5 result = 0;
99 elmex 1.1 }
100 root 1.5 }
101     else
102     {
103 pippijn 1.12 LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title);
104 root 1.5 result = 0;
105 elmex 1.1 }
106     }
107 root 1.5
108     return result;
109 elmex 1.1 }
110    
111     /*
112     * init_formulae() - Builds up the lists of formula from the file in
113     * the libdir. -b.t.
114     */
115 root 1.5 void
116     init_formulae (void)
117     {
118     static int has_been_done = 0;
119 elmex 1.1 FILE *fp;
120     char filename[MAX_BUF], buf[MAX_BUF], *cp, *next;
121 root 1.5 recipe *formula = NULL;
122     recipelist *fl = init_recipelist ();
123 elmex 1.1 linked_char *tmp;
124     int value, comp;
125    
126 root 1.5 if (!formulalist)
127     formulalist = fl;
128    
129     if (has_been_done)
130 elmex 1.1 return;
131 root 1.5 else
132     has_been_done = 1;
133    
134     sprintf (filename, "%s/formulae", settings.datadir);
135 pippijn 1.12 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
136 root 1.5 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
137     {
138     LOG (llevError, "Can't open %s.\n", filename);
139     return;
140     }
141    
142     while (fgets (buf, MAX_BUF, fp) != NULL)
143     {
144     if (*buf == '#')
145     continue;
146     if ((cp = strchr (buf, '\n')) != NULL)
147     *cp = '\0';
148     cp = buf;
149     while (*cp == ' ') /* Skip blanks */
150     cp++;
151    
152 root 1.18 if (!strncmp (cp, "object", 6))
153 root 1.5 {
154     formula = get_empty_formula ();
155     formula->title = strchr (cp, ' ') + 1;
156     }
157     else if (!strncmp (cp, "keycode", 7))
158 root 1.18 formula->keycode = strchr (cp, ' ') + 1;
159 root 1.5 else if (sscanf (cp, "trans %d", &value))
160 root 1.18 formula->transmute = (uint16) value;
161 root 1.5 else if (sscanf (cp, "yield %d", &value))
162 root 1.18 formula->yield = (uint16) value;
163 root 1.5 else if (sscanf (cp, "chance %d", &value))
164 root 1.18 formula->chance = (uint16) value;
165 root 1.5 else if (sscanf (cp, "exp %d", &value))
166 root 1.18 formula->exp = (uint16) value;
167 root 1.5 else if (sscanf (cp, "diff %d", &value))
168 root 1.18 formula->diff = (uint16) value;
169 root 1.5 else if (!strncmp (cp, "ingred", 6))
170     {
171     int numb_ingred = 1;
172    
173     cp = strchr (cp, ' ') + 1;
174     do
175     {
176     if ((next = strchr (cp, ',')) != NULL)
177     {
178     *(next++) = '\0';
179     numb_ingred++;
180     }
181 root 1.18
182 root 1.5 tmp = new linked_char;
183    
184     tmp->name = cp;
185     tmp->next = formula->ingred;
186     formula->ingred = tmp;
187     /* each ingredient's ASCII value is coadded. Later on this
188     * value will be used allow us to search the formula lists
189     * quickly for the right recipe.
190     */
191     formula->index += strtoint (cp);
192     }
193     while ((cp = next) != NULL);
194 root 1.18
195 root 1.5 /* now find the correct (# of ingred ordered) formulalist */
196     fl = formulalist;
197     while (numb_ingred != 1)
198     {
199     if (!fl->next)
200     fl->next = init_recipelist ();
201 root 1.18
202 root 1.5 fl = fl->next;
203     numb_ingred--;
204     }
205 root 1.18
206 root 1.5 fl->total_chance += formula->chance;
207     fl->number++;
208     formula->next = fl->items;
209     fl->items = formula;
210     }
211     else if (!strncmp (cp, "arch", 4))
212     {
213     build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names);
214     check_recipe (formula);
215     }
216     else if (!strncmp (cp, "skill", 5))
217 root 1.18 formula->skill = strchr (cp, ' ') + 1;
218 root 1.5 else if (!strncmp (cp, "cauldron", 8))
219 root 1.18 formula->cauldron = strchr (cp, ' ') + 1;
220 root 1.5 else
221     LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
222     }
223 root 1.18
224 root 1.5 LOG (llevDebug, "done.\n");
225     close_and_delete (fp, comp);
226 elmex 1.1 /* Lastly, lets check for problems in formula we got */
227 root 1.5 check_formulae ();
228 elmex 1.1 }
229    
230     /* check_formulae()- since we are doing a squential search on the
231     * formulae lists now, we have to be carefull that we dont have 2
232     * formula with the exact same index value. Under the new nbatches
233     * code, it is possible to have multiples of ingredients in a cauldron
234     * which could result in an index formula mismatch. We *don't* check for
235     * that possibility here. -b.t.
236     */
237 root 1.5 void
238     check_formulae (void)
239     {
240 elmex 1.1 recipelist *fl;
241     recipe *check, *formula;
242     int numb = 1;
243    
244 pippijn 1.13 LOG (llevDebug, "Checking formulae lists...\n");
245 elmex 1.1
246 root 1.5 for (fl = formulalist; fl != NULL; fl = fl->next)
247     {
248     for (formula = fl->items; formula != NULL; formula = formula->next)
249     for (check = formula->next; check != NULL; check = check->next)
250     if (check->index == formula->index)
251     {
252     LOG (llevError, " ERROR: On %d ingred list: ", numb);
253     LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n",
254     formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index);
255     }
256     numb++;
257     }
258 elmex 1.1
259 root 1.5 LOG (llevDebug, "done.\n");
260 elmex 1.1
261     }
262    
263     /* Borrowed (again) from the artifacts code for this */
264    
265 root 1.5 void
266     dump_alchemy (void)
267     {
268     recipelist *fl = formulalist;
269     recipe *formula = NULL;
270 elmex 1.1 linked_char *next;
271 root 1.5 int num_ingred = 1;
272 elmex 1.1
273 root 1.5 fprintf (logfile, "\n");
274     while (fl)
275     {
276     fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
277     num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
278     for (formula = fl->items; formula != NULL; formula = formula->next)
279     {
280     artifact *art = NULL;
281     char buf[MAX_BUF];
282     size_t i;
283    
284     for (i = 0; i < formula->arch_names; i++)
285     {
286     const char *string = formula->arch_name[i];
287    
288 root 1.6 if (archetype::find (string) != NULL)
289 root 1.5 {
290     art = locate_recipe_artifact (formula, i);
291     if (!art && strcmp (formula->title, "NONE"))
292 root 1.18 LOG (llevError, "Formula %s has no artifact!\n", &formula->title);
293 root 1.5 else
294     {
295     if (strcmp (formula->title, "NONE"))
296     sprintf (buf, "%s of %s", string, &formula->title);
297     else
298     sprintf (buf, "%s", string);
299     fprintf (logfile, "%-30s(%d) bookchance %3d ", buf, formula->index, formula->chance);
300     fprintf (logfile, "skill %s", &formula->skill);
301     fprintf (logfile, "\n");
302     if (formula->ingred != NULL)
303     {
304     int nval = 0, tval = 0;
305    
306     fprintf (logfile, "\tIngred: ");
307     for (next = formula->ingred; next != NULL; next = next->next)
308     {
309     if (nval != 0)
310     fprintf (logfile, ",");
311     fprintf (logfile, "%s(%d)", &next->name, (nval = strtoint (next->name)));
312     tval += nval;
313     }
314     fprintf (logfile, "\n");
315     if (tval != formula->index)
316     fprintf (logfile, "WARNING:ingredient list and formula values not equal.\n");
317     }
318     if (formula->skill != NULL)
319     fprintf (logfile, "\tSkill Required: %s", &formula->skill);
320     if (formula->cauldron != NULL)
321     fprintf (logfile, "\tCauldron: %s\n", &formula->cauldron);
322     fprintf (logfile, "\tDifficulty: %d\t Exp: %d\n", formula->diff, formula->exp);
323     }
324 root 1.2 }
325 root 1.5 else
326     LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title);
327     }
328     }
329     fprintf (logfile, "\n");
330     fl = fl->next;
331     num_ingred++;
332     }
333 elmex 1.1 }
334    
335     /* Find a treasure with a matching name. The 'depth' parameter is
336     * only there to prevent infinite loops in treasure lists (a list
337     * referencing another list pointing back to the first one). */
338 root 1.5 archetype *
339     find_treasure_by_name (const treasure *t, const char *name, int depth)
340 elmex 1.1 {
341     treasurelist *tl;
342 root 1.5 archetype *at;
343 elmex 1.1
344     if (depth > 10)
345 elmex 1.10 return 0;
346    
347     while (t)
348 elmex 1.1 {
349 elmex 1.10 if (t->name)
350 root 1.2 {
351     tl = find_treasurelist (t->name);
352 elmex 1.10
353     if (tl)
354     {
355     at = find_treasure_by_name (tl->items, name, depth + 1);
356    
357     if (at)
358     return at;
359     }
360 root 1.2 }
361 elmex 1.1 else
362 root 1.2 {
363 elmex 1.14 if (t->item && !strcasecmp (t->item->clone.name, name))
364 root 1.2 return t->item;
365     }
366 elmex 1.10
367     if (t->next_yes)
368 root 1.2 {
369     at = find_treasure_by_name (t->next_yes, name, depth);
370 elmex 1.10 if (at)
371 root 1.2 return at;
372     }
373 elmex 1.10
374     if (t->next_no)
375 root 1.2 {
376     at = find_treasure_by_name (t->next_no, name, depth);
377 elmex 1.10 if (at)
378 root 1.2 return at;
379     }
380 elmex 1.1 t = t->next;
381     }
382 elmex 1.10 return 0;
383 elmex 1.1 }
384    
385     /* If several archetypes have the same name, the value of the first
386     * one with that name will be returned. This happens for the
387     * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the
388     * monsters' body parts, there may be several monsters with the same
389     * name. This is not a problem if these monsters have the same level
390     * (e.g. sage & c_sage) or if only one of the monsters generates the
391     * body parts that we are looking for (e.g. big_dragon and
392     * big_dragon_worthless). */
393 root 1.5 long
394     find_ingred_cost (const char *name)
395 elmex 1.1 {
396 root 1.5 archetype *at;
397     archetype *at2;
398 elmex 1.1 artifactlist *al;
399 root 1.5 artifact *art;
400     long mult;
401     char *cp;
402     char part1[100];
403     char part2[100];
404 elmex 1.1
405     /* same as atoi(), but skip number */
406     mult = 0;
407     while (isdigit (*name))
408     {
409     mult = 10 * mult + (*name - '0');
410     name++;
411     }
412 root 1.16
413 elmex 1.1 if (mult > 0)
414     name++;
415     else
416     mult = 1;
417 root 1.16
418 elmex 1.1 /* first, try to match the name of an archetype */
419 root 1.5 for (at = first_archetype; at != NULL; at = at->next)
420 elmex 1.1 {
421     if (at->clone.title != NULL)
422 root 1.2 {
423     /* inefficient, but who cares? */
424 root 1.3 sprintf (part1, "%s %s", &at->clone.name, &at->clone.title);
425 root 1.5 if (!strcasecmp (part1, name))
426 root 1.2 return mult * at->clone.value;
427     }
428 root 1.5 if (!strcasecmp (at->clone.name, name))
429 root 1.2 return mult * at->clone.value;
430 elmex 1.1 }
431 root 1.16
432 elmex 1.1 /* second, try to match an artifact ("arch of something") */
433     cp = strstr (name, " of ");
434     if (cp != NULL)
435     {
436     strcpy (part1, name);
437     part1[cp - name] = '\0';
438     strcpy (part2, cp + 4);
439     /* find the first archetype matching the first part of the name */
440 root 1.5 for (at = first_archetype; at != NULL; at = at->next)
441     if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
442 root 1.2 break;
443 elmex 1.1 if (at != NULL)
444 root 1.2 {
445     /* find the first artifact derived from that archetype (same type) */
446     for (al = first_artifactlist; al != NULL; al = al->next)
447     if (al->type == at->clone.type)
448     {
449     for (art = al->items; art != NULL; art = art->next)
450 root 1.5 if (!strcasecmp (art->item->name, part2))
451 root 1.2 return mult * at->clone.value * art->item->value;
452     }
453     }
454 elmex 1.1 }
455 root 1.16
456 elmex 1.1 /* third, try to match a body part ("arch's something") */
457     cp = strstr (name, "'s ");
458 root 1.16 if (cp)
459 elmex 1.1 {
460     strcpy (part1, name);
461     part1[cp - name] = '\0';
462     strcpy (part2, cp + 3);
463     /* examine all archetypes matching the first part of the name */
464 root 1.5 for (at = first_archetype; at != NULL; at = at->next)
465     if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
466 root 1.2 {
467     if (at->clone.randomitems != NULL)
468     {
469 root 1.5 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0);
470 elmex 1.10 if (at2)
471 root 1.2 return mult * at2->clone.value * isqrt (at->clone.level * 2);
472     }
473     }
474 elmex 1.1 }
475 root 1.16
476 elmex 1.1 /* failed to find any matching items -- formula should be checked */
477     return -1;
478     }
479    
480     /* code copied from dump_alchemy() and modified by Raphael Quinet */
481 root 1.5 void
482     dump_alchemy_costs (void)
483 elmex 1.1 {
484 root 1.5 recipelist *fl = formulalist;
485     recipe *formula = NULL;
486 elmex 1.1 linked_char *next;
487 root 1.5 int num_ingred = 1;
488     int num_errors = 0;
489 elmex 1.1 long cost;
490     long tcost;
491    
492     fprintf (logfile, "\n");
493 root 1.5 while (fl)
494     {
495     fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
496     num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
497     for (formula = fl->items; formula != NULL; formula = formula->next)
498     {
499     artifact *art = NULL;
500     archetype *at = NULL;
501     char buf[MAX_BUF];
502     size_t i;
503    
504     for (i = 0; i < formula->arch_names; i++)
505 root 1.2 {
506 root 1.5 const char *string = formula->arch_name[i];
507    
508 root 1.6 if ((at = archetype::find (string)) != NULL)
509 root 1.2 {
510 root 1.5 art = locate_recipe_artifact (formula, i);
511     if (!art && strcmp (formula->title, "NONE"))
512     LOG (llevError, "Formula %s has no artifact\n", &formula->title);
513 root 1.2 else
514     {
515 root 1.5 if (!strcmp (formula->title, "NONE"))
516     sprintf (buf, "%s", string);
517     else
518     sprintf (buf, "%s of %s", string, &formula->title);
519     fprintf (logfile, "\n%-40s bookchance %3d skill %s\n", buf, formula->chance, &(formula->skill));
520     if (formula->ingred != NULL)
521     {
522     tcost = 0;
523     for (next = formula->ingred; next != NULL; next = next->next)
524     {
525     cost = find_ingred_cost (next->name);
526     if (cost < 0)
527     num_errors++;
528     fprintf (logfile, "\t%-33s%5ld\n", &next->name, cost);
529     if (cost < 0 || tcost < 0)
530     tcost = -1;
531     else
532     tcost += cost;
533     }
534     if (art != NULL && art->item != NULL)
535     cost = at->clone.value * art->item->value;
536     else
537     cost = at->clone.value;
538     fprintf (logfile, "\t\tBuying result costs: %5ld", cost);
539     if (formula->yield > 1)
540     {
541     fprintf (logfile, " to %ld (max %d items)\n", cost * formula->yield, formula->yield);
542     cost = cost * (formula->yield + 1L) / 2L;
543     }
544     else
545     fprintf (logfile, "\n");
546     fprintf (logfile, "\t\tIngredients cost: %5ld\n\t\tComment: ", tcost);
547     if (tcost < 0)
548     fprintf (logfile, "Could not find some ingredients. Check the formula!\n");
549     else if (tcost > cost)
550     fprintf (logfile, "Ingredients are much too expensive. Useless formula.\n");
551     else if (tcost * 2L > cost)
552     fprintf (logfile, "Ingredients are too expensive.\n");
553     else if (tcost * 10L < cost)
554     fprintf (logfile, "Ingredients are too cheap.\n");
555     else
556     fprintf (logfile, "OK.\n");
557     }
558 root 1.2 }
559     }
560 root 1.5 else
561     LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title);
562 root 1.2 }
563     }
564 root 1.5 fprintf (logfile, "\n");
565     fl = fl->next;
566     num_ingred++;
567     }
568 elmex 1.1 if (num_errors > 0)
569 root 1.5 fprintf (logfile, "WARNING: %d objects required by the formulae do not exist in the game.\n", num_errors);
570 elmex 1.1 }
571    
572 root 1.5 const char *
573     ingred_name (const char *name)
574     {
575     const char *cp = name;
576    
577     if (atoi (cp))
578     cp = strchr (cp, ' ') + 1;
579 elmex 1.1 return cp;
580     }
581    
582     /* strtoint() - we use this to convert buf into an integer
583     * equal to the coadded sum of the (lowercase) character
584     * ASCII values in buf (times prepended integers).
585     */
586    
587 root 1.5 int
588     strtoint (const char *buf)
589     {
590     const char *cp = ingred_name (buf);
591     int val = 0, len = strlen (cp), mult = numb_ingred (buf);
592 elmex 1.1
593 root 1.5 while (len)
594     {
595     val += tolower (*cp);
596     cp++;
597     len--;
598     }
599     return val * mult;
600 elmex 1.1 }
601    
602 root 1.5 artifact *
603     locate_recipe_artifact (const recipe *rp, size_t idx)
604     {
605 root 1.18 archetype *at = archetype::find (rp->arch_name [idx]);
606 elmex 1.1
607 root 1.18 if (at)
608     if (artifactlist *al = find_artifactlist (at->clone.type))
609     for (artifact *art = al->items; art; art = art->next)
610     if (art->item->name == rp->title)
611     return art;
612 elmex 1.1
613 root 1.18 return 0;
614 elmex 1.1 }
615    
616 root 1.5 int
617     numb_ingred (const char *buf)
618     {
619 elmex 1.1 int numb;
620    
621 root 1.5 if ((numb = atoi (buf)))
622     return numb;
623     else
624     return 1;
625     }
626    
627     recipelist *
628     get_random_recipelist (void)
629     {
630     recipelist *fl = NULL;
631     int number = 0, roll = 0;
632    
633     /* first, determine # of recipelist we have */
634     for (fl = get_formulalist (1); fl; fl = fl->next)
635     number++;
636    
637     /* now, randomly choose one */
638     if (number > 0)
639 root 1.15 roll = rndm (number);
640 root 1.5
641     fl = get_formulalist (1);
642     while (roll && fl)
643     {
644     if (fl->next)
645     fl = fl->next;
646     else
647     break;
648     roll--;
649     }
650     if (!fl) /* failed! */
651     LOG (llevError, "get_random_recipelist(): no recipelists found!\n");
652     else if (fl->total_chance == 0)
653     fl = get_random_recipelist ();
654    
655     return fl;
656 elmex 1.1 }
657    
658 root 1.5 recipe *
659     get_random_recipe (recipelist * rpl)
660     {
661     recipelist *fl = rpl;
662     recipe *rp = NULL;
663     int r = 0;
664    
665     /* looks like we have to choose a random one */
666     if (fl == NULL)
667     if ((fl = get_random_recipelist ()) == NULL)
668     return rp;
669    
670     if (fl->total_chance > 0)
671     {
672 root 1.15 r = rndm (fl->total_chance);
673 root 1.5 for (rp = fl->items; rp; rp = rp->next)
674     {
675     r -= rp->chance;
676     if (r < 0)
677     break;
678     }
679 elmex 1.1 }
680     return rp;
681     }
682    
683 root 1.5 void
684     free_all_recipes (void)
685 elmex 1.1 {
686 root 1.5 recipelist *fl = formulalist, *flnext;
687     recipe *formula = NULL, *next;
688     linked_char *lchar, *charnext;
689    
690     LOG (llevDebug, "Freeing all the recipes\n");
691     for (fl = formulalist; fl != NULL; fl = flnext)
692     {
693     flnext = fl->next;
694    
695     for (formula = fl->items; formula != NULL; formula = next)
696     {
697     next = formula->next;
698    
699     free (formula->arch_name[0]);
700     free (formula->arch_name);
701    
702     for (lchar = formula->ingred; lchar; lchar = charnext)
703     {
704     charnext = lchar->next;
705     delete lchar;
706 root 1.2 }
707 root 1.5 delete formula;
708 root 1.2 }
709 root 1.3
710 root 1.5 delete fl;
711 elmex 1.1 }
712     }
713    
714     /**
715     * Split a comma separated string list into words.
716     *
717     * @param str the string to split
718     *
719     * @param result_list pointer to return value for the newly created list; the
720     * caller is responsible for freeing both *result_list and **result_list.
721     *
722     * @param result_size pointer to return value for the size of the newly
723     * created list
724     */
725 root 1.5 static void
726     build_stringlist (const char *str, char ***result_list, size_t * result_size)
727 elmex 1.1 {
728 root 1.5 char *dup;
729     char *p;
730     size_t size;
731     size_t i;
732 elmex 1.1
733 root 1.11 dup = strdup (str);
734 root 1.5 if (dup == NULL)
735     fatal (OUT_OF_MEMORY);
736 elmex 1.1
737 root 1.5 size = 0;
738     for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
739     size++;
740 elmex 1.1
741 root 1.5 *result_list = (char **) malloc (size * sizeof (*result_list));
742 root 1.3
743 root 1.5 *result_size = size;
744 elmex 1.1
745 root 1.5 for (i = 0; i < size; i++)
746     {
747     (*result_list)[i] = dup;
748     dup = dup + strlen (dup) + 1;
749 elmex 1.1 }
750     }