1 /*
2 * s3fs - FUSE-based file system backed by Amazon S3
3 *
4 * Copyright(C) 2007 Randy Rizun <rrizun@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #ifndef S3FS_STRING_UTIL_H_
22 #define S3FS_STRING_UTIL_H_
23
24 //
25 // A collection of string utilities for manipulating URLs and HTTP responses.
26 //
27 //-------------------------------------------------------------------
28 // Global variables
29 //-------------------------------------------------------------------
30 extern const char SPACES[];
31
32 //-------------------------------------------------------------------
33 // Inline functions
34 //-------------------------------------------------------------------
is_prefix(const char * str,const char * prefix)35 static inline int is_prefix(const char *str, const char *prefix) { return strncmp(str, prefix, strlen(prefix)) == 0; }
SAFESTRPTR(const char * strptr)36 static inline const char* SAFESTRPTR(const char *strptr) { return strptr ? strptr : ""; }
37
38 //-------------------------------------------------------------------
39 // Templates
40 //-------------------------------------------------------------------
41 template <class T> std::string str(T value);
42
43 //-------------------------------------------------------------------
44 // Macros(WTF8)
45 //-------------------------------------------------------------------
46 #define WTF8_ENCODE(ARG) \
47 std::string ARG##_buf; \
48 const char * ARG = _##ARG; \
49 if (use_wtf8 && s3fs_wtf8_encode( _##ARG, 0 )) { \
50 s3fs_wtf8_encode( _##ARG, &ARG##_buf); \
51 ARG = ARG##_buf.c_str(); \
52 }
53
54 //-------------------------------------------------------------------
55 // Utilities
56 //-------------------------------------------------------------------
57 //
58 // Convert string to off_t. Returns false on bad input.
59 // Replacement for C++11 std::stoll.
60 //
61 bool s3fs_strtoofft(off_t* value, const char* str, int base = 0);
62 //
63 // This function returns 0 if a value that cannot be converted is specified.
64 // Only call if 0 is considered an error and the operation can continue.
65 //
66 off_t cvt_strtoofft(const char* str, int base);
67
68 //
69 // String Manipulation
70 //
71 std::string trim_left(const std::string &s, const char *t = SPACES);
72 std::string trim_right(const std::string &s, const char *t = SPACES);
73 std::string trim(const std::string &s, const char *t = SPACES);
74 std::string lower(std::string s);
75
76 //
77 // Date string
78 //
79 std::string get_date_rfc850();
80 void get_date_sigv3(std::string& date, std::string& date8601);
81 std::string get_date_string(time_t tm);
82 std::string get_date_iso8601(time_t tm);
83 bool get_unixtime_from_iso8601(const char* pdate, time_t& unixtime);
84 bool convert_unixtime_from_option_arg(const char* argv, time_t& unixtime);
85
86 //
87 // For encoding
88 //
89 std::string urlEncode(const std::string &s);
90 std::string urlEncode2(const std::string &s);
91 std::string urlDecode(const std::string& s);
92
93 bool takeout_str_dquart(std::string& str);
94 bool get_keyword_value(const std::string& target, const char* keyword, std::string& value);
95
96 //
97 // For binary string
98 //
99 std::string s3fs_hex_lower(const unsigned char* input, size_t length);
100 std::string s3fs_hex_upper(const unsigned char* input, size_t length);
101 char* s3fs_base64(const unsigned char* input, size_t length);
102 unsigned char* s3fs_decode64(const char* input, size_t* plength);
103
104 //
105 // WTF8
106 //
107 bool s3fs_wtf8_encode(const char *s, std::string *result);
108 std::string s3fs_wtf8_encode(const std::string &s);
109 bool s3fs_wtf8_decode(const char *s, std::string *result);
110 std::string s3fs_wtf8_decode(const std::string &s);
111
112 #endif // S3FS_STRING_UTIL_H_
113
114 /*
115 * Local variables:
116 * tab-width: 4
117 * c-basic-offset: 4
118 * End:
119 * vim600: expandtab sw=4 ts=4 fdm=marker
120 * vim<600: expandtab sw=4 ts=4
121 */
122