1 // -*- C -*-
2
3 /*
4 * Gnome Chemisty Utils
5 * chemistry.cc
6 *
7 * Copyright (C) 2003-2011 Jean Bréfort <jean.brefort@normalesup.org>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 * USA
23 */
24
25 #include "config.h"
26 #include "chemistry.h"
27 #include "element.h"
28 #include <sstream>
29 #include <cstdarg>
30 #include <cstring>
31
32 extern "C"
33 {
34
35 using namespace gcu;
gcu_element_get_default_color(int Z)36 const double* gcu_element_get_default_color (int Z)
37 {
38 return Element::GetElement (Z)->GetDefaultColor ();
39 }
40
gcu_element_get_symbol(int Z)41 char const *gcu_element_get_symbol (int Z)
42 {
43 return Element::Symbol (Z);
44 }
45
gcu_element_get_name(int Z)46 char const *gcu_element_get_name (int Z)
47 {
48 return Element::GetElement (Z)->GetName ();
49 }
50
gcu_element_get_Z(char const * symbol)51 int gcu_element_get_Z (char const *symbol)
52 {
53 return Element::Z (symbol);
54 }
55
gcu_element_get_radius(GcuAtomicRadius * radius)56 int gcu_element_get_radius (GcuAtomicRadius *radius)
57 {
58 return Element::GetRadius (radius);
59 }
60
gcu_element_get_electronegativity(GcuElectronegativity * en)61 int gcu_element_get_electronegativity (GcuElectronegativity *en)
62 {
63 return Element::GetElectronegativity(en);
64 }
65
gcu_element_get_radii(int Z)66 const GcuAtomicRadius** gcu_element_get_radii (int Z)
67 {
68 return Element::GetElement (Z)->GetRadii ();
69 }
70
gcu_element_get_electronegativities(int Z)71 const GcuElectronegativity** gcu_element_get_electronegativities (int Z)
72 {
73 return Element::GetElement (Z)->GetElectronegativities ();
74 }
75
gcu_element_load_databases(char const * name,...)76 void gcu_element_load_databases (char const *name, ...)
77 {
78 va_list l;
79 char const *base = name;
80 va_start (l, name);
81 while (base != NULL) {
82 if (!strcmp (base, "radii"))
83 Element::LoadRadii ();
84 else if (!strcmp (base, "elecprops"))
85 Element::LoadElectronicProps ();
86 else if (!strcmp (base, "isotopes"))
87 Element::LoadIsotopes ();
88 base = va_arg (l, char const*);
89 }
90 va_end (l);
91 }
92
gcu_value_get_string(GcuValue const * value)93 char* gcu_value_get_string (GcuValue const *value)
94 {
95 std::ostringstream s;
96 char const *cloc = setlocale (LC_NUMERIC, NULL);
97 if (cloc != NULL) {
98 std::locale loc (cloc);
99 s.imbue (loc);
100 }
101 char *str;
102 if (value->value < 0)
103 s << "−";
104 if (value->delta > 0) {
105 int delta = value->delta, prec = value->prec;
106 while (delta >= 100) {
107 delta /= 10;
108 prec--;
109 }
110 // FIXME: what to do if we have a negative precision?
111 s.precision (prec);
112 s << std::fixed << fabs (value->value) << '(' << delta << ')';
113 } else {
114 s.precision (value->prec);
115 s << std::fixed << fabs (value->value);
116 }
117 str = strdup (s.str ().c_str ());
118 return str;
119 }
120
gcu_dimensional_value_get_string(GcuDimensionalValue const * value)121 char *gcu_dimensional_value_get_string (GcuDimensionalValue const *value)
122 {
123 std::ostringstream s;
124 char const *cloc = setlocale (LC_NUMERIC, NULL);
125 if (cloc != NULL) {
126 std::locale loc (cloc);
127 s.imbue (loc);
128 }
129 char *str;
130 if (value->value < 0)
131 s << "−";
132 if (value->delta > 0) {
133 int delta = value->delta, prec = value->prec;
134 while (delta >= 100) {
135 delta /= 10;
136 prec--;
137 }
138 // FIXME: what to do if we have a negative precision?
139 s.precision (prec);
140 s << std::fixed << fabs (value->value) << '(' << delta << ") " << value->unit;
141 } else {
142 s.precision (value->prec);
143 s << std::fixed << fabs (value->value) << " " << value->unit;
144 }
145 str = strdup (s.str ().c_str ());
146 return str;
147 }
148
gcu_element_get_ionization_energy(int Z,int rank)149 GcuDimensionalValue const *gcu_element_get_ionization_energy (int Z, int rank)
150 {
151 return Element::GetElement(Z)->GetIonizationEnergy (rank);
152 }
153
gcu_element_get_electron_affinity(int Z,int rank)154 GcuDimensionalValue const *gcu_element_get_electron_affinity (int Z, int rank)
155 {
156 return Element::GetElement(Z)->GetElectronAffinity (rank);
157 }
158
gcu_element_get_weight_as_string(int Z)159 char *gcu_element_get_weight_as_string (int Z)
160 {
161 Element *elt = Element::GetElement (Z);
162 gcu::DimensionalValue const *value = (elt)? elt->GetWeight (): NULL;
163 if (value) {
164 GcuDimensionalValue val = value->GetValue ();
165 return elt->GetStability ()? gcu_value_get_string (reinterpret_cast < GcuValue * > (&val)): g_strdup_printf("(%g)", val.value);
166 } else
167 return NULL;
168 }
169
gcu_element_get_weight(int Z)170 double gcu_element_get_weight (int Z)
171 {
172 Element *elt = Element::GetElement (Z);
173 return elt? elt->GetWeight ()->GetAsDouble (): 0.;
174 }
175
gcu_element_get_electronic_configuration(int Z)176 char const *gcu_element_get_electronic_configuration (int Z)
177 {
178 Element *elt = Element::GetElement (Z);
179 return (elt)? elt->GetElectronicConfiguration ().c_str (): NULL;
180 }
181
182 } //extern "C"
183