Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
jami-daemon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
jami-daemon
Commits
3cfcd8ed
Commit
3cfcd8ed
authored
19 years ago
by
yanmorin
Browse files
Options
Downloads
Patches
Plain Diff
Add the ArgTokenizer class
parent
0c458403
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/gui/server/argtokenizer.h
+112
-0
112 additions, 0 deletions
src/gui/server/argtokenizer.h
with
112 additions
and
0 deletions
src/gui/server/argtokenizer.h
0 → 100644
+
112
−
0
View file @
3cfcd8ed
/**
* Copyright (C) 2005 Savoir-Faire Linux inc.
* Author: Yan Morin <yan.morin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __ARG_TOKENIZER__
#define __ARG_TOKENIZER__
#include
<list>
#include
<string>
#include
<iostream>
typedef
std
::
list
<
std
::
string
>
TokenList
;
/**
Separate a string into token
a b "c d" = 3 tokens: [a], [b], [c d]
Example:
#include <argtokenizer.h>
ArgTokenizer tokenizer;
TokenList tList = tokenizer.tokenize(std::string("bla bla bla"));
TokenList::iterator iter = tList.begin();
while( iter != tList.end() ) {
std::cout << *iter << std::endl;
}
*/
class
ArgTokenizer
{
public:
ArgTokenizer
()
{}
// ctor
~
ArgTokenizer
()
{}
// dtor
TokenList
tokenize
(
const
std
::
string
&
str
)
{
TokenList
stack
;
// token stack
std
::
string
::
size_type
length
=
str
.
size
();
std
::
string
::
size_type
i
,
pos
;
std
::
string
temp
;
bool
inToken
=
false
;
// if we are inside a token or not
bool
inQuote
=
false
;
// look if we are inside a "quoted-string"
char
lastChar
=
'\0'
;
// lastChar for \" escaping inside quote
char
c
;
pos
=
0
;
// position inside quoted string, to escape backslashed-doublequote
for
(
i
=
0
;
i
<
length
;
i
++
)
{
c
=
str
[
i
];
// for the new token
if
(
inToken
==
false
)
{
if
(
c
==
' '
)
{
continue
;
}
// escape space outside a token
else
if
(
c
==
'"'
)
{
inToken
=
true
;
inQuote
=
true
;
lastChar
=
'\0'
;
pos
=
0
;
continue
;
}
else
{
inToken
=
true
;
}
}
if
(
inToken
)
{
// we are inside a token
if
(
inQuote
)
{
// we are looking for a " token
if
(
c
==
'"'
)
{
if
(
lastChar
==
'\\'
)
{
temp
[
pos
-
1
]
=
'"'
;
}
else
{
// end of the string
if
(
temp
.
size
())
{
stack
.
push_back
(
temp
);
temp
=
""
;
}
temp
=
""
;
inToken
=
false
;
inQuote
=
false
;
}
}
else
{
// normal character to append
temp
+=
c
;
pos
++
;
}
lastChar
=
c
;
}
else
{
// not in quote, stop to first space
if
(
c
==
' '
)
{
if
(
temp
.
size
())
{
stack
.
push_back
(
temp
);
temp
=
""
;
}
inToken
=
false
;
}
else
{
temp
+=
c
;
}
}
}
}
if
(
temp
.
size
())
{
stack
.
push_back
(
temp
);
}
// add last keyword
return
stack
;
}
// look at http://yansanmo.no-ip.org/test/cpp/argtokenizer.h
// for test
};
#endif // __ARG_TOKENIZER__
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment