source: rrlib_util/sFileIOUtils.h @ 111:56ac6d8b1149

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

cleaned GetHostName()

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