source: rrlib_util/sFileIOUtils.h @ 142:9fdf72ae3cdd

Last change on this file since 142:9fdf72ae3cdd was 142:9fdf72ae3cdd, checked in by Jens Wettach <wettach@…>, 4 years ago

reorganized sFileIOUtils (now only functions from rrlib/util/fileio.h should be used); added unit tests for fileio

File size: 10.5 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/util/sFileIOUtils.h
23 *
24 * \author  Bernd Helge Schaefer
25 * \author  Jens Wettach
26 *
27 * \date    2005-11-15
28 *
29 * \brief   Contains static class sFileIOUtils
30 *
31 */
32//----------------------------------------------------------------------
33// Includes
34//----------------------------------------------------------------------
35#ifndef _rrlib_util_sFileIOUtils_h_
36#define _rrlib_util_sFileIOUtils_h_
37
38//rrlib includes
39#include "rrlib/util/string.h"
40#include "rrlib/logging/messages.h"
41
42//STL includes
43#include <string>
44#include <vector>
45#include <map>
46#include <fstream>
47#include <iterator>
48
49//boost includes
50#include <boost/iostreams/filtering_stream.hpp>
51#include <boost/iostreams/filter/bzip2.hpp>
52
53//----------------------------------------------------------------------
54// Namespace declaration
55//----------------------------------------------------------------------
56namespace rrlib
57{
58namespace util
59{
60
61class sFileIOUtils
62{
63public:
64  /*!
65   * \brief Returns all files in a given directory
66   *
67   * \return Returns whether or not the read was possible
68   */
69  static bool GetAllFilesInDirectory(std::string dir, std::vector<std::string> &files)
70  __attribute__((deprecated("USE rrlib::util::fileio::GetAllFilesInDirectory()")));
71
72  /*!
73   * \brief Returns the current directory
74   *
75   * \return Returns a string containing the current directory
76   */
77  static std::string GetCurrentDirectory()
78  __attribute__((deprecated("USE rrlib::util::fileio::GetCurrentDirectory()")));
79
80  /*!
81   * \brief Setsthe current directory
82   *
83   * Changes the current directory to \param dir .
84   * \return returns an error code similar to the chdir C command
85   */
86  static bool ChangeCurrentDirectory(const std::string& dir)
87  __attribute__((deprecated("USE rrlib::util::fileio::ChangeCurrentDirectory()")));
88
89  /*!
90   * \brief Creates a new directory at the path provided as parameter.
91   *
92   * \param path Complete relative or absolute path to be created.
93   * \return returns false if directory creation fails, true otherwise.
94   * The existance of the folder is not regarded as a failure here!
95   */
96  static bool CreateDirectory(const std::string& path)
97  __attribute__((deprecated("USE rrlib::util::fileio::CreateDirectory()")));
98
99  /*!
100   * \brief Creates a new temporary file via "mktemp".
101   *
102   * \return the new temp file
103   */
104  static std::string CreateTempfile()
105  __attribute__((deprecated("USE rrlib::util::fileio::CreateTempFile()")));
106
107  /*!
108   * \brief Removes file with given name.
109   */
110  static void DeleteFile(const std::string& filename)
111  __attribute__((deprecated("USE rrlib::util::fileio::DeleteFile()")));
112
113  /*!
114   * \brief Test if file exists for given file name
115   *
116   * \returns true if file exists
117   */
118  static bool FileExists(const std::string &filename)
119  __attribute__((deprecated("USE rrlib::util::fileio::FileExists()")));
120
121  /*!
122   * \brief Counts # lines in given file
123   *
124   * \returns # lines
125   */
126  static size_t CountLineNumbers(const std::string &filename)
127  __attribute__((deprecated("USE rrlib::util::fileio::CountLineNumbers()")));
128
129
130  /*!
131   * \brief Writes content of given container to file with given name
132   *
133   * \returns true iff file could be processed as expected
134   *
135   * \note If filename has suffix ".bz2" it is compressed accordingly.
136   */
137  template <class ConstForwardIterator>
138  static bool WriteContainerToFile(ConstForwardIterator begin, ConstForwardIterator end, const std::string& filename)
139  __attribute__((deprecated("USE rrlib::serialization instead")));
140
141
142  template <class Container>
143  static bool WriteContainerToFile(const Container &container, const std::string& filename)
144  __attribute__((deprecated("USE rrlib::serialization instead")));
145
146
147  /*!
148   * \brief Reads file with given name and writes its content to given container
149   *
150   * \returns true iff file could be processed as expected
151   *
152   * \note If filename has suffix ".bz2" it is decompressed accordingly before reading.
153   */
154  template <class Container>
155  static bool ReadContainerFromFile(Container &container, const std::string& filename)
156  __attribute__((deprecated("USE rrlib::serialization instead")));
157
158
159  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
160   *
161   * \param file_name   file name to be expanded (will contain the result afterwards)
162   *
163   * \returns Whether the method could be executed without failure or not
164   */
165  static bool ShellExpandFilename(std::string &file_name)
166  __attribute__((__warn_unused_result__))
167  __attribute__((deprecated("USE rrlib::util::fileio::ShellExpandFilename()")));
168
169  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
170   *
171   * \param file_name   file name to be expanded
172   *
173   * \returns Expanded file name
174   */
175  static std::string ShellExpandFilename(const std::string &file_name)
176  __attribute__((__warn_unused_result__))
177  __attribute__((deprecated("USE rrlib::util::fileio::ShellExpandFilename()")));
178
179  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
180  *
181  * Thus variables as e.g. $MCAPROJECTHOME can be used for specifying OIV files in scene description files.
182  *
183  * \param result      reference to std::string object that should contain the result afterwards
184  * \param file_name   file name to be expanded
185  *
186  * \returns Whether the method could be executed without failure or not
187  */
188  static bool ShellExpandFilename(std::string &result, const std::string& file_name)
189  __attribute__((__warn_unused_result__))
190  __attribute__((deprecated("USE rrlib::util::fileio::ShellExpandFilename()")));
191
192  /*!
193   *    Splits the given filename into directory, single file name and file extension
194   *    \param complete_name complete file name (input)
195   *    \param file_dir directory (output)
196   *    \param file_base single file name (output)
197   *    \param file_ext file extension (output)
198   */
199  static void SplitFullQualifiedFilename(const std::string& complete_name, std::string& file_dir, std::string& file_base, std::string& file_ext)
200  __attribute__((deprecated("USE rrlib::util::fileio::SplitFullQualifiedFilename()")));
201
202};
203
204
205//======================================================================
206// Implementation of template functions
207//======================================================================
208
209//----------------------------------------------------------------------
210// class sFileIOUtils::WriteContainerToFile()
211//----------------------------------------------------------------------
212template <class ConstForwardIterator>
213bool sFileIOUtils::WriteContainerToFile(ConstForwardIterator begin, ConstForwardIterator end, const std::string& filename)
214{
215  std::ofstream output_file_stream;
216  RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got file <", filename, ">");
217  if (util::EndsWith(filename, ".bz2"))
218  {
219    RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "compressing");
220    boost::iostreams::filtering_ostream out;
221    out.push(boost::iostreams::bzip2_compressor());
222    output_file_stream.open(filename.c_str());
223    if (output_file_stream)
224    {
225      out.push(output_file_stream);
226      std::copy(begin, end, std::ostream_iterator<typename ConstForwardIterator::value_type>(out));
227      return true;
228    }
229  }
230  else
231  {
232    output_file_stream.open(filename.c_str());
233    if (output_file_stream)
234    {
235      std::copy(begin, end, std::ostream_iterator<typename ConstForwardIterator::value_type>(output_file_stream));
236      output_file_stream.close();
237      return true;
238    }
239  }
240  return false;
241};
242
243//----------------------------------------------------------------------
244// class sFileIOUtils::WriteContainerToFile()
245//----------------------------------------------------------------------
246template <class Container>
247bool sFileIOUtils::WriteContainerToFile(const Container &container, const std::string& filename)
248{
249  return WriteContainerToFile(container.begin(), container.end(), filename);
250};
251
252//----------------------------------------------------------------------
253// class sFileIOUtils::ReadContainerFromFile()
254//----------------------------------------------------------------------
255template <class Container>
256bool sFileIOUtils::ReadContainerFromFile(Container &container, const std::string& filename)
257{
258  std::ifstream input_file_stream;
259  RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got file <", filename, ">");
260  if (util::EndsWith(filename, ".bz2"))
261  {
262    RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "decompressing");
263    boost::iostreams::filtering_istream in;
264    in.push(boost::iostreams::bzip2_decompressor());
265    input_file_stream.open(filename.c_str());
266    if (input_file_stream)
267    {
268      in.push(input_file_stream);
269      std::copy(std::istream_iterator<typename Container::value_type>(in), std::istream_iterator<typename Container::value_type>(), std::back_inserter(container));
270      RRLIB_LOG_PRINT(ERROR, "got ", container.size(), " container elements from file ", filename);
271      return true;
272    }
273  }
274  else
275  {
276    input_file_stream.open(filename.c_str());
277    if (input_file_stream)
278    {
279      std::copy(std::istream_iterator<typename Container::value_type>(input_file_stream), std::istream_iterator<typename Container::value_type>(), std::back_inserter(container));
280      RRLIB_LOG_PRINT(ERROR, "got ", container.size(), " container elements from file ", filename);
281    }
282    input_file_stream.close();
283    return true;
284  }
285  return false;
286};
287
288
289//----------------------------------------------------------------------
290// End of namespace declaration
291//----------------------------------------------------------------------
292}; // namespace util
293}; // namespace rrlib
294#endif
Note: See TracBrowser for help on using the repository browser.