tesseract  4.0.0-1-g2a2b
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  * Created: Tuesday, July 9, 2013
6  *
7  * (C) Copyright 2013, Google Inc.
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9  * use this file except in compliance with the License. You may obtain a copy
10  * of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
11  * by applicable law or agreed to in writing, software distributed under the
12  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
13  * OF ANY KIND, either express or implied. See the License for the specific
14  * language governing permissions and limitations under the License.
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 "tprintf.h"
33 
34 namespace tesseract {
35 
37 // File::
39 FILE* File::Open(const std::string& filename, const std::string& mode) {
40  return fopen(filename.c_str(), mode.c_str());
41 }
42 
43 FILE* File::OpenOrDie(const std::string& filename,
44  const std::string& mode) {
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 }
52 
53 void File::WriteStringToFileOrDie(const std::string& str,
54  const std::string& filename) {
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 }
63 
64 bool File::Readable(const std::string& filename) {
65  FILE* stream = fopen(filename.c_str(), "rb");
66  if (stream == nullptr) {
67  return false;
68  }
69  fclose(stream);
70  return true;
71 }
72 
73 bool File::ReadFileToString(const std::string& filename, std::string* out) {
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 }
81 
82 std::string File::JoinPath(const std::string& prefix, const std::string& suffix) {
83  return (prefix.empty() || prefix[prefix.size() - 1] == '/')
84  ? prefix + suffix
85  : prefix + "/" + suffix;
86 }
87 
88 bool File::Delete(const char* pathname) {
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 }
96 
97 #ifdef _WIN32
98 bool File::DeleteMatchingFiles(const char* pattern) {
99  WIN32_FIND_DATA data;
100  BOOL result = TRUE;
101  HANDLE handle = FindFirstFile(pattern, &data);
102  bool all_deleted = true;
103  if (handle != INVALID_HANDLE_VALUE) {
104  for (; result; result = FindNextFile(handle, &data)) {
105  all_deleted &= File::Delete(data.cFileName);
106  }
107  FindClose(handle);
108  }
109  return all_deleted;
110 }
111 #else
112 bool File::DeleteMatchingFiles(const char* pattern) {
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 }
124 #endif
125 
127 // InputBuffer::
130  : stream_(stream) {
131  fseek(stream_, 0, SEEK_END);
132  filesize_ = ftell(stream_);
133  fseek(stream_, 0, SEEK_SET);
134 }
135 
136 InputBuffer::InputBuffer(FILE* stream, size_t)
137  : stream_(stream) {
138  fseek(stream_, 0, SEEK_END);
139  filesize_ = ftell(stream_);
140  fseek(stream_, 0, SEEK_SET);
141 }
142 
144  if (stream_ != nullptr) {
145  fclose(stream_);
146  }
147 }
148 
149 bool InputBuffer::Read(std::string* out) {
150  char buf[BUFSIZ + 1];
151  int l;
152  while ((l = fread(buf, 1, BUFSIZ, stream_)) > 0) {
153  if (ferror(stream_)) {
154  clearerr(stream_);
155  return false;
156  }
157  buf[l] = 0;
158  out->append(buf);
159  }
160  return true;
161 }
162 
164  int ret = fclose(stream_);
165  stream_ = nullptr;
166  return ret == 0;
167 }
168 
170 // OutputBuffer::
172 
174  : stream_(stream) {
175 }
176 
177 OutputBuffer::OutputBuffer(FILE* stream, size_t)
178  : stream_(stream) {
179 }
180 
182  if (stream_ != nullptr) {
183  fclose(stream_);
184  }
185 }
186 
187 void OutputBuffer::WriteString(const std::string& str) {
188  fputs(str.c_str(), stream_);
189 }
190 
192  int ret = fclose(stream_);
193  stream_ = nullptr;
194  return ret == 0;
195 }
196 
197 } // namespace tesseract
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:112
#define TRUE
Definition: capi.h:51
InputBuffer(FILE *stream)
Definition: fileio.cpp:129
static bool ReadFileToString(const std::string &filename, std::string *out)
Definition: fileio.cpp:73
#define BOOL
Definition: capi.h:50
static FILE * OpenOrDie(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:43
void WriteString(const std::string &str)
Definition: fileio.cpp:187
static bool Delete(const char *pathname)
Definition: fileio.cpp:88
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
Definition: fileio.cpp:53
static bool Readable(const std::string &filename)
Definition: fileio.cpp:64
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
Definition: fileio.cpp:82
bool Read(std::string *out)
Definition: fileio.cpp:149
OutputBuffer(FILE *stream)
Definition: fileio.cpp:173
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:39
#define ASSERT_HOST(x)
Definition: errcode.h:84