DrainFileStream.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*!
  2. * \file DrainFileStream.h
  3. * \brief Flux binaire sur fichier contenant toutes les données de l'arborescence de MntSurf.
  4. * \author The VLE Development Team
  5. * See the AUTHORS or Authors.txt file
  6. * \version 2.0
  7. * \date 13 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 DRAINFILESTREAM_H_
  27. #define DRAINFILESTREAM_H_
  28. #include "FileStream.h"
  29. #include "Coordinates.h"
  30. #include <typeinfo>
  31. #include <vector>
  32. /*! \class DrainFileStream
  33. *
  34. * \brief gère un flux binaire en lecture et écriture sur le fichier de drain.
  35. */
  36. template <class X, class Y, class T = FileStream<X,Y> >
  37. class DrainFileStream : public T
  38. {
  39. public :
  40. DrainFileStream(std::string & filePath) : T(filePath)
  41. {
  42. }
  43. ~DrainFileStream() {}
  44. typename T::returnType * read() const throw (FileNotFound, ReadError, InvalidFileFormat, std::bad_alloc)
  45. {
  46. std::ifstream drainFile(T::_filePath.c_str(),std::ios::binary);
  47. if(not drainFile)
  48. throw FileNotFound("Exception opening file\n: "+ T:: _filePath);
  49. if(T::_format.compare("") == 0)
  50. return readStandardFormat(drainFile);
  51. throw InvalidFileFormat("Exception Unknown file format "+T::_filePath+"\n");
  52. }
  53. void write(const typename T::returnType & object, const std::string & extension) const
  54. {
  55. /* std::string filePath(_filePath);
  56. filePath.append(extension);
  57. std::ofstream drainFile(filePath.c_str(), std::ios::trunc);
  58. if(not drainFile)
  59. throw FileNotFound("Exception opening file "+ filePath +"\n");
  60. if(_format.compare("") == 0)
  61. return writeStandardFormat(drainFile, object);
  62. throw InvalidFileFormat("Exception Unknown file format "+ filePath +"\n");
  63. */
  64. }
  65. private :
  66. typename T::returnType * readStandardFormat(std::ifstream & file) const throw(ReadError, std::bad_alloc)
  67. {
  68. try
  69. {
  70. typename T::returnType * tmpVec = new typename T::returnType();
  71. Y tmp;
  72. do
  73. {
  74. file.read(reinterpret_cast<char *>(&tmp), sizeof(Y));
  75. if(not file.eof() and not file.bad())
  76. tmpVec->push_back(tmp);
  77. }
  78. while(file.good());
  79. if(file.bad() or not file.eof())
  80. {
  81. file.close();
  82. throw ReadError("Exception Reading data"+ T::_filePath);
  83. }
  84. file.close();
  85. return tmpVec;
  86. }
  87. catch(std::bad_alloc & e)
  88. {
  89. throw;
  90. }
  91. }
  92. void writeStandardFormat(std::ofstream & file, const typename T::returnType & object) const
  93. {
  94. Y tmp;
  95. for(int i = 0; i < object.size() ; i++)
  96. {
  97. tmp = object.at(i);
  98. file.write(reinterpret_cast<char *>(&tmp), sizeof(Y));
  99. }
  100. file.close();
  101. }
  102. };
  103. #endif /* DRAINFILESTREAM_H_ */