aboutsummaryrefslogtreecommitdiffstats
path: root/src/kvilib/file/KviFileUtils.h
blob: c36032b6b28f9a28aa0b53f4380571488d3dc4bb (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#ifndef _KVI_FILEUTILS_H_
#define _KVI_FILEUTILS_H_
//=============================================================================
//
//   File : KviFileUtils.h
//   Creation date : Fri Dec 25 1998 18:27:04 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 1998-2010 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your option) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

/**
* \file KviFileUtils.h
* \author Szymon Stefanek
* \brief File utilities functions
*
* \def KVI_PATH_SEPARATOR Defines the path separator, UNIX or win32
* \def KVI_PATH_SEPARATOR_CHAR Defines the path separator char, UNIX or win32
*/

#include "kvi_settings.h"

#include <QFile>

class QByteArray;
class QString;
#include <QStringList>

#if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
#define KVI_PATH_SEPARATOR "\\"
#define KVI_PATH_SEPARATOR_CHAR '\\'
#else
#define KVI_PATH_SEPARATOR "/"
#define KVI_PATH_SEPARATOR_CHAR '/'
#endif

// #warning "Add kvi_trashFile(const char * path) ? - is it needed in the whole app"
// #warning "or should it be available only for dirbrowser module?"
/**
* \namespace KviFileUtils
* \brief A namespace to handle file utilities functions
*/
namespace KviFileUtils
{
	//KVILIB_API bool readLine(QFile * f,QString &szBuffer,bool bClearBuffer = true);
	//KVILIB_API bool loadFileStripCR(const QString &szPath,QString &szBuffer);

	/**
	* \brief Loads the file at szPath to szBuffer eventually converting from UTF-8
	* \param szPath The path to the file to load
	* \param szBuffer The buffer where to load the file
	* \param bUtf8 Where to convert from UTF-8
	* \return bool
	*/
	KVILIB_API bool loadFile(const QString & szPath, QString & szBuffer, bool bUtf8 = true);

	/**
	* \brief Adjusts the file path to the current platform
	* \param szPath The path to the file
	* \return void
	*/
	KVILIB_API void adjustFilePath(QString & szPath);

	/**
	* \brief Returns true if szPath points to an existing directory
	* \param szPath The path to the directory
	* \return bool
	*/
	KVILIB_API bool directoryExists(const QString & szPath);

	/**
	* \brief Returns true if szPath points to an existing file
	* \param szPath The path to the file
	* \return bool
	*/
	inline bool fileExists(const QString & szPath) { return QFile::exists(szPath); }

	/**
	* \brief Removes a file
	* \param szPath The file to remove
	* \return bool
	*/
	KVILIB_API bool removeFile(const QString & szPath);

	/**
	* \brief Removes a file
	* \param pcPath The file to remove
	* \return bool
	*/
	KVILIB_API bool removeFile(const char * pcPath);

	/**
	* \brief Removes a dir (must be empty)
	* \param szPath The directory to remove
	* \return bool
	*/
	KVILIB_API bool removeDir(const QString & szPath);

	/**
	* \brief Removes a dir (must be empty)
	* \param pcPath The directory to remove
	* \return bool
	*/
	KVILIB_API bool removeDir(const char * pcPath);

	/**
	* \brief Removes a dir recursively
	* \param szPath The path of the directory to remove
	* \return bool
	*/
	KVILIB_API bool deleteDir(const QString & szPath);

	/**
	* \brief Writes a complete file (UTF-8 version)
	* \param szPath The path to the file
	* \param oData The data to write
	* \param bAppend If we want to append data or overwrite
	* \return bool
	*/
	KVILIB_API bool writeFile(const QString & szPath, const QByteArray & oData, bool bAppend = false);

	/**
	* \brief Writes a complete file (UTF-8 version)
	* \param szPath The path to the file
	* \param szData The data to write
	* \param bAppend If we want to append data or overwrite
	* \return bool
	*/
	KVILIB_API bool writeFile(const QString & szPath, const QString & szData, bool bAppend = false);

	/**
	* \brief Writes a complete file (UTF-8 version)
	* \param pcPath The path to the file
	* \param szData The data to write
	* \param bAppend If we want to append data or overwrite
	* \return bool
	*/
	KVILIB_API bool writeFile(const char * pcPath, const QString & szData, bool bAppend = false);

	/**
	* \brief Writes a complete file (local 8 bit version)
	* \param szPath The path to the file
	* \param szData The data to write
	* \param bAppend If we want to append data or overwrite
	* \return bool
	*/
	KVILIB_API bool writeFileLocal8Bit(const QString & szPath, const QString & szData, bool bAppend = false);

	/**
	* \brief Writes a complete file (local 8 bit version)
	* \param pcPath The path to the file
	* \param szData The data to write
	* \param bAppend If we want to append data or overwrite
	* \return bool
	*/
	KVILIB_API bool writeFileLocal8Bit(const char * pcPath, const QString & szData, bool bAppend = false);

	/**
	* \brief Reads a complete file and puts it in the string szBuffer
	*
	* The file must be smaller than uMaxSize bytes
	* \param szPath The path to the file to read
	* \param szBuffer The buffer where to load the file
	* \param uMaxSize The maximum size of the file to read
	* \return bool
	*/
	KVILIB_API bool readFile(const QString & szPath, QString & szBuffer, unsigned int uMaxSize = 65535);

	/**
	* \brief Reads a complete file and puts it in the string szBuffer
	*
	* The file must be smaller than uMaxSize bytes
	* \param pcPath The path to the file to read
	* \param szBuffer The buffer where to load the file
	* \param uMaxSize The maximum size of the file to read
	* \return bool
	*/
	KVILIB_API bool readFile(const char * pcPath, QString & szBuffer, unsigned int uMaxSize = 65535);

	/**
	* \brief Extracts the filename from a complete path (strips leading path)
	*
	* Extracts the filename from a complete path (strips leading path).
	* If szFileNameWithPath ends with a trailing slash/backslash then if bAllowEmpty is set
	* to true then the name is considered to be empty, if bAllowEmpty is set to false
	* then the trailing slash/backslash is stripped.
	*
	* \param szFileNameWithPath The complete path to the file
	* \return QString
	*/
	KVILIB_API QString extractFileName(const QString & szFileNameWithPath, bool bAllowEmpty = true);

	/**
	* \brief Extracts the filename from a complete path (strips leading path)
	* \param szFileNameWithPath The complete path
	* \return QString
	*/
	KVILIB_API QString extractFilePath(const QString & szFileNameWithPath);

	/**
	* \brief Copy the file (cp -f)
	* \param szSrc The source file
	* \param szDst The destination file
	* \return bool
	*/
	KVILIB_API bool copyFile(const QString & szSrc, const QString & szDst);

	/**
	* \brief Copy the file (cp -f)
	* \param pcSrc The source file
	* \param pcDst The destination file
	* \return bool
	*/
	KVILIB_API bool copyFile(const char * pcSrc, const char * pcDst);

	/**
	* \brief Rename or move the file (mv)
	* \param szSrc The source file
	* \param szDst The destination file
	* \return bool
	*/
	KVILIB_API bool renameFile(const QString & szSrc, const QString & szDst);

	/**
	* \brief Rename or move the file (mv)
	* \param pcSrc The source file
	* \param pcDst The destination file
	* \return bool
	*/
	KVILIB_API bool renameFile(const char * pcSrc, const char * pcDst);

	/**
	* \brief Create a directory (mkdir)
	* \param szPath The path to the directory
	* \return bool
	*/
	KVILIB_API bool makeDir(const QString & szPath);

	/**
	* \brief Create a directory (mkdir)
	* \param pcPath The path to the directory
	* \return bool
	*/
	KVILIB_API bool makeDir(const char * pcPath);

	/**
	* \brief Reads a text line, returns false if EOF is reached
	* \param pFile The source file
	* \param szBuffer The buffer where to store the line read
	* \param bUtf8 If we want to convert from UTF-8
	* \return bool
	*/
	KVILIB_API bool readLine(QFile * pFile, QString & szBuffer, bool bUtf8 = true);

	/**
	* \brief Reads text lines, returns false if EOF is reached
	* \param pFile The source file
	* \param buffer The buffer where to store the lines read
	* \param iStartLine The number of the first line to read
	* \param iCount The number of lines to read
	* \param bUtf8 If we want to convert from UTF-8
	* \return bool
	*/
	KVILIB_API bool readLines(QFile * pFile, QStringList & buffer, int iStartLine = 0, int iCount = -1, bool bUtf8 = true);

	/**
	* \brief Returns true if the file is readable, false otherwise
	* \param szFname The source file
	* \return bool
	*/
	KVILIB_API bool isReadable(const QString & szFname);

	/**
	* \brief Returns true if the path is absolute, false otherwise
	* \param szPath The path to check
	* \return bool
	*/
	KVILIB_API bool isAbsolutePath(const QString & szPath);

	/**
	* \brief Translates ANY string into a valid filename (with no path!)
	*
	* There is NO way to come back to the original string the algo is
	* one-way only
	* \param szPath
	* \return void
	*/
	KVILIB_API void encodeFileName(QString & szPath);

	/**
	* \brief Removes any unusable character from a filename (with no path!)
	*
	* There is NO way to come back to the original string the algo is
	* one-way only
	* \param szPath
	* \return void
	*/
	KVILIB_API void cleanFileName(QString & szPath);

	/**
	* Build a recursive file listing
	*/
	KVILIB_API QStringList getFileListing(const QString & szPath);
}

#endif //_KVI_FILEUTILS_H