source: rrlib_util/fileio.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: 5.9 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    2016-04-28
28 *
29 * \brief   Contains functions for file handling
30 *
31 */
32//----------------------------------------------------------------------
33// Includes
34//----------------------------------------------------------------------
35#ifndef _rrlib_util_fileio_h_
36#define _rrlib_util_fileio_h_
37
38//rrlib includes
39#include "rrlib/logging/messages.h"
40
41//STL includes
42#include <string>
43#include <vector>
44
45//----------------------------------------------------------------------
46// Namespace declaration
47//----------------------------------------------------------------------
48namespace rrlib
49{
50namespace util
51{
52namespace fileio
53{
54/*!
55 * \brief Returns all files in a given directory
56 *
57 * \return true if the directory could be read
58 * \throws runtime_error if \param directory cannot be opened
59 */
60bool GetAllFilesInDirectory(const std::string& directory, std::vector<std::string> &files);
61
62/*!
63 * \brief Returns the current directory
64 *
65 * \return a string containing the current directory
66 * \throws runtime_error if the current directory cannot be retrieved
67 */
68std::string GetCurrentDirectory();
69
70/*!
71 * \brief Changes the current directory to \param directory .
72 * \return true if the directory could be changed
73 * \throws runtime_error if the directory cannot be changed
74 */
75bool ChangeCurrentDirectory(const std::string& directory);
76
77/*!
78 * \brief Creates a new directory at the \param path.
79 *
80 * \param path Complete relative or absolute path to be created.
81 * \return returns true if directory could be created
82 * \throws runtime_error if the directory cannot be created
83 * The existance of the folder is not regarded as a failure here!
84 */
85bool CreateDirectory(const std::string& path);
86
87/*!
88 * \brief Creates a new temporary file via "mktemp".
89 * \param root_directory directory for the temporary file (optional)
90 * \return the newly created temporary file
91 * \throws runtime_error if the file creation fails
92 */
93std::string CreateTempFile(const std::string& root_directory = "");
94
95/*!
96 * \brief Creates a new temporary directory via "mktemp".
97 * \param root_directory directory for the temporary directory (optional)
98 * \return the newly created temporary directory
99 * \throws runtime_error if the directory creation fails
100 */
101std::string CreateTempDirectory(const std::string& root_directory = "");
102
103/*!
104 * \brief Removes the file with \param filename.
105 * \return true if file could be removed
106 * \throws runtime_error if file removal fails
107 */
108bool DeleteFile(const std::string& filename);
109
110/*!
111 * \brief Removes the directory with \param directoryname.
112 * \return true if directory could be removed
113 * \throws runtime_error if directory removal fails
114 */
115bool DeleteDirectory(const std::string& directoryname);
116
117/*!
118 * \brief Tests if file with \param filename exists
119 *
120 * \return true if file exists, false otherwise
121 */
122bool FileExists(const std::string &filename);
123
124/*!
125 * \brief Counts number of lines in \param filename
126 *
127 * \return numboer of lines
128 * \throws runtime_error if file cannot be opened
129 */
130size_t CountLineNumbers(const std::string &filename);
131
132/*! Expands the \param filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
133 *
134 * \param filename file name to be expanded (will contain the result afterwards)
135 *
136 * \return true if file name could be expanded
137 * \throws runtime_error if expansion of file name failed
138 */
139bool ShellExpandFilename(std::string &filename) __attribute__((__warn_unused_result__));
140
141/*! Expands the \param filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
142 *
143 * \param filename file name to be expanded
144 *
145 * \return expanded file name in case of success
146 * \throws runtime_error if expansion of file name failed
147 */
148std::string ShellExpandFilename(const std::string &file_name)  __attribute__((__warn_unused_result__));
149
150/*! Expands the \param filename via a pipe and echo command in order to replace all contained environment variables with their actual value.
151 *
152 * \param filename file name to be expanded
153 * \param result will contain the expanded file name
154 *
155 * \return true if file name could be expanded
156 * \throws runtime_error if expansion of file name failed
157 */
158bool ShellExpandFilename(std::string &result, const std::string& file_name) __attribute__((__warn_unused_result__));
159
160/*!
161 *    Splits the given filename into directory, single file name and file extension
162 *    \param complete_name complete file name (input)
163 *    \param file_dir directory (output)
164 *    \param file_base single file name (output)
165 *    \param file_ext file extension (output)
166 */
167void SplitFullQualifiedFilename(const std::string& complete_name, std::string& file_dir, std::string& file_base, std::string& file_ext);
168
169//----------------------------------------------------------------------
170// End of namespace declaration
171//----------------------------------------------------------------------
172}; // namespace fileio
173}; // namespace util
174}; // namespace rrlib
175#endif
176
Note: See TracBrowser for help on using the repository browser.