tesseract  5.0.0-alpha-619-ge9db
fileio.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: fileio.cpp
3  * Description: File I/O utilities.
4  * Author: Samuel Charron
5  *
6  * (C) Copyright 2013, Google Inc.
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy
9  * of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
10  * by applicable law or agreed to in writing, software distributed under the
11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
12  * OF ANY KIND, either express or implied. See the License for the specific
13  * language governing permissions and limitations under the License.
14  *
15  **********************************************************************/
16 
17 #ifdef _WIN32
18 #ifndef unlink
19 #include <io.h>
20 #endif
21 #else
22 #include <glob.h>
23 #include <unistd.h>
24 #endif
25 
26 #include <cstdlib>
27 #include <cstdio>
28 #include <string>
29 #include <cerrno>
30 
31 #include "errcode.h"
32 #include "fileio.h"
33 #include "host.h" // includes windows.h for BOOL, ...
34 #include "tprintf.h"
35 
36 
37 namespace tesseract {
38 
40 // File::
42 FILE* File::Open(const std::string& filename, const std::string& mode) {
43  return fopen(filename.c_str(), mode.c_str());
44 }
45 
46 FILE* File::OpenOrDie(const std::string& filename,
47  const std::string& mode) {
48  FILE* stream = fopen(filename.c_str(), mode.c_str());
49  if (stream == nullptr) {
50  tprintf("Unable to open '%s' in mode '%s': %s\n", filename.c_str(),
51  mode.c_str(), strerror(errno));
52  }
53  return stream;
54 }
55 
57  const std::string& filename) {
58  FILE* stream = fopen(filename.c_str(), "wb");
59  if (stream == nullptr) {
60  tprintf("Unable to open '%s' for writing: %s\n",
61  filename.c_str(), strerror(errno));
62  return;
63  }
64  fputs(str.c_str(), stream);
65  ASSERT_HOST(fclose(stream) == 0);
66 }
67 
68 bool File::Readable(const std::string& filename) {
69  FILE* stream = fopen(filename.c_str(), "rb");
70  if (stream == nullptr) {
71  return false;
72  }
73  fclose(stream);
74  return true;
75 }
76 
77 bool File::ReadFileToString(const std::string& filename, std::string* out) {
78  FILE* stream = File::Open(filename.c_str(), "rb");
79  if (stream == nullptr) return false;
80  InputBuffer in(stream);
81  *out = "";
82  in.Read(out);
83  return in.CloseFile();
84 }
85 
86 std::string File::JoinPath(const std::string& prefix, const std::string& suffix) {
87  return (prefix.empty() || prefix[prefix.size() - 1] == '/')
88  ? prefix + suffix
89  : prefix + "/" + suffix;
90 }
91 
92 bool File::Delete(const char* pathname) {
93 #if !defined(_WIN32) || defined(__MINGW32__)
94  const int status = unlink(pathname);
95 #else
96  const int status = _unlink(pathname);
97 #endif
98  if (status != 0) {
99  tprintf("ERROR: Unable to delete file '%s$: %s\n", pathname,
100  strerror(errno));
101  return false;
102  }
103  return true;
104 }
105 
106 #ifdef _WIN32
107 bool File::DeleteMatchingFiles(const char* pattern) {
108  WIN32_FIND_DATA data;
109  BOOL result = TRUE;
110  HANDLE handle = FindFirstFile(pattern, &data);
111  bool all_deleted = true;
112  if (handle != INVALID_HANDLE_VALUE) {
113  for (; result; result = FindNextFile(handle, &data)) {
114  all_deleted &= File::Delete(data.cFileName);
115  }
116  FindClose(handle);
117  }
118  return all_deleted;
119 }
120 #else
121 bool File::DeleteMatchingFiles(const char* pattern) {
122  glob_t pglob;
123  char **paths;
124  bool all_deleted = true;
125  if (glob(pattern, 0, nullptr, &pglob) == 0) {
126  for (paths = pglob.gl_pathv; *paths != nullptr; paths++) {
127  all_deleted &= File::Delete(*paths);
128  }
129  globfree(&pglob);
130  }
131  return all_deleted;
132 }
133 #endif
134 
136 // InputBuffer::
138 InputBuffer::InputBuffer(FILE* stream)
139  : stream_(stream) {
140 }
141 
142 InputBuffer::InputBuffer(FILE* stream, size_t)
143  : stream_(stream) {
144 }
145 
147  if (stream_ != nullptr) {
148  fclose(stream_);
149  }
150 }
151 
152 bool InputBuffer::Read(std::string* out) {
153  char buf[BUFSIZ + 1];
154  int l;
155  while ((l = fread(buf, 1, BUFSIZ, stream_)) > 0) {
156  if (ferror(stream_)) {
157  clearerr(stream_);
158  return false;
159  }
160  buf[l] = 0;
161  out->append(buf);
162  }
163  return true;
164 }
165 
166 bool InputBuffer::CloseFile() {
167  int ret = fclose(stream_);
168  stream_ = nullptr;
169  return ret == 0;
170 }
171 
173 // OutputBuffer::
175 
176 OutputBuffer::OutputBuffer(FILE* stream)
177  : stream_(stream) {
178 }
179 
180 OutputBuffer::OutputBuffer(FILE* stream, size_t)
181  : stream_(stream) {
182 }
183 
185  if (stream_ != nullptr) {
186  fclose(stream_);
187  }
188 }
189 
190 void OutputBuffer::WriteString(const std::string& str) {
191  fputs(str.c_str(), stream_);
192 }
193 
195  int ret = fclose(stream_);
196  stream_ = nullptr;
197  return ret == 0;
198 }
199 
200 } // namespace tesseract
string
std::string string
Definition: equationdetect_test.cc:21
tesseract::OutputBuffer::WriteString
void WriteString(const std::string &str)
Definition: fileio.cpp:202
tesseract::File::Delete
static bool Delete(const char *pathname)
Definition: fileio.cpp:104
tesseract::InputBuffer::InputBuffer
InputBuffer(FILE *stream)
Definition: fileio.cpp:150
host.h
tesseract::File::Readable
static bool Readable(const std::string &filename)
Definition: fileio.cpp:80
tesseract::File::Open
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:54
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
tesseract::InputBuffer
Definition: fileio.h:81
tesseract::InputBuffer::CloseFile
bool CloseFile()
Definition: fileio.cpp:178
tesseract::File::JoinPath
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
Definition: fileio.cpp:98
fileio.h
tesseract::File::OpenOrDie
static FILE * OpenOrDie(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:58
BOOL
#define BOOL
Definition: capi.h:43
tesseract::OutputBuffer::~OutputBuffer
~OutputBuffer()
Definition: fileio.cpp:196
tesseract::File::WriteStringToFileOrDie
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
Definition: fileio.cpp:68
tesseract
Definition: baseapi.h:65
tprintf.h
tesseract::File::ReadFileToString
static bool ReadFileToString(const std::string &filename, std::string *out)
Definition: fileio.cpp:89
tesseract::OutputBuffer::CloseFile
bool CloseFile()
Definition: fileio.cpp:206
TRUE
#define TRUE
Definition: capi.h:44
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
errcode.h
tesseract::InputBuffer::~InputBuffer
~InputBuffer()
Definition: fileio.cpp:158
tesseract::File::DeleteMatchingFiles
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:133
tesseract::OutputBuffer::OutputBuffer
OutputBuffer(FILE *stream)
Definition: fileio.cpp:188
tesseract::InputBuffer::Read
bool Read(std::string *out)
Definition: fileio.cpp:164