7 #ifndef __STDC_FORMAT_MACROS
9 #define __STDC_FORMAT_MACROS
28 #ifdef CVMFS_NAMESPACE_GUARD
29 namespace CVMFS_NAMESPACE_GUARD {
32 const char b64_table[] = {
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
33 'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
34 'W',
'X',
'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
35 'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
36 's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'0',
'1',
'2',
37 '3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'};
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60,
46 61, -1, -1, -1, 0, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
47 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1,
48 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
49 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68 const std::string::value_type b)
const {
69 return std::tolower(a) == std::tolower(b);
79 snprintf(buffer,
sizeof(buffer),
"%" PRId64, value);
80 return string(buffer);
85 snprintf(buffer,
sizeof(buffer),
"%" PRIu64, value);
86 return string(buffer);
91 snprintf(buffer,
sizeof(buffer),
"%02x", value);
92 return string(buffer);
97 snprintf(buffer,
sizeof(buffer),
"%.03f", value);
98 return string(buffer);
107 localtime_r(&seconds, ×tamp);
109 gmtime_r(&seconds, ×tamp);
112 const char *months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
113 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"};
115 snprintf(buffer,
sizeof(buffer),
"%d %s %d %02d:%02d:%02d", timestamp.tm_mday,
116 months[timestamp.tm_mon], timestamp.tm_year + 1900,
117 timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec);
119 return string(buffer);
127 localtime_r(&seconds, ×tamp);
129 const char *months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
130 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"};
133 snprintf(buffer,
sizeof(buffer),
"%d %s %d %02d:%02d:%02d %s",
134 timestamp.tm_mday, months[timestamp.tm_mon],
135 timestamp.tm_year + 1900, timestamp.tm_hour, timestamp.tm_min,
136 timestamp.tm_sec, timestamp.tm_zone);
138 return string(buffer);
146 const char *months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
147 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"};
148 const char *day_of_week[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"};
151 time_t now = time(NULL);
152 gmtime_r(&now, ×tamp);
155 snprintf(buffer,
sizeof(buffer),
"%s, %02d %s %d %02d:%02d:%02d %s",
156 day_of_week[timestamp.tm_wday], timestamp.tm_mday,
157 months[timestamp.tm_mon], timestamp.tm_year + 1900,
158 timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec,
160 return string(buffer);
169 time_t now = time(NULL);
170 gmtime_r(&now, ×tamp);
173 snprintf(buffer,
sizeof(buffer),
"%04d%02d%02dT%02d%02d%02dZ",
174 timestamp.tm_year + 1900, timestamp.tm_mon + 1, timestamp.tm_mday,
175 timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec);
176 return string(buffer);
185 gmtime_r(&when, ×tamp);
188 snprintf(buffer,
sizeof(buffer),
"%04d%02d%02d%02d%02d%02d",
189 timestamp.tm_year + 1900, timestamp.tm_mon + 1, timestamp.tm_mday,
190 timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec);
191 return string(buffer);
197 int64_t msec = value.tv_sec * 1000;
198 msec += value.tv_usec / 1000;
199 snprintf(buffer,
sizeof(buffer),
"%" PRId64
".%03d", msec,
200 static_cast<int>(value.tv_usec % 1000));
201 return string(buffer);
210 unsigned length = iso8601.length();
214 if ((iso8601[4] !=
'-') || (iso8601[7] !=
'-') || (iso8601[10] !=
'T')
215 || (iso8601[13] !=
':') || (iso8601[16] !=
':') || (iso8601[19] !=
'Z')) {
220 memset(&tm_wl, 0,
sizeof(
struct tm));
221 tm_wl.tm_year =
static_cast<int>(
String2Int64(iso8601.substr(0, 4))) - 1900;
222 tm_wl.tm_mon =
static_cast<int>(
String2Int64(iso8601.substr(5, 2))) - 1;
223 tm_wl.tm_mday =
static_cast<int>(
String2Int64(iso8601.substr(8, 2)));
224 tm_wl.tm_hour =
static_cast<int>(
String2Int64(iso8601.substr(11, 2)));
225 tm_wl.tm_min =
static_cast<int>(
String2Int64(iso8601.substr(14, 2)));
226 tm_wl.tm_sec =
static_cast<int>(
String2Int64(iso8601.substr(17, 2)));
227 utc_time = timegm(&tm_wl);
236 sscanf(value.c_str(),
"%" PRId64, &result);
242 if (sscanf(value.c_str(),
"%" PRIu64, &result) == 1) {
260 long long myval = strtoll(value.c_str(), &endptr, 10);
261 if ((value.size() == 0) || (endptr != (value.c_str() + value.size()))
276 sscanf(value.c_str(),
"%" PRIu64
" %" PRIu64, a, b);
280 const bool ignore_case) {
281 if (prefix.length() > str.length())
284 for (
unsigned i = 0, l = prefix.length(); i < l; ++i) {
286 if (toupper(str[i]) != toupper(prefix[i]))
289 if (str[i] != prefix[i])
296 bool HasSuffix(
const std::string &str,
const std::string &suffix,
297 const bool ignore_case) {
298 if (suffix.size() > str.size())
300 const IgnoreCaseComperator icmp;
302 ? std::equal(suffix.rbegin(), suffix.rend(), str.rbegin(), icmp)
303 : std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
312 vector<string> result;
315 if (1 == max_chunks) {
316 result.push_back(str);
321 const unsigned size = str.size();
325 for (i = 0; i <
size; ++i) {
326 if (str[i] == delim) {
327 result.push_back(str.substr(marker, i - marker));
331 if (++chunks == max_chunks)
337 result.push_back(str.substr(marker));
342 size_t pos_start = 0, pos_end = 0, delim_len = delim.length();
343 std::string substring;
344 std::vector<std::string> result;
346 while ((pos_end = str.find(delim, pos_start)) != string::npos) {
347 substring = str.substr(pos_start, pos_end - pos_start);
348 pos_start = pos_end + delim_len;
349 result.push_back(substring);
352 result.push_back(str.substr(pos_start));
356 string JoinStrings(
const vector<string> &strings,
const string &joint) {
358 const unsigned size = strings.size();
362 for (
unsigned i = 1; i <
size; ++i)
363 result += joint + strings[i];
370 map<char, string> *content) {
373 while (pos < buffer_size) {
374 if (static_cast<char>(buffer[pos]) ==
'\n') {
379 const string tail = (line.length() == 1) ?
"" : line.substr(1);
381 if (line[0] !=
'Z') {
382 (*content)[line[0]] = tail;
384 if (content->find(line[0]) == content->end()) {
385 (*content)[line[0]] = tail;
387 (*content)[line[0]] = (*content)[line[0]] +
"|" + tail;
393 line +=
static_cast<char>(buffer[pos]);
400 int fd = open(filename.c_str(), O_RDONLY);
404 unsigned char buffer[4096];
405 ssize_t num_bytes = read(fd, buffer,
sizeof(buffer));
408 if ((num_bytes <= 0) || (
unsigned(num_bytes) >=
sizeof(buffer)))
417 while ((pos < text_size) && (text[pos] !=
'\n'))
419 return string(text, pos);
427 if (ferror(f) && (errno == EINTR)) {
430 }
else if (retval == EOF) {
433 char c =
static_cast<char>(retval);
438 return (retval != EOF) || !line->empty();
446 retval = read(fd, &c, 1);
450 if ((retval == -1) && (errno == EINTR)) {
460 return (retval == 1) || !line->empty();
466 string Trim(
const string &raw,
bool trim_newline) {
470 unsigned start_pos = 0;
471 for (; (start_pos < raw.length())
472 && (raw[start_pos] ==
' ' || raw[start_pos] ==
'\t'
474 && (raw[start_pos] ==
'\n' || raw[start_pos] ==
'\r')));
477 unsigned end_pos = raw.length() - 1;
479 (end_pos >= start_pos)
480 && (raw[end_pos] ==
' ' || raw[end_pos] ==
'\t'
481 || (trim_newline && (raw[end_pos] ==
'\n' || raw[end_pos] ==
'\r')));
485 return raw.substr(start_pos, end_pos - start_pos + 1);
489 const std::string &toTrim,
490 const int trimMode) {
491 std::string trimmed = path;
492 if (trimmed != toTrim) {
494 && (trimmed.size() > toTrim.size())) {
495 trimmed = trimmed.substr(toTrim.size());
498 && (trimmed.size() > toTrim.size())) {
499 trimmed = trimmed.substr(0, trimmed.size() - toTrim.size());
509 string result(mixed_case);
510 for (
unsigned i = 0, l = result.length(); i < l; ++i) {
511 result[i] =
static_cast<char>(toupper(result[i]));
516 string ReplaceAll(
const string &haystack,
const string &needle,
517 const string &replace_by) {
518 string result(haystack);
520 const unsigned needle_size = needle.size();
524 while ((pos = result.find(needle, pos)) != string::npos)
525 result.replace(pos, needle_size, replace_by);
529 static inline void Base64Block(
const unsigned char input[3],
const char *table,
531 output[0] = table[(input[0] & 0xFD) >> 2];
532 output[1] = table[((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4)];
533 output[2] = table[((input[1] & 0x0F) << 2) | ((input[2] & 0xD0) >> 6)];
534 output[3] = table[input[2] & 0x3F];
539 result.reserve((data.length() + 3) * 4 / 3);
541 const unsigned char *data_ptr =
reinterpret_cast<const unsigned char *
>(
543 const unsigned length = data.length();
544 while (pos + 2 < length) {
545 char encoded_block[4];
547 result.append(encoded_block, 4);
550 if (length % 3 != 0) {
551 unsigned char input[3];
552 input[0] = data_ptr[pos];
553 input[1] = ((length % 3) == 2) ? data_ptr[pos + 1] : 0;
555 char encoded_block[4];
557 result.append(encoded_block, 2);
558 result.push_back(((length % 3) == 2) ? encoded_block[2] :
'=');
559 result.push_back(
'=');
569 string base64 =
Base64(data);
570 for (
unsigned i = 0, l = base64.length(); i < l; ++i) {
571 if (base64[i] ==
'+') {
573 }
else if (base64[i] ==
'/') {
581 unsigned char output[3]) {
583 for (
int i = 0; i < 4; ++i) {
589 output[0] = (dec[0] << 2) | (dec[1] >> 4);
590 output[1] = ((dec[1] & 0x0F) << 4) | (dec[2] >> 2);
591 output[2] = ((dec[2] & 0x03) << 6) | dec[3];
598 bool Debase64(
const string &data,
string *decoded) {
600 decoded->reserve((data.length() + 4) * 3 / 4);
602 const unsigned char *data_ptr =
reinterpret_cast<const unsigned char *
>(
604 const unsigned length = data.length();
607 if ((length % 4) != 0)
610 while (pos < length) {
611 unsigned char decoded_block[3];
615 decoded->append(reinterpret_cast<char *>(decoded_block), 3);
619 for (
int i = 0; i < 2; ++i) {
621 if (data[pos] ==
'=')
622 decoded->erase(decoded->length() - 1);
631 if (source.empty() || (num_lines == 0))
634 int l =
static_cast<int>(source.length());
636 for (; i >= 0; --i) {
637 char c = source.data()[i];
639 if (num_lines == 0) {
640 return source.substr(i + 1);
656 char date_and_time[100];
657 time_t t = time(NULL);
658 gmtime_r(&t, &time_ptr);
660 if (!strftime(date_and_time, 100, format.c_str(), &time_ptr)) {
663 std::string timestamp(date_and_time);
667 #ifdef CVMFS_NAMESPACE_GUARD
string GetLineMem(const char *text, const int text_size)
std::string GetGMTimestamp(const std::string &format)
string StringifyLocalTime(const time_t seconds)
std::string IsoTimestamp()
string Trim(const string &raw, bool trim_newline)
string ReplaceAll(const string &haystack, const string &needle, const string &replace_by)
static void Base64Block(const unsigned char input[3], const char *table, char output[4])
CVMFS_EXPORT const LogSource source
string JoinStrings(const vector< string > &strings, const string &joint)
string Tail(const string &source, unsigned num_lines)
string StringifyTime(const time_t seconds, const bool utc)
string StringifyDouble(const double value)
std::string StringifyUint(const uint64_t value)
bool Debase64(const string &data, string *decoded)
string StringifyByteAsHex(const unsigned char value)
string StringifyBool(const bool value)
bool String2Uint64Parse(const std::string &value, uint64_t *result)
string Base64Url(const string &data)
std::string RfcTimestamp()
int64_t String2Int64(const string &value)
bool GetLineFile(FILE *f, std::string *line)
string ToUpper(const string &mixed_case)
vector< string > SplitString(const string &str, char delim)
bool HasSuffix(const std::string &str, const std::string &suffix, const bool ignore_case)
std::string WhitelistTimestamp(time_t when)
vector< string > SplitStringBounded(unsigned max_chunks, const string &str, char delim)
string StringifyInt(const int64_t value)
vector< string > SplitStringMultiChar(const string &str, const string &delim)
bool HasPrefix(const string &str, const string &prefix, const bool ignore_case)
time_t IsoTimestamp2UtcTime(const std::string &iso8601)
bool GetLineFd(const int fd, std::string *line)
string Base64(const string &data)
uint64_t String2Uint64(const string &value)
bool operator()(const std::string::value_type a, const std::string::value_type b) const
bool ParseKeyvalPath(const string &filename, map< char, string > *content)
std::string TrimString(const std::string &path, const std::string &toTrim, const int trimMode)
static bool Debase64Block(const unsigned char input[4], unsigned char output[3])
CVMFS_EXPORT const LogSource const int const char * format
string StringifyTimeval(const timeval value)
const int8_t db64_table[]
void ParseKeyvalMem(const unsigned char *buffer, const unsigned buffer_size, map< char, string > *content)
void String2Uint64Pair(const string &value, uint64_t *a, uint64_t *b)