Util.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "SharedHandle.h"
  39. #include "IntSequence.h"
  40. #include <sys/time.h>
  41. #include <string>
  42. #include <utility>
  43. #include <deque>
  44. #include <iosfwd>
  45. namespace aria2 {
  46. class Randomizer;
  47. class BitfieldMan;
  48. class BinaryStream;
  49. class FileEntry;
  50. #define STRTOLL(X) strtoll(X, (char**)NULL, 10)
  51. #define START_INDEX(OFFSET, PIECE_LENGTH) ((OFFSET)/(PIECE_LENGTH))
  52. #define END_INDEX(OFFSET, LENGTH, PIECE_LENGTH) (((OFFSET)+(LENGTH)-1)/(PIECE_LENGTH))
  53. #define DIV_FLOOR(X,Y) ((X)/(Y)+((X)%(Y)? 1:0))
  54. class Util {
  55. public:
  56. static void split(std::pair<std::string, std::string>& hp,
  57. const std::string& src, char delim);
  58. static std::pair<std::string, std::string>
  59. split(const std::string& src, const std::string& delims);
  60. static std::string llitos(int64_t value, bool comma = false);
  61. static std::string ullitos(uint64_t value, bool comma = false);
  62. static std::string itos(int32_t value, bool comma = false);
  63. static std::string uitos(uint32_t value, bool comma = false);
  64. static std::string itos(int16_t value, bool comma = false);
  65. static std::string uitos(uint16_t value, bool comma = false);
  66. /**
  67. * Computes difference in micro-seconds between tv1 and tv2,
  68. * assuming tv1 is newer than tv2.
  69. * If tv1 is older than tv2, then this method returns 0.
  70. */
  71. static int64_t difftv(struct timeval tv1, struct timeval tv2);
  72. static int32_t difftvsec(struct timeval tv1, struct timeval tv2);
  73. /**
  74. * Take a string src which is a deliminated list and add its elements
  75. * into result. result is not cleared before conversion begins.
  76. */
  77. static void slice(std::deque<std::string>& result, const std::string& src,
  78. char delim, bool trim = false);
  79. static std::string trim(const std::string& src, const std::string& trimCharset = "\r\n\t ");
  80. static bool startsWith(const std::string& target, const std::string& part);
  81. static bool endsWith(const std::string& target, const std::string& part);
  82. static std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr);
  83. static std::string urlencode(const unsigned char* target, int32_t len);
  84. static std::string urlencode(const std::string& target)
  85. {
  86. return urlencode((const unsigned char*)target.c_str(), target.size());
  87. }
  88. static bool shouldUrlencode(const char c);
  89. static std::string urldecode(const std::string& target);
  90. static std::string torrentUrlencode(const unsigned char* target, int32_t len);
  91. static std::string toHex(const unsigned char* src, int32_t len);
  92. static std::string toHex(const std::string& src)
  93. {
  94. return toHex(reinterpret_cast<const unsigned char*>(src.c_str()), src.size());
  95. }
  96. static FILE* openFile(const std::string& filename, const std::string& mode);
  97. static void fileCopy(const std::string& destFile, const std::string& src);
  98. static void rangedFileCopy(const std::string& destFile, const std::string& src, int64_t srcOffset, int64_t length);
  99. static bool isPowerOf(int32_t num, int32_t base);
  100. static std::string secfmt(int32_t sec);
  101. static int32_t expandBuffer(char** pbuf, int32_t curLength, int32_t newLength);
  102. static void unfoldRange(const std::string& src, std::deque<int32_t>& range);
  103. static int32_t parseInt(const std::string& s, int32_t base = 10);
  104. static int64_t parseLLInt(const std::string& s, int32_t base = 10);
  105. static IntSequence parseIntRange(const std::string& src);
  106. // this function temporarily put here
  107. static std::string getContentDispositionFilename(const std::string& header);
  108. static int32_t countBit(uint32_t n);
  109. static std::string randomAlpha(int32_t length,
  110. const SharedHandle<Randomizer>& randomizer);
  111. static std::string toUpper(const std::string& src);
  112. static std::string toLower(const std::string& src);
  113. static bool isNumbersAndDotsNotation(const std::string& name);
  114. static void setGlobalSignalHandler(int32_t signal, void (*handler)(int), int32_t flags);
  115. static void indexRange(int32_t& startIndex, int32_t& endIndex,
  116. int64_t offset,
  117. int32_t srcLength, int32_t destLength);
  118. static std::string getHomeDir();
  119. static int64_t getRealSize(const std::string& sizeWithUnit);
  120. static std::string abbrevSize(int64_t size);
  121. static time_t httpGMT(const std::string& httpTimeFormat);
  122. static void toStream(std::ostream& os,
  123. const std::deque<SharedHandle<FileEntry> >& entries);
  124. static void sleep(long seconds);
  125. static void usleep(long microseconds);
  126. static bool isNumber(const std::string& what);
  127. static bool isLowercase(const std::string& what);
  128. static bool isUppercase(const std::string& what);
  129. static int32_t alphaToNum(const std::string& alphabets);
  130. static void mkdirs(const std::string& dirpath);
  131. static void convertBitfield(BitfieldMan* dest, const BitfieldMan* src);
  132. // binaryStream has to be opened before calling this function.
  133. static std::string toString(const SharedHandle<BinaryStream>& binaryStream);
  134. #ifdef HAVE_POSIX_MEMALIGN
  135. static void* allocateAlignedMemory(size_t alignment, size_t size);
  136. #endif // HAVE_POSIX_MEMALIGN
  137. };
  138. } // namespace aria2
  139. #endif // _D_UTIL_H_