ElevationFileStream.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*!
  2. * \file ElevationFileStream.h
  3. * \brief Flux sur le fichier d'altitudes.
  4. * \author The VLE Development Team
  5. * See the AUTHORS or Authors.txt file
  6. * \version 2.0
  7. * \date 11 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 ELEVATIONFILESTREAM_H_
  27. #define ELEVATIONFILESTREAM_H_
  28. #include "FileStream.h"
  29. /*! \class ElevationFileStream
  30. *
  31. * \brief gère un flux en lecture et écriture sur le fichier d'altitudes.
  32. */
  33. class ElevationFileStream: public FileStream<corsenContainers::vecS,float>
  34. {
  35. public :
  36. ElevationFileStream(const std::string & filePath) : FileStream<corsenContainers::vecS,float>(filePath) {}
  37. virtual ~ElevationFileStream(){}
  38. returnType * read() const throw (FileNotFound, ReadError, InvalidFileFormat, std::bad_alloc)
  39. {
  40. std::ifstream altFile(_filePath.c_str(),std::ios::binary);
  41. if(not altFile)
  42. throw FileNotFound("Exception opening file\n "+_filePath);
  43. if(_format.compare("") == 0)
  44. return readStandardFormat(altFile);
  45. throw InvalidFileFormat("Exception Unknown file format "+_filePath+"\n");
  46. }
  47. returnType * read(const std::string & extension) const throw (FileNotFound, ReadError, InvalidFileFormat, std::bad_alloc)
  48. {
  49. std::string filePath(_filePath);
  50. filePath.append(extension);
  51. std::ifstream altFile(filePath.c_str(),std::ios::binary);
  52. if(not altFile)
  53. throw FileNotFound("Exception opening file "+filePath+"\n");
  54. if(extension.compare(".sav") == 0)
  55. return readStandardFormat(altFile);
  56. throw InvalidFileFormat("Exception Unknown file format "+filePath+"\n");
  57. }
  58. void write(const returnType & object, const std::string & extension) const throw (FileNotFound, InvalidFileFormat)
  59. {
  60. std::string filePath(_filePath);
  61. filePath.append(extension);
  62. std::ofstream altFile(filePath.c_str(), std::ios::trunc);
  63. if(not altFile)
  64. throw FileNotFound("Exception opening file "+ filePath +"\n");
  65. if(_format.compare("") == 0)
  66. return writeStandardFormat(altFile, object);
  67. throw InvalidFileFormat("Exception Unknown file format "+ filePath +"\n");
  68. }
  69. private :
  70. returnType * readStandardFormat(std::ifstream & file) const throw (ReadError, std::bad_alloc)
  71. {
  72. // get length of file:
  73. file.seekg (0, file.end);
  74. int length = file.tellg();
  75. file.seekg (0, file.beg);
  76. try
  77. {
  78. returnType * tmpVec = new returnType();
  79. /* double tailleeffective = static_cast<double>(length) / static_cast<double>(sizeof(float)); */
  80. #ifdef DEBUG
  81. std::cout<<"------ ElevationFileStream --------- \n";
  82. std::cout<<"Taille en octets : "<<length <<"\nTaille du type de retour : "<<sizeof(float)<<"\nTaille du vec d'élévation : "<<length/sizeof(returnType)
  83. <<"\nTaille effective : "<< tailleeffective <<std::endl;
  84. std::cout<<"------ ElevationFileStream --------- \n";
  85. #endif
  86. float tmp;
  87. do
  88. {
  89. file.read(reinterpret_cast<char *>(&tmp), sizeof(float));
  90. if(not file.eof() and not file.bad())
  91. tmpVec->push_back(tmp);
  92. }
  93. while(file.good());
  94. if(file.bad() or not file.eof())
  95. {
  96. file.close();
  97. throw ReadError("Exception Reading data"+ _filePath);
  98. }
  99. file.close();
  100. return tmpVec;
  101. }
  102. catch(std::bad_alloc & e)
  103. {
  104. throw;
  105. }
  106. }
  107. void writeStandardFormat(std::ofstream & file, const returnType & object) const throw (std::exception)
  108. {
  109. float tmp;
  110. for(returnType::const_iterator it = object.begin(); it!= object.end() ; ++it)
  111. {
  112. tmp = *it;
  113. file.write(reinterpret_cast<char *>(&tmp), sizeof(float));
  114. }
  115. file.close();
  116. }
  117. };
  118. #endif /* ELEVATIONFILESTREAM_H_ */