1#ifndef PSEUDO_DETECT_FORMAT_HPP
2#define PSEUDO_DETECT_FORMAT_HPP
25#include <rapidxml.hpp>
32namespace pseudopotential {
34static pseudopotential::format detect_format(
const std::string &filename) {
37 struct stat file_stat;
38 if (
stat(filename.c_str(), &file_stat) == -1)
39 return pseudopotential::format::FILE_NOT_FOUND;
40 if (S_ISDIR(file_stat.st_mode))
41 return pseudopotential::format::FILE_NOT_FOUND;
44 std::ifstream
file(filename.c_str());
47 return pseudopotential::format::FILE_NOT_FOUND;
50 std::string extension = filename.substr(filename.find_last_of(
".") + 1);
51 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
53 if (extension ==
"psp8" || extension ==
"drh")
54 return pseudopotential::format::PSP8;
55 if (extension ==
"psf")
56 return pseudopotential::format::PSF;
57 if (extension ==
"cpi")
58 return pseudopotential::format::CPI;
59 if (extension ==
"fhi")
60 return pseudopotential::format::FHI;
61 if (extension ==
"hgh")
62 return pseudopotential::format::HGH;
65 std::vector<char> buffer((std::istreambuf_iterator<char>(
file)),
66 std::istreambuf_iterator<char>());
67 buffer.push_back(
'\0');
69 rapidxml::xml_document<> doc;
71 doc.parse<0>(&buffer[0]);
73 if (doc.first_node(
"fpmd:species"))
74 return pseudopotential::format::QSO;
75 if (doc.first_node(
"qbox:species"))
76 return pseudopotential::format::QSO;
77 if (doc.first_node(
"PP_INFO"))
78 return pseudopotential::format::UPF1;
79 if (doc.first_node(
"UPF"))
80 return pseudopotential::format::UPF2;
81 if (doc.first_node(
"psml"))
82 return pseudopotential::format::PSML;
83 }
catch (rapidxml::parse_error xml_error) {
94 return pseudopotential::format::UNKNOWN;
if write to the Free Software Franklin Fifth USA !The assertions are ignored if the code is compiled in not debug when !prints out the assertion string
if write to the Free Software Franklin Fifth USA !The assertions are ignored if the code is compiled in not debug when !prints out the assertion the file
int stat(const char *__restrict __file, struct stat *__restrict __buf) __attribute__((__nothrow__