Skip to content

Commit 052d42b

Browse files
committed
Add Options Parser
1 parent 0537519 commit 052d42b

File tree

8 files changed

+110
-33
lines changed

8 files changed

+110
-33
lines changed

build.gyp

+11
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@
195195
'test/configfile_test.cpp'
196196
],
197197
},
198+
{
199+
'target_name': 'args-test',
200+
'type': 'executable',
201+
"dependencies": [
202+
"CPP-SystemRT-lib"
203+
],
204+
'sources': [
205+
'test/args_test.cpp'
206+
],
207+
},
198208
{
199209
'target_name': 'CPP-SystemRT-lib',
200210
'type': 'static_library',
@@ -207,6 +217,7 @@
207217
'src/log-common.cpp',
208218
'src/filestream.cpp',
209219
'src/thread.cpp',
220+
'src/options.cpp',
210221
'src/socket.cpp',
211222
'src/path.cpp',
212223
'src/configfile.cpp',

include/CppSystemRT.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "CppSystemRT/stdafx.h"
55

66
#include "CppSystemRT/Error.hpp"
7+
#include "CppSystemRT/Options.hpp"
78
#include "CppSystemRT/File.hpp"
89
#include "CppSystemRT/FileStream.hpp"
910
#include "CppSystemRT/ConfigFile.hpp"

include/CppSystemRT/FileStream.hpp

+13-29
Original file line numberDiff line numberDiff line change
@@ -45,45 +45,29 @@ class OFileStreamBuf: public std::streambuf {
4545
File& file;
4646
};
4747

48-
class IFileStream
49-
: public std::istream
48+
template <typename StreamType, typename StreamBuf>
49+
class FileStream
50+
: public StreamType
5051
{
5152
public:
52-
IFileStream(File& file)
53-
: std::istream(new IFileStreamBuf(file))
53+
FileStream(File& file)
54+
: StreamType(new StreamBuf(file))
5455
{
55-
exceptions(std::ios_base::badbit);
56+
StreamType::exceptions(std::ios_base::badbit);
5657
}
57-
explicit IFileStream(File* file)
58-
: std::istream(new IFileStreamBuf(*file))
58+
explicit FileStream(File* file)
59+
: StreamType(new StreamBuf(*file))
5960
{
60-
exceptions(std::ios_base::badbit);
61+
StreamType::exceptions(std::ios_base::badbit);
6162
}
62-
virtual ~IFileStream()
63+
virtual ~FileStream()
6364
{
64-
delete rdbuf();
65+
delete StreamType::rdbuf();
6566
}
6667
};
6768

68-
class OFileStream
69-
: public std::ostream
70-
{
71-
public:
72-
OFileStream(File& file)
73-
: std::ostream(new OFileStreamBuf(file))
74-
{
75-
exceptions(std::ios_base::badbit);
76-
}
77-
explicit OFileStream(File* file)
78-
: std::ostream(new OFileStreamBuf(*file))
79-
{
80-
exceptions(std::ios_base::badbit);
81-
}
82-
virtual ~OFileStream()
83-
{
84-
delete rdbuf();
85-
}
86-
};
69+
typedef FileStream<std::istream, IFileStreamBuf> IFileStream;
70+
typedef FileStream<std::ostream, OFileStreamBuf> OFileStream;
8771

8872
}
8973

include/CppSystemRT/Options.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _CPPSYSTEMRT_OPTIONS_HPP
2+
#define _CPPSYSTEMRT_OPTIONS_HPP
3+
4+
namespace CppSystemRT {
5+
6+
namespace Options {
7+
void Parse(int const& argc, char** argv, std::function<void(std::string const&, std::string const&)> f_arg);
8+
};
9+
10+
}
11+
12+
#endif

include/CppSystemRT/stdafx.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <vector>
1515
#include <map>
1616
#include <sstream>
17+
#include <functional>
1718
#include <cctype>
1819
#include <limits>
1920
#include <atomic>

src/options.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <CppSystemRT.hpp>
2+
3+
namespace CppSystemRT {
4+
5+
void Options::Parse(int const& argc, char** argv, std::function<void(std::string const&, std::string const&)> f_arg) {
6+
int current_argc = 0;
7+
8+
while (current_argc < argc) {
9+
char* arg = argv[current_argc++];
10+
char* ch = arg;
11+
std::string option;
12+
std::string value;
13+
14+
if (*ch == '-') {
15+
ch++;
16+
17+
if (*ch == '-') {
18+
option = ++ch;
19+
} else if (*ch && isalnum(*ch)) {
20+
option = *ch;
21+
ch++;
22+
23+
if (*ch) {
24+
value = ch;
25+
}
26+
}
27+
} else
28+
current_argc--;
29+
30+
if (value.empty() && (current_argc < argc)) {
31+
char* c = argv[current_argc];
32+
if (*c != '-') {
33+
value = c;
34+
current_argc++;
35+
}
36+
}
37+
38+
if (!option.empty() || !value.empty())
39+
f_arg(option, value);
40+
}
41+
}
42+
43+
}

src/path.cpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
#ifdef _WIN32
44
#define getcwd _getcwd
5+
#define PATH_SEP '\\'
6+
#define GetExecPath(path, pathsz) ::GetModuleFileName(GetModuleHandle(NULL), path, pathsz)
7+
#else
8+
#define PATH_SEP '/'
9+
#define GetExecPath(path, pathsz) ::readlink("/proc/self/exe", path, pathsz)
510
#endif
611

712
namespace CppSystemRT {
@@ -12,10 +17,19 @@ namespace {
1217
class PathParser {
1318
public:
1419
PathParser() {
15-
char cwd[Path::MAX_SZ];
16-
if (getcwd(cwd, sizeof(cwd)) != NULL)
17-
vars["CWD"] = cwd;
18-
20+
char path[Path::MAX_SZ]{0};
21+
if (getcwd(path, sizeof(path)) != NULL)
22+
vars["CWD"] = path;
23+
24+
if (GetExecPath(path, Path::MAX_SZ) > 0) {
25+
vars["EWD"] = path;
26+
vars["EWD"] = vars["EWD"].substr(0, vars["EWD"].find_last_of(PATH_SEP));
27+
}
28+
#ifdef _WIN32
29+
vars["SYSNAME"] = "win";
30+
#else
31+
vars["SYSNAME"] = "linux";
32+
#endif
1933
vars["EXE"] = Path::EXE_EXT;
2034
vars["SOBJ"] = Path::SOBJ_EXT;
2135
}

test/args_test.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <CppSystemRT.hpp>
2+
3+
using namespace CppSystemRT;
4+
5+
int main(int const argc, char** argv) {
6+
Options::Parse(argc-1, &argv[1], [] (std::string const& param, std::string const& value) {
7+
std::cout << param << " -> " << value << std::endl;
8+
});
9+
10+
return 0;
11+
}

0 commit comments

Comments
 (0)