LayersTextFileStream.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * \file LayersTextFileStream.h
  3. * \author The VLE Development Team
  4. * See the AUTHORS or Authors.txt file
  5. * \version 2.0
  6. * \date 3 juin 2013
  7. * \brief Flux texte sur fichier de couches.
  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 LAYERSTEXTFILESTREAM_H_
  27. #define LAYERSTEXTFILESTREAM_H_
  28. #include <iostream>
  29. #include <vector>
  30. #include <list>
  31. #include <cstdlib>
  32. #include <cstdio>
  33. #include <stdbool.h>
  34. #include <string>
  35. #include <sstream>
  36. #include "FileStream.h"
  37. #include "Layer.h"
  38. #include "Constantes.h"
  39. /*! \class LayersTextFileStream
  40. *
  41. * \brief gère un flux texte en lecture et écriture sur le fichier de couches.
  42. */
  43. class LayersTextFileStream : public FileStream<corsenContainers::vecS,Layer>
  44. {
  45. public :
  46. LayersTextFileStream(std::string & filePath) : FileStream<corsenContainers::vecS,Layer>(filePath) {}
  47. virtual ~LayersTextFileStream() {}
  48. public :
  49. /*!
  50. * \brief Lecture
  51. *
  52. * Ouvre le fichier de couches et lance la méthode de lecture adaptée.
  53. * \param void
  54. * \return l'objet contenant les données lues.
  55. */
  56. returnType * read() const throw (FileNotFound, InvalidFileFormat)
  57. {
  58. std::ifstream file(_filePath.c_str(),std::ios::in);
  59. if(not file)
  60. throw FileNotFound("Exception opening file\n "+_filePath);
  61. if(_format.compare("") == 0)
  62. return readStandardFormat(file);
  63. throw InvalidFileFormat("Exception Unknown file format "+_filePath+"\n");
  64. }
  65. /*!
  66. * \brief Écriture
  67. * Ouvre le fichier de couches et lance la méthode d'écriture adaptée.
  68. * \param object : objet contenant les informations à écrire
  69. * \param extension : format du fichier pour la sauvegarde
  70. * \return void
  71. */
  72. void write(const returnType & object, const std::string & extension) const throw (FileNotFound, InvalidFileFormat)
  73. {
  74. std::string filePath(_filePath);
  75. filePath.append(extension);
  76. std::ofstream file(filePath.c_str() ,std::ios::app);
  77. if(not file)
  78. throw ("Exception opening file\n "+ filePath);
  79. if(_format.compare("") == 0)
  80. return writeStandardFormat(file, object);
  81. throw InvalidFileFormat("Exception Unknown file format "+ filePath + "\n");
  82. }
  83. private:
  84. /*!
  85. * \brief Lecture au format standard
  86. *
  87. * Lit dans le fichier '_filePath' le label et la couleur des couches de segments raster.
  88. * \param file fichier dans lequel lire les données.
  89. * \return l'objet contenant les données lues.
  90. */
  91. returnType * readStandardFormat(std::ifstream & file) const
  92. {
  93. std::string line;
  94. std::vector<Layer> * layers = new std::vector<Layer>();
  95. while(std::getline(file,line))
  96. layers->push_back(Layer(line));
  97. file.close();
  98. return layers;
  99. }
  100. /*!
  101. * \brief Écriture au format standard
  102. *
  103. * Écrit dans le fichier '_filePath' le label et la la couleur des couches de segments raster.
  104. * \param file fichier dans lequel écrire les données.
  105. * \param object l'objet contenant les informations à écrire.
  106. * \return void
  107. */
  108. void writeStandardFormat(std::ofstream & file, const returnType & object) const
  109. {
  110. for (std::vector<Layer>::const_iterator it = object.begin(); it!= object.end(); it++)
  111. it->toTextFile(file);
  112. }
  113. };
  114. #endif /* LAYERSTEXTFILESTREAM_H_ */