OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
cmdline.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1994, 1996 by Borland International, All Rights Reserved
4//
5///\file
6/// class TCmdLine implementation
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/cmdline.h>
10#include <owl/private/memory.h>
11
12namespace owl {
13
14const tchar whitespace[] = _T(" \t");
15const tchar terminator[] = _T("=/ \t\""); // remove /- to dissallow separating there
16
18{
19 Buffer = new tchar[cmdLine.length() + 1];
20 ::_tcscpy(Buffer, cmdLine.c_str());
21 Reset();
22}
23
25{
26 Token = TokenStart = Buffer;
27 TokenLen = 0;
28 Kind = Start;
29}
30
32{
33 delete [] Buffer;
34}
35
37{
38 // Done parsing, no more tokens
39 //
40 if (Kind == Done)
41 return Kind;
42
43 // Move Token ptr to next token, by copying over current token, or by ptr
44 // adjustment. TokenStart stays right past previous token
45 //
46 if (removeCurrent) {
47 ::_tcscpy(TokenStart, Token+TokenLen);
48 Token = TokenStart;
49 }
50 else {
51 Token += TokenLen;
52 TokenStart = Token;
53 }
54
55 // Adjust token ptr to begining of token & determine kind
56 //
57 Token += _tcsspn(Token, whitespace); // skip leading whitespace
58 switch (*Token) {
59 case 0:
60 Kind = Done;
61 break;
62 case _T('='):
63 Kind = Value;
64 Token++;
65 break;
66 case _T('-'):
67 case _T('/'):
68 Kind = Option;
69 Token++;
70 Token += _tcsspn(Token, whitespace); // skip any more whitespace
71 if (*Token == _T('\"')) {
72 Token++; // skip the quote
73 TokenLen = static_cast<int>(_tcscspn(Token, _T("\"")));
74 Token[TokenLen] = 0; // remove quote
75 TokenLen++; // next char
76 return Kind;
77 }
78 break;
79 case _T('\"'):
80 Kind = Name;
81 Token++; // skip the quote
82 TokenLen = static_cast<int>(_tcscspn(Token, _T("\"")));
83 Token[TokenLen] = 0; // remove quote
84 TokenLen++; // next char
85 return Kind;
86 default:
87 Kind = Name;
88 }
89 Token += _tcsspn(Token, whitespace); // skip any more whitespace
90 TokenLen = static_cast<int>(_tcscspn(Token, terminator));
91 return Kind;
92}
93
95{
96 if (TokenLen > 0 && Token[TokenLen - 1] == 0)
97 return tstring(Token, TokenLen - 1); // Trim trailing null character that was put when removing the enclosing double quotes
98 else
99 return tstring(Token, TokenLen);
100}
101
102
103
104} // OWL namespace
105/* ========================================================================== */
106
TCmdLine(const tstring &cmdLine)
Definition cmdline.cpp:17
@ Option
Option type token. Leading / or - skipped by Token.
Definition cmdline.h:40
@ Value
Value for name or option. Leading : or = skipped by Token.
Definition cmdline.h:41
@ Start
No tokens have been parsed yet.
Definition cmdline.h:38
@ Done
No more tokens.
Definition cmdline.h:42
@ Name
Name type token, has no leading / or -.
Definition cmdline.h:39
tstring GetToken() const
Definition cmdline.cpp:94
void Reset()
Definition cmdline.cpp:24
TKind NextToken(bool removeCurrent=false)
Definition cmdline.cpp:36
Command line parsing class.
#define _tcscpy
Definition cygwin.h:79
#define _tcscspn
Definition cygwin.h:82
#define _T(x)
Definition cygwin.h:51
#define _tcsspn
Definition cygwin.h:81
Reliable platform independent header for common memory and string functions.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
const tchar terminator[]
Definition cmdline.cpp:15
const tchar whitespace[]
Definition cmdline.cpp:14
char tchar
Definition defs.h:77
std::string tstring
Definition defs.h:79