• R/O
  • SSH
  • HTTPS

gtaconfig: 提交


Commit MetaInfo

修订版23 (tree)
时间2019-02-12 02:01:06
作者quiret

Log Message

- added command-line option parser (from pefrmdllembed)

更改概述

差异

--- include/gtaconfig/include.h (revision 22)
+++ include/gtaconfig/include.h (revision 23)
@@ -9,4 +9,5 @@
99 #include "../../src/ide.h"
1010 #include "../../src/ini.h"
1111 #include "../../src/ipl.h"
12-#include "../../src/objdat.h"
\ No newline at end of file
12+#include "../../src/objdat.h"
13+#include "option.h"
\ No newline at end of file
--- include/gtaconfig/option.h (nonexistent)
+++ include/gtaconfig/option.h (revision 23)
@@ -0,0 +1,38 @@
1+#ifndef _OPTION_UTILITIES_
2+#define _OPTION_UTILITIES_
3+
4+#include <string>
5+
6+struct OptionParser
7+{
8+ OptionParser( const char *args[], size_t numArgs );
9+ ~OptionParser( void );
10+
11+ std::string FetchOption( void );
12+
13+ inline size_t GetArgIndex( void ) const { return this->curArg; }
14+ inline const char* GetArgPointer( void ) const { return this->curArgPtr; }
15+
16+private:
17+ inline const char* UpdateArgPtr( size_t argIdx, size_t numArgs )
18+ {
19+ if ( argIdx < numArgs )
20+ {
21+ return this->args[ argIdx ];
22+ }
23+ else
24+ {
25+ return nullptr;
26+ }
27+ }
28+
29+ // Management variables.
30+ const char **args;
31+ size_t numArgs;
32+
33+ // Current iteration status.
34+ size_t curArg;
35+ const char *curArgPtr;
36+};
37+
38+#endif //_OPTION_UTILITIES_
\ No newline at end of file
--- src/option.cpp (nonexistent)
+++ src/option.cpp (revision 23)
@@ -0,0 +1,91 @@
1+#include "StdInc.h"
2+
3+#include "option.h"
4+
5+OptionParser::OptionParser( const char *args[], size_t numArgs )
6+{
7+ this->args = args;
8+ this->numArgs = numArgs;
9+
10+ this->curArg = 0;
11+
12+ this->curArgPtr = UpdateArgPtr( 0, numArgs );
13+}
14+
15+OptionParser::~OptionParser( void )
16+{
17+ return;
18+}
19+
20+static const char OPTION_INITIALIZER = '-';
21+
22+std::string OptionParser::FetchOption( void )
23+{
24+ size_t argIdx = this->curArg;
25+ size_t numArgs = this->numArgs;
26+
27+ bool isParsingOption = false;
28+ std::string optString;
29+
30+ const char *argPtr = this->curArgPtr;
31+
32+ if ( argIdx < numArgs )
33+ {
34+ while ( true )
35+ {
36+ // Check if we can fetch from the current argument.
37+ char c = *argPtr;
38+
39+ bool advanceIndex = false;
40+
41+ if ( c == 0 )
42+ {
43+ // Need to advance to next argument.
44+ advanceIndex = true;
45+ }
46+ else
47+ {
48+ argPtr++;
49+
50+ if ( c == OPTION_INITIALIZER )
51+ {
52+ isParsingOption = true;
53+ }
54+ else if ( isParsingOption )
55+ {
56+ if ( c == ' ' || c == '\t' )
57+ {
58+ // Terminate the option parsing.
59+ advanceIndex = true;
60+ }
61+ else
62+ {
63+ optString += c;
64+ }
65+ }
66+ else
67+ {
68+ // Failure. No options.
69+ argPtr--;
70+ break;
71+ }
72+ }
73+
74+ if ( advanceIndex )
75+ {
76+ argIdx++;
77+ argPtr = UpdateArgPtr( argIdx, numArgs );
78+
79+ if ( isParsingOption || argIdx >= numArgs )
80+ {
81+ break;
82+ }
83+ }
84+ }
85+ }
86+
87+ this->curArg = argIdx;
88+ this->curArgPtr = argPtr;
89+
90+ return optString;
91+}
\ No newline at end of file
Show on old repository browser