-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperiodic-table-data.cpp
More file actions
47 lines (39 loc) · 1.37 KB
/
periodic-table-data.cpp
File metadata and controls
47 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <nlohmann/json.hpp>
#include <boost/format.hpp>
#include <fstream>
#include <stdlib.h>
#include "periodic-table-data.h"
#include "xerror.h"
PeriodicTableData PeriodicTableData::singleInstance; // statically initialized instance
PeriodicTableData::PeriodicTableData() {
using json = nlohmann::json;
auto getStr = [](json &j) {return j.is_null() ? "" : j;};
auto getFlt = [](json &j) -> double {return j.is_null() ? 0. : (double)j;};
std::ifstream file;
file.open("contrib/Periodic-Table-JSON/PeriodicTableJSON.json", std::ifstream::in);
if (!file.is_open())
abort();
auto parsed = json::parse(file);
auto elts = parsed["elements"];
data.resize(elts.size());
unsigned eIdx = 0;
for (auto e : elts) {
auto &eData = data[eIdx++];
eData.name = e["name"];
eData.appearance = getStr(e["appearance"]);
eData.atomic_mass = e["atomic_mass"];
eData.boil = getFlt(e["boil"]);
eData.category = e["category"];
eData.symbol = e["symbol"];
symToElt[eData.symbol] = eIdx;
}
}
const PeriodicTableData::ElementData& PeriodicTableData::operator()(unsigned elt) const {
return data[elt-1];
}
unsigned PeriodicTableData::elementFromSymbol(const std::string &sym) const {
auto i = symToElt.find(sym);
if (i == symToElt.end())
ERROR(str(boost::format("Not an element name: %1%") % sym));
return i->second;
}