SlopeFileStream.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*!
  2. * \file SlopeFileStream.h
  3. * \brief Flux binaire sur fichier de pentes.
  4. * \author The VLE Development Team
  5. * See the AUTHORS or Authors.txt file
  6. * \version 2.0
  7. * \date 20 juin 2013
  8. */
  9. /*
  10. * Copyright (C) 2012-2013 ULCO http://www.univ-littoral.fr
  11. * Copyright (C) 2012-2013 INRA http://www.inra.fr
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #ifndef SLOPEFILESTREAM_H_
  27. #define SLOPEFILESTREAM_H_
  28. // read a file and save it into memory
  29. #include <sstream>
  30. #include "FileStream.h"
  31. /*! \class SlopeFileStream
  32. * \brief gère un flux binaire en lecture et écriture sur le fichier de pentes.
  33. *
  34. */
  35. class SlopeFileStream: public FileStream<corsenContainers::vecS,float>
  36. {
  37. public :
  38. SlopeFileStream(const std::string & filePath) : FileStream<corsenContainers::vecS,float>(filePath) {}
  39. virtual ~SlopeFileStream(){}
  40. returnType * read() const throw (FileNotFound, ReadError, InvalidFileFormat, std::bad_alloc)
  41. {
  42. std::ifstream gradientFile(_filePath.c_str(),std::ios::binary);
  43. if(not gradientFile)
  44. throw FileNotFound("Exception opening file\n "+_filePath);
  45. if(_format.compare("") == 0)
  46. return readStandardFormat(gradientFile);
  47. throw InvalidFileFormat("Exception Unknown file format "+_filePath+"\n");
  48. }
  49. void write(const returnType & /* object */, const std::string & /* extension */) const
  50. {
  51. }
  52. returnType * readStandardFormat(std::ifstream & file) const throw (ReadError, std::bad_alloc)
  53. {
  54. int i = 0;
  55. float tmp;
  56. try
  57. {
  58. returnType * gradientVec = new returnType();
  59. do
  60. {
  61. file.read(reinterpret_cast<char *>(&tmp), sizeof(float));
  62. if(file.eof())
  63. {
  64. file.close();
  65. #ifdef DEBUG
  66. std::cout<<"SlopeFileStream : all characters read successfully.\n";
  67. #endif
  68. break;
  69. }
  70. else if(file.bad())
  71. {
  72. file.close();
  73. std::stringstream ss;
  74. ss << i * sizeof(float);
  75. throw ReadError("Exception Reading file, only " + ss.str() + " bytes could be read");
  76. }
  77. else
  78. {
  79. gradientVec->push_back(tmp);
  80. i++;
  81. }
  82. }
  83. while(file.good());
  84. return gradientVec;
  85. }
  86. catch(std::bad_alloc & e)
  87. {
  88. throw;
  89. }
  90. }
  91. void writeStandardFormat(std::ofstream & /* file */,
  92. const returnType & /* object */) const
  93. {
  94. }
  95. };
  96. #endif /* SLOPEFILESTREAM_H_ */