1 // This is core/vsl/vsl_quick_file.h 2 #ifndef vsl_quick_file_h_ 3 #define vsl_quick_file_h_ 4 //: 5 // \file 6 // \brief Functions for quickly loading and saving binary files. 7 // \author Ian Scott 8 // 9 // All the functions return true if successful. 10 // The functions will also output a success or failure message to stderr by 11 // default, although you may substitute any std::ostream, or \c (std::ostream*)0 to avoid the 12 // message. 13 // 14 // For these templated functions to work, the object must have vsl_b_read and 15 // vsl_b_write functions defined for them 16 17 #include <string> 18 #include <iostream> 19 #include <cerrno> 20 #ifdef _MSC_VER 21 # include <vcl_msvc_warnings.h> 22 #endif 23 #include "vsl_binary_io.h" 24 25 26 //: Load something from a file 27 template <class T> 28 inline bool vsl_quick_file_load(T &data, 29 const std::string& path, 30 std::ostream* errorStream = &std::cerr) 31 { 32 vsl_b_ifstream bfs(path); 33 if ( !(!bfs)) 34 { 35 vsl_b_read(bfs,data); 36 if (!(!bfs)) 37 { 38 // Check that we have reached the end of the file. 39 char dummy; 40 vsl_b_read(bfs,dummy); 41 if (bfs.is().eof()) 42 { 43 bfs.close(); 44 if (errorStream) 45 *errorStream << "Successfully loaded: " << path << '\n'; 46 return true; 47 } 48 } 49 } 50 bfs.close(); 51 if (errorStream) 52 *errorStream << "Unable to load: "<< path <<'\n'; 53 return false; 54 } 55 56 57 //: Save something to a file 58 template <class T> 59 inline bool vsl_quick_file_save(const T &data, 60 const std::string& path, 61 std::ostream* errorStream = &std::cerr) 62 { 63 vsl_b_ofstream bfs(path); 64 if (!(!bfs)) 65 { 66 vsl_b_write(bfs,data); 67 if (!(!bfs)) 68 { 69 bfs.close(); 70 if (errorStream) 71 *errorStream << "Successfully saved: "<< path <<'\n'; 72 return true; 73 } 74 } 75 bfs.close(); 76 if (errorStream) 77 *errorStream << "Unable to save: " << path << '\n'; 78 return false; 79 } 80 81 // Load two objects from a file 82 template <class T, class S> 83 inline bool vsl_quick_file_load(T &data1, 84 S &data2, 85 const std::string& path, 86 std::ostream* errorStream = &std::cerr) 87 { 88 vsl_b_ifstream bfs(path); 89 int reason = errno; 90 if ( !(!bfs)) 91 { 92 vsl_b_read(bfs,data1); 93 vsl_b_read(bfs,data2); 94 if (!(!bfs)) 95 { 96 // Check that we have reached the end of the file. 97 char dummy; 98 vsl_b_read(bfs,dummy); 99 if (bfs.is().eof()) 100 { 101 bfs.close(); 102 if (errorStream) 103 *errorStream << "Successfully loaded: " << path << '\n'; 104 return true; 105 } 106 } 107 } 108 reason = errno; 109 bfs.close(); 110 if (errorStream) 111 *errorStream << "Unable to load: "<< path <<'\n'; 112 return false; 113 } 114 115 // Save two objects to a file 116 template <class T, class S> 117 inline bool vsl_quick_file_save(const T &data1, 118 const S &data2, 119 const std::string& path, 120 std::ostream* errorStream = &std::cerr) 121 { 122 vsl_b_ofstream bfs(path); 123 if (!(!bfs)) 124 { 125 vsl_b_write(bfs,data1); 126 vsl_b_write(bfs,data2); 127 if (!(!bfs)) 128 { 129 bfs.close(); 130 if (errorStream) 131 *errorStream << "Successfully saved: "<< path <<'\n'; 132 return true; 133 } 134 } 135 bfs.close(); 136 if (errorStream) 137 *errorStream << "Unable to save: " << path << '\n'; 138 return false; 139 } 140 141 // Load three objects from a file 142 template <class T, class S, class U> 143 inline bool vsl_quick_file_load(T &data1, 144 S &data2, U &data3, 145 const std::string& path, 146 std::ostream* errorStream = &std::cerr) 147 { 148 vsl_b_ifstream bfs(path); 149 if ( !(!bfs)) 150 { 151 vsl_b_read(bfs,data1); 152 vsl_b_read(bfs,data2); 153 vsl_b_read(bfs,data3); 154 if (!(!bfs)) 155 { 156 // Check that we have reached the end of the file. 157 char dummy; 158 vsl_b_read(bfs,dummy); 159 if (bfs.is().eof()) 160 { 161 bfs.close(); 162 if (errorStream) 163 *errorStream << "Successfully loaded: " << path << '\n'; 164 return true; 165 } 166 } 167 } 168 bfs.close(); 169 if (errorStream) 170 *errorStream << "Unable to load: "<< path <<'\n'; 171 return false; 172 } 173 174 // Save three objects to a file 175 template <class T, class S, class U> 176 inline bool vsl_quick_file_save(const T &data1, 177 const S &data2, const U &data3, 178 const std::string& path, 179 std::ostream* errorStream = &std::cerr) 180 { 181 vsl_b_ofstream bfs(path); 182 if (!(!bfs)) 183 { 184 vsl_b_write(bfs,data1); 185 vsl_b_write(bfs,data2); 186 vsl_b_write(bfs,data3); 187 if (!(!bfs)) 188 { 189 bfs.close(); 190 if (errorStream) 191 *errorStream << "Successfully saved: "<< path <<'\n'; 192 return true; 193 } 194 } 195 bfs.close(); 196 if (errorStream) 197 *errorStream << "Unable to save: " << path << '\n'; 198 return false; 199 } 200 201 // Load four objects from a file 202 template <class T, class S, class U, class V> 203 inline bool vsl_quick_file_load(T &data1, 204 S &data2, U &data3, V &data4, 205 const std::string& path, 206 std::ostream* errorStream = &std::cerr) 207 { 208 vsl_b_ifstream bfs(path); 209 if ( !(!bfs)) 210 { 211 vsl_b_read(bfs,data1); 212 vsl_b_read(bfs,data2); 213 vsl_b_read(bfs,data3); 214 vsl_b_read(bfs,data4); 215 if (!(!bfs)) 216 { 217 // Check that we have reached the end of the file. 218 char dummy; 219 vsl_b_read(bfs,dummy); 220 if (bfs.is().eof()) 221 { 222 bfs.close(); 223 if (errorStream) 224 *errorStream << "Successfully loaded: " << path << '\n'; 225 return true; 226 } 227 } 228 } 229 bfs.close(); 230 if (errorStream) 231 *errorStream << "Unable to load: "<< path <<'\n'; 232 return false; 233 } 234 235 // Save four objects to a file 236 template <class T, class S, class U, class V> 237 inline bool vsl_quick_file_save(const T &data1, const S &data2, 238 const U &data3, const V &data4, 239 const std::string& path, 240 std::ostream* errorStream = &std::cerr) 241 { 242 vsl_b_ofstream bfs(path); 243 if (!(!bfs)) 244 { 245 vsl_b_write(bfs,data1); 246 vsl_b_write(bfs,data2); 247 vsl_b_write(bfs,data3); 248 vsl_b_write(bfs,data4); 249 if (!(!bfs)) 250 { 251 bfs.close(); 252 if (errorStream) 253 *errorStream << "Successfully saved: "<< path <<'\n'; 254 return true; 255 } 256 } 257 bfs.close(); 258 if (errorStream) 259 *errorStream << "Unable to save: " << path << '\n'; 260 return false; 261 } 262 263 // Load five objects from a file 264 template <class T, class S, class U, class V, class W> 265 inline bool vsl_quick_file_load(T &data1, 266 S &data2, U &data3, V &data4, W &data5, 267 const std::string& path, 268 std::ostream* errorStream = &std::cerr) 269 { 270 vsl_b_ifstream bfs(path); 271 if ( !(!bfs)) 272 { 273 vsl_b_read(bfs,data1); 274 vsl_b_read(bfs,data2); 275 vsl_b_read(bfs,data3); 276 vsl_b_read(bfs,data4); 277 vsl_b_read(bfs,data5); 278 if (!(!bfs)) 279 { 280 // Check that we have reached the end of the file. 281 char dummy; 282 vsl_b_read(bfs,dummy); 283 if (bfs.is().eof()) 284 { 285 bfs.close(); 286 if (errorStream) 287 *errorStream << "Successfully loaded: " << path << '\n'; 288 return true; 289 } 290 } 291 } 292 bfs.close(); 293 if (errorStream) 294 *errorStream << "Unable to load: "<< path <<'\n'; 295 return false; 296 } 297 298 // Save five objects to a file 299 template <class T, class S, class U, class V, class W> 300 inline bool vsl_quick_file_save(const T &data1, const S &data2, const U &data3, 301 const V &data4, const W &data5, 302 const std::string& path, 303 std::ostream* errorStream = &std::cerr) 304 { 305 vsl_b_ofstream bfs(path); 306 if (!(!bfs)) 307 { 308 vsl_b_write(bfs,data1); 309 vsl_b_write(bfs,data2); 310 vsl_b_write(bfs,data3); 311 vsl_b_write(bfs,data4); 312 vsl_b_write(bfs,data5); 313 if (!(!bfs)) 314 { 315 bfs.close(); 316 if (errorStream) 317 *errorStream << "Successfully saved: "<< path <<'\n'; 318 return true; 319 } 320 } 321 bfs.close(); 322 if (errorStream) 323 *errorStream << "Unable to save: " << path << '\n'; 324 return false; 325 } 326 327 328 // Load six objects from a file 329 template <class T, class S, class U, class V, class W, class X> 330 inline bool vsl_quick_file_load(T &data1, S &data2, U &data3, 331 V &data4, W &data5, X &data6, 332 const std::string& path, 333 std::ostream* errorStream = &std::cerr) 334 { 335 vsl_b_ifstream bfs(path); 336 if ( !(!bfs)) 337 { 338 vsl_b_read(bfs,data1); 339 vsl_b_read(bfs,data2); 340 vsl_b_read(bfs,data3); 341 vsl_b_read(bfs,data4); 342 vsl_b_read(bfs,data5); 343 vsl_b_read(bfs,data6); 344 if (!(!bfs)) 345 { 346 // Check that we have reached the end of the file. 347 char dummy; 348 vsl_b_read(bfs,dummy); 349 if (bfs.is().eof()) 350 { 351 bfs.close(); 352 if (errorStream) 353 *errorStream << "Successfully loaded: " << path << '\n'; 354 return true; 355 } 356 } 357 } 358 bfs.close(); 359 if (errorStream) 360 *errorStream << "Unable to load: "<< path <<'\n'; 361 return false; 362 } 363 364 // Save six objects to a file 365 template <class T, class S, class U, class V, class W, class X> 366 inline bool vsl_quick_file_save(const T &data1, const S &data2, const U &data3, 367 const V &data4, const W &data5, const X &data6, 368 const std::string& path, 369 std::ostream* errorStream = &std::cerr) 370 { 371 vsl_b_ofstream bfs(path); 372 if (!(!bfs)) 373 { 374 vsl_b_write(bfs,data1); 375 vsl_b_write(bfs,data2); 376 vsl_b_write(bfs,data3); 377 vsl_b_write(bfs,data4); 378 vsl_b_write(bfs,data5); 379 vsl_b_write(bfs,data6); 380 if (!(!bfs)) 381 { 382 bfs.close(); 383 if (errorStream) 384 *errorStream << "Successfully saved: "<< path <<'\n'; 385 return true; 386 } 387 } 388 bfs.close(); 389 if (errorStream) 390 *errorStream << "Unable to save: " << path << '\n'; 391 return false; 392 } 393 #endif // vsl_quick_file_h_ 394