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

Subversion Repositories altor32

[/] [altor32/] [trunk/] [or1k-sim/] [getopt_win32.c] - Blame information for rev 28

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 28 ultra_embe
// XGetopt.c  Version 1.2
2
//
3
// Author:  Hans Dietrich
4
//          hdietrich2@hotmail.com
5
//
6
// Description:
7
//     XGetopt.cpp implements getopt(), a function to parse command lines.
8
//
9
// History
10
//     Version 1.2 - 2003 May 17
11
//     - Added Unicode support
12
//
13
//     Version 1.1 - 2002 March 10
14
//     - Added example to XGetopt.cpp module header 
15
//
16
// This software is released into the public domain.
17
// You are free to use it in any way you like.
18
//
19
// This software is provided "as is" with no expressed
20
// or implied warranty.  I accept no liability for any
21
// damage or loss of business that this software may cause.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
 
25
#ifdef WIN32
26
 
27
///////////////////////////////////////////////////////////////////////////////
28
// if you are not using precompiled headers then include these lines:
29
#include <windows.h>
30
#include <stdio.h>
31
#include <tchar.h>
32
///////////////////////////////////////////////////////////////////////////////
33
 
34
 
35
#include "getopt_win32.h"
36
 
37
 
38
///////////////////////////////////////////////////////////////////////////////
39
//
40
//  X G e t o p t . c
41
//
42
//
43
//  NAME
44
//       getopt -- parse command line options
45
//
46
//  SYNOPSIS
47
//       int getopt(int argc, TCHAR *argv[], TCHAR *optstring)
48
//
49
//       extern TCHAR *optarg;
50
//       extern int optind;
51
//
52
//  DESCRIPTION
53
//       The getopt() function parses the command line arguments. Its
54
//       arguments argc and argv are the argument count and array as
55
//       passed into the application on program invocation.  In the case
56
//       of Visual C++ programs, argc and argv are available via the
57
//       variables __argc and __argv (double underscores), respectively.
58
//       getopt returns the next option letter in argv that matches a
59
//       letter in optstring.  (Note:  Unicode programs should use
60
//       __targv instead of __argv.  Also, all character and string
61
//       literals should be enclosed in _T( ) ).
62
//
63
//       optstring is a string of recognized option letters;  if a letter
64
//       is followed by a colon, the option is expected to have an argument
65
//       that may or may not be separated from it by white space.  optarg
66
//       is set to point to the start of the option argument on return from
67
//       getopt.
68
//
69
//       Option letters may be combined, e.g., "-ab" is equivalent to
70
//       "-a -b".  Option letters are case sensitive.
71
//
72
//       getopt places in the external variable optind the argv index
73
//       of the next argument to be processed.  optind is initialized
74
//       to 0 before the first call to getopt.
75
//
76
//       When all options have been processed (i.e., up to the first
77
//       non-option argument), getopt returns EOF, optarg will point
78
//       to the argument, and optind will be set to the argv index of
79
//       the argument.  If there are no non-option arguments, optarg
80
//       will be set to NULL.
81
//
82
//       The special option "--" may be used to delimit the end of the
83
//       options;  EOF will be returned, and "--" (and everything after it)
84
//       will be skipped.
85
//
86
//  RETURN VALUE
87
//       For option letters contained in the string optstring, getopt
88
//       will return the option letter.  getopt returns a question mark (?)
89
//       when it encounters an option letter not included in optstring.
90
//       EOF is returned when processing is finished.
91
//
92
//  BUGS
93
//       1)  Long options are not supported.
94
//       2)  The GNU double-colon extension is not supported.
95
//       3)  The environment variable POSIXLY_CORRECT is not supported.
96
//       4)  The + syntax is not supported.
97
//       5)  The automatic permutation of arguments is not supported.
98
//       6)  This implementation of getopt() returns EOF if an error is
99
//           encountered, instead of -1 as the latest standard requires.
100
//
101
//  EXAMPLE
102
//       BOOL CMyApp::ProcessCommandLine(int argc, TCHAR *argv[])
103
//       {
104
//           int c;
105
//
106
//           while ((c = getopt(argc, argv, _T("aBn:"))) != EOF)
107
//           {
108
//               switch (c)
109
//               {
110
//                   case _T('a'):
111
//                       TRACE(_T("option a\n"));
112
//                       //
113
//                       // set some flag here
114
//                       //
115
//                       break;
116
//
117
//                   case _T('B'):
118
//                       TRACE( _T("option B\n"));
119
//                       //
120
//                       // set some other flag here
121
//                       //
122
//                       break;
123
//
124
//                   case _T('n'):
125
//                       TRACE(_T("option n: value=%d\n"), atoi(optarg));
126
//                       //
127
//                       // do something with value here
128
//                       //
129
//                       break;
130
//
131
//                   case _T('?'):
132
//                       TRACE(_T("ERROR: illegal option %s\n"), argv[optind-1]);
133
//                       return FALSE;
134
//                       break;
135
//
136
//                   default:
137
//                       TRACE(_T("WARNING: no handler for option %c\n"), c);
138
//                       return FALSE;
139
//                       break;
140
//               }
141
//           }
142
//           //
143
//           // check for non-option args here
144
//           //
145
//           return TRUE;
146
//       }
147
//
148
///////////////////////////////////////////////////////////////////////////////
149
 
150
TCHAR    *optarg;        // global argument pointer
151
int        optind = 0;     // global argv index
152
 
153
int getopt(int argc, TCHAR *argv[], TCHAR *optstring)
154
{
155
    static TCHAR *next = NULL;
156
    TCHAR c;
157
    TCHAR *cp;
158
 
159
    if (optind == 0)
160
        next = NULL;
161
 
162
    optarg = NULL;
163
 
164
    if (next == NULL || *next == _T('\0'))
165
    {
166
        if (optind == 0)
167
            optind++;
168
 
169
        if (optind >= argc || argv[optind][0] != _T('-') || argv[optind][1] == _T('\0'))
170
        {
171
            optarg = NULL;
172
            if (optind < argc)
173
                optarg = argv[optind];
174
            return EOF;
175
        }
176
 
177
        if (_tcscmp(argv[optind], _T("--")) == 0)
178
        {
179
            optind++;
180
            optarg = NULL;
181
            if (optind < argc)
182
                optarg = argv[optind];
183
            return EOF;
184
        }
185
 
186
        next = argv[optind];
187
        next++;        // skip past -
188
        optind++;
189
    }
190
 
191
    c = *next++;
192
    cp = _tcschr(optstring, c);
193
 
194
    if (cp == NULL || c == _T(':'))
195
        return _T('?');
196
 
197
    cp++;
198
    if (*cp == _T(':'))
199
    {
200
        if (*next != _T('\0'))
201
        {
202
            optarg = next;
203
            next = NULL;
204
        }
205
        else if (optind < argc)
206
        {
207
            optarg = argv[optind];
208
            optind++;
209
        }
210
        else
211
        {
212
            return _T('?');
213
        }
214
    }
215
 
216
    return c;
217
}
218
#endif

powered by: WebSVN 2.1.0

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