Commit 23fc6756f1894e1af35853eb2251f08d5b25cf30
1 parent
0fe8d447
Add QUtil::FileCloser to the public API
Showing
3 changed files
with
32 additions
and
20 deletions
ChangeLog
| 1 | +2022-05-18 Jay Berkenbilt <ejb@ql.org> | ||
| 2 | + | ||
| 3 | + * Add QUtil::FileCloser to the public API. This is a simple inline | ||
| 4 | + class to help with automatic file closing. | ||
| 5 | + | ||
| 1 | 2022-05-17 Jay Berkenbilt <ejb@ql.org> | 6 | 2022-05-17 Jay Berkenbilt <ejb@ql.org> |
| 2 | 7 | ||
| 3 | * Allow passing *uninitialized* (not null) objects to | 8 | * Allow passing *uninitialized* (not null) objects to |
include/qpdf/QUtil.hh
| @@ -114,6 +114,33 @@ namespace QUtil | @@ -114,6 +114,33 @@ namespace QUtil | ||
| 114 | QPDF_DLL | 114 | QPDF_DLL |
| 115 | FILE* fopen_wrapper(std::string const&, FILE*); | 115 | FILE* fopen_wrapper(std::string const&, FILE*); |
| 116 | 116 | ||
| 117 | + // This is a little class to help with automatic closing files. | ||
| 118 | + // You can do something like | ||
| 119 | + // | ||
| 120 | + // QUtil::FileCloser fc(QUtil::safe_fopen(filename, "rb")); | ||
| 121 | + // | ||
| 122 | + // and then use fc.f to the file. Be sure to actually declare a | ||
| 123 | + // variable of type FileCloser. Using it as a temporary won't work | ||
| 124 | + // because it will close the file as soon as it goes out of scope. | ||
| 125 | + class FileCloser | ||
| 126 | + { | ||
| 127 | + public: | ||
| 128 | + FileCloser(FILE* f) : | ||
| 129 | + f(f) | ||
| 130 | + { | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + ~FileCloser() | ||
| 134 | + { | ||
| 135 | + if (f) { | ||
| 136 | + fclose(f); | ||
| 137 | + f = nullptr; | ||
| 138 | + } | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + FILE* f; | ||
| 142 | + }; | ||
| 143 | + | ||
| 117 | // Attempt to open the file read only and then close again | 144 | // Attempt to open the file read only and then close again |
| 118 | QPDF_DLL | 145 | QPDF_DLL |
| 119 | bool file_can_be_opened(char const* filename); | 146 | bool file_can_be_opened(char const* filename); |
libqpdf/QUtil.cc
| @@ -305,26 +305,6 @@ static std::map<unsigned long, unsigned char> unicode_to_pdf_doc = { | @@ -305,26 +305,6 @@ static std::map<unsigned long, unsigned char> unicode_to_pdf_doc = { | ||
| 305 | {0x20ac, 0xa0}, | 305 | {0x20ac, 0xa0}, |
| 306 | }; | 306 | }; |
| 307 | 307 | ||
| 308 | -namespace | ||
| 309 | -{ | ||
| 310 | - class FileCloser | ||
| 311 | - { | ||
| 312 | - public: | ||
| 313 | - FileCloser(FILE* f) : | ||
| 314 | - f(f) | ||
| 315 | - { | ||
| 316 | - } | ||
| 317 | - | ||
| 318 | - ~FileCloser() | ||
| 319 | - { | ||
| 320 | - fclose(f); | ||
| 321 | - } | ||
| 322 | - | ||
| 323 | - private: | ||
| 324 | - FILE* f; | ||
| 325 | - }; | ||
| 326 | -} // namespace | ||
| 327 | - | ||
| 328 | template <typename T> | 308 | template <typename T> |
| 329 | static std::string | 309 | static std::string |
| 330 | int_to_string_base_internal(T num, int base, int length) | 310 | int_to_string_base_internal(T num, int base, int length) |