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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [argtable2/] [arg_str.c] - Blame information for rev 714

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_STDLIB_H
28
#include <stdlib.h>
29
#endif
30
 
31
#include "argtable2.h"
32
 
33
/* local error codes */
34
enum {EMINCOUNT=1,EMAXCOUNT};
35
 
36
static void resetfn(struct arg_str *parent)
37
    {
38
    /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
39
    parent->count=0;
40
    }
41
 
42
static int scanfn(struct arg_str *parent, const char *argval)
43
    {
44
    int errorcode = 0;
45
 
46
    if (parent->count == parent->hdr.maxcount)
47
        {
48
        /* maximum number of arguments exceeded */
49
        errorcode = EMAXCOUNT;
50
        }
51
    else if (!argval)
52
        {
53
        /* a valid argument with no argument value was given. */
54
        /* This happens when an optional argument value was invoked. */
55
        /* leave parent arguiment value unaltered but still count the argument. */
56
        parent->count++;
57
        }
58
    else
59
        {
60
        parent->sval[parent->count++] = argval;
61
        }
62
 
63
    /*printf("%s:scanfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
64
    return errorcode;
65
    }
66
 
67
static int checkfn(struct arg_str *parent)
68
    {
69
    int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
70
    /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
71
    return errorcode;
72
    }
73
 
74
static void errorfn(struct arg_str *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
75
    {
76
    const char *shortopts = parent->hdr.shortopts;
77
    const char *longopts  = parent->hdr.longopts;
78
    const char *datatype  = parent->hdr.datatype;
79
 
80
    /* make argval NULL safe */
81
    argval = argval ? argval : "";
82
 
83
    fprintf(fp,"%s: ",progname);
84
    switch(errorcode)
85
        {
86
        case EMINCOUNT:
87
            fputs("missing option ",fp);
88
            arg_print_option(fp,shortopts,longopts,datatype,"\n");
89
            break;
90
 
91
        case EMAXCOUNT:
92
            fputs("excess option ",fp);
93
            arg_print_option(fp,shortopts,longopts,argval,"\n");
94
            break;
95
        }
96
    }
97
 
98
 
99
struct arg_str* arg_str0(const char* shortopts,
100
                         const char* longopts,
101
                         const char *datatype,
102
                         const char *glossary)
103
    {
104
    return arg_strn(shortopts,longopts,datatype,0,1,glossary);
105
    }
106
 
107
struct arg_str* arg_str1(const char* shortopts,
108
                         const char* longopts,
109
                         const char *datatype,
110
                         const char *glossary)
111
    {
112
    return arg_strn(shortopts,longopts,datatype,1,1,glossary);
113
    }
114
 
115
 
116
struct arg_str* arg_strn(const char* shortopts,
117
                         const char* longopts,
118
                         const char *datatype,
119
                         int mincount,
120
                         int maxcount,
121
                         const char *glossary)
122
    {
123
    size_t nbytes;
124
    struct arg_str *result;
125
 
126
        /* foolproof things by ensuring maxcount is not less than mincount */
127
        maxcount = (maxcount<mincount) ? mincount : maxcount;
128
 
129
    nbytes = sizeof(struct arg_str)     /* storage for struct arg_str */
130
           + maxcount * sizeof(char*);  /* storage for sval[maxcount] array */
131
 
132
    result = (struct arg_str*)malloc(nbytes);
133
    if (result)
134
        {
135
        int i;
136
 
137
        /* init the arg_hdr struct */
138
        result->hdr.flag      = ARG_HASVALUE;
139
        result->hdr.shortopts = shortopts;
140
        result->hdr.longopts  = longopts;
141
        result->hdr.datatype  = datatype ? datatype : "<string>";
142
        result->hdr.glossary  = glossary;
143
        result->hdr.mincount  = mincount;
144
        result->hdr.maxcount  = maxcount;
145
        result->hdr.parent    = result;
146
        result->hdr.resetfn   = (arg_resetfn*)resetfn;
147
        result->hdr.scanfn    = (arg_scanfn*)scanfn;
148
        result->hdr.checkfn   = (arg_checkfn*)checkfn;
149
        result->hdr.errorfn   = (arg_errorfn*)errorfn;
150
 
151
        /* store the sval[maxcount] array immediately after the arg_str struct */
152
        result->sval  = (const char**)(result+1);
153
        result->count = 0;
154
 
155
        /* foolproof the string pointers by initialising them to reference empty strings */
156
        for (i=0; i<maxcount; i++)
157
            { result->sval[i] = ""; }
158
        }
159
    /*printf("arg_strn() returns %p\n",result);*/
160
    return result;
161
    }

powered by: WebSVN 2.1.0

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