dodo
0.0.1
A C++ library to create containerized Linux services
|
Go to the documentation of this file.
31 #include <sys/types.h>
33 #include <sys/statvfs.h>
40 std::ifstream f(file);
51 if ( c < 32 || c == 127 ) {
63 case 10:
return " LF";
64 case 11:
return " VT";
65 case 12:
return " FF";
66 case 13:
return " CR";
67 case 14:
return " SO";
68 case 15:
return " SI";
69 case 16:
return "DLE";
70 case 17:
return "DC1";
71 case 18:
return "DC2";
72 case 19:
return "DC3";
73 case 20:
return "DC4";
74 case 21:
return "NAK";
75 case 22:
return "SYN";
76 case 23:
return "ETB";
77 case 24:
return "CAN";
78 case 25:
return " EM";
79 case 26:
return "SUB";
80 case 27:
return "ESC";
81 case 28:
return " FS";
82 case 29:
return " GS";
83 case 30:
return " RS";
84 case 31:
return " US";
85 case 127:
return "DEL";
86 default:
return "???";
88 }
else if ( c < 255 ) {
90 ss << std::setfill(
' ') << std::setw(3) << c;
94 ss << std::setw(3) << std::setfill(
'0') << std::hex << std::setw(3) << c;
106 std::ios_base::fmtflags orgflags = out.flags();
109 std::stringstream binary_line;
110 std::stringstream char_line;
111 while ( idx < s.size() ) {
112 if ( line_idx >= width ) {
113 out << std::setfill(
'0') << std::setw(6) << idx-width << binary_line.str() << std::endl;
114 out << std::string(6,
' ') << char_line.str() << std::endl;
115 binary_line.str(
"" );
119 unsigned int v = (
unsigned char)s[idx];
120 binary_line <<
' ' << std::setw(2) << std::setfill(
'0') << std::hex << v <<
' ';
121 char_line <<
strASCII( s[idx] ) <<
' ';
126 out << std::setfill(
'0') << std::setw(6) << idx-width << binary_line.str() << std::endl;
127 out << std::string(6,
' ') << char_line.str();
129 out.flags( orgflags );
134 long length = BIO_get_mem_data( bio, &data );
135 BIO_get_mem_data( bio, &data );
136 return std::string( data, length );
142 unsigned long error = 0;
145 while ( ( error = ERR_get_error() ) ) {
146 ERR_error_string_n( error, errbuf,
sizeof(errbuf) );
148 if ( terminator ) out << terminator;
155 std::stringstream ss;
162 std::ifstream ifs( filename.c_str() );
163 std::string line =
"";
165 getline( ifs, line );
171 std::ifstream ifs( filename.c_str() );
172 std::vector<std::string> tmp;
173 std::string line =
"";
174 getline( ifs, line );
175 if ( ifs.good() )
throw_Exception(
"failed to open '" << filename <<
"'" );
176 while ( ifs.good() ) {
177 tmp.push_back( line );
178 getline( ifs, line );
183 std::vector<std::string>
fileReadStrings(
const std::string &filename,
const std::regex& exp ) {
184 std::ifstream ifs( filename.c_str() );
185 std::vector<std::string> tmp;
186 std::string line =
"";
187 getline( ifs, line );
188 if ( !ifs.good() )
throw_Exception(
"failed to open '" << filename <<
"'" );
189 while ( ifs.good() ) {
191 if ( std::regex_match(line, m, exp ) ) tmp.push_back( line );
192 getline( ifs, line );
198 std::ostringstream o;
199 for (
auto c = s.cbegin(); c != s.cend(); c++ ) {
201 case '"': o <<
"\\\"";
break;
202 case '\\': o <<
"\\\\";
break;
203 case '\b': o <<
"\\b";
break;
204 case '\f': o <<
"\\f";
break;
205 case '\n': o <<
"\\n";
break;
206 case '\r': o <<
"\\r";
break;
207 case '\t': o <<
"\\t";
break;
209 if (
'\x00' <= *c && *c <=
'\x1f') {
211 << std::hex << std::setw(4) << std::setfill(
'0') << (int)*c;
223 int r = stat( path.c_str(), &buf );
224 if ( r != 0 )
return false;
225 result = S_ISREG(buf.st_mode);
226 result = result && ( !access( path.c_str(), F_OK | R_OK ) );
233 int r = stat( path.c_str(), &buf );
234 if ( r != 0 )
return false;
235 result = S_ISDIR(buf.st_mode);
242 int r = stat( path.c_str(), &buf );
243 if ( r != 0 )
return false;
244 result = result && S_ISDIR(buf.st_mode);
245 result = result && (S_IWUSR & buf.st_mode);
251 if ( statvfs( path.c_str(), &stat) != 0 )
return false;
252 avail = stat.f_bsize * stat.f_bavail;
257 struct stat stat_buf;
258 int rc = stat( filename.c_str(), &stat_buf );
259 return rc == 0 ? stat_buf.st_size : 0;
264 gmtime_r( &tv.tv_sec, &utc );
265 std::stringstream ss;
266 ss << std::setfill(
'0') << std::setw(4) << utc.tm_year + 1900 <<
"-";
267 ss << std::setfill(
'0') << std::setw(2) << utc.tm_mon+1 <<
"-";
268 ss << std::setfill(
'0') << std::setw(2) << utc.tm_mday <<
"T";
269 ss << std::setfill(
'0') << std::setw(2) << utc.tm_hour <<
":";
270 ss << std::setfill(
'0') << std::setw(2) << utc.tm_min <<
":";
271 ss << std::setfill(
'0') << std::setw(2) << utc.tm_sec <<
".";
272 ss << std::setfill(
'0') << std::setw(6) << tv.tv_usec <<
"Z";
276 template <
class T> T
YAML_read_key(
const YAML::Node &node,
const std::string& key ) {
278 return node[key].as<T>();
323 template std::string YAML_read_key<std::string>(
const YAML::Node &,
const std::string& );
332 const std::string& key,
333 const T& default_value ) {
335 return node[key].as<T>();
336 }
else return default_value;
373 template std::string YAML_read_key_default<std::string>(
const YAML::Node &,
const std::string&,
const std::string& );
template long YAML_read_key_default< long >(const YAML::Node &, const std::string &, const long &)
Instantiate template YAML_read_key for long.
std::string escapeJSON(const std::string &s)
Escape a JSOn string.
template double YAML_read_key< double >(const YAML::Node &, const std::string &)
Instantiate template YAML_read_key for double.
template unsigned int YAML_read_key< unsigned int >(const YAML::Node &, const std::string &)
Instantiate template YAML_read_key for unsigned int.
bool fileReadInt(const std::string &file, int &i)
Read from a file, expecting it to contain a (signed) int.
std::string fileReadString(const std::string &filename)
Read the file as a single string.
template uint16_t YAML_read_key< uint16_t >(const YAML::Node &, const std::string &)
Instantiate template YAML_read_key for uint16_t.
bool directoryExists(const std::string &path)
Return true when the directory exists.
template bool YAML_read_key< bool >(const YAML::Node &, const std::string &)
Instantiate template YAML_read_key for bool.
template size_t YAML_read_key_default< size_t >(const YAML::Node &, const std::string &, const size_t &)
Instantiate template YAML_read_key for size_t.
size_t writeSSLErrors(std::ostream &out, char terminator)
Write OpenSSL errors occurred in this thread to ostream, and clear their error state.
template double YAML_read_key_default< double >(const YAML::Node &, const std::string &, const double &)
Instantiate template YAML_read_key for double.
std::string strASCII(char c)
Return ASCII representation of char.
template unsigned int YAML_read_key_default< unsigned int >(const YAML::Node &, const std::string &, const unsigned int &)
Instantiate template YAML_read_key for unsigned int.
void dumpBinaryData(std::ostream &out, const std::string &s, size_t width)
Dump binary data to a std::ostream.
#define throw_Exception(what)
Throws an Exception, passes FILE and LINE to constructor.
bool availableFileSpace(const std::string &path, size_t &avail)
Return true when the free space could be determined, and set in avail.
std::string bio2String(BIO *bio)
Convert the data contents of an OpenSSL BIO to a std::string.
T YAML_read_key_default(const YAML::Node &node, const std::string &key, const T &default_value)
Template function to check existence and read YAML values of arbitrary type.
size_t getFileSize(const std::string &path)
Return the size of the file.
std::string getSSLErrors(char terminator)
Get all OpenSSL errors as a single string, and clear their error state.
Common and utility interfaces.
T YAML_read_key(const YAML::Node &node, const std::string &key)
Template function to check existence and read YAML values of arbitrary type.
template long YAML_read_key< long >(const YAML::Node &, const std::string &)
Instantiate template YAML_read_key for long.
std::vector< std::string > fileReadStrings(const std::string &filename)
Read the file as vector of strings.
template int YAML_read_key< int >(const YAML::Node &, const std::string &)
Instantiate template YAML_read_key for int.
std::string formatDateTimeUTC(const struct timeval &tv)
Return a datetime string in UTC (2020-07-01T20:14:36.442929Z)
bool fileReadAccess(const std::string &path)
Return true when the file exists and the calling user has read access.
template bool YAML_read_key_default< bool >(const YAML::Node &, const std::string &, const bool &)
Instantiate template YAML_read_key for bool.
template size_t YAML_read_key< size_t >(const YAML::Node &, const std::string &)
Instantiate template YAML_read_key for size_t.
bool directoryWritable(const std::string &path)
Return true when the directory exists and is writable to the caller.
template int YAML_read_key_default< int >(const YAML::Node &, const std::string &, const int &)
Instantiate template YAML_read_key for int.