Skip to content

Commit e539a53

Browse files
author
dyzheng
committed
No.2:replace atoi/atof with safe parsing helper
1 parent e97cf59 commit e539a53

2 files changed

Lines changed: 49 additions & 10 deletions

File tree

32 KB
Binary file not shown.

source/source_cell/read_pp_upf201.cpp

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,45 @@ void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, st
195195
return;
196196
}
197197

198+
namespace//辅助函数用于后续类型替换
199+
{
200+
int parse_int(const std::string& s)
201+
{
202+
try
203+
{
204+
size_t pos = 0;
205+
int value = std::stoi(s, &pos);
206+
if (pos != s.size())
207+
{
208+
throw std::runtime_error("extra characters");
209+
}
210+
return value;
211+
}
212+
catch (...)
213+
{
214+
throw std::runtime_error("Invalid integer value: " + s);
215+
}
216+
}
217+
218+
double parse_double(const std::string& s)
219+
{
220+
try
221+
{
222+
size_t pos = 0;
223+
double value = std::stod(s, &pos);
224+
if (pos != s.size())
225+
{
226+
throw std::runtime_error("extra characters");
227+
}
228+
return value;
229+
}
230+
catch (...)
231+
{
232+
throw std::runtime_error("Invalid floating-point value: " + s);
233+
}
234+
}
235+
}
236+
198237
void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& pp)
199238
{
200239
if (!ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_HEADER"))
@@ -303,35 +342,35 @@ void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& p
303342
}
304343
else if (name[ip] == "z_valence")
305344
{
306-
pp.zv = std::stod(val[ip]);
345+
pp.zv = parse_double(val[ip]);
307346
}
308347
else if (name[ip] == "total_psenergy")
309348
{
310-
pp.etotps = atof(val[ip].c_str());
349+
pp.etotps = parse_double(val[ip]);
311350
}
312351
else if (name[ip] == "wfc_cutoff")
313352
{
314-
pp.ecutwfc = atof(val[ip].c_str());
353+
pp.ecutwfc = parse_double(val[ip]);
315354
}
316355
else if (name[ip] == "rho_cutoff")
317356
{
318-
pp.ecutrho = atof(val[ip].c_str());
357+
pp.ecutrho = parse_double(val[ip]);
319358
}
320359
else if (name[ip] == "l_max")
321360
{
322-
pp.lmax = atoi(val[ip].c_str());
361+
pp.lmax = parse_int(val[ip]);
323362
}
324363
else if (name[ip] == "l_max_rho")
325364
{
326-
this->lmax_rho = atoi(val[ip].c_str());
365+
this->lmax_rho = parse_int(val[ip]);
327366
}
328367
else if (name[ip] == "l_local")
329368
{
330-
this->lloc = atoi(val[ip].c_str());
369+
this->lloc = parse_int(val[ip]);
331370
}
332371
else if (name[ip] == "mesh_size")
333372
{
334-
pp.mesh = atoi(val[ip].c_str());
373+
pp.mesh = parse_int(val[ip]);
335374
this->mesh_changed = false;
336375
if (pp.mesh % 2 == 0)
337376
{
@@ -341,11 +380,11 @@ void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& p
341380
}
342381
else if (name[ip] == "number_of_wfc")
343382
{
344-
pp.nchi = atoi(val[ip].c_str());
383+
pp.nchi = parse_int(val[ip]);
345384
}
346385
else if (name[ip] == "number_of_proj")
347386
{
348-
pp.nbeta = atoi(val[ip].c_str());
387+
pp.nbeta = parse_int(val[ip]);
349388
}
350389
else
351390
{

0 commit comments

Comments
 (0)