BELFEM
0.9.0
Berkeley Lab Finite Element Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
fn_GT_parse.hpp
Go to the documentation of this file.
1
/*
2
* BELFEM -- The Berkeley Lab Finite Element Framework
3
* Copyright (c) 2026, The Regents of the University of California,
4
* through Lawrence Berkeley National Laboratory (subject to receipt of any required
5
* approvals from the U.S. Dept. of Energy). All rights reserved.
6
*
7
* Developers: Christian Messe, Gregory Giard
8
*
9
* See the top-level LICENSE file for the complete license and disclaimer.
10
*/
11
12
#ifndef BELFEM_FN_GT_PARSE_HPP
13
#define BELFEM_FN_GT_PARSE_HPP
14
15
#include <charconv>
16
17
#include "
typedefs.hpp
"
18
#include "
assert.hpp
"
19
20
namespace
belfem
21
{
22
namespace
gastables
23
{
24
//------------------------------------------------------------------------------
25
26
// strip leading/trailing whitespace and a leading '+'
27
// ( from_chars accepts neither )
28
inline
std::string_view
29
trim_field
(
const
std::string_view aField )
30
{
31
size_t
tBegin = aField.find_first_not_of(
" \t\r\n"
);
32
if
( tBegin == std::string_view::npos )
33
{
34
return
std::string_view();
35
}
36
size_t
tEnd = aField.find_last_not_of(
" \t\r\n"
);
37
std::string_view aView = aField.substr( tBegin, tEnd - tBegin + 1 );
38
if
( ! aView.empty() && aView.front() ==
'+'
)
39
{
40
aView.remove_prefix( 1 );
41
}
42
return
aView;
43
}
44
45
//------------------------------------------------------------------------------
46
51
inline
real
52
parse_real
(
const
std::string_view aField,
const
char
* aContext )
53
{
54
std::string_view tView =
trim_field
( aField );
55
56
real
tValue;
57
std::from_chars_result tResult =
58
std::from_chars( tView.data(), tView.data() + tView.size(), tValue );
59
60
BELFEM_ERROR
( tResult.ec == std::errc() && tResult.ptr == tView.data() + tView.size(),
61
"Malformed number '%.*s' while reading %s"
,
62
(
int
) aField.size(), aField.data(), aContext );
63
64
return
tValue;
65
}
66
67
//------------------------------------------------------------------------------
68
73
inline
real
74
parse_real_optional
(
const
std::string_view aField,
const
real
aFallback,
75
const
char
* aContext )
76
{
77
std::string_view tView =
trim_field
( aField );
78
79
if
( tView.empty() )
80
{
81
return
aFallback;
82
}
83
84
real
tValue;
85
std::from_chars_result tResult =
86
std::from_chars( tView.data(), tView.data() + tView.size(), tValue );
87
88
BELFEM_ERROR
( tResult.ec == std::errc() && tResult.ptr == tView.data() + tView.size(),
89
"Malformed number '%.*s' while reading %s"
,
90
(
int
) aField.size(), aField.data(), aContext );
91
92
return
tValue;
93
}
94
95
//------------------------------------------------------------------------------
96
100
inline
int
101
parse_int
(
const
std::string_view aField,
const
char
* aContext )
102
{
103
std::string_view tView =
trim_field
( aField );
104
105
int
aValue = 0;
106
std::from_chars_result tResult =
107
std::from_chars( tView.data(), tView.data() + tView.size(), aValue );
108
109
BELFEM_ERROR
( tResult.ec == std::errc() && tResult.ptr == tView.data() + tView.size(),
110
"Malformed integer '%.*s' while reading %s"
,
111
(
int
) aField.size(), aField.data(), aContext );
112
113
return
aValue;
114
}
115
116
//------------------------------------------------------------------------------
117
122
inline
std::string
123
parse_field
(
const
std::string & aLine,
const
size_t
aPos,
124
const
size_t
aCount,
const
char
* aContext )
125
{
126
BELFEM_ERROR
( aPos < aLine.length(),
127
"Line too short while reading %s : '%s'"
,
128
aContext, aLine.c_str() );
129
130
return
aLine.substr( aPos, aCount );
131
}
132
133
//------------------------------------------------------------------------------
134
}
135
}
136
#endif
//BELFEM_FN_GT_PARSE_HPP
assert.hpp
BELFEM_ERROR
#define BELFEM_ERROR(aCheck,...)
Definition
assert.hpp:264
belfem::gastables
Definition
cl_Gas.hpp:33
belfem::gastables::parse_int
int parse_int(const std::string_view aField, const char *aContext)
locale-independent, non-throwing replacement for std::stoi
Definition
fn_GT_parse.hpp:101
belfem::gastables::trim_field
std::string_view trim_field(const std::string_view aField)
Definition
fn_GT_parse.hpp:29
belfem::gastables::parse_real
real parse_real(const std::string_view aField, const char *aContext)
locale-independent, non-throwing replacement for std::stod in the fixed-column parsers; aborts with c...
Definition
fn_GT_parse.hpp:52
belfem::gastables::parse_field
std::string parse_field(const std::string &aLine, const size_t aPos, const size_t aCount, const char *aContext)
bounds-checked substring for fixed-column records; aborts with context if the line is shorter than th...
Definition
fn_GT_parse.hpp:123
belfem::gastables::parse_real_optional
real parse_real_optional(const std::string_view aField, const real aFallback, const char *aContext)
like parse_real, but an empty or whitespace-only field returns the given fallback instead of aborting
Definition
fn_GT_parse.hpp:74
belfem
USER GUIDES:
Definition
cl_Capacitor.cpp:16
belfem::real
double real
Definition
typedefs.hpp:36
typedefs.hpp
src
physics
gastables
fn_GT_parse.hpp
Generated by
1.18.0