File: args.h

package info (click to toggle)
swftools 0.9.2%2Bds1-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,488 kB
  • sloc: ansic: 122,576; sh: 8,494; cpp: 8,020; yacc: 2,260; lisp: 904; makefile: 581; python: 304
file content (250 lines) | stat: -rw-r--r-- 6,568 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* args.h
   Routines to simplify argument handling

   Part of the swftools package.

   Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef __args_h__
#define __args_h__

#ifndef NO_ARGPARSER

extern int args_callback_option(char*,char*);
extern int args_callback_longoption(char*,char*);
extern int args_callback_command(char*,char*);
extern void args_callback_usage(char*name);

//int argn;
//char**argv;

static void processargs(int argn2,char**argv2)
{
    int t;
    if(argn2==1)
    {
        args_callback_usage(argv2[0]);
        exit(1);
    }
    for(t=1;t<argn2;t++)
    {
        char*next;
        if(t<argn2-1) next=argv2[t+1];
        else        next=0;

        if(argv2[t][0]=='-' && argv2[t][1])
        {
            if(argv2[t][1]=='-')
            {
                if(!strcmp(&argv2[t][2],"help")) 
                {
                    args_callback_usage(argv2[0]);
                    exit(1);
                }
                t+=args_callback_longoption(&argv2[t][2],next);
            }
            else
            {
                if(strchr("?h",argv2[t][1]))
                {
                    args_callback_usage(argv2[0]);
                    exit(1);
                }
                if(argv2[t][1]) // this might be something like e.g. -xvf
                {
                    char buf[2];
                    int s=1;
                    int ret;
                    buf[1]=0;
                    do{
                        if(argv2[t][s+1]) {
                          buf[0] = argv2[t][s];
                          ret = args_callback_option(buf,&argv2[t][s+1]);
                        }
                        else {
                          t+= args_callback_option(&argv2[t][s], next);
                          break;
                        }
                        s++;
                    } while(!ret);
                }
            }
        }
        else
        {
	    int num = args_callback_command(argv2[t],next);
	    if(num>2) {
		fprintf(stderr, "internal error in command line parsing\n");
		exit(1);
	    }
            t+=num;
        }
    }
}

struct options_t
{
    const char*shortoption;
    const char*longoption;
};

static int args_long2shortoption(struct options_t*options, char*name, char*val)
{
    char*equal = strchr(name,'=');
    if (equal) {
        *equal = 0;
        equal++;
    }
    while(options->shortoption) {
        if(!strcmp(options->longoption, name)) {
                char*tmp = (char*)malloc(strlen(options->shortoption)
                        +(equal?strlen(equal)+2:2));
                strcpy(tmp, options->shortoption);
                if(equal) {
                    //strcpy(&tmp[strlen(tmp)], equal);
                    int ret = args_callback_option(tmp, equal);
                    if(!ret) {
                        fprintf(stderr, "Warning: Option --%s takes no parameter.\n", name);
                    }
                    return 0;
                }
                return args_callback_option(tmp,val);
        }
        options++;
    }
    fprintf(stderr, "Unknown option: --%s\n", name);
    exit(1);
}

#endif

/* check whether the value t is in a given range.
  examples: 3 is in range 1-10: true
            7 is in range 2-4,6,8-10: false
	    9 is in range 1,2,3-12: true
*/
static char is_in_range(int t, char*irange)
{
    char*pos = irange;
    char*digits;
    int num;
    char range = 0;
    int last=0;
    char tmp;

    if(!irange)  // no range resembles (-OO,OO)
	return 1;

    while(*pos)
    {
	while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
	    pos++;

	digits = pos;
	while(*digits>='0' && *digits<='9')
	    digits++;
	if(digits == pos) {
	    fprintf(stderr, "Error: \"%s\" is not a valid format (digit expected)\n",irange);
	    exit(1);
        }
	
	tmp=*digits;*digits=0;
	num = atoi(pos);
	*digits=tmp;
	pos = digits;

	while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
	    pos++;

	if(range && last<=t && num>=t)
	    return 1;
	if(range) {
	    range = 0;
	    if(*pos)
	     pos ++;
	    continue;
	}

	if(*pos=='-')
	{
	    if(range) {
		fprintf(stderr, "Error: \"%s\" is not a valid format (too many '-'s)\n",irange);
		exit(1);
	    }
	    last = num;
	    range = 1;
	    if(*pos)
	     pos ++;
	    continue;
	} 
	else 
	{
	    /* if it isn't a '-', we assume it is a seperator like
	       ',', ';', ':', whatever. */
	    if(t == num)
		return 1;
	    if(*pos)
	     pos ++;
	    continue;
	}
    }
    if(range && last<=t)
	return 1;
    return 0;
}

static char* filename2template(char*filename, int*startindex)
{
    int l = strlen(filename);
    char*newname = (char*)malloc(l+5);
    /* first look whether the file is already numbered */
    while(1) {
        l--;
        if(l<0 || strchr("0123456789", filename[l]))
            break;
    };
    if(l>=0) {
        int lastdigit=l;
        int firstdigit=l;
        while(firstdigit && strchr("0123456789", filename[firstdigit-1]))
            firstdigit--;
        *startindex = atoi(filename+firstdigit);
        memcpy(newname, filename, firstdigit);
        sprintf(newname+firstdigit, "%%%dd", lastdigit+1-firstdigit);
        strcat(newname+firstdigit, filename+lastdigit+1);
        return newname;
    }
    /* if it isn't, try to paste a %d between filename and extension */
    char*dot = strrchr(filename, '.');
    if(dot) {
        int pos = dot-filename;
        memcpy(newname, filename, pos);
        newname[pos++] = '.';
        newname[pos++] = '%';
        newname[pos++] = 'd';
        strcpy(newname+pos, dot);
        *startindex = 1;
        return newname;
    }
    /* if that didn't work either, just append the number at the end */
    strcpy(newname, filename);
    strcat(newname, ".%d");
    *startindex = 1;
    return newname;
}

#endif //__args_h__