MRPT  2.0.4
vector_loadsave.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <string>
12 #include <vector>
13 
14 namespace mrpt::io
15 {
16 // clang-format off
17 /** @defgroup vector_loadsave Load and save vectors to files (in #include <mrpt/io/vector_loadsave.h>)
18  * \ingroup mrpt_io_grp
19  * @{ */
20 // clang-format on
21 
22 /** Saves a vector directly as a binary dump to a file:
23  * \return Returns false on any error, true on everything OK.
24  * \sa loadBinaryFile
25  */
27  const std::vector<uint8_t>& vec, const std::string& fileName);
28 
29 /** Loads a entire file as a vector of bytes.
30  * \return Returns false on any error, true on everything OK.
31  * \sa vectorToBinaryFile
32  */
33 bool loadBinaryFile(
34  std::vector<uint8_t>& out_data, const std::string& fileName);
35 
36 /** Loads a text file as a vector of string lines.
37  * \return Returns false on any error, true on everything OK.
38  * \sa file_get_contents()
39  */
40 bool loadTextFile(std::vector<std::string>& o, const std::string& fileName);
41 
42 /** Loads an entire text file and return its contents as a single std::string.
43  * \exception std::runtime_error On any read error.
44  * \sa loadBinaryFile(), loadTextFile()
45  * \note Relying on C++17 RVO to return a string without worring on
46  * return-by-value of big objects.
47  */
48 std::string file_get_contents(const std::string& fileName);
49 
50 /** A useful function for debugging, which saves a numeric std::vector as a
51  * plain-text file compatible with MATLAB.
52  * \return Returns false on any error, true on everything OK.
53  */
54 bool vectorToTextFile(
55  const std::vector<float>& vec, const std::string& fileName,
56  bool append = false, bool byRows = false);
57 //! \overload
58 bool vectorToTextFile(
59  const std::vector<double>& vec, const std::string& fileName,
60  bool append = false, bool byRows = false);
61 //! \overload
62 bool vectorToTextFile(
63  const std::vector<int>& vec, const std::string& fileName,
64  bool append = false, bool byRows = false);
65 //! \overload
66 bool vectorToTextFile(
67  const std::vector<size_t>& vec, const std::string& fileName,
68  bool append = false, bool byRows = false);
69 //! \overload
70 template <class EIGEN_MATRIX>
71 bool vectorToTextFile(const EIGEN_MATRIX& vec, const std::string& fileName)
72 {
73  try
74  {
75  vec.saveToTextFile(fileName);
76  return true;
77  }
78  catch (...)
79  {
80  return false;
81  }
82 }
83 
84 /** Load a numeric std::vector<double> from a text file (compat. with MATLAB)
85  * \return Returns false on any error, true on everything OK.
86  * \sa loadBinaryFile
87  */
89  std::vector<double>& vec, const std::string& fileName,
90  const bool byRows = false);
91 
92 /** @} */
93 
94 } // namespace mrpt::io
bool vectorToBinaryFile(const std::vector< uint8_t > &vec, const std::string &fileName)
Saves a vector directly as a binary dump to a file:
bool loadBinaryFile(std::vector< uint8_t > &out_data, const std::string &fileName)
Loads a entire file as a vector of bytes.
bool loadTextFile(std::vector< std::string > &o, const std::string &fileName)
Loads a text file as a vector of string lines.
std::string file_get_contents(const std::string &fileName)
Loads an entire text file and return its contents as a single std::string.
bool vectorToTextFile(const std::vector< float > &vec, const std::string &fileName, bool append=false, bool byRows=false)
A useful function for debugging, which saves a numeric std::vector as a plain-text file compatible wi...
bool vectorNumericFromTextFile(std::vector< double > &vec, const std::string &fileName, const bool byRows=false)
Load a numeric std::vector<double> from a text file (compat.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020