Util.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #include "Util.h"
  23. #include "DlAbortEx.h"
  24. #include "File.h"
  25. #include <errno.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. string Util::itos(int value, bool comma) {
  30. string str = llitos(value, comma);
  31. return str;
  32. }
  33. string Util::llitos(long long int value, bool comma)
  34. {
  35. string str;
  36. bool flag = false;
  37. if(value < 0) {
  38. flag = true;
  39. value = -value;
  40. } else if(value == 0) {
  41. str = "0";
  42. return str;
  43. }
  44. int count = 0;
  45. while(value) {
  46. ++count;
  47. char digit = value%10+'0';
  48. str.insert(str.begin(), digit);
  49. value /= 10;
  50. if(comma && count > 3 && count%3 == 1) {
  51. str.insert(str.begin()+1, ',');
  52. }
  53. }
  54. if(flag) {
  55. str.insert(str.begin(), '-');
  56. }
  57. return str;
  58. }
  59. string Util::trim(const string& src) {
  60. string::size_type sp = src.find_first_not_of(" ");
  61. string::size_type ep = src.find_last_not_of(" ");
  62. if(sp == string::npos || ep == string::npos) {
  63. return "";
  64. } else {
  65. return src.substr(sp, ep-sp+1);
  66. }
  67. }
  68. void Util::split(pair<string, string>& hp, const string& src, char delim) {
  69. hp.first = "";
  70. hp.second = "";
  71. string::size_type p = src.find(delim);
  72. if(p == string::npos) {
  73. hp.first = src;
  74. hp.second = "";
  75. } else {
  76. hp.first = trim(src.substr(0, p));
  77. hp.second = trim(src.substr(p+1));
  78. }
  79. }
  80. long long int Util::difftv(struct timeval tv1, struct timeval tv2) {
  81. if(tv1.tv_sec < tv2.tv_sec || tv1.tv_sec == tv2.tv_sec && tv1.tv_usec < tv2.tv_usec) {
  82. return 0;
  83. }
  84. return ((tv1.tv_sec-tv2.tv_sec)*1000000+
  85. tv1.tv_usec-tv2.tv_usec);
  86. }
  87. int Util::difftvsec(struct timeval tv1, struct timeval tv2) {
  88. if(tv1.tv_sec < tv2.tv_sec) {
  89. return 0;
  90. }
  91. return tv1.tv_sec-tv2.tv_sec;
  92. }
  93. void Util::slice(Strings& result, const string& src, char delim) {
  94. string::size_type p = 0;
  95. while(1) {
  96. string::size_type np = src.find(delim, p);
  97. if(np == string::npos) {
  98. string term = trim(src.substr(p));
  99. if(term.size() > 0) {
  100. result.push_back(term);
  101. }
  102. break;
  103. }
  104. string term = src.substr(p, np-p);
  105. p = np+1;
  106. result.push_back(trim(term));
  107. }
  108. }
  109. bool Util::startsWith(const string& target, const string& part) {
  110. if(target.size() < part.size()) {
  111. return false;
  112. }
  113. if(part == "") {
  114. return true;
  115. }
  116. if(target.find(part) == 0) {
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. }
  122. bool Util::endsWith(const string& target, const string& part) {
  123. if(target.size() < part.size()) {
  124. return false;
  125. }
  126. if(part == "") {
  127. return true;
  128. }
  129. if(target.rfind(part) == target.size()-part.size()) {
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. string Util::replace(const string& target, const string& oldstr, const string& newstr) {
  136. if(target == "" || oldstr == "" ) {
  137. return target;
  138. }
  139. string result;
  140. string::size_type p = 0;
  141. string::size_type np = target.find(oldstr);
  142. while(np != string::npos) {
  143. result += target.substr(p, np-p)+newstr;
  144. p = np+oldstr.size();
  145. np = target.find(oldstr, p);
  146. }
  147. result += target.substr(p);
  148. return result;
  149. }
  150. string Util::urlencode(const unsigned char* target, int len) {
  151. string dest;
  152. for(int i = 0; i < len; i++) {
  153. if(!('0' <= target[i] && target[i] <= '9' ||
  154. 'A' <= target[i] && target[i] <= 'Z' ||
  155. 'a' <= target[i] && target[i] <= 'z' ||
  156. '$' == target[i] || '-' == target[i] ||
  157. '_' == target[i] || '.' == target[i] ||
  158. '+' == target[i] || '!' == target[i] ||
  159. '*' == target[i] || '\'' == target[i] ||
  160. '(' == target[i] || ')' == target[i] ||
  161. ',' == target[i])) {
  162. char temp[4];
  163. sprintf(temp, "%%%02x", target[i]);
  164. temp[sizeof(temp)-1] = '\0';
  165. dest.append(temp);
  166. } else {
  167. dest += target[i];
  168. }
  169. }
  170. return dest;
  171. }
  172. string Util::toHex(const unsigned char* src, int len) {
  173. char* temp = new char[len*2+1];
  174. for(int i = 0; i < len; i++) {
  175. sprintf(temp+i*2, "%02x", src[i]);
  176. }
  177. temp[len*2] = '\0';
  178. string hex = temp;
  179. delete [] temp;
  180. return hex;
  181. }
  182. FILE* Util::openFile(const string& filename, const string& mode) {
  183. FILE* file = fopen(filename.c_str(), mode.c_str());
  184. return file;
  185. }
  186. void Util::fileCopy(const string& dest, const string& src) {
  187. File file(src);
  188. rangedFileCopy(dest, src, 0, file.size());
  189. }
  190. void Util::rangedFileCopy(const string& dest, const string& src, long long int srcOffset, long long int length) {
  191. int bufSize = 4096;
  192. char buf[bufSize];
  193. int destFd = -1;
  194. int srcFd = -1;
  195. try {
  196. if((destFd = open(dest.c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR)) == -1) {
  197. throw new DlAbortEx(strerror(errno));
  198. }
  199. if((srcFd = open(src.c_str(), O_RDONLY, S_IRUSR|S_IWUSR)) == -1) {
  200. throw new DlAbortEx(strerror(errno));
  201. }
  202. if(lseek(srcFd, srcOffset, SEEK_SET) != srcOffset) {
  203. throw new DlAbortEx(strerror(errno));
  204. }
  205. int x = length/bufSize;
  206. int r = length%bufSize;
  207. for(int i = 0; i < x; i++) {
  208. int readLength;
  209. if((readLength = read(srcFd, buf, bufSize)) == -1 || readLength != bufSize) {
  210. throw new DlAbortEx(strerror(errno));
  211. }
  212. if(write(destFd, buf, readLength) == -1) {
  213. throw new DlAbortEx(strerror(errno));
  214. }
  215. }
  216. if(r > 0) {
  217. int readLength;
  218. if((readLength = read(srcFd, buf, r)) == -1 || readLength != r) {
  219. throw new DlAbortEx(strerror(errno));
  220. }
  221. if(write(destFd, buf, r) == -1) {
  222. throw new DlAbortEx(strerror(errno));
  223. }
  224. }
  225. close(srcFd);
  226. close(destFd);
  227. srcFd = -1;
  228. destFd = -1;
  229. } catch(Exception* e) {
  230. if(srcFd != -1) {
  231. close(srcFd);
  232. }
  233. if(destFd != -1) {
  234. close(destFd);
  235. }
  236. throw;
  237. }
  238. }
  239. bool Util::isPowerOf(int num, int base) {
  240. if(base <= 0) { return false; }
  241. if(base == 1) { return true; }
  242. while(num%base == 0) {
  243. num /= base;
  244. if(num == 1) {
  245. return true;
  246. }
  247. }
  248. return false;
  249. }
  250. string Util::secfmt(int sec) {
  251. string str;
  252. if(sec >= 3600) {
  253. str = itos(sec/3600)+"h";
  254. sec %= 3600;
  255. }
  256. if(sec >= 60) {
  257. int min = sec/60;
  258. if(min < 10) {
  259. str += "0";
  260. }
  261. str += itos(min)+"m";
  262. sec %= 60;
  263. }
  264. if(sec < 10) {
  265. str += "0";
  266. }
  267. str += itos(sec)+"s";
  268. return str;
  269. }