Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Timo Koch
dune-common
Commits
be3454cd
Commit
be3454cd
authored
15 years ago
by
Carsten Gräser
Browse files
Options
Downloads
Patches
Plain Diff
Impoved parsing of quotes to allow quoted multiline values.
[[Imported from SVN: r5505]]
parent
df38fa6c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
common/configparser.cc
+38
-44
38 additions, 44 deletions
common/configparser.cc
common/configparser.hh
+5
-3
5 additions, 3 deletions
common/configparser.hh
with
43 additions
and
47 deletions
common/configparser.cc
+
38
−
44
View file @
be3454cd
...
...
@@ -30,14 +30,15 @@ void ConfigParser::parseFile(std::string file)
{
string
line
;
getline
(
in
,
line
);
line
=
trim
(
line
);
line
=
l
trim
(
line
);
switch
(
line
[
0
])
{
case
'#'
:
break
;
case
'['
:
line
=
rtrim
(
line
);
if
(
line
[
line
.
length
()
-
1
]
==
']'
)
{
prefix
=
trim
(
line
.
substr
(
1
,
line
.
length
()
-
2
));
prefix
=
rtrim
(
l
trim
(
line
.
substr
(
1
,
line
.
length
()
-
2
))
)
;
if
(
prefix
!=
""
)
prefix
+=
"."
;
}
...
...
@@ -46,24 +47,32 @@ void ConfigParser::parseFile(std::string file)
string
::
size_type
mid
=
line
.
find
(
"="
);
if
(
mid
!=
string
::
npos
)
{
string
key
=
prefix
+
trim
(
line
.
substr
(
0
,
mid
));
string
value
=
trim
(
line
.
substr
(
mid
+
1
));
string
key
=
prefix
+
rtrim
(
l
trim
(
line
.
substr
(
0
,
mid
))
)
;
string
value
=
l
trim
(
line
.
substr
(
mid
+
1
));
// handle quoted strings
if
(
value
.
length
()
>
1
)
if
(
value
.
length
()
>
0
)
{
switch
(
value
[
0
])
{
case
'\''
:
if
(
value
[
value
.
length
()
-
1
]
==
'\''
)
value
=
value
.
substr
(
1
,
value
.
length
()
-
2
);
break
;
case
'"'
:
if
(
value
[
value
.
length
()
-
1
]
==
'"'
)
value
=
value
.
substr
(
1
,
value
.
length
()
-
2
);
break
;
default
:
break
;
// handle quoted strings
if
((
value
[
0
]
==
'\''
)
or
(
value
[
0
]
==
'"'
))
{
char
quote
=
value
[
0
];
value
=
value
.
substr
(
1
);
while
(
*
(
rtrim
(
value
).
rbegin
())
!=
quote
)
{
if
(
not
in
.
eof
())
{
string
l
;
getline
(
in
,
l
);
value
=
value
+
"
\n
"
+
l
;
}
else
value
=
value
+
quote
;
}
value
=
rtrim
(
value
);
value
=
value
.
substr
(
0
,
value
.
length
()
-
1
);
}
else
value
=
rtrim
(
value
);
}
if
(
keysInFile
.
count
(
key
)
!=
0
)
...
...
@@ -116,7 +125,7 @@ void ConfigParser::report(const string prefix) const
ValueIt
vend
=
values
.
end
();
for
(;
vit
!=
vend
;
++
vit
)
cout
<<
prefix
+
vit
->
first
<<
" = "
<<
vit
->
second
<<
endl
;
cout
<<
prefix
+
vit
->
first
<<
" =
\"
"
<<
vit
->
second
<<
"
\"
"
<<
endl
;
typedef
map
<
string
,
ConfigParser
>::
const_iterator
SubIt
;
SubIt
sit
=
subs
.
begin
();
...
...
@@ -284,37 +293,22 @@ namespace Dune {
}
// end namespace Dune
static
inline
bool
is_space
(
char
c
)
string
ConfigParser
::
ltrim
(
const
string
&
s
)
{
switch
(
c
)
{
case
' '
:
case
'\n'
:
case
'\r'
:
return
true
;
default
:
return
false
;
}
std
::
size_t
firstNonWS
=
s
.
find_first_not_of
(
"
\t\n\r
"
);
if
(
firstNonWS
!=
string
::
npos
)
return
s
.
substr
(
firstNonWS
);
return
string
();
}
string
ConfigParser
::
trim
(
const
string
&
s
)
const
string
ConfigParser
::
r
trim
(
const
string
&
s
)
{
string
::
const_iterator
b
=
s
.
begin
();
string
::
const_iterator
e
=
s
.
end
();
for
(
;
;
)
if
(
b
!=
e
)
if
(
is_space
(
*
b
))
++
b
;
else
break
;
else
return
string
();
while
(
is_space
(
e
[
-
1
]))
--
e
;
std
::
size_t
lastNonWS
=
s
.
find_last_not_of
(
"
\t\n\r
"
);
return
string
(
b
,
e
);
if
(
lastNonWS
!=
string
::
npos
)
return
s
.
substr
(
0
,
lastNonWS
+
1
);
return
string
();
}
const
ConfigParser
::
KeyVector
&
ConfigParser
::
getValueKeys
()
const
...
...
This diff is collapsed.
Click to expand it.
common/configparser.hh
+
5
−
3
View file @
be3454cd
...
...
@@ -52,6 +52,9 @@ namespace Dune {
\endverbatim
*
* All keys with a common 'prefix.' belong to the same substructure called 'prefix'.
* Leading and trailing spaces and tabs are removed from the values unless you use
* single or double quotes around them.
* Using single or double quotes you can also have multiline values.
*
*/
class
ConfigParser
...
...
@@ -226,9 +229,8 @@ namespace Dune {
std
::
map
<
std
::
string
,
std
::
string
>
values
;
std
::
map
<
std
::
string
,
ConfigParser
>
subs
;
std
::
string
trim
(
const
std
::
string
&
s
)
const
;
static
std
::
string
ltrim
(
const
std
::
string
&
s
);
static
std
::
string
rtrim
(
const
std
::
string
&
s
);
};
}
// end namespace dune
...
...
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