Util.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <ostream>
  44. #include <numeric>
  45. #include <map>
  46. #include <iomanip>
  47. #include "SharedHandle.h"
  48. #include "IntSequence.h"
  49. #include "a2time.h"
  50. #include "a2netcompat.h"
  51. #include "a2functional.h"
  52. namespace aria2 {
  53. class Randomizer;
  54. class BitfieldMan;
  55. class BinaryStream;
  56. class FileEntry;
  57. #define STRTOLL(X) strtoll(X, (char**)0, 10)
  58. #define STRTOULL(X) strtoull(X, (char**)0, 10)
  59. #define START_INDEX(OFFSET, PIECE_LENGTH) ((OFFSET)/(PIECE_LENGTH))
  60. #define END_INDEX(OFFSET, LENGTH, PIECE_LENGTH) (((OFFSET)+(LENGTH)-1)/(PIECE_LENGTH))
  61. #define DIV_FLOOR(X,Y) ((X)/(Y)+((X)%(Y)? 1:0))
  62. #ifdef WORDS_BIGENDIAN
  63. inline uint64_t ntoh64(uint64_t x) { return x; }
  64. inline uint64_t hton64(uint64_t x) { return x; }
  65. #else // !WORDS_BIGENDIAN
  66. inline uint64_t byteswap64(uint64_t x) {
  67. uint64_t v1 = ntohl(x & 0x00000000ffffffff);
  68. uint64_t v2 = ntohl(x >> 32);
  69. return (v1 << 32)|v2;
  70. }
  71. inline uint64_t ntoh64(uint64_t x) { return byteswap64(x); }
  72. inline uint64_t hton64(uint64_t x) { return byteswap64(x); }
  73. #endif // !WORDS_BIGENDIAN
  74. namespace util {
  75. void split(std::pair<std::string, std::string>& hp,
  76. const std::string& src, char delim);
  77. std::pair<std::string, std::string>
  78. split(const std::string& src, const std::string& delims);
  79. template<typename T>
  80. std::string uitos(T value, bool comma = false)
  81. {
  82. std::string str;
  83. if(value == 0) {
  84. str = "0";
  85. return str;
  86. }
  87. unsigned int count = 0;
  88. while(value) {
  89. ++count;
  90. char digit = value%10+'0';
  91. str.insert(str.begin(), digit);
  92. value /= 10;
  93. if(comma && count > 3 && count%3 == 1) {
  94. str.insert(str.begin()+1, ',');
  95. }
  96. }
  97. return str;
  98. }
  99. template<typename T>
  100. std::string itos(T value, bool comma = false)
  101. {
  102. bool flag = false;
  103. if(value < 0) {
  104. flag = true;
  105. value = -value;
  106. }
  107. std::string str = uitos(value, comma);
  108. if(flag) {
  109. str.insert(str.begin(), '-');
  110. }
  111. return str;
  112. }
  113. /**
  114. * Computes difference in micro-seconds between tv1 and tv2,
  115. * assuming tv1 is newer than tv2.
  116. * If tv1 is older than tv2, then this method returns 0.
  117. */
  118. int64_t difftv(struct timeval tv1, struct timeval tv2);
  119. int32_t difftvsec(struct timeval tv1, struct timeval tv2);
  120. extern const std::string DEFAULT_TRIM_CHARSET;
  121. std::string trim(const std::string& src,
  122. const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
  123. void trimSelf(std::string& str,
  124. const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
  125. bool startsWith(const std::string& target, const std::string& part);
  126. bool endsWith(const std::string& target, const std::string& part);
  127. std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr);
  128. std::string urlencode(const unsigned char* target, size_t len);
  129. std::string urlencode(const std::string& target);
  130. bool inRFC3986ReservedChars(const char c);
  131. bool inRFC3986UnreservedChars(const char c);
  132. std::string urldecode(const std::string& target);
  133. std::string torrentUrlencode(const unsigned char* target, size_t len);
  134. std::string torrentUrlencode(const std::string& target);
  135. std::string toHex(const unsigned char* src, size_t len);
  136. std::string toHex(const char* src, size_t len);
  137. std::string toHex(const std::string& src);
  138. FILE* openFile(const std::string& filename, const std::string& mode);
  139. bool isPowerOf(int num, int base);
  140. std::string secfmt(time_t sec);
  141. int32_t parseInt(const std::string& s, int32_t base = 10);
  142. uint32_t parseUInt(const std::string& s, int base = 10);
  143. int64_t parseLLInt(const std::string& s, int32_t base = 10);
  144. uint64_t parseULLInt(const std::string& s, int base = 10);
  145. IntSequence parseIntRange(const std::string& src);
  146. // this function temporarily put here
  147. std::string getContentDispositionFilename(const std::string& header);
  148. std::string randomAlpha(size_t length,
  149. const SharedHandle<Randomizer>& randomizer);
  150. std::string toUpper(const std::string& src);
  151. std::string toLower(const std::string& src);
  152. bool isNumbersAndDotsNotation(const std::string& name);
  153. void setGlobalSignalHandler(int signal, void (*handler)(int), int flags);
  154. std::string getHomeDir();
  155. int64_t getRealSize(const std::string& sizeWithUnit);
  156. std::string abbrevSize(int64_t size);
  157. template<typename InputIterator>
  158. void toStream
  159. (InputIterator first, InputIterator last, std::ostream& os)
  160. {
  161. os << _("Files:") << "\n";
  162. os << "idx|path/length" << "\n";
  163. os << "===+===========================================================================" << "\n";
  164. int32_t count = 1;
  165. for(; first != last; ++first, ++count) {
  166. os << std::setw(3) << count << "|" << (*first)->getPath() << "\n";
  167. os << " |" << util::abbrevSize((*first)->getLength()) << "B ("
  168. << util::uitos((*first)->getLength(), true) << ")\n";
  169. os << "---+---------------------------------------------------------------------------" << "\n";
  170. }
  171. }
  172. void sleep(long seconds);
  173. void usleep(long microseconds);
  174. bool isNumber(const std::string& what);
  175. bool isLowercase(const std::string& what);
  176. bool isUppercase(const std::string& what);
  177. /**
  178. * Converts alphabets to unsigned int, assuming alphabets as a base 26
  179. * integer and 'a' or 'A' is 0.
  180. * This function assumes alphabets includes only a-z.
  181. * Upper case are allowed but all letters must be upper case.
  182. * If overflow occurs, returns 0.
  183. */
  184. unsigned int alphaToNum(const std::string& alphabets);
  185. void mkdirs(const std::string& dirpath);
  186. void convertBitfield(BitfieldMan* dest, const BitfieldMan* src);
  187. // binaryStream has to be opened before calling this function.
  188. std::string toString(const SharedHandle<BinaryStream>& binaryStream);
  189. #ifdef HAVE_POSIX_MEMALIGN
  190. void* allocateAlignedMemory(size_t alignment, size_t size);
  191. #endif // HAVE_POSIX_MEMALIGN
  192. std::pair<std::string, uint16_t>
  193. getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len);
  194. std::string htmlEscape(const std::string& src);
  195. // Joins path element specified in [first, last). If ".." is found,
  196. // it eats the previous element if it exists. If "." is found, it
  197. // is just ignored and it is not appeared in the result.
  198. template<typename InputIterator>
  199. std::string joinPath(InputIterator first, InputIterator last)
  200. {
  201. std::deque<std::string> elements;
  202. for(;first != last; ++first) {
  203. if(*first == "..") {
  204. if(!elements.empty()) {
  205. elements.pop_back();
  206. }
  207. } else if(*first == ".") {
  208. // do nothing
  209. } else {
  210. elements.push_back(*first);
  211. }
  212. }
  213. return strjoin(elements.begin(), elements.end(), "/");
  214. }
  215. // Parses INDEX=PATH format string. INDEX must be an unsigned
  216. // integer.
  217. std::map<size_t, std::string>::value_type
  218. parseIndexPath(const std::string& line);
  219. std::map<size_t, std::string> createIndexPathMap(std::istream& i);
  220. /**
  221. * Take a string src which is a deliminated list and add its elements
  222. * into result. result is stored in out.
  223. */
  224. template<typename OutputIterator>
  225. OutputIterator split(const std::string& src, OutputIterator out,
  226. const std::string& delims, bool doTrim = false)
  227. {
  228. std::string::size_type p = 0;
  229. while(1) {
  230. std::string::size_type np = src.find_first_of(delims, p);
  231. if(np == std::string::npos) {
  232. std::string term = src.substr(p);
  233. if(doTrim) {
  234. term = util::trim(term);
  235. }
  236. if(!term.empty()) {
  237. *out = term;
  238. ++out;
  239. }
  240. break;
  241. }
  242. std::string term = src.substr(p, np-p);
  243. if(doTrim) {
  244. term = util::trim(term);
  245. }
  246. p = np+1;
  247. if(!term.empty()) {
  248. *out = term;
  249. ++out;
  250. }
  251. }
  252. return out;
  253. }
  254. void generateRandomData(unsigned char* data, size_t length);
  255. } // namespace util
  256. } // namespace aria2
  257. #endif // _D_UTIL_H_