Util.h 3.9 KB

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