tesseract  4.00.00dev
tesseract::File Class Reference

#include <fileio.h>

Static Public Member Functions

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

Detailed Description

Definition at line 31 of file fileio.h.

Member Function Documentation

◆ Delete()

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

Definition at line 87 of file fileio.cpp.

87  {
88  const int status = unlink(pathname);
89  if (status != 0) {
90  tprintf("ERROR: Unable to delete file %s\n", pathname);
91  return false;
92  }
93  return true;
94 }
#define tprintf(...)
Definition: tprintf.h:31

◆ DeleteMatchingFiles()

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

Definition at line 111 of file fileio.cpp.

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

◆ JoinPath()

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

Definition at line 81 of file fileio.cpp.

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

◆ Open()

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

Definition at line 38 of file fileio.cpp.

38  {
39  return fopen(filename.c_str(), mode.c_str());
40 }
const char int mode
Definition: ioapi.h:38
const char * filename
Definition: ioapi.h:38

◆ OpenOrDie()

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

Definition at line 42 of file fileio.cpp.

43  {
44  FILE* stream = fopen(filename.c_str(), mode.c_str());
45  if (stream == nullptr) {
46  tprintf("Unable to open '%s' in mode '%s'\n", filename.c_str(),
47  mode.c_str());
48  }
49  return stream;
50 }
voidpf stream
Definition: ioapi.h:39
#define tprintf(...)
Definition: tprintf.h:31
const char int mode
Definition: ioapi.h:38
const char * filename
Definition: ioapi.h:38

◆ Readable()

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

Definition at line 63 of file fileio.cpp.

63  {
64  FILE* stream = fopen(filename.c_str(), "rb");
65  if (stream == nullptr) {
66  return false;
67  }
68  fclose(stream);
69  return true;
70 }
voidpf stream
Definition: ioapi.h:39
const char * filename
Definition: ioapi.h:38

◆ ReadFileToString()

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

Definition at line 72 of file fileio.cpp.

72  {
73  FILE* stream = File::Open(filename.c_str(), "rb");
74  if (stream == nullptr) return false;
75  InputBuffer in(stream);
76  *out = "";
77  in.Read(out);
78  return in.CloseFile();
79 }
voidpf stream
Definition: ioapi.h:39
static FILE * Open(const string &filename, const string &mode)
Definition: fileio.cpp:38
const char * filename
Definition: ioapi.h:38

◆ WriteStringToFileOrDie()

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

Definition at line 52 of file fileio.cpp.

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

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