vectorio.tcc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libfbasics - library of some basic tools
  4. * See file License for license information.
  5. */
  6. #include "core/basics/vectorio.h"
  7. #include <sstream>
  8. #include "core/basics/Exception.h"
  9. namespace NICE {
  10. template<class T>
  11. std::ostream& operator << (std::ostream& output, const std::vector<T>& v) {
  12. output << "< ";
  13. for (unsigned int i = 0; i < v.size(); i++) {
  14. output << v[i] << " ";
  15. }
  16. output << ">";
  17. return output;
  18. }
  19. template<class T>
  20. void writeVectorHumanReadable(std::ostream& output, const std::vector<T>& v) {
  21. output << "<" << std::endl;
  22. for (unsigned int i = 0; i < v.size(); i++) {
  23. output << v[i] << std::endl;
  24. }
  25. output << ">";
  26. }
  27. template<class T>
  28. std::istream& operator >> (std::istream& input, std::vector<T>& v) {
  29. char c;
  30. input >> c;
  31. if (c != '<') {
  32. input.putback ( c );
  33. int dataSize;
  34. // try to read the size, and do not care about
  35. input >> dataSize;
  36. input >> c;
  37. bool AwA (true); // animals with attribues come along with features directly written into the file, without size or brackets
  38. if ( (c != '<' ) && AwA)
  39. {
  40. input.putback(c);
  41. // input.putback(dataSize); // there is always a nasty white-space at the beginning
  42. }
  43. else if ( c != '<' )
  44. {
  45. fthrow(Exception, "syntax error reading vector");
  46. }
  47. }
  48. while (true) {
  49. char c = '\0';
  50. input >> c;
  51. if (c == '>' || input.eof()) {
  52. break;
  53. // FIXME check if end of stream or followed by whitespace
  54. }
  55. input.putback(c);
  56. // std::string s;
  57. // input >> s;
  58. // if (s == ">") {
  59. // break;
  60. // }
  61. // std::stringstream st(s);
  62. // T x;
  63. // st >> x;
  64. T x;
  65. input >> x;
  66. v.push_back(x);
  67. }
  68. return input;
  69. }
  70. template<class T>
  71. std::ostream& operator << (std::ostream& output, const std::vector<T*>& v) {
  72. output << "< ";
  73. for (unsigned int i = 0; i < v.size(); i++) {
  74. output << *(v[i]) << " ";
  75. }
  76. output << ">";
  77. return output;
  78. }
  79. template<class T>
  80. void writeVectorHumanReadable(std::ostream& output, const std::vector<T*>& v) {
  81. output << "<" << std::endl;
  82. for (unsigned int i = 0; i < v.size(); i++) {
  83. output << *(v[i]) << std::endl;
  84. }
  85. output << ">";
  86. }
  87. template<class T>
  88. std::istream& operator >> (std::istream& input, std::vector<T*>& v) {
  89. char c;
  90. input >> c;
  91. if (c != '<') {
  92. fthrow(Exception, "syntax error reading vector");
  93. }
  94. while (true) {
  95. std::string s;
  96. input >> s;
  97. if (s == ">") {
  98. break;
  99. }
  100. std::stringstream st(s);
  101. T* x = new T();
  102. st >> *x;
  103. v.push_back(x);
  104. }
  105. return input;
  106. }
  107. } // namespace