tesseract  4.0.0-1-g2a2b
tesseract::File Class Reference

#include <fileio.h>

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 29 of file fileio.h.

Member Function Documentation

◆ Delete()

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

Definition at line 88 of file fileio.cpp.

88  {
89  const int status = unlink(pathname);
90  if (status != 0) {
91  tprintf("ERROR: Unable to delete file %s\n", pathname);
92  return false;
93  }
94  return true;
95 }
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37

◆ DeleteMatchingFiles()

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

Definition at line 112 of file fileio.cpp.

112  {
113  glob_t pglob;
114  char **paths;
115  bool all_deleted = true;
116  if (glob(pattern, 0, nullptr, &pglob) == 0) {
117  for (paths = pglob.gl_pathv; *paths != nullptr; paths++) {
118  all_deleted &= File::Delete(*paths);
119  }
120  globfree(&pglob);
121  }
122  return all_deleted;
123 }
static bool Delete(const char *pathname)
Definition: fileio.cpp:88

◆ JoinPath()

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

Definition at line 82 of file fileio.cpp.

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

◆ Open()

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

Definition at line 39 of file fileio.cpp.

39  {
40  return fopen(filename.c_str(), mode.c_str());
41 }

◆ OpenOrDie()

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

Definition at line 43 of file fileio.cpp.

44  {
45  FILE* stream = fopen(filename.c_str(), mode.c_str());
46  if (stream == nullptr) {
47  tprintf("Unable to open '%s' in mode '%s'\n", filename.c_str(),
48  mode.c_str());
49  }
50  return stream;
51 }
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37

◆ 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) return false;
76  InputBuffer in(stream);
77  *out = "";
78  in.Read(out);
79  return in.CloseFile();
80 }
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:39

◆ WriteStringToFileOrDie()

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

Definition at line 53 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\n", filename.c_str());
58  return;
59  }
60  fputs(str.c_str(), stream);
61  ASSERT_HOST(fclose(stream) == 0);
62 }
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
#define ASSERT_HOST(x)
Definition: errcode.h:84

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