Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
openstructure
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Analyze
Contributor 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
schwede
openstructure
Commits
ee805811
Commit
ee805811
authored
13 years ago
by
Bienchen
Browse files
Options
Downloads
Patches
Plain Diff
Added conversion functions to StarParser
parent
94384319
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
modules/io/src/mol/star_parser.cc
+40
-1
40 additions, 1 deletion
modules/io/src/mol/star_parser.cc
modules/io/src/mol/star_parser.hh
+25
-1
25 additions, 1 deletion
modules/io/src/mol/star_parser.hh
modules/io/tests/test_star_parser.cc
+49
-0
49 additions, 0 deletions
modules/io/tests/test_star_parser.cc
with
114 additions
and
2 deletions
modules/io/src/mol/star_parser.cc
+
40
−
1
View file @
ee805811
...
...
@@ -45,7 +45,7 @@ StarParser::StarParser(const String& filename, bool items_as_row):
}
String
StarParser
::
FormatDiagnostic
(
StarDiagType
type
,
const
String
&
message
,
int
line
)
int
line
)
const
{
std
::
stringstream
ss
;
ss
<<
filename_
<<
":"
;
...
...
@@ -66,6 +66,45 @@ String StarParser::FormatDiagnostic(StarDiagType type, const String& message,
return
ss
.
str
();
}
float
StarParser
::
TryGetFloat
(
const
StringRef
&
data
,
const
String
&
name
)
const
{
std
::
pair
<
bool
,
float
>
value
=
data
.
to_float
();
if
(
!
value
.
first
)
{
throw
IOException
(
this
->
FormatDiagnostic
(
STAR_DIAG_ERROR
,
"Expecting floating point value for "
+
name
+
", found '"
+
data
.
str
()
+
"' instead."
,
line_num_
));
}
return
value
.
second
;
}
int
StarParser
::
TryGetInt
(
const
StringRef
&
data
,
const
String
&
name
)
const
{
std
::
pair
<
bool
,
int
>
value
=
data
.
to_int
();
if
(
!
value
.
first
)
{
throw
IOException
(
this
->
FormatDiagnostic
(
STAR_DIAG_ERROR
,
"Expecting integer value for "
+
name
+
", found '"
+
data
.
str
()
+
"' instead."
,
line_num_
));
}
return
value
.
second
;
}
bool
StarParser
::
TryGetBool
(
const
StringRef
&
data
,
const
String
&
name
)
const
{
if
(
data
.
length
()
==
1
)
{
if
(
data
[
0
]
==
'Y'
||
data
[
0
]
==
'y'
)
{
return
true
;
}
else
if
(
data
[
0
]
==
'N'
||
data
[
0
]
==
'n'
)
{
return
false
;
}
}
throw
IOException
(
this
->
FormatDiagnostic
(
STAR_DIAG_ERROR
,
"Expecting Boolean (Y/N) value for "
+
name
+
", found '"
+
data
.
str
()
+
"' instead."
,
line_num_
));
}
bool
StarParser
::
SplitLine
(
const
StringRef
&
line
,
std
::
vector
<
StringRef
>&
parts
,
bool
clear
)
...
...
This diff is collapsed.
Click to expand it.
modules/io/src/mol/star_parser.hh
+
25
−
1
View file @
ee805811
...
...
@@ -148,9 +148,33 @@ public:
/// OnBeginData() returned true.
virtual
void
OnEndData
()
{
}
/// \brief try to convert a value to float, on failure raise an exception.
///
/// \param data value to be converted
/// \param name to be included in the message
///
/// \return converted value
float
TryGetFloat
(
const
StringRef
&
data
,
const
String
&
name
)
const
;
/// \brief try to convert a value to integer, on failure raise an exception.
///
/// \param data value to be converted
/// \param name to be included in the message
///
/// \return converted value
int
TryGetInt
(
const
StringRef
&
data
,
const
String
&
name
)
const
;
/// \brief try to convert a value to bool, on failure raise an exception.
///
/// \param data value to be converted
/// \param name to be included in the message
///
/// \return converted value
bool
TryGetBool
(
const
StringRef
&
data
,
const
String
&
name
)
const
;
/// \brief format diagnostic and returns it as a string.
String
FormatDiagnostic
(
StarDiagType
type
,
const
String
&
message
,
int
line
=-
1
);
int
line
=-
1
)
const
;
void
SetFilename
(
const
String
&
filename
)
{
...
...
This diff is collapsed.
Click to expand it.
modules/io/tests/test_star_parser.cc
+
49
−
0
View file @
ee805811
...
...
@@ -395,5 +395,54 @@ BOOST_AUTO_TEST_CASE(star_loop_category_change_inplace)
BOOST_CHECK_THROW
(
star_p
.
Parse
(),
IOException
);
}
BOOST_AUTO_TEST_CASE
(
star_try_float_conversions
)
{
BOOST_MESSAGE
(
" Running star_try_float_conversions tests..."
);
std
::
ifstream
s
(
"testfiles/loop_category_change_inplace.cif"
);
DataItemTestParser
star_p
(
s
);
StringRef
data
=
StringRef
(
"1.5"
,
3
);
BOOST_CHECK_CLOSE
(
star_p
.
TryGetFloat
(
data
,
"positive float test"
),
1.5
f
,
0.001
f
);
data
=
StringRef
(
"foo"
,
3
);
BOOST_CHECK_THROW
(
star_p
.
TryGetFloat
(
data
,
"negative float test"
),
IOException
);
BOOST_MESSAGE
(
" done."
);
}
BOOST_AUTO_TEST_CASE
(
star_try_int_conversions
)
{
BOOST_MESSAGE
(
" Running star_try_int_conversions tests..."
);
std
::
ifstream
s
(
"testfiles/loop_category_change_inplace.cif"
);
DataItemTestParser
star_p
(
s
);
StringRef
data
=
StringRef
(
"101"
,
3
);
BOOST_CHECK_EQUAL
(
star_p
.
TryGetInt
(
data
,
"positive int test"
),
101
);
data
=
StringRef
(
"foo"
,
3
);
BOOST_CHECK_THROW
(
star_p
.
TryGetInt
(
data
,
"negative int test"
),
IOException
);
BOOST_MESSAGE
(
" done."
);
}
BOOST_AUTO_TEST_CASE
(
star_try_bool_conversions
)
{
BOOST_MESSAGE
(
" Running star_try_bool_conversions tests..."
);
std
::
ifstream
s
(
"testfiles/loop_category_change_inplace.cif"
);
DataItemTestParser
star_p
(
s
);
StringRef
data
=
StringRef
(
"Y"
,
1
);
BOOST_CHECK
(
star_p
.
TryGetBool
(
data
,
"positive bool test ("
+
data
.
str
()
+
")"
));
data
=
StringRef
(
"y"
,
1
);
BOOST_CHECK
(
star_p
.
TryGetBool
(
data
,
"positive bool test ("
+
data
.
str
()
+
")"
));
data
=
StringRef
(
"N"
,
1
);
BOOST_CHECK
(
!
star_p
.
TryGetBool
(
data
,
"positive bool test ("
+
data
.
str
()
+
")"
));
data
=
StringRef
(
"n"
,
1
);
BOOST_CHECK
(
!
star_p
.
TryGetBool
(
data
,
"positive bool test ("
+
data
.
str
()
+
")"
));
data
=
StringRef
(
"J"
,
1
);
BOOST_CHECK_THROW
(
star_p
.
TryGetInt
(
data
,
"negative bool test ("
+
data
.
str
()
+
")"
),
IOException
);
data
=
StringRef
(
"Foo"
,
3
);
BOOST_CHECK_THROW
(
star_p
.
TryGetInt
(
data
,
"negative bool test ("
+
data
.
str
()
+
")"
),
IOException
);
BOOST_MESSAGE
(
" done."
);
}
BOOST_AUTO_TEST_SUITE_END
();
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