Cloudy
Spectral Synthesis Code for Astrophysics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
service.h
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2023 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 
4 #ifndef SERVICE_H_
5 #define SERVICE_H_
6 
7 #include <string>
8 #include <vector>
9 
10 extern const double pos_pow10[];
11 extern const int max_pow10;
12 extern const double neg_pow10[];
13 extern const int min_pow10;
14 
15 // remove whitespace from the end of a string
16 void trimTrailingWhiteSpace( std::string &str );
17 // remove whitespace form the end of a char array
18 void trimTrailingWhiteSpace( char *str );
19 // remove whitespace from the beginning and end of a string
20 void trimWhiteSpace( string &str );
21 
22 // helper routine for DataParser -- efficiently read double
23 void FPRead(istringstream& iss, const string& s, double& value);
24 
25 // helper routine for DataParser -- efficiently read sys_float
26 inline void FPRead(istringstream& iss, const string& s, sys_float& value)
27 {
28  double x;
29  FPRead(iss, s, x);
30  value = sys_float(x);
31 }
32 
33 // helper routine for DataParser -- efficiently read signed 64-bit integer
34 void IntRead(istringstream& iss, const string& s, long long& value);
35 
36 // helper routine for DataParser -- efficiently read long
37 inline void IntRead(istringstream& iss, const string& s, long& value)
38 {
39  long long x;
40  IntRead(iss, s, x);
41  value = long(x);
42 }
43 
44 // helper routine for DataParser -- efficiently read int
45 inline void IntRead(istringstream& iss, const string& s, int& value)
46 {
47  long long x;
48  IntRead(iss, s, x);
49  value = int(x);
50 }
51 
52 // helper routine for DataParser -- efficiently read unsigned 64-bit integer
53 void IntRead(istringstream& iss, const string& s, unsigned long long& value);
54 
55 // helper routine for DataParser -- efficiently read unsigned int
56 inline void IntRead(istringstream& iss, const string& s, unsigned long& value)
57 {
58  unsigned long long x;
59  IntRead(iss, s, x);
60  value = (unsigned long)x;
61 }
62 
63 // helper routine for DataParser -- efficiently read unsigned int
64 inline void IntRead(istringstream& iss, const string& s, unsigned int& value)
65 {
66  unsigned long long x;
67  IntRead(iss, s, x);
68  value = (unsigned int)x;
69 }
70 
78 
80 void Split(const std::string& str, // input string
81  const std::string& sep, // separator, may be multiple characters
82  std::vector<std::string>& lst, // the separated items will be appended here
83  split_mode mode); // see above
84 
85 inline bool FindAndReplace(std::string& str,
86  const std::string& substr,
87  const std::string& newstr)
88 {
89  std::string::size_type ptr = str.find( substr );
90  if( ptr != std::string::npos )
91  str.replace( ptr, substr.length(), newstr );
92  return ptr != std::string::npos;
93 }
94 
95 inline bool FindAndErase(std::string& str,
96  const std::string& substr)
97 {
98  return FindAndReplace( str, substr, "" );
99 }
100 
101 void service(double tau, double a, double beta);
102 
104 inline void wr_block(const void *ptr,
105  size_t len,
106  FILE *fdes)
107 {
108  if( fwrite(ptr,len,size_t(1),fdes) != 1 ) {
109  printf( "wr_block: error writing to file\n" );
110  fclose(fdes);
112  }
113 }
114 
116 inline void wr_block(const void *ptr,
117  size_t len,
118  const char *fnam)
119 {
120  FILE *fdes = open_data( fnam, "wb" );
121  wr_block( ptr, len, fdes );
122  fclose(fdes);
123 }
124 
126 inline void rd_block(void *ptr,
127  size_t len,
128  FILE *fdes)
129 {
130  if( fread(ptr,len,size_t(1),fdes) != 1 ) {
131  printf( "rd_block: error reading from file\n" );
132  fclose(fdes);
134  }
135 }
136 
138 inline void rd_block(void *ptr,
139  size_t len,
140  const char *fnam)
141 {
142  FILE *fdes = open_data( fnam, "rb", AS_LOCAL_ONLY );
143  rd_block( ptr, len, fdes );
144  fclose(fdes);
145 }
146 
147 #endif /* SERVICE_ */
Definition: service.h:77
split_mode
Definition: service.h:77
Definition: service.h:77
void Split(const std::string &str, const std::string &sep, std::vector< std::string > &lst, split_mode mode)
bool FindAndErase(std::string &str, const std::string &substr)
Definition: service.h:95
FILE * open_data(const string &fname, const string &mode, access_scheme scheme, string *rpath)
Definition: cpu.cpp:722
void trimWhiteSpace(string &str)
Definition: service.cpp:159
const int min_pow10
Definition: service.cpp:390
void IntRead(istringstream &iss, const string &s, long long &value)
Definition: service.cpp:649
void trimTrailingWhiteSpace(std::string &str)
const double neg_pow10[]
Definition: service.cpp:356
void service(double tau, double a, double beta)
#define EXIT_FAILURE
Definition: cddefines.h:175
float sys_float
Definition: cddefines.h:130
void rd_block(void *ptr, size_t len, FILE *fdes)
Definition: service.h:126
#define cdEXIT(FAIL)
Definition: cddefines.h:480
void wr_block(const void *ptr, size_t len, FILE *fdes)
Definition: service.h:104
Definition: cpu.h:258
bool FindAndReplace(std::string &str, const std::string &substr, const std::string &newstr)
Definition: service.h:85
Definition: service.h:77
const int max_pow10
Definition: service.cpp:354
void FPRead(istringstream &iss, const string &s, double &value)
Definition: service.cpp:548
const double pos_pow10[]
Definition: service.cpp:320