PIPS
args.c
Go to the documentation of this file.
1 /*
2 
3  $Id: args.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 /*
28  * there was some static buffer management here.
29  * now it relies on dynamic arrays.
30  */
31 #include <stdlib.h>
32 #include <stdio.h>
33 
34 #include "genC.h"
35 #include "misc.h"
36 
37 void
39 {
40  int index = 0;
41  MAP(STRING, s, gen_array_addto(a, index++, s), l);
42  gen_free_list(l);
43 }
44 
45 /* Just modify the strings in a list from an array of strings. The
46  array of string must have at least as much as strings as in the
47  list. No free is done. */
48 void
50 {
51  int index = 0;
52  MAPL(scons, STRING(CAR(scons)) = gen_array_item(a, index++), l);
53 }
54 
55 
56 /* Sort a list of strings.
57  */
58 void
60 {
61  size_t number_of_strings = gen_length(l);
62  gen_array_t a = gen_array_make(number_of_strings);
63  list_to_array(l, a);
64  pips_assert("same length", number_of_strings == gen_array_nitems(a));
65  gen_array_sort(a);
67  gen_array_free(a);
68 }
69 
70 
71 /* Return the malloc()ed version of the concatenation of all the strings in
72  the list */
73 string
75 {
76  string result = NULL;
77  if (l == NIL) return strdup("");
78  MAP(STRING,s, {
79  if (result == NULL)
80  result = strdup((const char *)s);
81  else {
82  string new_result = strdup(concatenate(result,s,NULL));
83  free(result);
84  result = new_result;
85  }
86  }, l);
87  return result;
88 }
void list_to_array(list l, gen_array_t a)
args.c
Definition: args.c:38
void update_list_from_array(list l, gen_array_t a)
Just modify the strings in a list from an array of strings.
Definition: args.c:49
void sort_list_of_strings(list l)
Sort a list of strings.
Definition: args.c:59
string list_to_string(list l)
Return the malloc()ed version of the concatenation of all the strings in the list.
Definition: args.c:74
size_t gen_array_nitems(const gen_array_t a)
Definition: array.c:131
gen_array_t gen_array_make(size_t size)
declarations...
Definition: array.c:40
void gen_array_addto(gen_array_t a, size_t i, void *what)
Definition: array.c:87
void gen_array_sort(gen_array_t a)
Definition: array.c:164
void * gen_array_item(const gen_array_t a, size_t i)
Definition: array.c:143
void gen_array_free(gen_array_t a)
Definition: array.c:70
#define STRING(x)
Definition: genC.h:87
void free(void *)
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
size_t gen_length(const list l)
Definition: list.c:150
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
#define MAPL(_map_list_cp, _code, _l)
Apply some code on the addresses of all the elements of a list.
Definition: newgen_list.h:203
#define MAP(_map_CASTER, _map_item, _map_code, _map_list)
Apply/map an instruction block on all the elements of a list (old fashioned)
Definition: newgen_list.h:226
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
char * strdup()
The structure used to build lists in NewGen.
Definition: newgen_list.h:41