1 /*
2 Copyright (C) 1997-2000 Ondrej Popp
3 This code is generated with Ondrej Popp's C3PO.
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 
19 */
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 
25 #include "std_macr.h"
26 #include "structur.h"
27 #include "Error.h"
28 #include "nameset.h"
29 
getBasename(text input_file)30 expfun text getBasename(text input_file)
31 {
32    static char base[200];
33    int i;
34    strcpy(base, input_file);
35    i = (int)strlen(base) - 1;
36    while(i >= 0)
37    {
38       if (base[i] eq '/')
39          return base + i + 1;
40       i = i - 1;
41    }
42    return base;
43 }
getCore(text input_file)44 expfun text getCore(text input_file)
45 {
46    static char core[200];
47    int i;
48    strcpy(core, getBasename(input_file));
49    i = (int)strlen(core) - 1;
50    while(i >= 0)
51    {
52       if (core[i] eq '.')
53          core[i] = '\0';
54       i = i - 1;
55    }
56    return core;
57 }
inArgList(text name,int argc,char ** argv)58 expfun c3po_bool inArgList(text name,
59                       int argc,
60                       char** argv)
61 {
62    c3po_bool found = false;
63    int index = 1;
64    while(index < argc)
65    {
66       text arg;
67       arg = *(argv + index);
68       if (strcmp(name, arg) eq 0)
69       {
70          found = true;
71          break;
72       }
73       index = index + 1;
74    }
75    return found;
76 }
getArg(text name,int argc,char ** argv)77 expfun text getArg(text name,
78                    int argc,
79                    char** argv)
80 {
81    int index = 1;
82    text arg = NULL;
83    text current_arg;
84    c3po_bool found = false;
85    while(index < argc)
86    {
87       current_arg = *(argv + index);
88       if (strcmp(name, current_arg) eq 0)
89       {
90          found = true;
91          break;
92       }
93 
94       index = index + 1;
95    }
96 
97    if (found eq true)
98    {
99       index = index + 1;
100       if (index < argc)
101       {
102          current_arg = *(argv + index);
103          if ( *current_arg ne '-')
104             arg = current_arg;
105       }
106    }
107 
108    return arg;
109 }
getArgs(text name,int argc,char ** argv)110 expfun nameset getArgs(text name,
111                        int argc,
112                        char** argv)
113 {
114    int index = 0;
115    nameset args = NULL;
116    c3po_bool found = false;
117    while(index < argc)
118    {
119       text arg;
120       arg = *(argv + index);
121       if (strcmp(name, arg) eq 0)
122       {
123          index = index + 1;
124          if (index < argc)
125          {
126             args = namesetNew();
127             found = true;
128          }
129          else
130             return NULL;
131       }
132 
133       if (found eq true)
134       {
135          arg = *(argv + index);
136          if ( *arg eq '-')
137          {
138             if (args->size eq 0)
139             {
140                free((void *) args);
141                args = NULL;
142             }
143             break;
144          }
145          else
146             addToNamesetArray(arg, args);
147       }
148 
149       index = index + 1;
150    }
151 
152    return args;
153 }
154 
isInt(text string)155 expfun c3po_bool isInt(text string)
156 {
157    unsigned int index = 0;
158    c3po_bool is_int = true;
159    while(index < strlen(string))
160    {
161       if (isdigit(string[index]) ne 0)
162          index = index + 1;
163       else if ((index eq 0) and (string[0] eq '-'))
164          index = index + 1;
165       else
166       {
167          is_int = false;
168          break;
169       }
170    }
171    return is_int;
172 }
173 
isFloat(text string)174 expfun c3po_bool isFloat(text string)
175 {
176    unsigned int index = 0;
177    c3po_bool digits = true;
178    c3po_bool found_point = false;
179 
180    while(index < strlen(string))
181    {
182       if (isdigit(string[index]) ne 0)
183          index = index + 1;
184       else if (string[index] eq '.')
185       {
186          index = index + 1;
187          found_point = true;
188       }
189       else
190       {
191          digits = false;
192          break;
193       }
194    }
195 
196    if (found_point eq true)
197    {
198       if (digits eq true)
199          return true;
200       else
201          return false;
202    }
203    else
204       return false;
205 }
dumpCommandline(text file,int argc,char ** argv)206 expfun void dumpCommandline(text file,
207                             int argc,
208                             char** argv)
209 {
210    FILE *fp;
211 
212    fp = fopen(file, "a");
213    if (fp ne NULL)
214    {
215       int index = 0;
216       fprintf(stderr, "\n- appending commandline to '%s'\n", file);
217       while(index < argc)
218       {
219          fprintf(fp, "%s ", argv[index]);
220          index = index + 1;
221       }
222       fprintf(fp, "\n");
223       fclose(fp);
224    }
225    else
226    {
227       c3po_beep(1);
228       fprintf(stderr, "- couldn't append commandline to '%s'\n", file);
229    }
230 }
231