mirror of
https://github.com/qpdf/qpdf.git
synced 2025-01-02 22:50:20 +00:00
Add QUtil::FileCloser to the public API
This commit is contained in:
parent
0fe8d44762
commit
23fc6756f1
@ -1,3 +1,8 @@
|
||||
2022-05-18 Jay Berkenbilt <ejb@ql.org>
|
||||
|
||||
* Add QUtil::FileCloser to the public API. This is a simple inline
|
||||
class to help with automatic file closing.
|
||||
|
||||
2022-05-17 Jay Berkenbilt <ejb@ql.org>
|
||||
|
||||
* Allow passing *uninitialized* (not null) objects to
|
||||
|
@ -114,6 +114,33 @@ namespace QUtil
|
||||
QPDF_DLL
|
||||
FILE* fopen_wrapper(std::string const&, FILE*);
|
||||
|
||||
// This is a little class to help with automatic closing files.
|
||||
// You can do something like
|
||||
//
|
||||
// QUtil::FileCloser fc(QUtil::safe_fopen(filename, "rb"));
|
||||
//
|
||||
// and then use fc.f to the file. Be sure to actually declare a
|
||||
// variable of type FileCloser. Using it as a temporary won't work
|
||||
// because it will close the file as soon as it goes out of scope.
|
||||
class FileCloser
|
||||
{
|
||||
public:
|
||||
FileCloser(FILE* f) :
|
||||
f(f)
|
||||
{
|
||||
}
|
||||
|
||||
~FileCloser()
|
||||
{
|
||||
if (f) {
|
||||
fclose(f);
|
||||
f = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
FILE* f;
|
||||
};
|
||||
|
||||
// Attempt to open the file read only and then close again
|
||||
QPDF_DLL
|
||||
bool file_can_be_opened(char const* filename);
|
||||
|
@ -305,26 +305,6 @@ static std::map<unsigned long, unsigned char> unicode_to_pdf_doc = {
|
||||
{0x20ac, 0xa0},
|
||||
};
|
||||
|
||||
namespace
|
||||
{
|
||||
class FileCloser
|
||||
{
|
||||
public:
|
||||
FileCloser(FILE* f) :
|
||||
f(f)
|
||||
{
|
||||
}
|
||||
|
||||
~FileCloser()
|
||||
{
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
private:
|
||||
FILE* f;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
template <typename T>
|
||||
static std::string
|
||||
int_to_string_base_internal(T num, int base, int length)
|
||||
|
Loading…
Reference in New Issue
Block a user