tesseract v5.3.3.20231005
tesseract::File Class Reference

#include <fileio.h>

Inheritance diagram for tesseract::File:
file

Static Public Member Functions

static FILE * OpenOrDie (const std::string &filename, const std::string &mode)
 
static FILE * Open (const std::string &filename, const std::string &mode)
 
static void WriteStringToFileOrDie (const std::string &str, const std::string &filename)
 
static bool Readable (const std::string &filename)
 
static bool ReadFileToString (const std::string &filename, std::string *out)
 
static std::string JoinPath (const std::string &prefix, const std::string &suffix)
 
static bool Delete (const char *pathname)
 
static bool DeleteMatchingFiles (const char *pattern)
 

Detailed Description

Definition at line 44 of file fileio.h.

Member Function Documentation

◆ Delete()

bool tesseract::File::Delete ( const char *  pathname)
static

Definition at line 89 of file fileio.cpp.

89 {
90#if !defined(_WIN32) || defined(__MINGW32__)
91 const int status = unlink(pathname);
92#else
93 const int status = _unlink(pathname);
94#endif
95 if (status != 0) {
96 tprintf("ERROR: Unable to delete file '%s$: %s\n", pathname, strerror(errno));
97 return false;
98 }
99 return true;
100}
void tprintf(const char *format,...)
Definition: tprintf.cpp:41

◆ DeleteMatchingFiles()

bool tesseract::File::DeleteMatchingFiles ( const char *  pattern)
static

Definition at line 117 of file fileio.cpp.

117 {
118 glob_t pglob;
119 char **paths;
120 bool all_deleted = true;
121 if (glob(pattern, 0, nullptr, &pglob) == 0) {
122 for (paths = pglob.gl_pathv; *paths != nullptr; paths++) {
123 all_deleted &= File::Delete(*paths);
124 }
125 globfree(&pglob);
126 }
127 return all_deleted;
128}
static bool Delete(const char *pathname)
Definition: fileio.cpp:89

◆ JoinPath()

std::string tesseract::File::JoinPath ( const std::string &  prefix,
const std::string &  suffix 
)
static

Definition at line 84 of file fileio.cpp.

84 {
85 return (prefix.empty() || prefix[prefix.size() - 1] == '/') ? prefix + suffix
86 : prefix + "/" + suffix;
87}

◆ Open()

FILE * tesseract::File::Open ( const std::string &  filename,
const std::string &  mode 
)
static

Definition at line 41 of file fileio.cpp.

41 {
42 return fopen(filename.c_str(), mode.c_str());
43}

◆ OpenOrDie()

FILE * tesseract::File::OpenOrDie ( const std::string &  filename,
const std::string &  mode 
)
static

Definition at line 45 of file fileio.cpp.

45 {
46 FILE *stream = fopen(filename.c_str(), mode.c_str());
47 if (stream == nullptr) {
48 tprintf("Unable to open '%s' in mode '%s': %s\n", filename.c_str(), mode.c_str(),
49 strerror(errno));
50 }
51 return stream;
52}

◆ Readable()

bool tesseract::File::Readable ( const std::string &  filename)
static

Definition at line 64 of file fileio.cpp.

64 {
65 FILE *stream = fopen(filename.c_str(), "rb");
66 if (stream == nullptr) {
67 return false;
68 }
69 fclose(stream);
70 return true;
71}

◆ ReadFileToString()

bool tesseract::File::ReadFileToString ( const std::string &  filename,
std::string *  out 
)
static

Definition at line 73 of file fileio.cpp.

73 {
74 FILE *stream = File::Open(filename.c_str(), "rb");
75 if (stream == nullptr) {
76 return false;
77 }
78 InputBuffer in(stream);
79 *out = "";
80 in.Read(out);
81 return in.CloseFile();
82}
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:41

◆ WriteStringToFileOrDie()

void tesseract::File::WriteStringToFileOrDie ( const std::string &  str,
const std::string &  filename 
)
static

Definition at line 54 of file fileio.cpp.

54 {
55 FILE *stream = fopen(filename.c_str(), "wb");
56 if (stream == nullptr) {
57 tprintf("Unable to open '%s' for writing: %s\n", filename.c_str(), strerror(errno));
58 return;
59 }
60 fputs(str.c_str(), stream);
61 ASSERT_HOST(fclose(stream) == 0);
62}
#define ASSERT_HOST(x)
Definition: errcode.h:54

The documentation for this class was generated from the following files: