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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [or1ksim/] [argtable2/] [arg_int.c] - Blame information for rev 443

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,EBADINT};
35
 
36
static void resetfn(struct arg_int *parent)
37
    {
38
    /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
39
    parent->count=0;
40
    }
41
 
42
static int scanfn(struct arg_int *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
        int val;
61
        char *end;
62
 
63
        /* extract base10 integer from argval into val */
64
        val = (int)strtol(argval,&end,10);
65
 
66
        /* if success then store result in parent->ival[] array otherwise return error*/
67
        if (*end==0)
68
            parent->ival[parent->count++] = val;
69
        else
70
            errorcode = EBADINT;
71
        }
72
 
73
    /* printf("%s:scanfn(%p,%p) returns %d\n",__FILE__,parent,argval,errorcode); */
74
    return errorcode;
75
    }
76
 
77
static int checkfn(struct arg_int *parent)
78
    {
79
    int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
80
    /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
81
    return errorcode;
82
    }
83
 
84
static void errorfn(struct arg_int *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
85
    {
86
    const char *shortopts = parent->hdr.shortopts;
87
    const char *longopts  = parent->hdr.longopts;
88
    const char *datatype  = parent->hdr.datatype;
89
 
90
    /* make argval NULL safe */
91
    argval = argval ? argval : "";
92
 
93
    fprintf(fp,"%s: ",progname);
94
    switch(errorcode)
95
        {
96
        case EMINCOUNT:
97
            fputs("missing option ",fp);
98
            arg_print_option(fp,shortopts,longopts,datatype,"\n");
99
            break;
100
 
101
        case EMAXCOUNT:
102
            fputs("excess option ",fp);
103
            arg_print_option(fp,shortopts,longopts,argval,"\n");
104
            break;
105
 
106
        case EBADINT:
107
            fprintf(fp,"invalid argument \"%s\" to option ",argval);
108
            arg_print_option(fp,shortopts,longopts,datatype,"\n");
109
            break;
110
        }
111
    }
112
 
113
 
114
struct arg_int* arg_int0(const char* shortopts,
115
                         const char* longopts,
116
                         const char *datatype,
117
                         const char *glossary)
118
    {
119
    return arg_intn(shortopts,longopts,datatype,0,1,glossary);
120
    }
121
 
122
struct arg_int* arg_int1(const char* shortopts,
123
                         const char* longopts,
124
                         const char *datatype,
125
                         const char *glossary)
126
    {
127
    return arg_intn(shortopts,longopts,datatype,1,1,glossary);
128
    }
129
 
130
 
131
struct arg_int* arg_intn(const char* shortopts,
132
                         const char* longopts,
133
                         const char *datatype,
134
                         int mincount,
135
                         int maxcount,
136
                         const char *glossary)
137
    {
138
    size_t nbytes;
139
    struct arg_int *result;
140
 
141
        /* foolproof things by ensuring maxcount is not less than mincount */
142
        maxcount = (maxcount<mincount) ? mincount : maxcount;
143
 
144
    nbytes = sizeof(struct arg_int)     /* storage for struct arg_int */
145
           + maxcount * sizeof(int);    /* storage for ival[maxcount] array */
146
 
147
    result = (struct arg_int*)malloc(nbytes);
148
    if (result)
149
        {
150
        /* init the arg_hdr struct */
151
        result->hdr.flag      = ARG_HASVALUE;
152
        result->hdr.shortopts = shortopts;
153
        result->hdr.longopts  = longopts;
154
        result->hdr.datatype  = datatype ? datatype : "<int>";
155
        result->hdr.glossary  = glossary;
156
        result->hdr.mincount  = mincount;
157
        result->hdr.maxcount  = maxcount;
158
        result->hdr.parent    = result;
159
        result->hdr.resetfn   = (arg_resetfn*)resetfn;
160
        result->hdr.scanfn    = (arg_scanfn*)scanfn;
161
        result->hdr.checkfn   = (arg_checkfn*)checkfn;
162
        result->hdr.errorfn   = (arg_errorfn*)errorfn;
163
 
164
        /* store the ival[maxcount] array immediately after the arg_int struct */
165
        result->ival  = (int*)(result+1);
166
        result->count = 0;
167
        }
168
    /*printf("arg_intn() returns %p\n",result);*/
169
    return result;
170
    }

powered by: WebSVN 2.1.0

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