-
Notifications
You must be signed in to change notification settings - Fork 221
2026PKUCourseHW5:replace atoi/atof with safe parsing helper #7142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
e539a53
0ede783
39c4f54
75ee501
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,6 +195,45 @@ void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, st | |
| return; | ||
| } | ||
|
|
||
| namespace//辅助函数用于后续类型替换 | ||
| { | ||
| int parse_int(const std::string& s) | ||
| { | ||
| try | ||
| { | ||
|
||
| size_t pos = 0; | ||
| int value = std::stoi(s, &pos); | ||
| if (pos != s.size()) | ||
| { | ||
| throw std::runtime_error("extra characters"); | ||
| } | ||
|
||
| return value; | ||
| } | ||
| catch (...) | ||
| { | ||
| throw std::runtime_error("Invalid integer value: " + s); | ||
| } | ||
| } | ||
|
|
||
| double parse_double(const std::string& s) | ||
| { | ||
| try | ||
| { | ||
| size_t pos = 0; | ||
| double value = std::stod(s, &pos); | ||
| if (pos != s.size()) | ||
| { | ||
| throw std::runtime_error("extra characters"); | ||
| } | ||
| return value; | ||
| } | ||
| catch (...) | ||
| { | ||
| throw std::runtime_error("Invalid floating-point value: " + s); | ||
| } | ||
|
||
| } | ||
| } | ||
|
|
||
| void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& pp) | ||
| { | ||
| 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 | |
| } | ||
| else if (name[ip] == "z_valence") | ||
| { | ||
| pp.zv = std::stod(val[ip]); | ||
| pp.zv = parse_double(val[ip]); | ||
| } | ||
| else if (name[ip] == "total_psenergy") | ||
| { | ||
| pp.etotps = atof(val[ip].c_str()); | ||
| pp.etotps = parse_double(val[ip]); | ||
| } | ||
| else if (name[ip] == "wfc_cutoff") | ||
| { | ||
| pp.ecutwfc = atof(val[ip].c_str()); | ||
| pp.ecutwfc = parse_double(val[ip]); | ||
| } | ||
| else if (name[ip] == "rho_cutoff") | ||
| { | ||
| pp.ecutrho = atof(val[ip].c_str()); | ||
| pp.ecutrho = parse_double(val[ip]); | ||
| } | ||
| else if (name[ip] == "l_max") | ||
| { | ||
| pp.lmax = atoi(val[ip].c_str()); | ||
| pp.lmax = parse_int(val[ip]); | ||
| } | ||
| else if (name[ip] == "l_max_rho") | ||
| { | ||
| this->lmax_rho = atoi(val[ip].c_str()); | ||
| this->lmax_rho = parse_int(val[ip]); | ||
| } | ||
| else if (name[ip] == "l_local") | ||
| { | ||
| this->lloc = atoi(val[ip].c_str()); | ||
| this->lloc = parse_int(val[ip]); | ||
| } | ||
| else if (name[ip] == "mesh_size") | ||
| { | ||
| pp.mesh = atoi(val[ip].c_str()); | ||
| pp.mesh = parse_int(val[ip]); | ||
| this->mesh_changed = false; | ||
| if (pp.mesh % 2 == 0) | ||
| { | ||
|
|
@@ -341,11 +380,11 @@ void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& p | |
| } | ||
| else if (name[ip] == "number_of_wfc") | ||
| { | ||
| pp.nchi = atoi(val[ip].c_str()); | ||
| pp.nchi = parse_int(val[ip]); | ||
| } | ||
| else if (name[ip] == "number_of_proj") | ||
| { | ||
| pp.nbeta = atoi(val[ip].c_str()); | ||
| pp.nbeta = parse_int(val[ip]); | ||
| } | ||
| else | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.