PyNE C++
utils.h
1 
7 // Header for general library file.
8 
9 #ifndef PYNE_KMMHYNANYFF5BFMEYIP7TUNLHA
10 #define PYNE_KMMHYNANYFF5BFMEYIP7TUNLHA
11 
12 //standard libraries
13 #include <string>
14 #include <string.h>
15 #include <sstream>
16 #include <cctype>
17 #include <stdlib.h>
18 #include <iostream>
19 #include <iomanip>
20 #include <cmath>
21 #include <exception>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <cstdlib>
25 #include <vector>
26 #include <algorithm>
27 
28 #if (__GNUC__ >= 4)
29  #include <cmath>
30  #define isnan(x) std::isnan(x)
31 #else
32  #include <math.h>
33  #define isnan(x) __isnand((double)x)
34 #endif
35 
36 #ifdef __WIN_MSVC__
37  #define isnan(x) ((x) != (x))
38 #endif
39 
40 #ifndef JSON_IS_AMALGAMATION
41  #define JSON_IS_AMALGAMATION
42 #endif
43 
45 namespace pyne {
46 
47  void pyne_start ();
48 
50  extern std::string PYNE_DATA;
51  extern std::string NUC_DATA_PATH;
52  extern std::string VERSION;
53 
54  // String Transformations
56  static std::string digits = "0123456789";
58  static std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
60  static std::string words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
61 
65  std::string to_str(int t);
66  std::string to_str(unsigned int t);
67  std::string to_str(double t);
68  std::string to_str(bool t);
70 
71  int to_int(std::string s);
72 
73  double to_dbl(std::string s);
74 
77  double endftod_cpp(char * s);
78  double endftod_f(char * s);
79  extern double (*endftod)(char * s);
80 
81  void use_fast_endftod();
82 
84  std::string to_upper(std::string s);
85 
87  std::string to_lower(std::string s);
88 
90  std::string capitalize(std::string s);
91 
93  std::string comment_line_wrapping(std::string line, std::string comment_prefix = "",
94  int line_length = 79);
95 
100  std::string get_flag(char line[], int max_l);
101 
103  std::string remove_substring(std::string s, std::string substr);
104 
106  std::string remove_characters(std::string s, std::string chars);
107 
109  std::string replace_all_substrings(std::string s, std::string substr,
110  std::string repstr);
111 
113  std::string last_char(std::string s);
114 
117  std::string slice_from_end(std::string s, int n=-1, int l=1);
118 
120  bool ternary_ge(int a, int b, int c);
121 
123  bool contains_substring(std::string s, std::string substr);
124 
127  std::string natural_naming(std::string name);
128 
129  // split a string into a vector of string using a delimiter
130  std::vector<std::string> split_string(std::string lists, std::string delimiter = " ");
131 
132  // join the vector element into a string, each values will be delimited ny the delimiter
133  template<typename T>
134  std::string join_to_string(std::vector<T> vect, std::string delimiter = " "){
135  std::stringstream out;
136  out << std::setiosflags(std::ios::fixed) << std::setprecision(6);
137 
138  // ensure there is at least 1 element in the vector
139  if (vect.size() == 0)
140  return out.str();
141  // no delimiter needed before the first element
142  out << vect[0];
143  for( int i = 1; i < vect.size(); i++)
144  out << delimiter << vect[i];
145  return out.str();
146  };
147  template std::string join_to_string(std::vector<int> vect,
148  std::string delimiter);
149  template std::string join_to_string(std::vector<double> vect,
150  std::string delimiter);
151  template std::string join_to_string(std::vector<std::string> vect, std::string delimiter);
152 
154  double slope (double x2, double y2, double x1, double y1);
155 
158  double solve_line (double x, double x2, double y2, double x1, double y1);
159 
160  double tanh(double x);
161  double coth(double x);
162 
163 
164  // File Helpers
166  bool file_exists(std::string strfilename);
167 
168  // turns the filename string into the full file path
169  std::string get_full_filepath(char* filename);
170  // turns the filename string into the full file path
171  std::string get_full_filepath(std::string filename);
172 
173  // Message Helpers
174  extern bool USE_WARNINGS;
176  bool toggle_warnings();
177 
179  void warning(std::string s);
180 
183  class FileNotFound : public std::exception
184  {
185  public:
186 
189 
191  ~FileNotFound () throw () {};
192 
194  FileNotFound(std::string fname)
195  {
196  FNF_message = "File not found";
197  if (!fname.empty())
198  FNF_message += ": " + fname;
199  };
200 
202  virtual const char* what() const throw()
203  {
204  return FNF_message.c_str();
205  };
206 
207  private:
208  std::string FNF_message;
209  };
210 
212  class ValueError : public std::exception
213  {
214  public:
215 
217  ValueError () {};
218 
220  ~ValueError () throw () {};
221 
223  ValueError(std::string msg)
224  {
225  message = msg;
226  };
227 
229  virtual const char* what() const throw()
230  {
231  std::string msgstr ("ValueError: ");
232  if (!message.empty())
233  msgstr += message;
234  const char* msgstr_rtn = msgstr.c_str();
235  return msgstr_rtn;
236  };
237 
238  private:
239  std::string message;
240  };
241 
242 
243 // End PyNE namespace
244 }
245 
246 #endif // PYNE_KMMHYNANYFF5BFMEYIP7TUNLHA
double to_dbl(std::string s)
Converts a valid string to a float using atof().
Definition: utils.cpp:80
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
Definition: data.cpp:835
std::string get_flag(char line[], int max_l)
Definition: utils.cpp:200
std::string natural_naming(std::string name)
Definition: utils.cpp:274
std::string last_char(std::string s)
Returns the last character in a string.
Definition: utils.cpp:249
double coth(double x)
The hyperbolic cotangent function.
Definition: utils.cpp:341
std::string PYNE_DATA
Path to the directory containing the PyNE data.
Definition: utils.cpp:14
bool ternary_ge(int a, int b, int c)
Returns true if a <= b <= c and flase otherwise.
Definition: utils.cpp:261
std::string to_lower(std::string s)
Returns an all lower case copy of the string.
Definition: utils.cpp:160
Definition: utils.h:183
bool contains_substring(std::string s, std::string substr)
Returns true if substr is in s.
Definition: utils.cpp:267
bool toggle_warnings()
Toggles warnings on and off.
Definition: utils.cpp:396
double endftod_f(char *s)
Converts a string from ENDF format to a float.
Definition: utils.cpp:139
std::string NUC_DATA_PATH
Path to the nuc_data.h5 file.
Definition: utils.h:51
ValueError(std::string msg)
constructor with the filename fname.
Definition: utils.h:223
double endftod_cpp(char *s)
Definition: utils.cpp:84
~FileNotFound()
default destructor
Definition: utils.h:191
std::string slice_from_end(std::string s, int n=-1, int l=1)
Definition: utils.cpp:255
FileNotFound()
default constructor
Definition: utils.h:188
int to_int(std::string s)
Converts a string of digits to an int using atoi().
Definition: utils.cpp:76
std::string to_upper(std::string s)
switches endftod to fast cpp version
Definition: utils.cpp:153
ValueError()
default constructor
Definition: utils.h:217
~ValueError()
default destructor
Definition: utils.h:220
double slope(double x2, double y2, double x1, double y1)
Finds the slope of a line from the points (x1, y1) and (x2, y2).
Definition: utils.cpp:326
std::string comment_line_wrapping(std::string line, std::string comment_prefix="", int line_length=79)
Forms and returns the wrapped lines with a lenght up to line_lenght.
Definition: utils.cpp:167
FileNotFound(std::string fname)
constructor with the filename fname.
Definition: utils.h:194
std::string capitalize(std::string s)
Returns a capitalized copy of the string.
Definition: utils.cpp:187
virtual const char * what() const
Creates a helpful error message.
Definition: utils.h:202
void warning(std::string s)
Prints a warning message.
Definition: utils.cpp:401
A container representing enrichment cascades.
Definition: _atomic_data.h:16
std::string replace_all_substrings(std::string s, std::string substr, std::string repstr)
Replaces all instance of substr in s with repstr.
Definition: utils.cpp:237
bool file_exists(std::string strfilename)
Returns true if the file can be found.
Definition: utils.cpp:349
void pyne_start()
Initializes PyNE based on environment.
Definition: utils.cpp:18
double(* endftod)(char *s)
endftod function pointer. defaults to fortran
Definition: utils.cpp:147
std::string remove_characters(std::string s, std::string chars)
Removes all characters in the string chars from s.
Definition: utils.cpp:228
std::string VERSION
PyNE version number.
Definition: utils.cpp:16
virtual const char * what() const
Creates a helpful error message.
Definition: utils.h:229
double solve_line(double x, double x2, double y2, double x1, double y1)
Definition: utils.cpp:332
double tanh(double x)
The hyperbolic tangent function.
Definition: utils.cpp:337
Exception representing value errors of all kinds.
Definition: utils.h:212
std::string remove_substring(std::string s, std::string substr)
Creates a copy of s with all instances of substr taken out.
Definition: utils.cpp:217