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{
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
116inline 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
126inline 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
138inline 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_ */
float sys_float
Definition cddefines.h:130
#define EXIT_FAILURE
Definition cddefines.h:175
#define cdEXIT(FAIL)
Definition cddefines.h:480
FILE * open_data(const string &fname, const string &mode, access_scheme scheme, string *rpath)
Definition cpu.cpp:722
@ AS_LOCAL_ONLY
Definition cpu.h:258
const double neg_pow10[]
Definition service.cpp:356
const int min_pow10
Definition service.cpp:390
const int max_pow10
Definition service.cpp:354
const double pos_pow10[]
Definition service.cpp:320
void IntRead(istringstream &iss, const string &s, long long &value)
Definition service.cpp:649
void service(double tau, double a, double beta)
void trimTrailingWhiteSpace(std::string &str)
void trimWhiteSpace(string &str)
Definition service.cpp:159
void FPRead(istringstream &iss, const string &s, double &value)
Definition service.cpp:548
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
void rd_block(void *ptr, size_t len, FILE *fdes)
Definition service.h:126
void Split(const std::string &str, const std::string &sep, std::vector< std::string > &lst, split_mode mode)
void wr_block(const void *ptr, size_t len, FILE *fdes)
Definition service.h:104
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