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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [argtable2/] [arg_dbl.c] - Blame information for rev 90

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,EBADDOUBLE};
35
 
36
static void resetfn(struct arg_dbl *parent)
37
    {
38
    /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
39
    parent->count=0;
40
    }
41
 
42
static int scanfn(struct arg_dbl *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
        double val;
61
        char *end;
62
 
63
        /* extract double from argval into val */
64
        val = strtod(argval,&end);
65
 
66
        /* if success then store result in parent->dval[] array otherwise return error*/
67
        if (*end==0)
68
            parent->dval[parent->count++] = val;
69
        else
70
            errorcode = EBADDOUBLE;
71
        }
72
 
73
    /*printf("%s:scanfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
74
    return errorcode;
75
    }
76
 
77
static int checkfn(struct arg_dbl *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_dbl *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 EBADDOUBLE:
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_dbl* arg_dbl0(const char* shortopts,
115
                               const char* longopts,
116
                               const char *datatype,
117
                               const char *glossary)
118
    {
119
    return arg_dbln(shortopts,longopts,datatype,0,1,glossary);
120
    }
121
 
122
struct arg_dbl* arg_dbl1(const char* shortopts,
123
                               const char* longopts,
124
                               const char *datatype,
125
                               const char *glossary)
126
    {
127
    return arg_dbln(shortopts,longopts,datatype,1,1,glossary);
128
    }
129
 
130
 
131
struct arg_dbl* arg_dbln(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_dbl *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_dbl)     /* storage for struct arg_dbl */
145
           + (maxcount+1) * sizeof(double);  /* storage for dval[maxcount] array plus one extra for padding to memory boundary */
146
 
147
    result = (struct arg_dbl*)malloc(nbytes);
148
    if (result)
149
        {
150
        size_t addr;
151
        size_t rem;
152
 
153
        /* init the arg_hdr struct */
154
        result->hdr.flag      = ARG_HASVALUE;
155
        result->hdr.shortopts = shortopts;
156
        result->hdr.longopts  = longopts;
157
        result->hdr.datatype  = datatype ? datatype : "<double>";
158
        result->hdr.glossary  = glossary;
159
        result->hdr.mincount  = mincount;
160
        result->hdr.maxcount  = maxcount;
161
        result->hdr.parent    = result;
162
        result->hdr.resetfn   = (arg_resetfn*)resetfn;
163
        result->hdr.scanfn    = (arg_scanfn*)scanfn;
164
        result->hdr.checkfn   = (arg_checkfn*)checkfn;
165
        result->hdr.errorfn   = (arg_errorfn*)errorfn;
166
 
167
        /* Store the dval[maxcount] array on the first double boundary that immediately follows the arg_dbl struct. */
168
        /* We do the memory alignment purely for SPARC and Motorola systems. They require floats and doubles to be  */
169
        /* aligned on natural boundaries */
170
        addr = (size_t)(result+1);
171
        rem  = addr % sizeof(double);
172
        result->dval  = (double*)(addr + sizeof(double) - rem);
173
        /* printf("addr=%p, dval=%p, sizeof(double)=%d rem=%d\n", addr, result->dval, (int)sizeof(double), (int)rem); */
174
 
175
        result->count = 0;
176
        }
177
    /*printf("arg_dbln() returns %p\n",result);*/
178
    return result;
179
    }

powered by: WebSVN 2.1.0

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