OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [or1ksim/] [argtable2/] [arg_file.c] - Blame information for rev 178

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jeremybenn
/*********************************************************************
2
This file is part of the argtable2 library.
3
Copyright (C) 1998-2001,2003-2008 Stewart Heitmann
4
sheitmann@users.sourceforge.net
5
 
6
The argtable2 library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public License as
8
published by the Free Software Foundation; either version 2 of the
9
License, or (at your option) any later version.
10
 
11
This software is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
Library General Public License for more details.
15
 
16
You should have received a copy of the GNU Library General Public
17
License along with this library; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19
USA.
20
**********************************************************************/
21
 
22
/* config.h must be included before anything else */
23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25
#endif
26
 
27
#ifdef HAVE_STRING_H
28
#include <string.h>
29
#endif
30
 
31
#ifdef HAVE_STDLIB_H
32
#include <stdlib.h>
33
#endif
34
 
35
#include "argtable2.h"
36
 
37
#ifdef WIN32
38
# define FILESEPARATOR '\\'
39
#else
40
# define FILESEPARATOR '/'
41
#endif
42
 
43
/* local error codes */
44
enum {EMINCOUNT=1,EMAXCOUNT};
45
 
46
 
47
static void resetfn(struct arg_file *parent)
48
    {
49
    /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
50
    parent->count=0;
51
    }
52
 
53
 
54
/* Returns ptr to the base filename within *filename */
55
static const char* arg_basename(const char *filename)
56
    {
57
    const char *result = (filename ? strrchr(filename,FILESEPARATOR) : NULL);
58
    if (result)
59
        result++;
60
    else
61
        result = filename;
62
    return result;
63
    }
64
 
65
 
66
/* Returns ptr to the file extension within *filename */
67
static const char* arg_extension(const char *filename)
68
    {
69
    const char *result = (filename ? strrchr(filename,'.') : NULL);
70
    if (filename && !result)
71
        result = filename+strlen(filename);
72
    return result;
73
    }
74
 
75
 
76
static int scanfn(struct arg_file *parent, const char *argval)
77
    {
78
    int errorcode = 0;
79
 
80
    if (parent->count == parent->hdr.maxcount)
81
        {
82
        /* maximum number of arguments exceeded */
83
        errorcode = EMAXCOUNT;
84
        }
85
    else if (!argval)
86
        {
87
        /* a valid argument with no argument value was given. */
88
        /* This happens when an optional argument value was invoked. */
89
        /* leave parent arguiment value unaltered but still count the argument. */
90
        parent->count++;
91
        }
92
    else
93
        {
94
        parent->filename[parent->count]  = argval;
95
        parent->basename[parent->count]  = arg_basename(argval);
96
        parent->extension[parent->count] = arg_extension(argval);
97
        parent->count++;
98
        }
99
 
100
    /*printf("%s:scanfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
101
    return errorcode;
102
    }
103
 
104
 
105
static int checkfn(struct arg_file *parent)
106
    {
107
    int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
108
    /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
109
    return errorcode;
110
    }
111
 
112
 
113
static void errorfn(struct arg_file *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
114
    {
115
    const char *shortopts = parent->hdr.shortopts;
116
    const char *longopts  = parent->hdr.longopts;
117
    const char *datatype  = parent->hdr.datatype;
118
 
119
    /* make argval NULL safe */
120
    argval = argval ? argval : "";
121
 
122
    fprintf(fp,"%s: ",progname);
123
    switch(errorcode)
124
        {
125
        case EMINCOUNT:
126
            fputs("missing option ",fp);
127
            arg_print_option(fp,shortopts,longopts,datatype,"\n");
128
            break;
129
 
130
        case EMAXCOUNT:
131
            fputs("excess option ",fp);
132
            arg_print_option(fp,shortopts,longopts,argval,"\n");
133
            break;
134
 
135
        default:
136
            fprintf(fp,"unknown error at \"%s\"\n",argval);
137
        }
138
    }
139
 
140
 
141
struct arg_file* arg_file0(const char* shortopts,
142
                           const char* longopts,
143
                           const char *datatype,
144
                           const char *glossary)
145
    {
146
    return arg_filen(shortopts,longopts,datatype,0,1,glossary);
147
    }
148
 
149
 
150
struct arg_file* arg_file1(const char* shortopts,
151
                           const char* longopts,
152
                           const char *datatype,
153
                           const char *glossary)
154
    {
155
    return arg_filen(shortopts,longopts,datatype,1,1,glossary);
156
    }
157
 
158
 
159
struct arg_file* arg_filen(const char* shortopts,
160
                           const char* longopts,
161
                           const char *datatype,
162
                           int mincount,
163
                           int maxcount,
164
                           const char *glossary)
165
    {
166
    size_t nbytes;
167
    struct arg_file *result;
168
 
169
        /* foolproof things by ensuring maxcount is not less than mincount */
170
        maxcount = (maxcount<mincount) ? mincount : maxcount;
171
 
172
    nbytes = sizeof(struct arg_file)     /* storage for struct arg_file */
173
           + sizeof(char*) * maxcount    /* storage for filename[maxcount] array */
174
           + sizeof(char*) * maxcount    /* storage for basename[maxcount] array */
175
           + sizeof(char*) * maxcount;   /* storage for extension[maxcount] array */
176
 
177
    result = (struct arg_file*)malloc(nbytes);
178
    if (result)
179
        {
180
        int i;
181
 
182
        /* init the arg_hdr struct */
183
        result->hdr.flag      = ARG_HASVALUE;
184
        result->hdr.shortopts = shortopts;
185
        result->hdr.longopts  = longopts;
186
        result->hdr.glossary  = glossary;
187
        result->hdr.datatype  = datatype ? datatype : "<file>";
188
        result->hdr.mincount  = mincount;
189
        result->hdr.maxcount  = maxcount;
190
        result->hdr.parent    = result;
191
        result->hdr.resetfn   = (arg_resetfn*)resetfn;
192
        result->hdr.scanfn    = (arg_scanfn*)scanfn;
193
        result->hdr.checkfn   = (arg_checkfn*)checkfn;
194
        result->hdr.errorfn   = (arg_errorfn*)errorfn;
195
 
196
        /* store the filename,basename,extension arrays immediately after the arg_file struct */
197
        result->filename  = (const char**)(result+1);
198
        result->basename  = result->filename + maxcount;
199
        result->extension = result->basename + maxcount;
200
        result->count = 0;
201
 
202
        /* foolproof the string pointers by initialising them with empty strings */
203
        for (i=0; i<maxcount; i++)
204
            {
205
            result->filename[i] = "";
206
            result->basename[i] = "";
207
            result->extension[i] = "";
208
            }
209
        }
210
    /*printf("arg_filen() returns %p\n",result);*/
211
    return result;
212
    }

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.