str_util.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // This file is part of BOINC.
  2. // http://boinc.berkeley.edu
  3. // Copyright (C) 2008 University of California
  4. //
  5. // BOINC is free software; you can redistribute it and/or modify it
  6. // under the terms of the GNU Lesser General Public License
  7. // as published by the Free Software Foundation,
  8. // either version 3 of the License, or (at your option) any later version.
  9. //
  10. // BOINC is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. // See the GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef BOINC_STR_UTIL_H
  18. #define BOINC_STR_UTIL_H
  19. #include <string>
  20. #include <vector>
  21. #include <string.h>
  22. #define safe_strcpy(x, y) strlcpy(x, y, sizeof(x))
  23. #define safe_strcat(x, y) strlcat(x, y, sizeof(x))
  24. extern void strcpy_overlap(char*, const char*);
  25. extern int ndays_to_string(double x, int smallest_timescale, char *buf);
  26. extern void nbytes_to_string(double nbytes, double total_bytes, char* str, int len);
  27. extern int parse_command_line(char*, char**);
  28. extern void c2x(char *what);
  29. extern void strip_whitespace(char *str);
  30. extern void strip_whitespace(std::string&);
  31. extern void strip_quotes(char *str);
  32. extern void strip_quotes(std::string&);
  33. extern void unescape_os_release(char *str);
  34. extern void collapse_whitespace(char *str);
  35. extern void collapse_whitespace(std::string&);
  36. extern char* time_to_string(double);
  37. extern char* precision_time_to_string(double);
  38. extern void secs_to_hmsf(double, char*);
  39. extern std::string timediff_format(double);
  40. inline bool empty(char* p) {
  41. return p[0] == 0;
  42. }
  43. inline bool ends_with(std::string const& s, std::string const& suffix) {
  44. return
  45. s.size()>=suffix.size() &&
  46. s.substr(s.size()-suffix.size()) == suffix;
  47. }
  48. inline bool ends_with(const char* s, const char* suffix) {
  49. int m = (int)strlen(s), n = (int)strlen(suffix);
  50. if (n > m) return false;
  51. return (strcmp(suffix, s+m-n) == 0);
  52. }
  53. inline bool starts_with(std::string const& s, std::string const& prefix) {
  54. return s.substr(0, prefix.size()) == prefix;
  55. }
  56. inline bool starts_with(const char* s, const char* prefix) {
  57. return (strncmp(s, prefix, strlen(prefix)) == 0);
  58. }
  59. inline void downcase_string(std::string& w) {
  60. for (std::string::iterator p = w.begin(); p != w.end(); ++p) {
  61. *p = (char)tolower((int)*p);
  62. }
  63. }
  64. inline void downcase_string(char* p) {
  65. while (*p) {
  66. *p = (char)tolower((int)*p);
  67. p++;
  68. }
  69. }
  70. extern int string_substitute(
  71. const char* haystack, char* out, int out_len,
  72. const char* needle, const char* target
  73. );
  74. // convert UNIX time to MySQL timestamp (yyyymmddhhmmss)
  75. //
  76. extern void mysql_timestamp(double, char*);
  77. // parse host.serialnum into component parts.
  78. // Given a string of the form
  79. // [BOINC|7.2.42][CUDA|GeForce GTX 860M|1|2048MB|34052|101][INTEL|Intel(R) HD Graphics 4600|1|1752MB||102][vbox|4.2.16]
  80. // split it into the BOINC, vbox, and other (coproc) parts
  81. //
  82. extern void parse_serialnum(char* in, char* boinc, char* vbox, char* coprocs);
  83. // take a malloced string.
  84. // if \n is not last char, add it.
  85. //
  86. extern char* lf_terminate(char*);
  87. extern const char* network_status_string(int);
  88. extern const char* rpc_reason_string(int);
  89. extern const char* suspend_reason_string(int reason);
  90. extern const char* run_mode_string(int mode);
  91. extern const char* battery_state_string(int state);
  92. extern const char* result_client_state_string(int state);
  93. extern const char* result_scheduler_state_string(int state);
  94. extern const char* active_task_state_string(int state);
  95. extern const char* batch_state_string(int state);
  96. extern void strip_translation(char* p);
  97. extern std::vector<std::string> split(std::string, char delim);
  98. extern bool is_valid_filename(const char*);
  99. extern int path_to_filename(std::string fpath, std::string& fname);
  100. extern int path_to_filename(std::string fpath, char* &fname);
  101. #endif