Streamable.cpp 2.8 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. /*****************************************************************************/
  7. #include "core/basics/Streamable.h"
  8. #include <stdexcept>
  9. #include <fstream>
  10. #include <sstream>
  11. #include <core/basics/gzstream.h>
  12. #include <core/basics/bzstream.h>
  13. #include "core/basics/Exception.h"
  14. namespace NICE {
  15. const char* GERMAN_LOCALE = "de_DE.utf8";
  16. const char* US_ENGLISH_LOCALE = "en_US.utf8";
  17. // const char* US_ENGLISH_LOCALE = "en_US";
  18. // set the global locale to US english at startup
  19. class __SetLocale__ {
  20. private:
  21. inline __SetLocale__() {
  22. std::locale::global(std::locale(US_ENGLISH_LOCALE));
  23. }
  24. static __SetLocale__ instance;
  25. };
  26. void Streamable::readFromFile(const char* filename, Format compression)
  27. {
  28. switch(compression) {
  29. case ASCII: {
  30. std::ifstream file(filename);
  31. if (!file.good() || !file.is_open()) {
  32. fthrow(Exception, "readFromFile(): could not open file: " << filename);
  33. }
  34. read(file);
  35. file.close();
  36. break;
  37. }
  38. #ifdef NICE_USELIB_ZLIB
  39. case ASCII_GZ: {
  40. igzstream file(filename);
  41. if (!file.good()) {
  42. fthrow(Exception, "readFromFile(): could not open file: " << filename);
  43. }
  44. read(file);
  45. file.close();
  46. break;
  47. }
  48. #endif
  49. #ifdef NICE_USELIB_BZLIB
  50. case ASCII_BZ: {
  51. ibzstream file(filename);
  52. if (!file.good()) {
  53. fthrow(Exception, "readFromFile(): could not open file: " << filename);
  54. }
  55. read(file);
  56. file.close();
  57. break;
  58. }
  59. #endif
  60. default:
  61. fthrow(Exception,
  62. "File cannot be read! Compression method not supported.");
  63. break;
  64. }
  65. }
  66. void Streamable::writeToFile(const char* filename, Format compression) const
  67. {
  68. switch(compression) {
  69. case ASCII: {
  70. std::ofstream file(filename);
  71. if (!file.good()) {
  72. fthrow(Exception, "writeToFile(): could not open file: " << filename);
  73. }
  74. write(file);
  75. file.close();
  76. break;
  77. }
  78. #ifdef NICE_USELIB_ZLIB
  79. case ASCII_GZ: {
  80. ogzstream file(filename);
  81. if (!file.good()) {
  82. fthrow(Exception, "writeToFile(): could not open file: " << filename);
  83. }
  84. write(file);
  85. file.close();
  86. break;
  87. }
  88. #endif
  89. #ifdef NICE_USELIB_BZLIB
  90. case ASCII_BZ: {
  91. obzstream file(filename);
  92. if (!file.good()) {
  93. fthrow(Exception, "writeToFile(): could not open file: " << filename);
  94. }
  95. write(file);
  96. file.close();
  97. break;
  98. }
  99. #endif
  100. default:
  101. fthrow(Exception,
  102. "File cannot be written! Compression method not supported.");
  103. break;
  104. }
  105. }
  106. std::string Streamable::toString() const {
  107. std::stringstream s;
  108. s << *this;
  109. return s.str();
  110. }
  111. }; // namespace NICE