Util.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifdef ENABLE_MESSAGE_DIGEST
  39. #include "messageDigest.h"
  40. #endif // ENABLE_MESSAGE_DIGEST
  41. #include <string>
  42. #include <utility>
  43. #include <deque>
  44. #include <sys/time.h>
  45. #include <stdio.h>
  46. using namespace std;
  47. #define STRTOLL(X) strtoll(X, (char**)NULL, 10);
  48. class Util {
  49. public:
  50. static void split(pair<string, string>& hp, const string& src, char delim);
  51. static string llitos(long long int value, bool comma = false);
  52. static string itos(int value, bool comma = false);
  53. /**
  54. * Computes difference in micro-seconds between tv1 and tv2,
  55. * assuming tv1 is newer than tv2.
  56. * If tv1 is older than tv2, then this method returns 0.
  57. */
  58. static long long int difftv(struct timeval tv1, struct timeval tv2);
  59. static int difftvsec(struct timeval tv1, struct timeval tv2);
  60. /**
  61. * Take a string src which is a deliminated list and add its elements
  62. * into result. result is not cleared before conversion begins.
  63. */
  64. static void slice(Strings& result, const string& src, char delim);
  65. static string trim(const string& src);
  66. static bool startsWith(const string& target, const string& part);
  67. static bool endsWith(const string& target, const string& part);
  68. static string replace(const string& target, const string& oldstr, const string& newstr);
  69. static string urlencode(const unsigned char* target, int len);
  70. static string urldecode(const string& target);
  71. static string torrentUrlencode(const unsigned char* target, int len);
  72. static string toHex(const unsigned char* src, int len);
  73. static FILE* openFile(const string& filename, const string& mode);
  74. static void fileCopy(const string& destFile, const string& src);
  75. static void rangedFileCopy(const string& destFile, const string& src, long long int srcOffset, long long int length);
  76. static bool isPowerOf(int num, int base);
  77. static string secfmt(int sec);
  78. static int expandBuffer(char** pbuf, int curLength, int newLength);
  79. static void unfoldRange(const string& src, Integers& range);
  80. // this function temporarily put here
  81. static string getContentDispositionFilename(const string& header);
  82. // digest must be at least 20 bytes long.
  83. #ifdef ENABLE_MESSAGE_DIGEST
  84. static void sha1Sum(unsigned char* digest, const void* data, int dataLength);
  85. #endif // ENABLE_MESSAGE_DIGEST
  86. // Before call this method, allocate enough memory to the parameter "digest".
  87. // For sha1, you need 20 bytes. For md5, 16 bytes.
  88. #ifdef ENABLE_MESSAGE_DIGEST
  89. static void fileChecksum(const string& filename, unsigned char* digest,
  90. MessageDigestContext::DigestAlgo algo);
  91. #endif // ENABLE_MESSAGE_DIGEST
  92. #ifdef ENABLE_BITTORRENT
  93. static Integers computeFastSet(string ipaddr, const unsigned char* infoHash,
  94. int pieces, int fastSetSize);
  95. #endif // ENABLE_BITTORRENT
  96. static int countBit(unsigned int);
  97. static string randomAlpha(int length);
  98. static string toUpper(const string& src);
  99. static string toLower(const string& src);
  100. static bool isNumbersAndDotsNotation(const string& name);
  101. };
  102. #endif // _D_UTIL_H_