Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
lib4neuro
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
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
MolDyn
lib4neuro
Commits
92d451a6
Commit
92d451a6
authored
6 years ago
by
Martin Beseda
Browse files
Options
Downloads
Patches
Plain Diff
ENH: String-to-number cast was made more robust using Boost.
parent
965984a1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/CSVReader/CSVReader.cpp
+20
-4
20 additions, 4 deletions
src/CSVReader/CSVReader.cpp
with
20 additions
and
4 deletions
src/CSVReader/CSVReader.cpp
+
20
−
4
View file @
92d451a6
...
...
@@ -7,10 +7,14 @@
#include
<sstream>
#include
<filesystem>
#include
<regex>
#include
<algorithm>
#include
<boost/lexical_cast.hpp>
#include
<boost/algorithm/string/erase.hpp>
#include
"CSVReader.h"
#include
"../exceptions.h"
namespace
lib4neuro
{
CSVReader
::
CSVReader
(
std
::
string
file_path
,
std
::
string
delimiter
,
bool
ignore_first_line
)
{
if
(
!
std
::
filesystem
::
exists
(
file_path
))
{
...
...
@@ -79,17 +83,29 @@ namespace lib4neuro {
//TODO check empty values in data
std
::
vector
<
double
>
input
;
for
(
auto
ind
:
*
input_col_indices
)
{
std
::
string
s
;
try
{
/* Remove remaining spaces */
s
=
line
.
at
(
ind
);
boost
::
algorithm
::
erase_all
(
s
,
" "
);
/* Strip BOM */
// TODO solve in another way - work properly with different encodings!
boost
::
algorithm
::
erase_all
(
s
,
"\uEFBBBF"
);
// UTF-8
boost
::
algorithm
::
erase_all
(
s
,
"\uFEFF"
);
// UTF-16
/* Check, if the string is a number */
if
(
!
std
::
regex_match
(
line
.
at
(
ind
),
std
::
regex
(
(
"((
\\
+|-)?[[:digit:]]+)(
\\
.(([[:digit:]]+)?))?"
)
)))
{
THROW_RUNTIME_ERROR
(
std
::
string
(
"Value
\"
"
)
+
line
.
at
(
ind
)
+
"
\"
is not numerical and so it cannot be used in Data Set!"
);
}
double
tmp
=
boost
::
lexical_cast
<
double
>
(
s
);
/* Add loaded number to the vector of inputs */
input
.
push_back
(
std
::
stod
(
line
.
at
(
ind
))
);
input
.
push_back
(
tmp
);
}
catch
(
const
std
::
out_of_range
&
e
)
{
THROW_OUT_OF_RANGE_ERROR
(
"Non-existing index specified ("
+
std
::
to_string
(
ind
)
+
")!"
);
}
catch
(
const
boost
::
bad_lexical_cast
&
e
)
{
THROW_RUNTIME_ERROR
(
"Value
\"
"
+
s
+
"
\"
is not numerical and so it cannot be used in Data Set!"
);
}
}
...
...
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