BinStreamable.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/BinStreamable.h"
  8. #include <stdexcept>
  9. #include <fstream>
  10. #include <core/basics/gzbinstream.h>
  11. #include "core/basics/Exception.h"
  12. namespace NICE {
  13. void BinStreamable::readFromBinFile(const char *filename, Format compression)
  14. {
  15. switch(compression) {
  16. case BIN: {
  17. ifbinstream file(filename);
  18. if (!file.good()) {
  19. fthrow(Exception, "readFromBinFile(): could not open file.");
  20. }
  21. readBin(file);
  22. file.close();
  23. break;
  24. }
  25. #ifdef NICE_USELIB_ZLIB
  26. case BIN_GZ: {
  27. igzbinstream file(filename);
  28. if (!file.good()) {
  29. fthrow(Exception, "readFromBinFile(): could not open file.");
  30. }
  31. readBin(file);
  32. file.close();
  33. break;
  34. }
  35. #endif
  36. default:
  37. fthrow(Exception,
  38. "File cannot be read! Compression method not supported.");
  39. break;
  40. }
  41. }
  42. void BinStreamable::writeToBinFile(const char *filename,
  43. Format compression) const
  44. {
  45. switch(compression) {
  46. case BIN: {
  47. ofbinstream file(filename);
  48. if (!file.good()) {
  49. fthrow(Exception, "writeToBinFile(): could not open file.");
  50. }
  51. writeBin(file);
  52. file.close();
  53. break;
  54. }
  55. #ifdef NICE_USELIB_ZLIB
  56. case BIN_GZ: {
  57. ogzbinstream file(filename);
  58. if (!file.good()) {
  59. fthrow(Exception, "writeToBinFile(): could not open file.");
  60. }
  61. writeBin(file);
  62. file.close();
  63. break;
  64. }
  65. #endif
  66. default:
  67. fthrow(Exception,
  68. "File cannot be written! Compression method not supported.");
  69. break;
  70. }
  71. }
  72. }; // namespace NICE