Util.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_UTIL_H_
  36. #define _D_UTIL_H_
  37. #include "common.h"
  38. #include <sys/time.h>
  39. #include <string>
  40. #include <utility>
  41. #include <deque>
  42. #include <iosfwd>
  43. #include "SharedHandle.h"
  44. #include "IntSequence.h"
  45. #include "a2time.h"
  46. #include "a2netcompat.h"
  47. namespace aria2 {
  48. class Randomizer;
  49. class BitfieldMan;
  50. class BinaryStream;
  51. class FileEntry;
  52. #define STRTOLL(X) strtoll(X, (char**)0, 10)
  53. #define STRTOULL(X) strtoull(X, (char**)0, 10)
  54. #define START_INDEX(OFFSET, PIECE_LENGTH) ((OFFSET)/(PIECE_LENGTH))
  55. #define END_INDEX(OFFSET, LENGTH, PIECE_LENGTH) (((OFFSET)+(LENGTH)-1)/(PIECE_LENGTH))
  56. #define DIV_FLOOR(X,Y) ((X)/(Y)+((X)%(Y)? 1:0))
  57. #ifdef WORDS_BIGENDIAN
  58. inline uint64_t ntoh64(uint64_t x) { return x; }
  59. inline uint64_t hton64(uint64_t x) { return x; }
  60. #else // !WORDS_BIGENDIAN
  61. inline uint64_t byteswap64(uint64_t x) {
  62. uint64_t v1 = ntohl(x & 0x00000000ffffffff);
  63. uint64_t v2 = ntohl(x >> 32);
  64. return (v1 << 32)|v2;
  65. }
  66. inline uint64_t ntoh64(uint64_t x) { return byteswap64(x); }
  67. inline uint64_t hton64(uint64_t x) { return byteswap64(x); }
  68. #endif // !WORDS_BIGENDIAN
  69. class Util {
  70. public:
  71. static void split(std::pair<std::string, std::string>& hp,
  72. const std::string& src, char delim);
  73. static std::pair<std::string, std::string>
  74. split(const std::string& src, const std::string& delims);
  75. template<typename T>
  76. static std::string uitos(T value, bool comma = false)
  77. {
  78. std::string str;
  79. if(value == 0) {
  80. str = "0";
  81. return str;
  82. }
  83. unsigned int count = 0;
  84. while(value) {
  85. ++count;
  86. char digit = value%10+'0';
  87. str.insert(str.begin(), digit);
  88. value /= 10;
  89. if(comma && count > 3 && count%3 == 1) {
  90. str.insert(str.begin()+1, ',');
  91. }
  92. }
  93. return str;
  94. }
  95. template<typename T>
  96. static std::string itos(T value, bool comma = false)
  97. {
  98. bool flag = false;
  99. if(value < 0) {
  100. flag = true;
  101. value = -value;
  102. }
  103. std::string str = uitos(value, comma);
  104. if(flag) {
  105. str.insert(str.begin(), '-');
  106. }
  107. return str;
  108. }
  109. /**
  110. * Computes difference in micro-seconds between tv1 and tv2,
  111. * assuming tv1 is newer than tv2.
  112. * If tv1 is older than tv2, then this method returns 0.
  113. */
  114. static int64_t difftv(struct timeval tv1, struct timeval tv2);
  115. static int32_t difftvsec(struct timeval tv1, struct timeval tv2);
  116. /**
  117. * Take a string src which is a deliminated list and add its elements
  118. * into result. result is not cleared before conversion begins.
  119. */
  120. static void slice(std::deque<std::string>& result, const std::string& src,
  121. char delim, bool trim = false);
  122. static const std::string DEFAULT_TRIM_CHARSET;
  123. static std::string trim(const std::string& src,
  124. const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
  125. static void trimSelf(std::string& str,
  126. const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
  127. static bool startsWith(const std::string& target, const std::string& part);
  128. static bool endsWith(const std::string& target, const std::string& part);
  129. static std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr);
  130. static std::string urlencode(const unsigned char* target, size_t len);
  131. static std::string urlencode(const std::string& target)
  132. {
  133. return urlencode((const unsigned char*)target.c_str(), target.size());
  134. }
  135. static bool inRFC3986ReservedChars(const char c);
  136. static bool inRFC3986UnreservedChars(const char c);
  137. static std::string urldecode(const std::string& target);
  138. static std::string torrentUrlencode(const unsigned char* target, size_t len);
  139. static std::string torrentUrlencode(const std::string& target)
  140. {
  141. return torrentUrlencode(reinterpret_cast<const unsigned char*>(target.c_str()),
  142. target.size());
  143. }
  144. static std::string toHex(const unsigned char* src, size_t len);
  145. static std::string toHex(const char* src, size_t len)
  146. {
  147. return toHex(reinterpret_cast<const unsigned char*>(src), len);
  148. }
  149. static std::string toHex(const std::string& src)
  150. {
  151. return toHex(reinterpret_cast<const unsigned char*>(src.c_str()), src.size());
  152. }
  153. static FILE* openFile(const std::string& filename, const std::string& mode);
  154. static void fileCopy(const std::string& destFile, const std::string& src);
  155. static void rangedFileCopy(const std::string& destFile, const std::string& src, off_t srcOffset, uint64_t length);
  156. static bool isPowerOf(int num, int base);
  157. static std::string secfmt(time_t sec);
  158. static size_t expandBuffer(char** pbuf, size_t curLength, size_t newLength);
  159. static void unfoldRange(const std::string& src, std::deque<int>& range);
  160. static int32_t parseInt(const std::string& s, int32_t base = 10);
  161. static uint32_t parseUInt(const std::string& s, int base = 10);
  162. static int64_t parseLLInt(const std::string& s, int32_t base = 10);
  163. static uint64_t parseULLInt(const std::string& s, int base = 10);
  164. static IntSequence parseIntRange(const std::string& src);
  165. // this function temporarily put here
  166. static std::string getContentDispositionFilename(const std::string& header);
  167. static unsigned int countBit(uint32_t n);
  168. static std::string randomAlpha(size_t length,
  169. const SharedHandle<Randomizer>& randomizer);
  170. static std::string toUpper(const std::string& src);
  171. static std::string toLower(const std::string& src);
  172. static bool isNumbersAndDotsNotation(const std::string& name);
  173. static void setGlobalSignalHandler(int signal, void (*handler)(int), int flags);
  174. static void indexRange(size_t& startIndex, size_t& endIndex,
  175. off_t offset,
  176. size_t srcLength, size_t destLength);
  177. static std::string getHomeDir();
  178. static int64_t getRealSize(const std::string& sizeWithUnit);
  179. static std::string abbrevSize(int64_t size);
  180. /**
  181. * Parses given httpTimeFormat and returns seconds ellapsed since epoc.
  182. * The available format is "%a, %Y-%m-%d %H:%M:%S GMT".
  183. * If specified date is later than "Tue, 2038-01-19 3:14:7 GMT",
  184. * this function returns INT32_MAX.
  185. * This function also cannot handle prior 1900-1-1 0:0:0 GMT.
  186. * If parse operation is failed, then return -1.
  187. */
  188. static time_t httpGMT(const std::string& httpTimeFormat);
  189. static void toStream(std::ostream& os,
  190. const std::deque<SharedHandle<FileEntry> >& entries);
  191. static void sleep(long seconds);
  192. static void usleep(long microseconds);
  193. static bool isNumber(const std::string& what);
  194. static bool isLowercase(const std::string& what);
  195. static bool isUppercase(const std::string& what);
  196. /**
  197. * Converts alphabets to unsigned int, assuming alphabets as a base 26
  198. * integer and 'a' or 'A' is 0.
  199. * This function assumes alphabets includes only a-z.
  200. * Upper case are allowed but all letters must be upper case.
  201. * If overflow occurs, returns 0.
  202. */
  203. static unsigned int alphaToNum(const std::string& alphabets);
  204. static void mkdirs(const std::string& dirpath);
  205. static void convertBitfield(BitfieldMan* dest, const BitfieldMan* src);
  206. // binaryStream has to be opened before calling this function.
  207. static std::string toString(const SharedHandle<BinaryStream>& binaryStream);
  208. #ifdef HAVE_POSIX_MEMALIGN
  209. static void* allocateAlignedMemory(size_t alignment, size_t size);
  210. #endif // HAVE_POSIX_MEMALIGN
  211. static std::pair<std::string, uint16_t>
  212. getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len);
  213. };
  214. } // namespace aria2
  215. #endif // _D_UTIL_H_