source: rrlib_util/sFileIOUtils.h @ 9:b4a8f6dd044f

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

added <sStringUtils>, <sFileIOUtils>, <tAutoRegister>, <tCallback> as lib <util>

File size: 10.4 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) AG Robotersysteme TU Kaiserslautern
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    sFileIOUtils.h
23 *
24 * \author  Bernd Helge Schaefer
25 * \author  Jens Wettach
26 * \date    15.11.05
27 *
28 * \brief   Contains static class sFileIOUtils
29 *
30 */
31//----------------------------------------------------------------------
32// Includes
33//----------------------------------------------------------------------
34#ifndef _rrlib_util_sFileIOUtils_h_
35#define _rrlib_util_sFileIOUtils_h_
36
37//STL includes
38#include <string>
39#include <vector>
40#include <map>
41
42extern "C"
43{
44#include <arpa/inet.h> // inet_ntoa
45}
46
47//----------------------------------------------------------------------
48// Namespace declaration
49//----------------------------------------------------------------------
50namespace rrlib
51{
52namespace util
53{
54
55class sFileIOUtils
56{
57public:
58  /*!
59   * \brief Returns all files in a given directory
60   *
61   * \return Returns whether or not the read was possible
62   */
63  static bool GetAllFilesInDirectory(std::string dir, std::vector<std::string> &files);
64
65  /*!
66   * \brief Returns the current directory
67   *
68   * \return Returns a string containing the current directory
69   */
70  static std::string GetCurrentDirectory();
71  /*!
72   * \brief Setsthe current directory
73   *
74   * Changes the current directory to \param dir .
75   * \return returns an error code similar to the chdir C command
76   */
77  static int ChangeCurrentDirectory(const std::string& dir);
78
79  /*!
80   * \brief Creates a new directory at the path provided as parameter.
81   *
82   * \param path Complete relative or absolute path to be created.
83   * \return returns false if directory creation fails, true otherwise.
84   * The existance of the folder is not regarded as a failure here!
85   */
86  static bool CreateDirectory(const std::string& path); // throw(runtime_error);
87
88  /*!
89   * \brief Creates a new temporary file via "mktemp".
90   *
91   * \return the new temp file
92   */
93  static std::string CreateTempfile();
94
95  /*!
96   * \brief Removes file with given name.
97   */
98  static void DeleteFile(const std::string& filename);
99
100  /*!
101   * \brief Calls "gzip" on input file and writes result to output file.
102   *
103   * \note output file gets suffix ".gz" automatically if not yet the case.
104   */
105  static void CompressFile(const std::string& input_filename, std::string& output_filename);
106
107  /*!
108   * \brief Calls "gunzip" on input file and writes result to output file.
109   *
110   * \note input file must have suffix ".gz".
111   */
112  static void DecompressFile(const std::string& input_filename, const std::string& output_filename);
113
114
115
116  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
117   *
118   * \param file_name   file name to be expanded (will contain the result afterwards)
119   *
120   * \returns Whether the method could be executed without failure or not
121   */
122  static bool ShellExpandFilename(std::string &file_name) __attribute__((__warn_unused_result__));
123
124  /*! Expands the given filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
125  *
126  * Thus variables as e.g. $MCAPROJECTHOME can be used for specifying OIV files in scene description files.
127  *
128  * \param result      reference to std::string object that should contain the result afterwards
129  * \param file_name   file name to be expanded
130  *
131  * \returns Whether the method could be executed without failure or not
132  */
133  static bool ShellExpandFilename(std::string &result, const std::string& file_name) __attribute__((__warn_unused_result__));
134
135  static std::string ShellExpandFilename(const std::string &file_name) __attribute__((deprecated));
136
137
138  /*!
139   *    Splits the given filename into directory, single file name and file extension
140   *    \param complete_name complete file name (input)
141   *    \param file_dir directory (output)
142   *    \param file_base single file name (output)
143   *    \param file_ext file extension (output)
144   */
145  static void SplitFullQualifiedFilename(const std::string& complete_name, std::string& file_dir, std::string& file_base, std::string& file_ext);
146
147
148  /*!
149   * Retrieves network host name via "hostname" and returns result.
150   * \param fqdn whether FQDN  (Fully  Qualified Domain Name) or short name shall be retrieved
151   */
152  static std::string GetHostName(bool fqdn = true);
153
154
155  /*!
156   * Retrieves ip address of host with given network name via "gethostbyname" and returns result.
157   */
158  static struct in_addr HostToIpViaGetHostByName(const std::string& name);
159
160  /*!
161   * Retrieves ip address of host with given network name via "nslookup" and returns result.
162   */
163  static struct in_addr HostToIpViaNslookup(const std::string& name);
164
165  /*!
166   * Retrieves ip address of host with given network name via "host" and returns result.
167   */
168  static struct in_addr HostToIpViaHost(const std::string& name);
169
170  /*!
171   * Retrieves ip address of host with given network name from "/etc/hosts" and returns result.
172   */
173  static struct in_addr HostToIpViaHostsFile(const std::string& name);
174
175  /*!
176   * Retrieves ip address of host with given network name using
177   * 1. \sa HostToIpViaHostsFile()
178   * 2. \sa HostToIpViaHostx()
179   * 2. \sa HostToIpViaNslookup()
180   */
181  static struct in_addr HostToIp(const std::string& name);
182
183  /*!
184   * Fetches file with given source_file_name from server (either addressed by network name or ip address) at given source_directory
185   * and puts it to the given target_directory (via <system( rsync -av ...)> ).
186   *
187   * \params source_host_name network name of server
188   * \params source_host_ip_address ip address name of server (used alternatively to its name)
189   * \params source_directory directory on server
190   * \params source_file_name file to fetch from server
191   * \params target_directory local directory for fetched file
192   * \params optional_rsync_flags optional flags for rsync command, appended to -av
193   *
194   * \returns result of system call
195   */
196  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 = "");
197
198
199  /*!
200   * Fetches files with given source_file_names from server (either addressed by network name or ip address) at given source_directory
201   * and puts it to the given target_directory (via <system( rsync -avr --files-from ...)> ). Uses temp file on local host to
202   * compose list with files to fetch.
203   *
204   * \params source_host_name network name of server
205   * \params source_host_ip_address ip address name of server (used alternatively to its name)
206   * \params source_directory directory on server
207   * \params source_file_names files to fetch from server
208   * \params target_directory local directory for fetched file
209   * \params optional_rsync_flags optional flags for rsync command, appended to -avr
210   *
211   * \returns result of system call
212   */
213  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 = "");
214
215
216  /*!
217   * 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.
218   * The following checks are performed (with decreasing priority):
219   * 1. check location of file: "./ + resource_repository + filename"
220   * 2. check location of file: "local_resource_directory + resource_repository + filename"
221   * 3. check location of file: "server_resource_directory + resource_repository + filename" --> rsync to "local_resource_directory + resource_repository + file_name"
222   * As soon as one check succeeds the remaining checks are not executed.
223   *
224   * \param file_name name of file to search and fetch (may contain relative path)
225   * \param full_local_file_name full qualified name of file on local host after successful check/fetch operation (result)
226   * \param resource_repository relative path to be used as resource repository (on local host and on server, may be empty)
227   * \param resource_server network name of resource server (optional)
228   * \param local_resource_directory absolute path to resource_repository on local host (may be empty)
229   * \param server_resource_directory absolute path to resource_repository on server (may be empty)
230   * \param use_cache whether to take local host name and server ip address from local static cache or not
231   *
232   * \returns success of check/fetch operation
233   */
234  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);
235
236  /*!
237   * Clear cached local host name and server ip addresses
238   * \sa CheckAndGetFile()
239   */
240  static void ClearCaches();
241
242  /*!
243   * Clear cached resource file names
244   * \sa CheckAndGetFile()
245   */
246  static void ClearResourceCache();
247
248private:
249  static std::string cached_local_host; //!< local static variable to store name of local host
250  static std::map<std::string, std::string> host_name_to_ip_cache; //!< local static variable to store server ip addresses
251  static std::map<std::string, std::string> resource_cache; //!< local static variable to store names of already loaded resource files
252};
253//----------------------------------------------------------------------
254// End of namespace declaration
255//----------------------------------------------------------------------
256}; // namespace util
257}; // namespace rrlib
258#endif
Note: See TracBrowser for help on using the repository browser.