Util.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. #include "Util.h"
  36. #include "File.h"
  37. #include "message.h"
  38. #include "Randomizer.h"
  39. #include "a2netcompat.h"
  40. #include "a2time.h"
  41. #include "DlAbortEx.h"
  42. #include "BitfieldMan.h"
  43. #include "DefaultDiskWriter.h"
  44. #include "FatalException.h"
  45. #include "FileEntry.h"
  46. #include <signal.h>
  47. #include <cerrno>
  48. #include <cassert>
  49. #include <cstring>
  50. #include <iomanip>
  51. #include <sstream>
  52. #include <algorithm>
  53. #ifndef HAVE_SLEEP
  54. # ifdef HAVE_WINSOCK_H
  55. # define WIN32_LEAN_AND_MEAN
  56. # include <windows.h>
  57. # endif // HAVE_WINSOCK_H
  58. #endif // HAVE_SLEEP
  59. namespace aria2 {
  60. std::string Util::trim(const std::string& src, const std::string& trimCharset)
  61. {
  62. std::string::size_type sp = src.find_first_not_of(trimCharset);
  63. std::string::size_type ep = src.find_last_not_of(trimCharset);
  64. if(sp == std::string::npos || ep == std::string::npos) {
  65. return "";
  66. } else {
  67. return src.substr(sp, ep-sp+1);
  68. }
  69. }
  70. void Util::split(std::pair<std::string, std::string>& hp, const std::string& src, char delim)
  71. {
  72. hp.first = "";
  73. hp.second = "";
  74. std::string::size_type p = src.find(delim);
  75. if(p == std::string::npos) {
  76. hp.first = src;
  77. hp.second = "";
  78. } else {
  79. hp.first = trim(src.substr(0, p));
  80. hp.second = trim(src.substr(p+1));
  81. }
  82. }
  83. std::pair<std::string, std::string> Util::split(const std::string& src, const std::string& delims)
  84. {
  85. std::pair<std::string, std::string> hp;
  86. hp.first = "";
  87. hp.second = "";
  88. std::string::size_type p = src.find_first_of(delims);
  89. if(p == std::string::npos) {
  90. hp.first = src;
  91. hp.second = "";
  92. } else {
  93. hp.first = trim(src.substr(0, p));
  94. hp.second = trim(src.substr(p+1));
  95. }
  96. return hp;
  97. }
  98. int64_t Util::difftv(struct timeval tv1, struct timeval tv2) {
  99. if(tv1.tv_sec < tv2.tv_sec || tv1.tv_sec == tv2.tv_sec && tv1.tv_usec < tv2.tv_usec) {
  100. return 0;
  101. }
  102. return ((int64_t)(tv1.tv_sec-tv2.tv_sec)*1000000+
  103. tv1.tv_usec-tv2.tv_usec);
  104. }
  105. int32_t Util::difftvsec(struct timeval tv1, struct timeval tv2) {
  106. if(tv1.tv_sec < tv2.tv_sec) {
  107. return 0;
  108. }
  109. return tv1.tv_sec-tv2.tv_sec;
  110. }
  111. void Util::slice(std::deque<std::string>& result, const std::string& src, char delim, bool doTrim) {
  112. std::string::size_type p = 0;
  113. while(1) {
  114. std::string::size_type np = src.find(delim, p);
  115. if(np == std::string::npos) {
  116. std::string term = src.substr(p);
  117. if(doTrim) {
  118. term = trim(term);
  119. }
  120. if(term.size()) {
  121. result.push_back(term);
  122. }
  123. break;
  124. }
  125. std::string term = src.substr(p, np-p);
  126. if(doTrim) {
  127. term = trim(term);
  128. }
  129. p = np+1;
  130. if(term.size()) {
  131. result.push_back(term);
  132. }
  133. }
  134. }
  135. bool Util::startsWith(const std::string& target, const std::string& part) {
  136. if(target.size() < part.size()) {
  137. return false;
  138. }
  139. if(part == "") {
  140. return true;
  141. }
  142. if(target.find(part) == 0) {
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. bool Util::endsWith(const std::string& target, const std::string& part) {
  149. if(target.size() < part.size()) {
  150. return false;
  151. }
  152. if(part == "") {
  153. return true;
  154. }
  155. if(target.rfind(part) == target.size()-part.size()) {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. }
  161. std::string Util::replace(const std::string& target, const std::string& oldstr, const std::string& newstr) {
  162. if(target == "" || oldstr == "" ) {
  163. return target;
  164. }
  165. std::string result;
  166. std::string::size_type p = 0;
  167. std::string::size_type np = target.find(oldstr);
  168. while(np != std::string::npos) {
  169. result += target.substr(p, np-p)+newstr;
  170. p = np+oldstr.size();
  171. np = target.find(oldstr, p);
  172. }
  173. result += target.substr(p);
  174. return result;
  175. }
  176. bool Util::shouldUrlencode(const char c)
  177. {
  178. return !(// ALPHA
  179. 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' ||
  180. // DIGIT
  181. '0' <= c && c <= '9' ||
  182. // safe
  183. '$' == c || '-' == c || '_' == c || '.' == c ||
  184. // extra
  185. '!' == c || '*' == c || '\'' == c ||'(' == c ||
  186. ')' == c || ',' == c ||
  187. // reserved
  188. ';' == c || '/' == c || '?' == c || ':' == c ||
  189. '@' == c || '&' == c || '=' == c || '+' == c);
  190. }
  191. std::string Util::urlencode(const unsigned char* target, int32_t len) {
  192. std::string dest;
  193. for(int32_t i = 0; i < len; i++) {
  194. if(shouldUrlencode(target[i])) {
  195. char temp[4];
  196. sprintf(temp, "%%%02x", target[i]);
  197. temp[sizeof(temp)-1] = '\0';
  198. dest.append(temp);
  199. } else {
  200. dest += target[i];
  201. }
  202. }
  203. return dest;
  204. }
  205. std::string Util::torrentUrlencode(const unsigned char* target, int32_t len) {
  206. std::string dest;
  207. for(int32_t i = 0; i < len; i++) {
  208. if('0' <= target[i] && target[i] <= '9' ||
  209. 'A' <= target[i] && target[i] <= 'Z' ||
  210. 'a' <= target[i] && target[i] <= 'z') {
  211. dest += target[i];
  212. } else {
  213. char temp[4];
  214. sprintf(temp, "%%%02x", target[i]);
  215. temp[sizeof(temp)-1] = '\0';
  216. dest.append(temp);
  217. }
  218. }
  219. return dest;
  220. }
  221. std::string Util::urldecode(const std::string& target) {
  222. std::string result;
  223. for(std::string::const_iterator itr = target.begin();
  224. itr != target.end(); itr++) {
  225. if(*itr == '%') {
  226. if(itr+1 != target.end() && itr+2 != target.end() &&
  227. isxdigit(*(itr+1)) && isxdigit(*(itr+2))) {
  228. result += Util::parseInt(std::string(itr+1, itr+3), 16);
  229. itr += 2;
  230. } else {
  231. result += *itr;
  232. }
  233. } else {
  234. result += *itr;
  235. }
  236. }
  237. return result;
  238. }
  239. std::string Util::toHex(const unsigned char* src, int32_t len) {
  240. char* temp = new char[len*2+1];
  241. for(int32_t i = 0; i < len; i++) {
  242. sprintf(temp+i*2, "%02x", src[i]);
  243. }
  244. temp[len*2] = '\0';
  245. std::string hex = temp;
  246. delete [] temp;
  247. return hex;
  248. }
  249. FILE* Util::openFile(const std::string& filename, const std::string& mode) {
  250. FILE* file = fopen(filename.c_str(), mode.c_str());
  251. return file;
  252. }
  253. void Util::fileCopy(const std::string& dest, const std::string& src) {
  254. File file(src);
  255. rangedFileCopy(dest, src, 0, file.size());
  256. }
  257. void Util::rangedFileCopy(const std::string& dest, const std::string& src, int64_t srcOffset, int64_t length)
  258. {
  259. int32_t bufSize = 4096;
  260. unsigned char buf[bufSize];
  261. DefaultDiskWriter srcdw;
  262. DefaultDiskWriter destdw;
  263. srcdw.openExistingFile(src);
  264. destdw.initAndOpenFile(dest);
  265. int32_t x = length/bufSize;
  266. int32_t r = length%bufSize;
  267. int64_t initialOffset = srcOffset;
  268. for(int32_t i = 0; i < x; ++i) {
  269. int32_t readLength = 0;
  270. while(readLength < bufSize) {
  271. int32_t ret = srcdw.readData(buf, bufSize-readLength, srcOffset);
  272. destdw.writeData(buf, ret, srcOffset-initialOffset);
  273. srcOffset += ret;
  274. readLength += ret;
  275. }
  276. }
  277. if(r > 0) {
  278. int32_t readLength = 0;
  279. while(readLength < r) {
  280. int32_t ret = srcdw.readData(buf, r-readLength, srcOffset);
  281. destdw.writeData(buf, ret, srcOffset-initialOffset);
  282. srcOffset += ret;
  283. readLength += ret;
  284. }
  285. }
  286. }
  287. bool Util::isPowerOf(int32_t num, int32_t base) {
  288. if(base <= 0) { return false; }
  289. if(base == 1) { return true; }
  290. while(num%base == 0) {
  291. num /= base;
  292. if(num == 1) {
  293. return true;
  294. }
  295. }
  296. return false;
  297. }
  298. std::string Util::secfmt(int32_t sec) {
  299. std::string str;
  300. if(sec >= 3600) {
  301. str = itos(sec/3600)+"h";
  302. sec %= 3600;
  303. }
  304. if(sec >= 60) {
  305. int32_t min = sec/60;
  306. if(min < 10) {
  307. str += "0";
  308. }
  309. str += itos(min)+"m";
  310. sec %= 60;
  311. }
  312. if(sec < 10) {
  313. str += "0";
  314. }
  315. str += itos(sec)+"s";
  316. return str;
  317. }
  318. int32_t Util::expandBuffer(char** pbuf, int32_t curLength, int32_t newLength) {
  319. char* newbuf = new char[newLength];
  320. memcpy(newbuf, *pbuf, curLength);
  321. delete [] *pbuf;
  322. *pbuf = newbuf;
  323. return newLength;
  324. }
  325. int32_t getNum(const char* buf, int32_t offset, int32_t length) {
  326. char* temp = new char[length+1];
  327. memcpy(temp, buf+offset, length);
  328. temp[length] = '\0';
  329. int32_t x = strtol(temp, NULL, 10);
  330. delete [] temp;
  331. return x;
  332. }
  333. void unfoldSubRange(const std::string& src, std::deque<int32_t>& range) {
  334. if(src.empty()) {
  335. return;
  336. }
  337. std::string::size_type p = src.find_first_of(",-");
  338. if(p == 0) {
  339. return;
  340. } else if(p == std::string::npos) {
  341. range.push_back(atoi(src.c_str()));
  342. } else {
  343. if(src.at(p) == ',') {
  344. int32_t num = getNum(src.c_str(), 0, p);
  345. range.push_back(num);
  346. unfoldSubRange(src.substr(p+1), range);
  347. } else if(src.at(p) == '-') {
  348. int32_t rightNumBegin = p+1;
  349. std::string::size_type nextDelim = src.find_first_of(",", rightNumBegin);
  350. if(nextDelim == std::string::npos) {
  351. nextDelim = src.size();
  352. }
  353. int32_t left = getNum(src.c_str(), 0, p);
  354. int32_t right = getNum(src.c_str(), rightNumBegin, nextDelim-rightNumBegin);
  355. for(int32_t i = left; i <= right; i++) {
  356. range.push_back(i);
  357. }
  358. if(src.size() > nextDelim) {
  359. unfoldSubRange(src.substr(nextDelim+1), range);
  360. }
  361. }
  362. }
  363. }
  364. void Util::unfoldRange(const std::string& src, std::deque<int32_t>& range) {
  365. unfoldSubRange(src, range);
  366. std::sort(range.begin(), range.end());
  367. range.erase(std::unique(range.begin(), range.end()), range.end());
  368. }
  369. int32_t Util::parseInt(const std::string& s, int32_t base)
  370. {
  371. std::string trimed = Util::trim(s);
  372. if(trimed.empty()) {
  373. throw new DlAbortEx(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  374. "empty string");
  375. }
  376. char* stop;
  377. errno = 0;
  378. long int v = strtol(trimed.c_str(), &stop, base);
  379. if(*stop != '\0') {
  380. throw new DlAbortEx(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  381. trimed.c_str());
  382. } else if((v == LONG_MIN || v == LONG_MAX) && errno == ERANGE || v > INT32_MAX || v < INT32_MIN) {
  383. throw new DlAbortEx(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  384. trimed.c_str());
  385. }
  386. return v;
  387. }
  388. int64_t Util::parseLLInt(const std::string& s, int32_t base)
  389. {
  390. std::string trimed = Util::trim(s);
  391. if(trimed.empty()) {
  392. throw new DlAbortEx(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  393. "empty string");
  394. }
  395. char* stop;
  396. errno = 0;
  397. int64_t v = strtoll(trimed.c_str(), &stop, base);
  398. if(*stop != '\0') {
  399. throw new DlAbortEx(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  400. trimed.c_str());
  401. } else if((v == INT64_MIN || v == INT64_MAX) && errno == ERANGE) {
  402. throw new DlAbortEx(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  403. trimed.c_str());
  404. }
  405. return v;
  406. }
  407. IntSequence Util::parseIntRange(const std::string& src)
  408. {
  409. IntSequence::Values values;
  410. std::string temp = src;
  411. while(temp.size()) {
  412. std::pair<std::string, std::string> p = Util::split(temp, ",");
  413. temp = p.second;
  414. if(p.first.empty()) {
  415. continue;
  416. }
  417. if(p.first.find("-") == std::string::npos) {
  418. int32_t v = Util::parseInt(p.first.c_str());
  419. values.push_back(IntSequence::Value(v, v+1));
  420. } else {
  421. std::pair<std::string, std::string> vp = Util::split(p.first.c_str(), "-");
  422. if(vp.first.empty() || vp.second.empty()) {
  423. throw new DlAbortEx(MSG_INCOMPLETE_RANGE, p.first.c_str());
  424. }
  425. int32_t v1 = Util::parseInt(vp.first.c_str());
  426. int32_t v2 = Util::parseInt(vp.second.c_str());
  427. values.push_back(IntSequence::Value(v1, v2+1));
  428. }
  429. }
  430. return values;
  431. }
  432. std::string Util::getContentDispositionFilename(const std::string& header) {
  433. std::string keyName = "filename=";
  434. std::string::size_type attributesp = header.find(keyName);
  435. if(attributesp == std::string::npos) {
  436. return "";
  437. }
  438. std::string::size_type filenamesp = attributesp+strlen(keyName.c_str());
  439. std::string::size_type filenameep;
  440. if(filenamesp == header.size()) {
  441. return "";
  442. }
  443. if(header[filenamesp] == '\'' || header[filenamesp] == '"') {
  444. char quoteChar = header[filenamesp];
  445. filenameep = header.find(quoteChar, filenamesp+1);
  446. } else {
  447. filenameep = header.find(';', filenamesp);
  448. }
  449. if(filenameep == std::string::npos) {
  450. filenameep = header.size();
  451. }
  452. return trim(header.substr(filenamesp, filenameep-filenamesp), "\r\n '\"");
  453. }
  454. static int32_t nbits[] = {
  455. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  456. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  457. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  458. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  459. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  460. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  461. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  462. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  463. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  464. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  465. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  466. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  467. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  468. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  469. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  470. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
  471. };
  472. int32_t Util::countBit(uint32_t n) {
  473. return
  474. nbits[n&0xffu]+
  475. nbits[(n >> 8)&0xffu]+
  476. nbits[(n >> 16)&0xffu]+
  477. nbits[(n >> 24)&0xffu];
  478. }
  479. std::string Util::randomAlpha(int32_t length, const RandomizerHandle& randomizer) {
  480. static const char *random_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  481. std::string str;
  482. for(int32_t i = 0; i < length; i++) {
  483. int32_t index = randomizer->getRandomNumber(strlen(random_chars));
  484. str += random_chars[index];
  485. }
  486. return str;
  487. }
  488. std::string Util::toUpper(const std::string& src) {
  489. std::string temp = src;
  490. std::transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
  491. return temp;
  492. }
  493. std::string Util::toLower(const std::string& src) {
  494. std::string temp = src;
  495. std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
  496. return temp;
  497. }
  498. bool Util::isNumbersAndDotsNotation(const std::string& name) {
  499. struct sockaddr_in sockaddr;
  500. if(inet_aton(name.c_str(), &sockaddr.sin_addr)) {
  501. return true;
  502. } else {
  503. return false;
  504. }
  505. }
  506. void Util::setGlobalSignalHandler(int32_t sig, void (*handler)(int), int32_t flags) {
  507. #ifdef HAVE_SIGACTION
  508. struct sigaction sigact;
  509. sigact.sa_handler = handler;
  510. sigact.sa_flags = flags;
  511. sigemptyset(&sigact.sa_mask);
  512. sigaction(sig, &sigact, NULL);
  513. #else
  514. signal(sig, handler);
  515. #endif // HAVE_SIGACTION
  516. }
  517. void Util::indexRange(int32_t& startIndex, int32_t& endIndex,
  518. int64_t offset, int32_t srcLength, int32_t destLength)
  519. {
  520. int64_t _startIndex = offset/destLength;
  521. int64_t _endIndex = (offset+srcLength-1)/destLength;
  522. assert(_startIndex <= INT32_MAX);
  523. assert(_endIndex <= INT32_MAX);
  524. startIndex = _startIndex;
  525. endIndex = _endIndex;
  526. }
  527. std::string Util::getHomeDir()
  528. {
  529. const char* p = getenv("HOME");
  530. if(p) {
  531. return p;
  532. } else {
  533. return "";
  534. }
  535. }
  536. int64_t Util::getRealSize(const std::string& sizeWithUnit)
  537. {
  538. std::string::size_type p = sizeWithUnit.find_first_of("KM");
  539. std::string size;
  540. int32_t mult = 1;
  541. if(p == std::string::npos) {
  542. size = sizeWithUnit;
  543. } else {
  544. if(sizeWithUnit[p] == 'K') {
  545. mult = 1024;
  546. } else if(sizeWithUnit[p] == 'M') {
  547. mult = 1024*1024;
  548. }
  549. size = sizeWithUnit.substr(0, p);
  550. }
  551. int64_t v = Util::parseLLInt(size);
  552. if(v < 0) {
  553. throw new DlAbortEx("Negative value detected: %s", sizeWithUnit.c_str());
  554. } else if(v*mult < 0) {
  555. throw new DlAbortEx(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  556. "overflow/underflow");
  557. }
  558. return v*mult;
  559. }
  560. std::string Util::abbrevSize(int64_t size)
  561. {
  562. if(size < 1024) {
  563. return Util::itos(size, true);
  564. }
  565. char units[] = { 'K', 'M' };
  566. int32_t numUnit = sizeof(units)/sizeof(char);
  567. int32_t i = 0;
  568. int32_t r = size&0x3ff;
  569. size >>= 10;
  570. for(; i < numUnit-1 && size >= 1024; ++i) {
  571. r = size&0x3ff;
  572. size >>= 10;
  573. }
  574. return Util::itos(size, true)+"."+Util::itos(r*10/1024)+units[i]+"i";
  575. }
  576. time_t Util::httpGMT(const std::string& httpStdTime)
  577. {
  578. struct tm tm;
  579. memset(&tm, 0, sizeof(tm));
  580. strptime(httpStdTime.c_str(), "%a, %Y-%m-%d %H:%M:%S GMT", &tm);
  581. time_t thetime = timegm(&tm);
  582. return thetime;
  583. }
  584. void Util::toStream(std::ostream& os, const FileEntries& fileEntries)
  585. {
  586. os << _("Files:") << "\n";
  587. os << "idx|path/length" << "\n";
  588. os << "===+===========================================================================" << "\n";
  589. int32_t count = 1;
  590. for(FileEntries::const_iterator itr = fileEntries.begin();
  591. itr != fileEntries.end(); count++, itr++) {
  592. os << std::setw(3) << count << "|" << (*itr)->getPath() << "\n";
  593. os << " |" << Util::abbrevSize((*itr)->getLength()) << "B" << "\n";
  594. os << "---+---------------------------------------------------------------------------" << "\n";
  595. }
  596. }
  597. void Util::sleep(long seconds) {
  598. #ifdef HAVE_SLEEP
  599. ::sleep(seconds);
  600. #elif defined(HAVE_USLEEP)
  601. ::usleep(seconds * 1000000);
  602. #elif defined(HAVE_WINSOCK2_H)
  603. ::Sleep(seconds * 1000);
  604. #else
  605. #error no sleep function is available (nanosleep?)
  606. #endif
  607. }
  608. void Util::usleep(long microseconds) {
  609. #ifdef HAVE_USLEEP
  610. ::usleep(microseconds);
  611. #elif defined(HAVE_WINSOCK2_H)
  612. LARGE_INTEGER current, freq, end;
  613. static enum {GET_FREQUENCY, GET_MICROSECONDS, SKIP_MICROSECONDS} state = GET_FREQUENCY;
  614. if (state == GET_FREQUENCY) {
  615. if (QueryPerformanceFrequency(&freq))
  616. state = GET_MICROSECONDS;
  617. else
  618. state = SKIP_MICROSECONDS;
  619. }
  620. long msec = microseconds / 1000;
  621. microseconds %= 1000;
  622. if (state == GET_MICROSECONDS && microseconds) {
  623. QueryPerformanceCounter(&end);
  624. end.QuadPart += (freq.QuadPart * microseconds) / 1000000;
  625. while (QueryPerformanceCounter(&current) && (current.QuadPart <= end.QuadPart))
  626. /* noop */ ;
  627. }
  628. if (msec)
  629. Sleep(msec);
  630. #else
  631. #error no usleep function is available (nanosleep?)
  632. #endif
  633. }
  634. bool Util::isNumber(const std::string& what)
  635. {
  636. if(what.empty()) {
  637. return false;
  638. }
  639. for(uint32_t i = 0; i < what.size(); ++i) {
  640. if(!isdigit(what[i])) {
  641. return false;
  642. }
  643. }
  644. return true;
  645. }
  646. bool Util::isLowercase(const std::string& what)
  647. {
  648. if(what.empty()) {
  649. return false;
  650. }
  651. for(uint32_t i = 0; i < what.size(); ++i) {
  652. if(!('a' <= what[i] && what[i] <= 'z')) {
  653. return false;
  654. }
  655. }
  656. return true;
  657. }
  658. bool Util::isUppercase(const std::string& what)
  659. {
  660. if(what.empty()) {
  661. return false;
  662. }
  663. for(uint32_t i = 0; i < what.size(); ++i) {
  664. if(!('A' <= what[i] && what[i] <= 'Z')) {
  665. return false;
  666. }
  667. }
  668. return true;
  669. }
  670. int32_t Util::alphaToNum(const std::string& alphabets)
  671. {
  672. if(alphabets.empty()) {
  673. return 0;
  674. }
  675. char base;
  676. if(islower(alphabets[0])) {
  677. base = 'a';
  678. } else {
  679. base = 'A';
  680. }
  681. int32_t num = 0;
  682. for(uint32_t i = 0; i < alphabets.size(); ++i) {
  683. int32_t v = alphabets[i]-base;
  684. num = num*26+v;
  685. }
  686. return num;
  687. }
  688. void Util::mkdirs(const std::string& dirpath)
  689. {
  690. File dir(dirpath);
  691. if(dir.isDir()) {
  692. // do nothing
  693. } else if(dir.exists()) {
  694. throw new DlAbortEx(EX_MAKE_DIR, dir.getPath().c_str(), "File already exists.");
  695. } else if(!dir.mkdirs()) {
  696. throw new DlAbortEx(EX_MAKE_DIR, dir.getPath().c_str(), strerror(errno));
  697. }
  698. }
  699. void Util::convertBitfield(BitfieldMan* dest, const BitfieldMan* src)
  700. {
  701. for(int32_t index = 0; index < dest->countBlock(); ++index) {
  702. if(src->isBitSetOffsetRange((int64_t)index*dest->getBlockLength(),
  703. dest->getBlockLength())) {
  704. dest->setBit(index);
  705. }
  706. }
  707. }
  708. std::string Util::toString(const BinaryStreamHandle& binaryStream)
  709. {
  710. std::stringstream strm;
  711. char data[2048];
  712. while(1) {
  713. int32_t dataLength = binaryStream->readData((unsigned char*)data, sizeof(data), strm.tellp());
  714. strm.write(data, dataLength);
  715. if(dataLength == 0) {
  716. break;
  717. }
  718. }
  719. return strm.str();
  720. }
  721. #ifdef HAVE_POSIX_MEMALIGN
  722. /**
  723. * In linux 2.6, alignment and size should be a multiple of 512.
  724. */
  725. void* Util::allocateAlignedMemory(size_t alignment, size_t size)
  726. {
  727. void* buffer;
  728. int32_t res;
  729. if((res = posix_memalign(&buffer, alignment, size)) != 0) {
  730. throw new FatalException("Error in posix_memalign: %s", strerror(res));
  731. }
  732. return buffer;
  733. }
  734. #endif // HAVE_POSIX_MEMALIGN
  735. } // namespace aria2