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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [argtable2/] [arg_date.c] - Blame information for rev 650

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
/* glibc2 needs this for strptime */
22
#define _XOPEN_SOURCE 
23
 
24
/* SunOS also requires this for strptime */
25
#define _XOPEN_VERSION 4 
26
 
27
/* config.h must be included before anything else */
28
#ifdef HAVE_CONFIG_H
29
#include "config.h"
30
#endif
31
 
32
#ifdef HAVE_STDLIB_H
33
#include <stdlib.h>
34
#endif
35
 
36
#ifdef HAVE_STRINGS_H
37
#include <strings.h>
38
#endif
39
 
40
#include "argtable2.h"
41
 
42
/* local error codes  */
43
enum {EMINCOUNT=1,EMAXCOUNT,EBADDATE};
44
 
45
static void resetfn(struct arg_date *parent)
46
    {
47
    /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
48
    parent->count=0;
49
    }
50
 
51
static int scanfn(struct arg_date *parent, const char *argval)
52
    {
53
    int errorcode = 0;
54
 
55
    if (parent->count == parent->hdr.maxcount )
56
        errorcode = EMAXCOUNT;
57
    else if (!argval)
58
        {
59
        /* no argument value was given, leave parent->tmval[] unaltered but still count it */
60
        parent->count++;
61
        }
62
    else
63
        {
64
        const char *pend;
65
        struct tm tm = parent->tmval[parent->count];
66
 
67
        /* parse the given argument value, store result in parent->tmval[] */
68
        pend = strptime(argval, parent->format, &tm);
69
        if (pend && pend[0]=='\0')
70
            parent->tmval[parent->count++] = tm;
71
        else
72
            errorcode = EBADDATE;
73
        }
74
 
75
    /*printf("%s:scanfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
76
    return errorcode;
77
    }
78
 
79
static int checkfn(struct arg_date *parent)
80
    {
81
    int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
82
 
83
    /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
84
    return errorcode;
85
    }
86
 
87
static void errorfn(struct arg_date *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
88
    {
89
    const char *shortopts = parent->hdr.shortopts;
90
    const char *longopts  = parent->hdr.longopts;
91
    const char *datatype  = parent->hdr.datatype;
92
 
93
    /* make argval NULL safe */
94
    argval = argval ? argval : "";
95
 
96
    fprintf(fp,"%s: ",progname);
97
    switch(errorcode)
98
        {
99
        case EMINCOUNT:
100
            fputs("missing option ",fp);
101
            arg_print_option(fp,shortopts,longopts,datatype,"\n");
102
            break;
103
 
104
        case EMAXCOUNT:
105
            fputs("excess option ",fp);
106
            arg_print_option(fp,shortopts,longopts,argval,"\n");
107
            break;
108
 
109
        case EBADDATE:
110
            {
111
            struct tm tm;
112
            char buff[200];
113
 
114
            fprintf(fp,"illegal timestamp format \"%s\"\n",argval);
115
            bzero(&tm,sizeof(tm));
116
            strptime("1999-12-31 23:59:59","%F %H:%M:%S",&tm);
117
            strftime(buff, sizeof(buff), parent->format, &tm);
118
            printf("correct format is \"%s\"\n", buff);
119
            break;
120
            }
121
        }
122
    }
123
 
124
 
125
struct arg_date* arg_date0(const char* shortopts,
126
                           const char* longopts,
127
                           const char* format,
128
                           const char *datatype,
129
                           const char *glossary)
130
    {
131
    return arg_daten(shortopts,longopts,format,datatype,0,1,glossary);
132
    }
133
 
134
struct arg_date* arg_date1(const char* shortopts,
135
                           const char* longopts,
136
                           const char* format,
137
                           const char *datatype,
138
                           const char *glossary)
139
    {
140
    return arg_daten(shortopts,longopts,format,datatype,1,1,glossary);
141
    }
142
 
143
 
144
struct arg_date* arg_daten(const char* shortopts,
145
                           const char* longopts,
146
                           const char* format,
147
                           const char *datatype,
148
                           int mincount,
149
                           int maxcount,
150
                           const char *glossary)
151
    {
152
    size_t nbytes;
153
    struct arg_date *result;
154
 
155
        /* foolproof things by ensuring maxcount is not less than mincount */
156
        maxcount = (maxcount<mincount) ? mincount : maxcount;
157
 
158
    /* default time format is the national date format for the locale */
159
    if (!format)
160
        format = "%x";
161
 
162
    nbytes = sizeof(struct arg_date)         /* storage for struct arg_date */
163
           + maxcount*sizeof(struct tm);     /* storage for tmval[maxcount] array */
164
 
165
    /* allocate storage for the arg_date struct + tmval[] array.    */
166
    /* we use calloc because we want the tmval[] array zero filled. */
167
    result = (struct arg_date*)calloc(1,nbytes);
168
    if (result)
169
        {
170
        /* init the arg_hdr struct */
171
        result->hdr.flag      = ARG_HASVALUE;
172
        result->hdr.shortopts = shortopts;
173
        result->hdr.longopts  = longopts;
174
        result->hdr.datatype  = datatype ? datatype : format;
175
        result->hdr.glossary  = glossary;
176
        result->hdr.mincount  = mincount;
177
        result->hdr.maxcount  = maxcount;
178
        result->hdr.parent    = result;
179
        result->hdr.resetfn   = (arg_resetfn*)resetfn;
180
        result->hdr.scanfn    = (arg_scanfn*)scanfn;
181
        result->hdr.checkfn   = (arg_checkfn*)checkfn;
182
        result->hdr.errorfn   = (arg_errorfn*)errorfn;
183
 
184
        /* store the tmval[maxcount] array immediately after the arg_date struct */
185
        result->tmval  = (struct tm*)(result+1);
186
 
187
        /* init the remaining arg_date member variables */
188
        result->count = 0;
189
        result->format = format;
190
        }
191
 
192
    /*printf("arg_daten() returns %p\n",result);*/
193
    return result;
194
    }

powered by: WebSVN 2.1.0

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