game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
file_handle.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// file_handle.h
4// ==============================================================================
5// RAII wrapper for FILE* handles to prevent resource leaks
6// ==============================================================================
7// This provides safe management of FILE* handles from fopen() and popen()
8// using RAII (Resource Acquisition Is Initialization) pattern.
9//
10// ## Usage Example
11// @code
12// // Safe file reading
13// {
14// FileHandle file("config.txt", "r");
15// if (file) {
16// char buffer[1024];
17// while (fgets(buffer, sizeof(buffer), file.get())) {
18// processLine(buffer);
19// }
20// } // File automatically closed here
21// }
22//
23// // Safe pipe usage
24// {
25// PipeHandle pipe("ls -l", "r");
26// if (pipe) {
27// char buffer[1024];
28// while (fgets(buffer, sizeof(buffer), pipe.get())) {
29// processOutput(buffer);
30// }
31// } // Pipe automatically closed here
32// }
33// @endcode
34// ==============================================================================
35
36#pragma once
37
38#include <cstdio>
39#include <string>
40
41// ==============================================================================
42// FileHandle - RAII wrapper for fopen/fclose
43// ==============================================================================
44
45/**
46 * @class FileHandle
47 * @brief RAII wrapper for FILE* from fopen()
48 *
49 * Automatically closes file handle on destruction, preventing resource leaks.
50 * Non-copyable but movable.
51 */
53{
54 public:
55 /**
56 * @brief Open file with specified mode
57 * @param filename Path to file
58 * @param mode File open mode ("r", "w", "a", etc.)
59 */
60 FileHandle(const char* filename, const char* mode) : file_(std::fopen(filename, mode)) {}
61
62 FileHandle(const std::string& filename, const char* mode)
63 : file_(std::fopen(filename.c_str(), mode))
64 {
65 }
66
67 /**
68 * @brief Destructor - automatically closes file
69 */
71 {
72 if (file_)
73 {
74 std::fclose(file_);
75 file_ = nullptr;
76 }
77 }
78
79 // Non-copyable
80 FileHandle(const FileHandle&) = delete;
81 FileHandle& operator=(const FileHandle&) = delete;
82
83 // Movable
84 FileHandle(FileHandle&& other) noexcept : file_(other.file_)
85 {
86 other.file_ = nullptr;
87 }
88
89 FileHandle& operator=(FileHandle&& other) noexcept
90 {
91 if (this != &other)
92 {
93 if (file_)
94 {
95 std::fclose(file_);
96 }
97 file_ = other.file_;
98 other.file_ = nullptr;
99 }
100 return *this;
101 }
102
103 /**
104 * @brief Get raw FILE* pointer
105 * @return FILE* pointer (nullptr if not open)
106 */
107 FILE* get() const
108 {
109 return file_;
110 }
111
112 /**
113 * @brief Check if file is open
114 * @return true if file handle is valid
115 */
116 bool isOpen() const
117 {
118 return file_ != nullptr;
119 }
120
121 /**
122 * @brief Explicit bool conversion for easy checking
123 */
124 explicit operator bool() const
125 {
126 return isOpen();
127 }
128
129 /**
130 * @brief Release ownership of FILE* without closing
131 * @return FILE* pointer (caller must close manually)
132 */
133 FILE* release()
134 {
135 FILE* temp = file_;
136 file_ = nullptr;
137 return temp;
138 }
139
140 private:
141 FILE* file_;
142};
143
144// ==============================================================================
145// PipeHandle - RAII wrapper for popen/pclose
146// ==============================================================================
147
148/**
149 * @class PipeHandle
150 * @brief RAII wrapper for FILE* from popen()
151 *
152 * Automatically closes pipe handle on destruction, preventing resource leaks.
153 * Non-copyable but movable.
154 */
156{
157 public:
158 /**
159 * @brief Open pipe for command execution
160 * @param command Shell command to execute
161 * @param mode Pipe mode ("r" for reading output, "w" for writing input)
162 */
163 PipeHandle(const char* command, const char* mode) : pipe_(::popen(command, mode)) {}
164
165 PipeHandle(const std::string& command, const char* mode) : pipe_(::popen(command.c_str(), mode))
166 {
167 }
168
169 /**
170 * @brief Destructor - automatically closes pipe
171 */
173 {
174 if (pipe_)
175 {
176 ::pclose(pipe_);
177 pipe_ = nullptr;
178 }
179 }
180
181 // Non-copyable
182 PipeHandle(const PipeHandle&) = delete;
183 PipeHandle& operator=(const PipeHandle&) = delete;
184
185 // Movable
186 PipeHandle(PipeHandle&& other) noexcept : pipe_(other.pipe_)
187 {
188 other.pipe_ = nullptr;
189 }
190
191 PipeHandle& operator=(PipeHandle&& other) noexcept
192 {
193 if (this != &other)
194 {
195 if (pipe_)
196 {
197 ::pclose(pipe_);
198 }
199 pipe_ = other.pipe_;
200 other.pipe_ = nullptr;
201 }
202 return *this;
203 }
204
205 /**
206 * @brief Get raw FILE* pointer
207 * @return FILE* pointer (nullptr if not open)
208 */
209 FILE* get() const
210 {
211 return pipe_;
212 }
213
214 /**
215 * @brief Check if pipe is open
216 * @return true if pipe handle is valid
217 */
218 bool isOpen() const
219 {
220 return pipe_ != nullptr;
221 }
222
223 /**
224 * @brief Close the pipe early and return pclose's exit status.
225 *
226 * Equivalent to letting the destructor run, except the caller gets
227 * the wait-status word back (the same value pclose() returns:
228 * a wait(2)-style status on success, or -1 on error). Use this
229 * when you need to distinguish "command succeeded" from "command
230 * crashed / exited non-zero" — the destructor swallows that info.
231 * Repeated close() calls are no-ops; the destructor won't double-close.
232 *
233 * Inspect the return value with the standard W* macros, e.g.:
234 * const int s = pipe.close();
235 * if (s == -1 || (WIFEXITED(s) && WEXITSTATUS(s) != 0)) ...
236 *
237 * @return pclose() return value, or 0 if pipe was already closed / never opened
238 */
239 int close()
240 {
241 if (!pipe_)
242 return 0;
243 const int status = ::pclose(pipe_);
244 pipe_ = nullptr;
245 return status;
246 }
247
248 /**
249 * @brief Explicit bool conversion for easy checking
250 */
251 explicit operator bool() const
252 {
253 return isOpen();
254 }
255
256 /**
257 * @brief Release ownership of FILE* without closing
258 * @return FILE* pointer (caller must close with pclose)
259 */
260 FILE* release()
261 {
262 FILE* temp = pipe_;
263 pipe_ = nullptr;
264 return temp;
265 }
266
267 private:
268 FILE* pipe_;
269};
FileHandle & operator=(const FileHandle &)=delete
FILE * release()
Release ownership of FILE* without closing.
FileHandle(const std::string &filename, const char *mode)
Definition file_handle.h:62
bool isOpen() const
Check if file is open.
~FileHandle()
Destructor - automatically closes file.
Definition file_handle.h:70
FILE * get() const
Get raw FILE* pointer.
FileHandle(const FileHandle &)=delete
FileHandle & operator=(FileHandle &&other) noexcept
Definition file_handle.h:89
FileHandle(FileHandle &&other) noexcept
Definition file_handle.h:84
FileHandle(const char *filename, const char *mode)
Open file with specified mode.
Definition file_handle.h:60
FILE * release()
Release ownership of FILE* without closing.
int close()
Close the pipe early and return pclose's exit status.
bool isOpen() const
Check if pipe is open.
FILE * get() const
Get raw FILE* pointer.
PipeHandle & operator=(PipeHandle &&other) noexcept
PipeHandle(const PipeHandle &)=delete
PipeHandle(const std::string &command, const char *mode)
PipeHandle(PipeHandle &&other) noexcept
~PipeHandle()
Destructor - automatically closes pipe.
PipeHandle & operator=(const PipeHandle &)=delete
PipeHandle(const char *command, const char *mode)
Open pipe for command execution.