source: rrlib_util/sFileIOUtils.h @ 91:f04d6cdf4e0a

Last change on this file since 91:f04d6cdf4e0a was 91:f04d6cdf4e0a, checked in by Jens Wettach <wettach@…>, 6 years ago

generalized container input/output

File size: 13.7 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
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 2
10// of the License, or (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
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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/sStringUtils.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
53extern "C"
54{
55#include <arpa/inet.h> // inet_ntoa
56}
57
58//----------------------------------------------------------------------
59// Namespace declaration
60//----------------------------------------------------------------------
61namespace rrlib
62{
63namespace util
64{
65
66class sFileIOUtils
67{
68public:
69  /*!
70   * \brief Returns all files in a given directory
71   *
72   * \return Returns whether or not the read was possible
73   */
74  static bool GetAllFilesInDirectory(std::string dir, std::vector<std::string> &files);
75
76  /*!
77   * \brief Returns the current directory
78   *
79   * \return Returns a string containing the current directory
80   */
81  static std::string GetCurrentDirectory();
82  /*!
83   * \brief Setsthe current directory
84   *
85   * Changes the current directory to \param dir .
86   * \return returns an error code similar to the chdir C command
87   */
88  static int ChangeCurrentDirectory(const std::string& dir);
89
90  /*!
91   * \brief Creates a new directory at the path provided as parameter.
92   *
93   * \param path Complete relative or absolute path to be created.
94   * \return returns false if directory creation fails, true otherwise.
95   * The existance of the folder is not regarded as a failure here!
96   */
97  static bool CreateDirectory(const std::string& path); // throw(runtime_error);
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
106  /*!
107   * \brief Removes file with given name.
108   */
109  static void DeleteFile(const std::string& filename);
110
111  /*!
112   * \brief Calls "gzip" on input file and writes result to output file.
113   *
114   * \note output file gets suffix ".gz" automatically if not yet the case.
115   */
116  static void CompressFile(const std::string& input_filename, std::string& output_filename);
117
118  /*!
119   * \brief Calls "gunzip" on input file and writes result to output file.
120   *
121   * \note input file must have suffix ".gz".
122   */
123  static void DecompressFile(const std::string& input_filename, const std::string& output_filename);
124
125
126  /*!
127   * \brief Test if file exists for given file name
128   *
129   * \returns true if file exists
130   */
131  static bool FileExists(const std::string &filename);
132
133
134  /*!
135   * \brief Writes content of given container to file with given name
136   *
137   * \returns true iff file could be processed as expected
138   *
139   * \note If filename has suffix ".bz2" it is compressed accordingly.
140   */
141  template <class ConstForwardIterator>
142  static bool WriteContainerToFile(ConstForwardIterator begin, ConstForwardIterator end, const std::string& filename)
143  {
144    std::ofstream output_file_stream;
145    RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got file <", filename, ">");
146    if (sStringUtils::EndsWith(filename, ".bz2"))
147    {
148      RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "compressing");
149      boost::iostreams::filtering_ostream out;
150      out.push(boost::iostreams::bzip2_compressor());
151      output_file_stream.open(filename.c_str());
152      if (output_file_stream)
153      {
154        out.push(output_file_stream);
155        std::copy(begin, end, std::ostream_iterator<typename ConstForwardIterator::value_type>(out));
156        return true;
157      }
158    }
159    else
160    {
161      output_file_stream.open(filename.c_str());
162      if (output_file_stream)
163      {
164        std::copy(begin, end, std::ostream_iterator<typename ConstForwardIterator::value_type>(output_file_stream));
165        output_file_stream.close();
166        return true;
167      }
168    }
169    return false;
170  }
171
172  template <class Container>
173  static bool WriteContainerToFile(const Container &container, const std::string& filename)
174  {
175    return WriteContainerToFile(container.begin(), container.end(), filename);
176  }
177
178  /*!
179   * \brief Reads file with given name and writes its content to given container
180   *
181   * \returns true iff file could be processed as expected
182   *
183   * \note If filename has suffix ".bz2" it is decompressed accordingly before reading.
184   */
185  template <class Container>
186  static bool ReadContainerFromFile(Container &container, const std::string& filename)
187  {
188    std::ifstream input_file_stream;
189    RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got file <", filename, ">");
190    if (sStringUtils::EndsWith(filename, ".bz2"))
191    {
192      RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "decompressing");
193      boost::iostreams::filtering_istream in;
194      in.push(boost::iostreams::bzip2_decompressor());
195      input_file_stream.open(filename.c_str());
196      if (input_file_stream)
197      {
198        in.push(input_file_stream);
199        std::copy(std::istream_iterator<typename Container::value_type>(in), std::istream_iterator<typename Container::value_type>(), std::back_inserter(container));
200        RRLIB_LOG_PRINT(ERROR, "got ", container.size(), " container elements from file ", filename);
201        return true;
202      }
203    }
204    else
205    {
206      input_file_stream.open(filename.c_str());
207      if (input_file_stream)
208      {
209        std::copy(std::istream_iterator<typename Container::value_type>(input_file_stream), std::istream_iterator<typename Container::value_type>(), std::back_inserter(container));
210        RRLIB_LOG_PRINT(ERROR, "got ", container.size(), " container elements from file ", filename);
211      }
212      input_file_stream.close();
213      return true;
214    }
215    return false;
216  }
217
218
219
220
221  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
222   *
223   * \param file_name   file name to be expanded (will contain the result afterwards)
224   *
225   * \returns Whether the method could be executed without failure or not
226   */
227  static bool ShellExpandFilename(std::string &file_name) __attribute__((__warn_unused_result__));
228
229  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
230  *
231  * Thus variables as e.g. $MCAPROJECTHOME can be used for specifying OIV files in scene description files.
232  *
233  * \param result      reference to std::string object that should contain the result afterwards
234  * \param file_name   file name to be expanded
235  *
236  * \returns Whether the method could be executed without failure or not
237  */
238  static bool ShellExpandFilename(std::string &result, const std::string& file_name) __attribute__((__warn_unused_result__));
239
240
241  /*!
242   *    Splits the given filename into directory, single file name and file extension
243   *    \param complete_name complete file name (input)
244   *    \param file_dir directory (output)
245   *    \param file_base single file name (output)
246   *    \param file_ext file extension (output)
247   */
248  static void SplitFullQualifiedFilename(const std::string& complete_name, std::string& file_dir, std::string& file_base, std::string& file_ext);
249
250
251  /*!
252   * Retrieves network host name via "hostname" and returns result.
253   * \param fqdn whether FQDN  (Fully  Qualified Domain Name) or short name shall be retrieved
254   */
255  static std::string GetHostName(bool fqdn = true);
256
257
258  /*!
259   * Retrieves ip address of host with given network name via "gethostbyname" and returns result.
260   */
261  static struct in_addr HostToIpViaGetHostByName(const std::string& name);
262
263  /*!
264   * Retrieves ip address of host with given network name via "nslookup" and returns result.
265   */
266  static struct in_addr HostToIpViaNslookup(const std::string& name);
267
268  /*!
269   * Retrieves ip address of host with given network name via "host" and returns result.
270   */
271  static struct in_addr HostToIpViaHost(const std::string& name);
272
273  /*!
274   * Retrieves ip address of host with given network name from "/etc/hosts" and returns result.
275   */
276  static struct in_addr HostToIpViaHostsFile(const std::string& name);
277
278  /*!
279   * Retrieves ip address of host with given network name using
280   * 1. \sa HostToIpViaHostsFile()
281   * 2. \sa HostToIpViaHostx()
282   * 2. \sa HostToIpViaNslookup()
283   */
284  static struct in_addr HostToIp(const std::string& name);
285
286  /*!
287   * Fetches file with given source_file_name from server (either addressed by network name or ip address) at given source_directory
288   * and puts it to the given target_directory (via <system( rsync -av ...)> ).
289   *
290   * \params source_host_name network name of server
291   * \params source_host_ip_address ip address name of server (used alternatively to its name)
292   * \params source_directory directory on server
293   * \params source_file_name file to fetch from server
294   * \params target_directory local directory for fetched file
295   * \params optional_rsync_flags optional flags for rsync command, appended to -av
296   *
297   * \returns result of system call
298   */
299  static int RSyncFile(const std::string& source_host_name, const std::string& source_host_ip_address, const std::string& source_directory, const std::string& source_file_name, const std::string& target_directory, const std::string &optional_rsync_flags = "");
300
301
302  /*!
303   * Fetches files with given source_file_names from server (either addressed by network name or ip address) at given source_directory
304   * and puts it to the given target_directory (via <system( rsync -avr --files-from ...)> ). Uses temp file on local host to
305   * compose list with files to fetch.
306   *
307   * \params source_host_name network name of server
308   * \params source_host_ip_address ip address name of server (used alternatively to its name)
309   * \params source_directory directory on server
310   * \params source_file_names files to fetch from server
311   * \params target_directory local directory for fetched file
312   * \params optional_rsync_flags optional flags for rsync command, appended to -avr
313   *
314   * \returns result of system call
315   */
316  static int RSyncFiles(const std::string& source_host_name, const std::string& source_host_ip_address, const std::string& source_directory, const std::vector<std::string>& source_file_names, const std::string& target_directory, const std::string &optional_rsync_flags = "");
317
318
319  /*!
320   * Checks whether given file is available on local host or can be rsync'ed from resource server and returns full qualified file_name on local host.
321   * The following checks are performed (with decreasing priority):
322   * 1. check location of file: "./ + resource_repository + filename"
323   * 2. check location of file: "local_resource_directory + resource_repository + filename"
324   * 3. check location of file: "server_resource_directory + resource_repository + filename" --> rsync to "local_resource_directory + resource_repository + file_name"
325   * As soon as one check succeeds the remaining checks are not executed.
326   *
327   * \param file_name name of file to search and fetch (may contain relative path)
328   * \param full_local_file_name full qualified name of file on local host after successful check/fetch operation (result)
329   * \param resource_repository relative path to be used as resource repository (on local host and on server, may be empty)
330   * \param resource_server network name of resource server (optional)
331   * \param local_resource_directory absolute path to resource_repository on local host (may be empty)
332   * \param server_resource_directory absolute path to resource_repository on server (may be empty)
333   * \param use_cache whether to take local host name and server ip address from local static cache or not
334   *
335   * \returns success of check/fetch operation
336   */
337  static bool CheckAndGetFile(const std::string &file_name, std::string &full_local_file_name, const std::string& resource_repository = "", const std::string& resource_server = "", const std::string& local_resource_directory = "", const std::string& server_resource_directory = "", bool use_cache = false);
338
339  /*!
340   * Clear cached local host name and server ip addresses
341   * \sa CheckAndGetFile()
342   */
343  static void ClearCaches();
344
345  /*!
346   * Clear cached resource file names
347   * \sa CheckAndGetFile()
348   */
349  static void ClearResourceCache();
350
351private:
352  static std::string cached_local_host; //!< local static variable to store name of local host
353  static std::map<std::string, std::string> host_name_to_ip_cache; //!< local static variable to store server ip addresses
354  static std::map<std::string, std::string> resource_cache; //!< local static variable to store names of already loaded resource files
355};
356//----------------------------------------------------------------------
357// End of namespace declaration
358//----------------------------------------------------------------------
359}; // namespace util
360}; // namespace rrlib
361#endif
Note: See TracBrowser for help on using the repository browser.