Data.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "Data.h"
  23. #include "MetaEntryVisitor.h"
  24. Data::Data(const char* data, int len, bool number):number(number) {
  25. if(data == NULL) {
  26. this->data = NULL;
  27. this->len = 0;
  28. } else {
  29. this->data = new char[len];
  30. memcpy(this->data, data, len);
  31. this->len = len;
  32. }
  33. }
  34. Data::~Data() {
  35. delete [] data;
  36. }
  37. string Data::toString() const {
  38. if(len == 0) {
  39. return "";
  40. } else {
  41. char* temp = new char[len+1];
  42. memcpy(temp, data, len);
  43. temp[len] = '\0';
  44. string str(temp);
  45. delete [] temp;
  46. return str;
  47. }
  48. }
  49. const char* Data::getData() const {
  50. if(this->len == 0) {
  51. return NULL;
  52. } else {
  53. return data;
  54. }
  55. }
  56. int Data::getLen() const {
  57. return len;
  58. }
  59. int Data::toInt() const {
  60. return (int)toLLInt();
  61. }
  62. long long int Data::toLLInt() const {
  63. if(len == 0) {
  64. return 0;
  65. } else {
  66. return strtoll(toString().c_str(), NULL, 10);
  67. }
  68. }
  69. bool Data::isNumber() const {
  70. return number;
  71. }
  72. void Data::accept(MetaEntryVisitor* v) const {
  73. v->visit(this);
  74. }