Cloudy
Spectral Synthesis Code for Astrophysics
Loading...
Searching...
No Matches
service.h
Go to the documentation of this file.
1/* This file is part of Cloudy and is copyright (C)1978-2025 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
10extern const double pos_pow10[];
11extern const int max_pow10;
12extern const double neg_pow10[];
13extern const int min_pow10;
14
15// remove whitespace from the end of a string
16void trimTrailingWhiteSpace( std::string &str );
17// remove whitespace form the end of a char array
18void trimTrailingWhiteSpace( char *str );
19// remove whitespace from the beginning and end of a string
20void trimWhiteSpace( string &str );
21
22// helper routine for DataParser -- efficiently read double
23void FPRead(istringstream& iss, const string& s, double& value);
24
25// helper routine for DataParser -- efficiently read sys_float
26inline 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
34void IntRead(istringstream& iss, const string& s, long long& value);
35
36// helper routine for DataParser -- efficiently read long
37inline 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
45inline 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
53void IntRead(istringstream& iss, const string& s, unsigned long long& value);
54
55// helper routine for DataParser -- efficiently read unsigned int
56inline 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
64inline 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
80void 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
85inline 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
95inline bool FindAndErase(std::string& str,
96 const std::string& substr)
97{
98 return FindAndReplace( str, substr, "" );
99}
100
101void service(double tau, double a, double beta);
102
104inline void wr_block(const void *ptr,
105 size_t len,
106 FILE *fdes,
107 const string& fnam = string())
108{
109 if( fwrite(ptr,len,size_t(1),fdes) != 1 ) {
110 fprintf( ioQQQ, "wr_block: error writing to file %s\n", fnam.c_str() );
111 fclose(fdes);
113 }
114}
115
117inline void wr_block(const void *ptr,
118 size_t len,
119 const string& fnam)
120{
121 FILE *fdes = open_data( fnam, "wb" );
122 wr_block( ptr, len, fdes, fnam );
123 fclose(fdes);
124}
125
127inline void rd_block(void *ptr,
128 size_t len,
129 FILE *fdes,
130 const string& fnam)
131{
132 if( fread(ptr,len,size_t(1),fdes) != 1 ) {
133 fprintf( ioQQQ, "rd_block: error reading from file %s\n", fnam.c_str() );
134 fclose(fdes);
136 }
137}
138
140inline void rd_block(void *ptr,
141 size_t len,
142 const string& fnam)
143{
144 FILE *fdes = open_data( fnam, "rb", AS_LOCAL_ONLY );
145 rd_block( ptr, len, fdes, fnam );
146 fclose(fdes);
147}
148
150const uintmax_t FS_UNKNOWN = static_cast<uintmax_t>(-1);
151
153uintmax_t FileSize(const string& fpath);
154
155#endif /* SERVICE_ */
FILE * ioQQQ
Definition cddefines.cpp:9
float sys_float
Definition cddefines.h:131
#define EXIT_FAILURE
Definition cddefines.h:188
#define cdEXIT(FAIL)
Definition cddefines.h:493
int fprintf(const Output &stream, const char *format,...)
Definition service.cpp:1328
FILE * open_data(const string &fname, const string &mode, access_scheme scheme, string *rpath)
Definition cpu.cpp:815
@ AS_LOCAL_ONLY
Definition cpu.h:258
const double neg_pow10[]
Definition service.cpp:359
const int min_pow10
Definition service.cpp:393
const int max_pow10
Definition service.cpp:357
const double pos_pow10[]
Definition service.cpp:323
void IntRead(istringstream &iss, const string &s, long long &value)
Definition service.cpp:652
void service(double tau, double a, double beta)
void trimTrailingWhiteSpace(std::string &str)
void trimWhiteSpace(string &str)
Definition service.cpp:162
void wr_block(const void *ptr, size_t len, FILE *fdes, const string &fnam=string())
Definition service.h:104
const uintmax_t FS_UNKNOWN
Definition service.h:150
void FPRead(istringstream &iss, const string &s, double &value)
Definition service.cpp:551
void rd_block(void *ptr, size_t len, FILE *fdes, const string &fnam)
Definition service.h:127
bool FindAndErase(std::string &str, const std::string &substr)
Definition service.h:95
bool FindAndReplace(std::string &str, const std::string &substr, const std::string &newstr)
Definition service.h:85
uintmax_t FileSize(const string &fpath)
Definition service.cpp:1890
void Split(const std::string &str, const std::string &sep, std::vector< std::string > &lst, split_mode mode)
split_mode
Definition service.h:77
@ SPM_RELAX
Definition service.h:77
@ SPM_STRICT
Definition service.h:77
@ SPM_KEEP_EMPTY
Definition service.h:77