FileStream.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*!
  2. * \file FileStream.h
  3. * \brief modèle de flux sur fichiers applicable à n'importe quelle classe.
  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 FILESTREAM_H_
  27. #define FILESTREAM_H_
  28. #include <iostream>
  29. #include <fstream>
  30. #include <vector>
  31. #include <boost/algorithm/string.hpp>
  32. #include "Exception.h"
  33. #include "Constantes.h"
  34. namespace corsenContainers
  35. {
  36. struct vecS{};
  37. struct listS{};
  38. struct CType {};
  39. template <class Selector, class ValueType>
  40. struct container_gen { };
  41. template <class ValueType>
  42. struct container_gen<vecS, ValueType> {
  43. typedef std::vector<ValueType> type;
  44. };
  45. template <class ValueType>
  46. struct container_gen<CType, ValueType> {
  47. typedef ValueType type;
  48. };
  49. template <class ValueType>
  50. struct container_gen<listS, ValueType> {
  51. typedef std::list<ValueType> type;
  52. };
  53. }
  54. template<class container, class valuetype>
  55. /*! \class FileStream
  56. *
  57. * \brief est une classe abstraite, elle sert de modèle aux différents lecteurs de fichiers.
  58. *
  59. * La classe gère la lecture et l'ecriture au format standard.
  60. */
  61. class FileStream
  62. {
  63. protected :
  64. std::string _filePath; /*!< chemin du fichier*/
  65. std::string _format; /*!< format du fichier*/
  66. public :
  67. /*!
  68. * \brief Constructeur
  69. *
  70. * Constructeur de la classe FileReader, analyse le chemin du fichier afin de déterminer son format.
  71. *
  72. * \param filePath : chemin du fichier.
  73. * \return void
  74. */
  75. FileStream(const std::string & filePath)
  76. {
  77. _filePath = filePath;
  78. _format = findFormat(filePath);
  79. }
  80. typedef typename corsenContainers::container_gen<container,valuetype>::type returnType;
  81. /*!
  82. * \brief Lecture
  83. *
  84. * Analyse le format du fichier et lance la méthode de lecture adaptée.
  85. *
  86. * \return un objet contenant les informations lues.
  87. */
  88. virtual returnType * read() const = 0;
  89. /*!
  90. * \brief écriture
  91. *
  92. * Analyse le format du fichier et lance la méthode d'écriture adaptée.
  93. *
  94. * \param object : objet contenant les informations à écrire
  95. * \param extension : format du fichier pour la sauvegarde
  96. * \return void
  97. */
  98. virtual void write(const returnType & object, const std::string & extension) const = 0;
  99. /*!
  100. * \brief Destructeur
  101. *
  102. * Destructeur de la classe FileStream
  103. */
  104. virtual ~FileStream(){}
  105. const std::string& getFilePath() const
  106. {
  107. return _filePath;
  108. }
  109. void setFilePath(const std::string& filePath)
  110. {
  111. _filePath = filePath;
  112. _format = findFormat(filePath);
  113. }
  114. protected :
  115. const std::string findFormat(const std::string & filePath) const
  116. {
  117. std::string format("");
  118. std::vector<std::string>Vec;
  119. std::string reversefilePath(filePath.rbegin(), filePath.rend());
  120. boost::split(Vec,reversefilePath,boost::is_any_of("\\/"));
  121. if(Vec.size() !=0)
  122. {
  123. std::string filePathEnd(Vec.at(0).rbegin(),Vec.at(0).rend());
  124. Vec.clear();
  125. boost::split(Vec, filePathEnd, boost::is_any_of("."));
  126. if(isHiddenFile(filePathEnd))
  127. {
  128. for(unsigned int i = 2; i<Vec.size();i++)
  129. format+="."+Vec.at(i);
  130. }
  131. else
  132. {
  133. for(unsigned int i = 1; i<Vec.size();i++)
  134. format+="."+Vec.at(i);
  135. }
  136. }
  137. else
  138. {
  139. boost::split(Vec, _filePath, boost::is_any_of("."));
  140. if(isHiddenFile(_filePath))
  141. {
  142. for(unsigned int i = 2; i<Vec.size();i++)
  143. format+="."+Vec.at(i);
  144. }
  145. else
  146. {
  147. for(unsigned int i = 1; i<Vec.size();i++)
  148. format+="."+Vec.at(i);
  149. }
  150. }
  151. return format;
  152. }
  153. bool isHiddenFile(const std::string & filePath) const
  154. {
  155. if(filePath.substr(0,1).compare(".") == 0)
  156. return true;
  157. return false;
  158. }
  159. private:
  160. /*!
  161. * \brief Lecture au format standard
  162. *
  163. * Lecture des données du fichier selon un formatage particulier .
  164. *
  165. * \param file fichier dans lequel lire les données.
  166. * \return l'objet contenant les informations lues.
  167. */
  168. virtual returnType * readStandardFormat(std::ifstream & file) const = 0;
  169. /*!
  170. * \brief écriture au format standard
  171. *
  172. * Écriture des données dans le fichier selon un formatage particulier.
  173. *
  174. * \param file fichier dans lequel écrire les données.
  175. * \param object l'objet contenant les informations à écrire.
  176. * \return void
  177. */
  178. virtual void writeStandardFormat(std::ofstream & file, const returnType & object) const = 0;
  179. };
  180. #endif /* FILESTREAM_H_ */