forked from douglascraigschmidt/CPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetopt.cpp
More file actions
113 lines (87 loc) · 2.34 KB
/
Copy pathgetopt.cpp
File metadata and controls
113 lines (87 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* getopt - get option letter from argv
*/
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
static char *scan = 0; /* Private scan pointer. */
extern char *index();
namespace parsing {
char *optarg; /* Global argument pointer. */
int optind = 0; /* Global argv index. */
std::string getfilename (std::string arg)
{
std::string::size_type lastFound = arg.find_last_of('\\');
if (lastFound == std::string::npos)
{
// we didn't find a Windows path, try a Unix path
lastFound = arg.find_last_of('/');
}
// if we found a match and it did not occur on the last character,
// return the string from the match to the end
if (lastFound != std::string::npos && lastFound < arg.length() - 1)
{
return arg.substr(lastFound+1);
}
// otherwise, return the incoming string
return arg;
}
std::string getpath (std::string arg)
{
// first, try to get the Windows path
std::string::size_type lastFound = arg.find_last_of('\\');
if (lastFound == std::string::npos)
{
// we didn't find a Windows path, try a Unix path
lastFound = arg.find_last_of('/');
}
// if we found a match
// return the string from beginning to and including the match
if (lastFound != std::string::npos)
{
return arg.substr(0,lastFound+1);
}
// otherwise, return an empty string
return "";
}
int
getopt(int argc, char *argv[], char *optstring)
{
register char c;
register char *place;
optarg = 0;
if (scan == 0 || *scan == '\0') {
if (optind == 0)
optind++;
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
return(EOF);
if (::strcmp(argv[optind], "--")==0) {
optind++;
return(EOF);
}
scan = argv[optind]+1;
optind++;
}
c = *scan++;
place = ::strchr(optstring, c);
if (place == 0 || c == ':') {
fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
return('?');
}
place++;
if (*place == ':') {
if (*scan != '\0') {
optarg = scan;
scan = 0;
} else if (optind < argc) {
optarg = argv[optind];
optind++;
} else {
fprintf(stderr, "%s: -%c argument missing\n", argv[0], c);
return('?');
}
}
return(c);
}
}