KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
fileutil.h
1/**
2 * FileUtil
3 *
4 * Some static helper methods for one line file reads / writes and other
5 * file manipulations.
6 *
7 * Stephen Punak, October 02 2020
8 */
9#ifndef FILEUTIL_H
10#define FILEUTIL_H
11
12#include <QByteArray>
13#include <QDateTime>
14#include <QString>
15#include "kanoopcommon.h"
16
17/**
18 * @brief Static helper methods for common file I/O operations.
19 */
20class KANOOP_EXPORT FileUtil
21{
22public:
23 /**
24 * @brief Compute the MD5 hash of a file as a hex string.
25 * @param filename Path to the file
26 * @return Hex-encoded MD5 string, or empty string on error
27 */
28 static QString getMD5String(const QString& filename);
29
30 /**
31 * @brief Compute the MD5 hash of a file as raw bytes.
32 * @param filename Path to the file
33 * @return Raw MD5 hash bytes, or empty array on error
34 */
35 static QByteArray getMD5Bytes(const QString& filename);
36
37 /**
38 * @brief Read the entire contents of a file into a byte array.
39 * @param filename Path to the file
40 * @param data Output byte array receiving file contents
41 * @return true on success, false on error
42 */
43 static bool readAllBytes(const QString& filename, QByteArray& data);
44
45 /**
46 * @brief Read all lines of a text file into a string list.
47 * @param filename Path to the file
48 * @param data Output string list receiving the lines
49 * @param encoding Text encoding to use (default UTF-8)
50 * @return true on success, false on error
51 */
52 static bool readAllLines(const QString& filename, QStringList& data, QStringConverter::Encoding encoding = QStringConverter::Utf8);
53
54 /**
55 * @brief Write a byte array to a file, overwriting any existing content.
56 * @param filename Path to the output file
57 * @param data Bytes to write
58 * @return true on success, false on error
59 */
60 static bool writeAllBytes(const QString& filename, const QByteArray& data);
61
62 /**
63 * @brief Write a byte array to a file using a specific text encoding.
64 * @param filename Path to the output file
65 * @param data Bytes to write
66 * @param encoding Text encoding used when writing
67 * @return true on success, false on error
68 */
69 static bool writeAllBytes(const QString& filename, const QByteArray& data, QStringConverter::Encoding encoding);
70
71 /**
72 * @brief Write a list of strings to a file, one per line.
73 * @param filename Path to the output file
74 * @param lines Lines to write
75 * @return true on success, false on error
76 */
77 static bool writeAllLines(const QString& filename, const QStringList& lines);
78
79 /**
80 * @brief Append a byte array to an existing file (or create if absent).
81 * @param filename Path to the file
82 * @param data Bytes to append
83 * @return true on success, false on error
84 */
85 static bool appendAllBytes(const QString& filename, const QByteArray& data);
86
87 /**
88 * @brief Count the number of lines in a text file.
89 * @param filename Path to the file
90 * @return Number of lines, or -1 on error
91 */
92 static int lineCount(const QString& filename);
93
94 /**
95 * @brief Test whether a file exists.
96 * @param filename Path to the file
97 * @return true if the file exists
98 */
99 static bool exists(const QString& filename);
100
101 /**
102 * @brief Remove (delete) a file.
103 * @param filename Path to the file to remove
104 * @return true on success, false on error
105 */
106 static bool remove(const QString& filename);
107
108 /**
109 * @brief Create an empty file or update its modification time if it exists.
110 * @param filename Path to the file
111 * @return true on success, false on error
112 */
113 static bool touch(const QString& filename);
114
115 /**
116 * @brief Move (rename) a file from one path to another.
117 * @param source Path to the source file
118 * @param destination Path to the destination
119 * @return true on success, false on error
120 */
121 static bool move(const QString& source, const QString& destination);
122
123 /**
124 * @brief Move a file into a destination directory, preserving its basename.
125 * @param sourceFilename Path to the source file
126 * @param destinationDirectory Path to the target directory
127 * @return true on success, false on error
128 */
129 static bool moveToDirectory(const QString& sourceFilename, const QString& destinationDirectory);
130
131 /**
132 * @brief Set the modification timestamp of a file.
133 * @param filename Path to the file
134 * @param value New modification timestamp
135 * @return true on success, false on error
136 */
137 static bool setModifyTime(const QString& filename, const QDateTime& value);
138
139 /**
140 * @brief Generate a unique temporary file path.
141 * @return Path string for a new temporary file
142 */
143 static QString getTempFilename();
144};
145
146#endif // FILEUTIL_H
FileUtil.
Definition fileutil.h:21
static bool exists(const QString &filename)
Test whether a file exists.
static bool remove(const QString &filename)
Remove (delete) a file.
static QString getTempFilename()
Generate a unique temporary file path.
static QByteArray getMD5Bytes(const QString &filename)
Compute the MD5 hash of a file as raw bytes.
static bool appendAllBytes(const QString &filename, const QByteArray &data)
Append a byte array to an existing file (or create if absent).
static bool touch(const QString &filename)
Create an empty file or update its modification time if it exists.
static bool writeAllLines(const QString &filename, const QStringList &lines)
Write a list of strings to a file, one per line.
static bool writeAllBytes(const QString &filename, const QByteArray &data, QStringConverter::Encoding encoding)
Write a byte array to a file using a specific text encoding.
static bool writeAllBytes(const QString &filename, const QByteArray &data)
Write a byte array to a file, overwriting any existing content.
static QString getMD5String(const QString &filename)
Compute the MD5 hash of a file as a hex string.
static int lineCount(const QString &filename)
Count the number of lines in a text file.
static bool moveToDirectory(const QString &sourceFilename, const QString &destinationDirectory)
Move a file into a destination directory, preserving its basename.
static bool setModifyTime(const QString &filename, const QDateTime &value)
Set the modification timestamp of a file.
static bool readAllLines(const QString &filename, QStringList &data, QStringConverter::Encoding encoding=QStringConverter::Utf8)
Read all lines of a text file into a string list.
static bool readAllBytes(const QString &filename, QByteArray &data)
Read the entire contents of a file into a byte array.
static bool move(const QString &source, const QString &destination)
Move (rename) a file from one path to another.