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 
18 #ifndef BOINC_STR_UTIL_H
19 #define BOINC_STR_UTIL_H
20 
21 #include <string>
22 #include <vector>
23 #include <string.h>
24 
25 #define safe_strcpy(x, y) strlcpy(x, y, sizeof(x))
26 #define safe_strcat(x, y) strlcat(x, y, sizeof(x))
27 
28 extern void strcpy_overlap(char*, const char*);
29 extern int ndays_to_string(double x, int smallest_timescale, char *buf);
30 extern void nbytes_to_string(double nbytes, double total_bytes, char* str, int len);
31 extern int parse_command_line(char*, char**);
32 extern void c2x(char *what);
33 extern void strip_whitespace(char *str);
34 extern void strip_whitespace(std::string&);
35 extern void strip_quotes(char *str);
36 extern void strip_quotes(std::string&);
37 extern void unescape_os_release(char *str);
38 extern void collapse_whitespace(char *str);
39 extern void collapse_whitespace(std::string&);
40 extern char* time_to_string(double);
41 extern char* precision_time_to_string(double);
42 extern void secs_to_hmsf(double, char*);
43 extern std::string timediff_format(double);
44 
ends_with(std::string const & s,std::string const & suffix)45 inline bool ends_with(std::string const& s, std::string const& suffix) {
46     return
47         s.size()>=suffix.size() &&
48         s.substr(s.size()-suffix.size()) == suffix;
49 }
50 
ends_with(const char * s,const char * suffix)51 inline bool ends_with(const char* s, const char* suffix) {
52     int m = (int)strlen(s), n = (int)strlen(suffix);
53     if (n > m) return false;
54     return (strcmp(suffix, s+m-n) == 0);
55 }
56 
starts_with(std::string const & s,std::string const & prefix)57 inline bool starts_with(std::string const& s, std::string const& prefix) {
58     return s.substr(0, prefix.size()) == prefix;
59 }
60 
starts_with(const char * s,const char * prefix)61 inline bool starts_with(const char* s, const char* prefix) {
62     return (strncmp(s, prefix, strlen(prefix)) == 0);
63 }
64 
downcase_string(std::string & w)65 inline void downcase_string(std::string& w) {
66     for (std::string::iterator p = w.begin(); p != w.end(); ++p) {
67         *p = (char)tolower((int)*p);
68     }
69 }
70 
downcase_string(char * p)71 inline void downcase_string(char* p) {
72     while (*p) {
73         *p = (char)tolower((int)*p);
74         p++;
75     }
76 }
77 
78 extern int string_substitute(
79     const char* haystack, char* out, int out_len,
80     const char* needle, const char* target
81 );
82 
83 // convert UNIX time to MySQL timestamp (yyyymmddhhmmss)
84 //
85 extern void mysql_timestamp(double, char*);
86 
87 // parse host.serialnum into component parts.
88 // Given a string of the form
89 // [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]
90 // split it into the BOINC, vbox, and other (coproc) parts
91 //
92 extern void parse_serialnum(char* in, char* boinc, char* vbox, char* coprocs);
93 
94 // take a malloced string.
95 // if \n is not last char, add it.
96 //
97 extern char* lf_terminate(char*);
98 
99 extern const char* network_status_string(int);
100 extern const char* rpc_reason_string(int);
101 extern const char* suspend_reason_string(int reason);
102 extern const char* run_mode_string(int mode);
103 extern const char* battery_state_string(int state);
104 extern const char* result_client_state_string(int state);
105 extern const char* result_scheduler_state_string(int state);
106 extern const char* active_task_state_string(int state);
107 extern const char* batch_state_string(int state);
108 
109 extern void strip_translation(char* p);
110 
111 extern std::vector<std::string> split(std::string, char delim);
112 
113 extern bool is_valid_filename(const char*);
114 
115 extern int path_to_filename(std::string fpath, std::string& fname);
116 extern int path_to_filename(std::string fpath, char* &fname);
117 #endif
118