source: rrlib_util/sFileIOUtils.h @ 84:f328970a6b47

Last change on this file since 84:f328970a6b47 was 84:f328970a6b47, checked in by Tobias Föhst <foehst@…>, 7 years ago

Merged with changes from RRLab

File size: 13.1 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 T>
142  static bool WriteContainerToFile(const std::vector<T> &content, 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(content.begin(), content.end(), std::ostream_iterator<T>(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(content.begin(), content.end(), std::ostream_iterator<T>(output_file_stream));
165        output_file_stream.close();
166        return true;
167      }
168    }
169    return false;
170  }
171
172  /*!
173   * \brief Reads file with given name and writes its content to given container
174   *
175   * \returns true iff file could be processed as expected
176   *
177   * \note If filename has suffix ".bz2" it is decompressed accordingly before reading.
178   */
179  template <class T>
180  static bool ReadContainerFromFile(std::vector<T> &content, const std::string& filename)
181  {
182    std::ifstream input_file_stream;
183    RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "got file <", filename, ">");
184    if (sStringUtils::EndsWith(filename, ".bz2"))
185    {
186      RRLIB_LOG_PRINT(DEBUG_VERBOSE_1, "decompressing");
187      boost::iostreams::filtering_istream in;
188      in.push(boost::iostreams::bzip2_decompressor());
189      input_file_stream.open(filename.c_str());
190      if (input_file_stream)
191      {
192        in.push(input_file_stream);
193        std::copy(std::istream_iterator<T>(in), std::istream_iterator<T>(), std::back_inserter(content));
194        return true;
195      }
196    }
197    else
198    {
199      input_file_stream.open(filename.c_str());
200      if (input_file_stream)
201      {
202        std::copy(std::istream_iterator<T>(input_file_stream), std::istream_iterator<T>(), std::back_inserter(content));
203      }
204      input_file_stream.close();
205      return true;
206    }
207    return false;
208  }
209
210
211
212
213  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
214   *
215   * \param file_name   file name to be expanded (will contain the result afterwards)
216   *
217   * \returns Whether the method could be executed without failure or not
218   */
219  static bool ShellExpandFilename(std::string &file_name) __attribute__((__warn_unused_result__));
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  * Thus variables as e.g. $MCAPROJECTHOME can be used for specifying OIV files in scene description files.
224  *
225  * \param result      reference to std::string object that should contain the result afterwards
226  * \param file_name   file name to be expanded
227  *
228  * \returns Whether the method could be executed without failure or not
229  */
230  static bool ShellExpandFilename(std::string &result, const std::string& file_name) __attribute__((__warn_unused_result__));
231
232
233  /*!
234   *    Splits the given filename into directory, single file name and file extension
235   *    \param complete_name complete file name (input)
236   *    \param file_dir directory (output)
237   *    \param file_base single file name (output)
238   *    \param file_ext file extension (output)
239   */
240  static void SplitFullQualifiedFilename(const std::string& complete_name, std::string& file_dir, std::string& file_base, std::string& file_ext);
241
242
243  /*!
244   * Retrieves network host name via "hostname" and returns result.
245   * \param fqdn whether FQDN  (Fully  Qualified Domain Name) or short name shall be retrieved
246   */
247  static std::string GetHostName(bool fqdn = true);
248
249
250  /*!
251   * Retrieves ip address of host with given network name via "gethostbyname" and returns result.
252   */
253  static struct in_addr HostToIpViaGetHostByName(const std::string& name);
254
255  /*!
256   * Retrieves ip address of host with given network name via "nslookup" and returns result.
257   */
258  static struct in_addr HostToIpViaNslookup(const std::string& name);
259
260  /*!
261   * Retrieves ip address of host with given network name via "host" and returns result.
262   */
263  static struct in_addr HostToIpViaHost(const std::string& name);
264
265  /*!
266   * Retrieves ip address of host with given network name from "/etc/hosts" and returns result.
267   */
268  static struct in_addr HostToIpViaHostsFile(const std::string& name);
269
270  /*!
271   * Retrieves ip address of host with given network name using
272   * 1. \sa HostToIpViaHostsFile()
273   * 2. \sa HostToIpViaHostx()
274   * 2. \sa HostToIpViaNslookup()
275   */
276  static struct in_addr HostToIp(const std::string& name);
277
278  /*!
279   * Fetches file with given source_file_name from server (either addressed by network name or ip address) at given source_directory
280   * and puts it to the given target_directory (via <system( rsync -av ...)> ).
281   *
282   * \params source_host_name network name of server
283   * \params source_host_ip_address ip address name of server (used alternatively to its name)
284   * \params source_directory directory on server
285   * \params source_file_name file to fetch from server
286   * \params target_directory local directory for fetched file
287   * \params optional_rsync_flags optional flags for rsync command, appended to -av
288   *
289   * \returns result of system call
290   */
291  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 = "");
292
293
294  /*!
295   * Fetches files with given source_file_names from server (either addressed by network name or ip address) at given source_directory
296   * and puts it to the given target_directory (via <system( rsync -avr --files-from ...)> ). Uses temp file on local host to
297   * compose list with files to fetch.
298   *
299   * \params source_host_name network name of server
300   * \params source_host_ip_address ip address name of server (used alternatively to its name)
301   * \params source_directory directory on server
302   * \params source_file_names files to fetch from server
303   * \params target_directory local directory for fetched file
304   * \params optional_rsync_flags optional flags for rsync command, appended to -avr
305   *
306   * \returns result of system call
307   */
308  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 = "");
309
310
311  /*!
312   * 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.
313   * The following checks are performed (with decreasing priority):
314   * 1. check location of file: "./ + resource_repository + filename"
315   * 2. check location of file: "local_resource_directory + resource_repository + filename"
316   * 3. check location of file: "server_resource_directory + resource_repository + filename" --> rsync to "local_resource_directory + resource_repository + file_name"
317   * As soon as one check succeeds the remaining checks are not executed.
318   *
319   * \param file_name name of file to search and fetch (may contain relative path)
320   * \param full_local_file_name full qualified name of file on local host after successful check/fetch operation (result)
321   * \param resource_repository relative path to be used as resource repository (on local host and on server, may be empty)
322   * \param resource_server network name of resource server (optional)
323   * \param local_resource_directory absolute path to resource_repository on local host (may be empty)
324   * \param server_resource_directory absolute path to resource_repository on server (may be empty)
325   * \param use_cache whether to take local host name and server ip address from local static cache or not
326   *
327   * \returns success of check/fetch operation
328   */
329  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);
330
331  /*!
332   * Clear cached local host name and server ip addresses
333   * \sa CheckAndGetFile()
334   */
335  static void ClearCaches();
336
337  /*!
338   * Clear cached resource file names
339   * \sa CheckAndGetFile()
340   */
341  static void ClearResourceCache();
342
343private:
344  static std::string cached_local_host; //!< local static variable to store name of local host
345  static std::map<std::string, std::string> host_name_to_ip_cache; //!< local static variable to store server ip addresses
346  static std::map<std::string, std::string> resource_cache; //!< local static variable to store names of already loaded resource files
347};
348//----------------------------------------------------------------------
349// End of namespace declaration
350//----------------------------------------------------------------------
351}; // namespace util
352}; // namespace rrlib
353#endif
Note: See TracBrowser for help on using the repository browser.