diskio_bones.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup diskio
  16. //! @{
  17. //! class for saving and loading matrices and fields - INTERNAL USE ONLY!
  18. class diskio
  19. {
  20. public:
  21. inline arma_deprecated static file_type guess_file_type(std::istream& f);
  22. private:
  23. template<typename eT> friend class Mat;
  24. template<typename eT> friend class Cube;
  25. template<typename eT> friend class SpMat;
  26. template<typename oT> friend class field;
  27. friend class Mat_aux;
  28. friend class Cube_aux;
  29. friend class SpMat_aux;
  30. friend class field_aux;
  31. template<typename eT> inline arma_cold static std::string gen_txt_header(const Mat<eT>&);
  32. template<typename eT> inline arma_cold static std::string gen_bin_header(const Mat<eT>&);
  33. template<typename eT> inline arma_cold static std::string gen_bin_header(const SpMat<eT>&);
  34. template<typename eT> inline arma_cold static std::string gen_txt_header(const Cube<eT>&);
  35. template<typename eT> inline arma_cold static std::string gen_bin_header(const Cube<eT>&);
  36. inline arma_cold static file_type guess_file_type_internal(std::istream& f);
  37. inline arma_cold static std::string gen_tmp_name(const std::string& x);
  38. inline arma_cold static bool safe_rename(const std::string& old_name, const std::string& new_name);
  39. template<typename eT> inline static bool convert_token(eT& val, const std::string& token);
  40. template<typename T> inline static bool convert_token(std::complex<T>& val, const std::string& token);
  41. template<typename eT> inline static std::streamsize prepare_stream(std::ostream& f);
  42. //
  43. // matrix saving
  44. template<typename eT> inline static bool save_raw_ascii (const Mat<eT>& x, const std::string& final_name);
  45. template<typename eT> inline static bool save_raw_binary (const Mat<eT>& x, const std::string& final_name);
  46. template<typename eT> inline static bool save_arma_ascii (const Mat<eT>& x, const std::string& final_name);
  47. template<typename eT> inline static bool save_csv_ascii (const Mat<eT>& x, const std::string& final_name, const field<std::string>& header, const bool with_header);
  48. template<typename eT> inline static bool save_arma_binary(const Mat<eT>& x, const std::string& final_name);
  49. template<typename eT> inline static bool save_pgm_binary (const Mat<eT>& x, const std::string& final_name);
  50. template<typename T> inline static bool save_pgm_binary (const Mat< std::complex<T> >& x, const std::string& final_name);
  51. template<typename eT> inline static bool save_hdf5_binary(const Mat<eT>& x, const hdf5_name& spec, std::string& err_msg);
  52. template<typename eT> inline static bool save_raw_ascii (const Mat<eT>& x, std::ostream& f);
  53. template<typename eT> inline static bool save_raw_binary (const Mat<eT>& x, std::ostream& f);
  54. template<typename eT> inline static bool save_arma_ascii (const Mat<eT>& x, std::ostream& f);
  55. template<typename eT> inline static bool save_csv_ascii (const Mat<eT>& x, std::ostream& f);
  56. template<typename T> inline static bool save_csv_ascii (const Mat< std::complex<T> >& x, std::ostream& f);
  57. template<typename eT> inline static bool save_arma_binary(const Mat<eT>& x, std::ostream& f);
  58. template<typename eT> inline static bool save_pgm_binary (const Mat<eT>& x, std::ostream& f);
  59. template<typename T> inline static bool save_pgm_binary (const Mat< std::complex<T> >& x, std::ostream& f);
  60. //
  61. // matrix loading
  62. template<typename eT> inline static bool load_raw_ascii (Mat<eT>& x, const std::string& name, std::string& err_msg);
  63. template<typename eT> inline static bool load_raw_binary (Mat<eT>& x, const std::string& name, std::string& err_msg);
  64. template<typename eT> inline static bool load_arma_ascii (Mat<eT>& x, const std::string& name, std::string& err_msg);
  65. template<typename eT> inline static bool load_csv_ascii (Mat<eT>& x, const std::string& name, std::string& err_msg, field<std::string>& header, const bool with_header);
  66. template<typename eT> inline static bool load_arma_binary(Mat<eT>& x, const std::string& name, std::string& err_msg);
  67. template<typename eT> inline static bool load_pgm_binary (Mat<eT>& x, const std::string& name, std::string& err_msg);
  68. template<typename T> inline static bool load_pgm_binary (Mat< std::complex<T> >& x, const std::string& name, std::string& err_msg);
  69. template<typename eT> inline static bool load_hdf5_binary(Mat<eT>& x, const hdf5_name& spec, std::string& err_msg);
  70. template<typename eT> inline static bool load_auto_detect(Mat<eT>& x, const std::string& name, std::string& err_msg);
  71. template<typename eT> inline static bool load_raw_ascii (Mat<eT>& x, std::istream& f, std::string& err_msg);
  72. template<typename eT> inline static bool load_raw_binary (Mat<eT>& x, std::istream& f, std::string& err_msg);
  73. template<typename eT> inline static bool load_arma_ascii (Mat<eT>& x, std::istream& f, std::string& err_msg);
  74. template<typename eT> inline static bool load_csv_ascii (Mat<eT>& x, std::istream& f, std::string& err_msg);
  75. template<typename T> inline static bool load_csv_ascii (Mat< std::complex<T> >& x, std::istream& f, std::string& err_msg);
  76. template<typename eT> inline static bool load_arma_binary(Mat<eT>& x, std::istream& f, std::string& err_msg);
  77. template<typename eT> inline static bool load_pgm_binary (Mat<eT>& x, std::istream& is, std::string& err_msg);
  78. template<typename T> inline static bool load_pgm_binary (Mat< std::complex<T> >& x, std::istream& is, std::string& err_msg);
  79. template<typename eT> inline static bool load_auto_detect(Mat<eT>& x, std::istream& f, std::string& err_msg);
  80. inline static void pnm_skip_comments(std::istream& f);
  81. //
  82. // sparse matrix saving
  83. template<typename eT> inline static bool save_csv_ascii (const SpMat<eT>& x, const std::string& final_name, const field<std::string>& header, const bool with_header);
  84. template<typename eT> inline static bool save_coord_ascii(const SpMat<eT>& x, const std::string& final_name);
  85. template<typename eT> inline static bool save_arma_binary(const SpMat<eT>& x, const std::string& final_name);
  86. template<typename eT> inline static bool save_csv_ascii (const SpMat<eT>& x, std::ostream& f);
  87. template<typename T> inline static bool save_csv_ascii (const SpMat< std::complex<T> >& x, std::ostream& f);
  88. template<typename eT> inline static bool save_coord_ascii(const SpMat<eT>& x, std::ostream& f);
  89. template<typename T> inline static bool save_coord_ascii(const SpMat< std::complex<T> >& x, std::ostream& f);
  90. template<typename eT> inline static bool save_arma_binary(const SpMat<eT>& x, std::ostream& f);
  91. //
  92. // sparse matrix loading
  93. template<typename eT> inline static bool load_csv_ascii (SpMat<eT>& x, const std::string& name, std::string& err_msg, field<std::string>& header, const bool with_header);
  94. template<typename eT> inline static bool load_coord_ascii(SpMat<eT>& x, const std::string& name, std::string& err_msg);
  95. template<typename eT> inline static bool load_arma_binary(SpMat<eT>& x, const std::string& name, std::string& err_msg);
  96. template<typename eT> inline static bool load_csv_ascii (SpMat<eT>& x, std::istream& f, std::string& err_msg);
  97. template<typename T> inline static bool load_csv_ascii (SpMat< std::complex<T> >& x, std::istream& f, std::string& err_msg);
  98. template<typename eT> inline static bool load_coord_ascii(SpMat<eT>& x, std::istream& f, std::string& err_msg);
  99. template<typename T> inline static bool load_coord_ascii(SpMat< std::complex<T> >& x, std::istream& f, std::string& err_msg);
  100. template<typename eT> inline static bool load_arma_binary(SpMat<eT>& x, std::istream& f, std::string& err_msg);
  101. //
  102. // cube saving
  103. template<typename eT> inline static bool save_raw_ascii (const Cube<eT>& x, const std::string& name);
  104. template<typename eT> inline static bool save_raw_binary (const Cube<eT>& x, const std::string& name);
  105. template<typename eT> inline static bool save_arma_ascii (const Cube<eT>& x, const std::string& name);
  106. template<typename eT> inline static bool save_arma_binary(const Cube<eT>& x, const std::string& name);
  107. template<typename eT> inline static bool save_hdf5_binary(const Cube<eT>& x, const hdf5_name& spec, std::string& err_msg);
  108. template<typename eT> inline static bool save_raw_ascii (const Cube<eT>& x, std::ostream& f);
  109. template<typename eT> inline static bool save_raw_binary (const Cube<eT>& x, std::ostream& f);
  110. template<typename eT> inline static bool save_arma_ascii (const Cube<eT>& x, std::ostream& f);
  111. template<typename eT> inline static bool save_arma_binary(const Cube<eT>& x, std::ostream& f);
  112. //
  113. // cube loading
  114. template<typename eT> inline static bool load_raw_ascii (Cube<eT>& x, const std::string& name, std::string& err_msg);
  115. template<typename eT> inline static bool load_raw_binary (Cube<eT>& x, const std::string& name, std::string& err_msg);
  116. template<typename eT> inline static bool load_arma_ascii (Cube<eT>& x, const std::string& name, std::string& err_msg);
  117. template<typename eT> inline static bool load_arma_binary(Cube<eT>& x, const std::string& name, std::string& err_msg);
  118. template<typename eT> inline static bool load_hdf5_binary(Cube<eT>& x, const hdf5_name& spec, std::string& err_msg);
  119. template<typename eT> inline static bool load_auto_detect(Cube<eT>& x, const std::string& name, std::string& err_msg);
  120. template<typename eT> inline static bool load_raw_ascii (Cube<eT>& x, std::istream& f, std::string& err_msg);
  121. template<typename eT> inline static bool load_raw_binary (Cube<eT>& x, std::istream& f, std::string& err_msg);
  122. template<typename eT> inline static bool load_arma_ascii (Cube<eT>& x, std::istream& f, std::string& err_msg);
  123. template<typename eT> inline static bool load_arma_binary(Cube<eT>& x, std::istream& f, std::string& err_msg);
  124. template<typename eT> inline static bool load_auto_detect(Cube<eT>& x, std::istream& f, std::string& err_msg);
  125. //
  126. // field saving and loading
  127. template<typename T1> inline static bool save_arma_binary(const field<T1>& x, const std::string& name);
  128. template<typename T1> inline static bool save_arma_binary(const field<T1>& x, std::ostream& f);
  129. template<typename T1> inline static bool load_arma_binary( field<T1>& x, const std::string& name, std::string& err_msg);
  130. template<typename T1> inline static bool load_arma_binary( field<T1>& x, std::istream& f, std::string& err_msg);
  131. template<typename T1> inline static bool load_auto_detect( field<T1>& x, const std::string& name, std::string& err_msg);
  132. template<typename T1> inline static bool load_auto_detect( field<T1>& x, std::istream& f, std::string& err_msg);
  133. inline static bool save_std_string(const field<std::string>& x, const std::string& name);
  134. inline static bool save_std_string(const field<std::string>& x, std::ostream& f);
  135. inline static bool load_std_string( field<std::string>& x, const std::string& name, std::string& err_msg);
  136. inline static bool load_std_string( field<std::string>& x, std::istream& f, std::string& err_msg);
  137. //
  138. // handling of PPM images by cubes
  139. template<typename T1> inline static bool save_ppm_binary(const Cube<T1>& x, const std::string& final_name);
  140. template<typename T1> inline static bool save_ppm_binary(const Cube<T1>& x, std::ostream& f);
  141. template<typename T1> inline static bool load_ppm_binary( Cube<T1>& x, const std::string& final_name, std::string& err_msg);
  142. template<typename T1> inline static bool load_ppm_binary( Cube<T1>& x, std::istream& f, std::string& err_msg);
  143. //
  144. // handling of PPM images by fields
  145. template<typename T1> inline static bool save_ppm_binary(const field<T1>& x, const std::string& final_name);
  146. template<typename T1> inline static bool save_ppm_binary(const field<T1>& x, std::ostream& f);
  147. template<typename T1> inline static bool load_ppm_binary( field<T1>& x, const std::string& final_name, std::string& err_msg);
  148. template<typename T1> inline static bool load_ppm_binary( field<T1>& x, std::istream& f, std::string& err_msg);
  149. };
  150. //! @}