tesseract  4.00.00dev
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 <stdlib.h>
27 #include <cstdio>
28 #include <string>
29 
30 #include "fileio.h"
31 #include "tprintf.h"
32 
33 namespace tesseract {
34 
36 // File::
38 FILE* File::Open(const string& filename, const string& mode) {
39  return fopen(filename.c_str(), mode.c_str());
40 }
41 
42 FILE* File::OpenOrDie(const string& filename,
43  const string& mode) {
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 }
51 
52 void File::WriteStringToFileOrDie(const string& str,
53  const string& filename) {
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 }
62 
63 bool File::Readable(const string& filename) {
64  FILE* stream = fopen(filename.c_str(), "rb");
65  if (stream == nullptr) {
66  return false;
67  }
68  fclose(stream);
69  return true;
70 }
71 
72 bool File::ReadFileToString(const string& filename, string* out) {
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 }
80 
81 string File::JoinPath(const string& prefix, const string& suffix) {
82  return (prefix.empty() || prefix[prefix.size() - 1] == '/')
83  ? prefix + suffix
84  : prefix + "/" + suffix;
85 }
86 
87 bool File::Delete(const char* pathname) {
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 }
95 
96 #ifdef _WIN32
97 bool File::DeleteMatchingFiles(const char* pattern) {
98  WIN32_FIND_DATA data;
99  BOOL result = TRUE;
100  HANDLE handle = FindFirstFile(pattern, &data);
101  bool all_deleted = true;
102  if (handle != INVALID_HANDLE_VALUE) {
103  for (; result; result = FindNextFile(handle, &data)) {
104  all_deleted &= File::Delete(data.cFileName);
105  }
106  FindClose(handle);
107  }
108  return all_deleted;
109 }
110 #else
111 bool File::DeleteMatchingFiles(const char* pattern) {
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 }
123 #endif
124 
126 // InputBuffer::
129  : stream_(stream) {
130  fseek(stream_, 0, SEEK_END);
131  filesize_ = ftell(stream_);
132  fseek(stream_, 0, SEEK_SET);
133 }
134 
136  : stream_(stream) {
137  fseek(stream_, 0, SEEK_END);
138  filesize_ = ftell(stream_);
139  fseek(stream_, 0, SEEK_SET);
140 }
141 
143  if (stream_ != nullptr) {
144  fclose(stream_);
145  }
146 }
147 
148 bool InputBuffer::Read(string* out) {
149  char buf[BUFSIZ + 1];
150  int l;
151  while ((l = fread(buf, 1, BUFSIZ, stream_)) > 0) {
152  if (ferror(stream_)) {
153  clearerr(stream_);
154  return false;
155  }
156  buf[l] = 0;
157  out->append(buf);
158  }
159  return true;
160 }
161 
163  int ret = fclose(stream_);
164  stream_ = nullptr;
165  return ret == 0;
166 }
167 
169 // OutputBuffer::
171 
173  : stream_(stream) {
174 }
175 
177  : stream_(stream) {
178 }
179 
181  if (stream_ != nullptr) {
182  fclose(stream_);
183  }
184 }
185 
186 void OutputBuffer::WriteString(const string& str) {
187  fputs(str.c_str(), stream_);
188 }
189 
191  int ret = fclose(stream_);
192  stream_ = nullptr;
193  return ret == 0;
194 }
195 
196 } // namespace tesseract
static void WriteStringToFileOrDie(const string &str, const string &filename)
Definition: fileio.cpp:52
OutputBuffer(FILE *stream)
Definition: fileio.cpp:172
#define TRUE
Definition: capi.h:45
voidpf stream
Definition: ioapi.h:39
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:111
static FILE * Open(const string &filename, const string &mode)
Definition: fileio.cpp:38
#define tprintf(...)
Definition: tprintf.h:31
#define BOOL
Definition: capi.h:44
#define SEEK_SET
Definition: ioapi.c:29
static string JoinPath(const string &prefix, const string &suffix)
Definition: fileio.cpp:81
#define ASSERT_HOST(x)
Definition: errcode.h:84
static FILE * OpenOrDie(const string &filename, const string &mode)
Definition: fileio.cpp:42
static bool Readable(const string &filename)
Definition: fileio.cpp:63
void WriteString(const string &str)
Definition: fileio.cpp:186
bool Read(string *out)
Definition: fileio.cpp:148
const char int mode
Definition: ioapi.h:38
static bool Delete(const char *pathname)
Definition: fileio.cpp:87
InputBuffer(FILE *stream)
Definition: fileio.cpp:128
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:17
const char * filename
Definition: ioapi.h:38
voidpf void * buf
Definition: ioapi.h:39
#define SEEK_END
Definition: ioapi.c:25
static bool ReadFileToString(const string &filename, string *out)
Definition: fileio.cpp:72