1 // SPDX-License-Identifier: Apache-2.0 2 // 3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au) 4 // Copyright 2008-2016 National ICT Australia (NICTA) 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // ------------------------------------------------------------------------ 17 18 19 //! \addtogroup diskio 20 //! @{ 21 22 23 //! class for saving and loading matrices and fields - INTERNAL USE ONLY! 24 class diskio 25 { 26 public: 27 28 inline arma_deprecated static file_type guess_file_type(std::istream& f); 29 30 31 private: 32 33 template<typename eT> friend class Mat; 34 template<typename eT> friend class Cube; 35 template<typename eT> friend class SpMat; 36 template<typename oT> friend class field; 37 38 friend class Mat_aux; 39 friend class Cube_aux; 40 friend class SpMat_aux; 41 friend class field_aux; 42 43 template<typename eT> inline arma_cold static std::string gen_txt_header(const Mat<eT>&); 44 template<typename eT> inline arma_cold static std::string gen_bin_header(const Mat<eT>&); 45 46 template<typename eT> inline arma_cold static std::string gen_bin_header(const SpMat<eT>&); 47 48 template<typename eT> inline arma_cold static std::string gen_txt_header(const Cube<eT>&); 49 template<typename eT> inline arma_cold static std::string gen_bin_header(const Cube<eT>&); 50 51 inline arma_cold static file_type guess_file_type_internal(std::istream& f); 52 53 inline arma_cold static std::string gen_tmp_name(const std::string& x); 54 55 inline arma_cold static bool safe_rename(const std::string& old_name, const std::string& new_name); 56 57 template<typename eT> inline static bool convert_token(eT& val, const std::string& token); 58 template<typename T> inline static bool convert_token(std::complex<T>& val, const std::string& token); 59 60 template<typename eT> inline static std::streamsize prepare_stream(std::ostream& f); 61 62 63 // 64 // matrix saving 65 66 template<typename eT> inline static bool save_raw_ascii (const Mat<eT>& x, const std::string& final_name); 67 template<typename eT> inline static bool save_raw_binary (const Mat<eT>& x, const std::string& final_name); 68 template<typename eT> inline static bool save_arma_ascii (const Mat<eT>& x, const std::string& final_name); 69 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, const char separator); 70 template<typename eT> inline static bool save_coord_ascii(const Mat<eT>& x, const std::string& final_name); 71 template<typename eT> inline static bool save_arma_binary(const Mat<eT>& x, const std::string& final_name); 72 template<typename eT> inline static bool save_pgm_binary (const Mat<eT>& x, const std::string& final_name); 73 template<typename T> inline static bool save_pgm_binary (const Mat< std::complex<T> >& x, const std::string& final_name); 74 template<typename eT> inline static bool save_hdf5_binary(const Mat<eT>& x, const hdf5_name& spec, std::string& err_msg); 75 76 template<typename eT> inline static bool save_raw_ascii (const Mat<eT>& x, std::ostream& f); 77 template<typename eT> inline static bool save_raw_binary (const Mat<eT>& x, std::ostream& f); 78 template<typename eT> inline static bool save_arma_ascii (const Mat<eT>& x, std::ostream& f); 79 template<typename eT> inline static bool save_csv_ascii (const Mat<eT>& x, std::ostream& f, const char separator); 80 template<typename T> inline static bool save_csv_ascii (const Mat< std::complex<T> >& x, std::ostream& f, const char separator); 81 template<typename eT> inline static bool save_coord_ascii(const Mat<eT>& x, std::ostream& f); 82 template<typename T> inline static bool save_coord_ascii(const Mat< std::complex<T> >& x, std::ostream& f); 83 template<typename eT> inline static bool save_arma_binary(const Mat<eT>& x, std::ostream& f); 84 template<typename eT> inline static bool save_pgm_binary (const Mat<eT>& x, std::ostream& f); 85 template<typename T> inline static bool save_pgm_binary (const Mat< std::complex<T> >& x, std::ostream& f); 86 87 88 // 89 // matrix loading 90 91 template<typename eT> inline static bool load_raw_ascii (Mat<eT>& x, const std::string& name, std::string& err_msg); 92 template<typename eT> inline static bool load_raw_binary (Mat<eT>& x, const std::string& name, std::string& err_msg); 93 template<typename eT> inline static bool load_arma_ascii (Mat<eT>& x, const std::string& name, std::string& err_msg); 94 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, const char separator); 95 template<typename eT> inline static bool load_coord_ascii(Mat<eT>& x, const std::string& name, std::string& err_msg); 96 template<typename eT> inline static bool load_arma_binary(Mat<eT>& x, const std::string& name, std::string& err_msg); 97 template<typename eT> inline static bool load_pgm_binary (Mat<eT>& x, const std::string& name, std::string& err_msg); 98 template<typename T> inline static bool load_pgm_binary (Mat< std::complex<T> >& x, const std::string& name, std::string& err_msg); 99 template<typename eT> inline static bool load_hdf5_binary(Mat<eT>& x, const hdf5_name& spec, std::string& err_msg); 100 template<typename eT> inline static bool load_auto_detect(Mat<eT>& x, const std::string& name, std::string& err_msg); 101 102 template<typename eT> inline static bool load_raw_ascii (Mat<eT>& x, std::istream& f, std::string& err_msg); 103 template<typename eT> inline static bool load_raw_binary (Mat<eT>& x, std::istream& f, std::string& err_msg); 104 template<typename eT> inline static bool load_arma_ascii (Mat<eT>& x, std::istream& f, std::string& err_msg); 105 template<typename eT> inline static bool load_csv_ascii (Mat<eT>& x, std::istream& f, std::string& err_msg, const char separator); 106 template<typename T> inline static bool load_csv_ascii (Mat< std::complex<T> >& x, std::istream& f, std::string& err_msg, const char separator); 107 template<typename eT> inline static bool load_coord_ascii(Mat<eT>& x, std::istream& f, std::string& err_msg); 108 template<typename T> inline static bool load_coord_ascii(Mat< std::complex<T> >& x, std::istream& f, std::string& err_msg); 109 template<typename eT> inline static bool load_arma_binary(Mat<eT>& x, std::istream& f, std::string& err_msg); 110 template<typename eT> inline static bool load_pgm_binary (Mat<eT>& x, std::istream& is, std::string& err_msg); 111 template<typename T> inline static bool load_pgm_binary (Mat< std::complex<T> >& x, std::istream& is, std::string& err_msg); 112 template<typename eT> inline static bool load_auto_detect(Mat<eT>& x, std::istream& f, std::string& err_msg); 113 114 inline static void pnm_skip_comments(std::istream& f); 115 116 117 // 118 // sparse matrix saving 119 120 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, const char separator); 121 template<typename eT> inline static bool save_coord_ascii(const SpMat<eT>& x, const std::string& final_name); 122 template<typename eT> inline static bool save_arma_binary(const SpMat<eT>& x, const std::string& final_name); 123 124 template<typename eT> inline static bool save_csv_ascii (const SpMat<eT>& x, std::ostream& f, const char separator); 125 template<typename T> inline static bool save_csv_ascii (const SpMat< std::complex<T> >& x, std::ostream& f, const char separator); 126 template<typename eT> inline static bool save_coord_ascii(const SpMat<eT>& x, std::ostream& f); 127 template<typename T> inline static bool save_coord_ascii(const SpMat< std::complex<T> >& x, std::ostream& f); 128 template<typename eT> inline static bool save_arma_binary(const SpMat<eT>& x, std::ostream& f); 129 130 131 // 132 // sparse matrix loading 133 134 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, const char separator); 135 template<typename eT> inline static bool load_coord_ascii(SpMat<eT>& x, const std::string& name, std::string& err_msg); 136 template<typename eT> inline static bool load_arma_binary(SpMat<eT>& x, const std::string& name, std::string& err_msg); 137 138 template<typename eT> inline static bool load_csv_ascii (SpMat<eT>& x, std::istream& f, std::string& err_msg, const char separator); 139 template<typename T> inline static bool load_csv_ascii (SpMat< std::complex<T> >& x, std::istream& f, std::string& err_msg, const char separator); 140 template<typename eT> inline static bool load_coord_ascii(SpMat<eT>& x, std::istream& f, std::string& err_msg); 141 template<typename T> inline static bool load_coord_ascii(SpMat< std::complex<T> >& x, std::istream& f, std::string& err_msg); 142 template<typename eT> inline static bool load_arma_binary(SpMat<eT>& x, std::istream& f, std::string& err_msg); 143 144 145 146 // 147 // cube saving 148 149 template<typename eT> inline static bool save_raw_ascii (const Cube<eT>& x, const std::string& name); 150 template<typename eT> inline static bool save_raw_binary (const Cube<eT>& x, const std::string& name); 151 template<typename eT> inline static bool save_arma_ascii (const Cube<eT>& x, const std::string& name); 152 template<typename eT> inline static bool save_arma_binary(const Cube<eT>& x, const std::string& name); 153 template<typename eT> inline static bool save_hdf5_binary(const Cube<eT>& x, const hdf5_name& spec, std::string& err_msg); 154 155 template<typename eT> inline static bool save_raw_ascii (const Cube<eT>& x, std::ostream& f); 156 template<typename eT> inline static bool save_raw_binary (const Cube<eT>& x, std::ostream& f); 157 template<typename eT> inline static bool save_arma_ascii (const Cube<eT>& x, std::ostream& f); 158 template<typename eT> inline static bool save_arma_binary(const Cube<eT>& x, std::ostream& f); 159 160 161 // 162 // cube loading 163 164 template<typename eT> inline static bool load_raw_ascii (Cube<eT>& x, const std::string& name, std::string& err_msg); 165 template<typename eT> inline static bool load_raw_binary (Cube<eT>& x, const std::string& name, std::string& err_msg); 166 template<typename eT> inline static bool load_arma_ascii (Cube<eT>& x, const std::string& name, std::string& err_msg); 167 template<typename eT> inline static bool load_arma_binary(Cube<eT>& x, const std::string& name, std::string& err_msg); 168 template<typename eT> inline static bool load_hdf5_binary(Cube<eT>& x, const hdf5_name& spec, std::string& err_msg); 169 template<typename eT> inline static bool load_auto_detect(Cube<eT>& x, const std::string& name, std::string& err_msg); 170 171 template<typename eT> inline static bool load_raw_ascii (Cube<eT>& x, std::istream& f, std::string& err_msg); 172 template<typename eT> inline static bool load_raw_binary (Cube<eT>& x, std::istream& f, std::string& err_msg); 173 template<typename eT> inline static bool load_arma_ascii (Cube<eT>& x, std::istream& f, std::string& err_msg); 174 template<typename eT> inline static bool load_arma_binary(Cube<eT>& x, std::istream& f, std::string& err_msg); 175 template<typename eT> inline static bool load_auto_detect(Cube<eT>& x, std::istream& f, std::string& err_msg); 176 177 178 // 179 // field saving and loading 180 181 template<typename T1> inline static bool save_arma_binary(const field<T1>& x, const std::string& name); 182 template<typename T1> inline static bool save_arma_binary(const field<T1>& x, std::ostream& f); 183 184 template<typename T1> inline static bool load_arma_binary( field<T1>& x, const std::string& name, std::string& err_msg); 185 template<typename T1> inline static bool load_arma_binary( field<T1>& x, std::istream& f, std::string& err_msg); 186 187 template<typename T1> inline static bool load_auto_detect( field<T1>& x, const std::string& name, std::string& err_msg); 188 template<typename T1> inline static bool load_auto_detect( field<T1>& x, std::istream& f, std::string& err_msg); 189 190 inline static bool save_std_string(const field<std::string>& x, const std::string& name); 191 inline static bool save_std_string(const field<std::string>& x, std::ostream& f); 192 193 inline static bool load_std_string( field<std::string>& x, const std::string& name, std::string& err_msg); 194 inline static bool load_std_string( field<std::string>& x, std::istream& f, std::string& err_msg); 195 196 197 198 // 199 // handling of PPM images by cubes 200 201 template<typename T1> inline static bool save_ppm_binary(const Cube<T1>& x, const std::string& final_name); 202 template<typename T1> inline static bool save_ppm_binary(const Cube<T1>& x, std::ostream& f); 203 204 template<typename T1> inline static bool load_ppm_binary( Cube<T1>& x, const std::string& final_name, std::string& err_msg); 205 template<typename T1> inline static bool load_ppm_binary( Cube<T1>& x, std::istream& f, std::string& err_msg); 206 207 208 // 209 // handling of PPM images by fields 210 211 template<typename T1> inline static bool save_ppm_binary(const field<T1>& x, const std::string& final_name); 212 template<typename T1> inline static bool save_ppm_binary(const field<T1>& x, std::ostream& f); 213 214 template<typename T1> inline static bool load_ppm_binary( field<T1>& x, const std::string& final_name, std::string& err_msg); 215 template<typename T1> inline static bool load_ppm_binary( field<T1>& x, std::istream& f, std::string& err_msg); 216 217 218 219 }; 220 221 222 223 //! @} 224