Debian Codegraph @

raster/r.li/r.li.cwed/utility.c

raw
1 /*
2 * \AUTHOR: Serena Pallecchi student of Computer Science University of Pisa
3 * (Italy) Commission from Faunalia Pontedera (PI) www.faunalia.it
4 *
5 * This program is free software under the GPL (>=v2)
6 * Read the COPYING file that comes with GRASS for details.
7 *
8 */
9
10 #include <string.h>
11
12 #include <grass/gis.h>
13 #include <grass/glocale.h>
14
15 #include "utility.h"
16
17 /*if occurred an error returns NULL */
18 char *concatena(const char *str1, const char *str2)
19 {
20 char *conc = (char *)G_malloc(strlen(str1) + strlen(str2) + 1);
21
22 if (conc == NULL) {
23 return NULL;
24 }
25 strcpy(conc, str1);
26 strcat(conc, str2);
27 return conc;
28 }
29
30 /* split_arg returns the array of token find in linea separated by separatore
31 * presente and write in argc the nummer of find token */
32 /*if occurred an error returns NULL */
33 char **split_arg(char *linea, char separatore, long *numerotoken)
34 {
35 char **argv; /* token array */
36 char *copialinea; /* line copy */
37
38 long i; /* find token number */
39 size_t it; /* iterator */
40 long num;
41
42 int term; /* =0 if last token has not /0 */
43
44 i = 0;
45 term = 0;
46
47 if (linea == NULL || linea[0] == '\0') {
48 *numerotoken = 0;
49 return NULL;
50 }
51
52 /* copy string */
53 copialinea = (char *)G_malloc(strlen(linea) + 1);
54 if (copialinea == NULL) {
55 return NULL;
56 }
57 strcpy(copialinea, linea);
58
59 argv = (char **)G_malloc(sizeof(char *));
60 if (argv == NULL) {
61 return NULL;
62 }
63
64 num = 0;
65 for (it = 0; it < strlen(copialinea); it++) {
66 if (copialinea[it] == separatore) {
67 while (copialinea[it + 1] == separatore)
68 it++;
69 if (i != 0) {
70 argv[i - 1] = G_realloc(argv[i - 1], (num + 1) * sizeof(char));
71 argv[i - 1][num] = '\0';
72 argv = (char **)G_realloc(argv, (i + 1) * sizeof(char *));
73 num = 0;
74 }
75 if ((it + 1) == strlen(copialinea))
76 term = 1;
77 }
78 else {
79 if (num == 0) {
80 i++;
81 argv[i - 1] = G_malloc(sizeof(char));
82 if (argv[i - 1] == NULL) {
83 return NULL;
84 }
85 }
86 else {
87 argv[i - 1] = G_realloc(argv[i - 1], (num + 1) * sizeof(char));
88 }
89
90 argv[i - 1][num] = copialinea[it];
91 num++;
92 }
93 }
94
95 if (!term) {
96 argv[i - 1] = G_realloc(argv[i - 1], (num + 1) * sizeof(char));
97 argv[i - 1][num] = '\0';
98 }
99
100 *numerotoken = i;
101
102 G_free(copialinea);
103
104 return argv;
105 }