tesseract  4.1.0
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 
30 #include "errcode.h"
31 #include "fileio.h"
32 #include "host.h" // includes windows.h for BOOL, ...
33 #include "tprintf.h"
34 
35 namespace tesseract {
36 
38 // File::
40 FILE* File::Open(const std::string& filename, const std::string& mode) {
41  return fopen(filename.c_str(), mode.c_str());
42 }
43 
44 FILE* File::OpenOrDie(const std::string& filename,
45  const std::string& mode) {
46  FILE* stream = fopen(filename.c_str(), mode.c_str());
47  if (stream == nullptr) {
48  tprintf("Unable to open '%s' in mode '%s'\n", filename.c_str(),
49  mode.c_str());
50  }
51  return stream;
52 }
53 
54 void File::WriteStringToFileOrDie(const std::string& str,
55  const std::string& filename) {
56  FILE* stream = fopen(filename.c_str(), "wb");
57  if (stream == nullptr) {
58  tprintf("Unable to open '%s' for writing\n", filename.c_str());
59  return;
60  }
61  fputs(str.c_str(), stream);
62  ASSERT_HOST(fclose(stream) == 0);
63 }
64 
65 bool File::Readable(const std::string& filename) {
66  FILE* stream = fopen(filename.c_str(), "rb");
67  if (stream == nullptr) {
68  return false;
69  }
70  fclose(stream);
71  return true;
72 }
73 
74 bool File::ReadFileToString(const std::string& filename, std::string* out) {
75  FILE* stream = File::Open(filename.c_str(), "rb");
76  if (stream == nullptr) return false;
77  InputBuffer in(stream);
78  *out = "";
79  in.Read(out);
80  return in.CloseFile();
81 }
82 
83 std::string File::JoinPath(const std::string& prefix, const std::string& suffix) {
84  return (prefix.empty() || prefix[prefix.size() - 1] == '/')
85  ? prefix + suffix
86  : prefix + "/" + suffix;
87 }
88 
89 bool File::Delete(const char* pathname) {
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\n", pathname);
97  return false;
98  }
99  return true;
100 }
101 
102 #ifdef _WIN32
103 bool File::DeleteMatchingFiles(const char* pattern) {
104  WIN32_FIND_DATA data;
105  BOOL result = TRUE;
106  HANDLE handle = FindFirstFile(pattern, &data);
107  bool all_deleted = true;
108  if (handle != INVALID_HANDLE_VALUE) {
109  for (; result; result = FindNextFile(handle, &data)) {
110  all_deleted &= File::Delete(data.cFileName);
111  }
112  FindClose(handle);
113  }
114  return all_deleted;
115 }
116 #else
117 bool File::DeleteMatchingFiles(const char* pattern) {
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 }
129 #endif
130 
132 // InputBuffer::
135  : stream_(stream) {
136  fseek(stream_, 0, SEEK_END);
137  filesize_ = ftell(stream_);
138  fseek(stream_, 0, SEEK_SET);
139 }
140 
141 InputBuffer::InputBuffer(FILE* stream, size_t)
142  : stream_(stream) {
143  fseek(stream_, 0, SEEK_END);
144  filesize_ = ftell(stream_);
145  fseek(stream_, 0, SEEK_SET);
146 }
147 
149  if (stream_ != nullptr) {
150  fclose(stream_);
151  }
152 }
153 
154 bool InputBuffer::Read(std::string* out) {
155  char buf[BUFSIZ + 1];
156  int l;
157  while ((l = fread(buf, 1, BUFSIZ, stream_)) > 0) {
158  if (ferror(stream_)) {
159  clearerr(stream_);
160  return false;
161  }
162  buf[l] = 0;
163  out->append(buf);
164  }
165  return true;
166 }
167 
169  int ret = fclose(stream_);
170  stream_ = nullptr;
171  return ret == 0;
172 }
173 
175 // OutputBuffer::
177 
179  : stream_(stream) {
180 }
181 
182 OutputBuffer::OutputBuffer(FILE* stream, size_t)
183  : stream_(stream) {
184 }
185 
187  if (stream_ != nullptr) {
188  fclose(stream_);
189  }
190 }
191 
192 void OutputBuffer::WriteString(const std::string& str) {
193  fputs(str.c_str(), stream_);
194 }
195 
197  int ret = fclose(stream_);
198  stream_ = nullptr;
199  return ret == 0;
200 }
201 
202 } // namespace tesseract
OutputBuffer(FILE *stream)
Definition: fileio.cpp:178
static bool Readable(const std::string &filename)
Definition: fileio.cpp:65
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
Definition: fileio.cpp:54
void WriteString(const std::string &str)
Definition: fileio.cpp:192
bool Read(std::string *out)
Definition: fileio.cpp:154
InputBuffer(FILE *stream)
Definition: fileio.cpp:134
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:40
static FILE * OpenOrDie(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:44
static bool ReadFileToString(const std::string &filename, std::string *out)
Definition: fileio.cpp:74
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
static bool Delete(const char *pathname)
Definition: fileio.cpp:89
#define ASSERT_HOST(x)
Definition: errcode.h:88
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:117
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
Definition: fileio.cpp:83
#define TRUE
Definition: capi.h:51
#define BOOL
Definition: capi.h:50