tesseract 4.1.1
Loading...
Searching...
No Matches
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
37namespace tesseract {
38
40// File::
42FILE* File::Open(const std::string& filename, const std::string& mode) {
43 return fopen(filename.c_str(), mode.c_str());
44}
45
46FILE* 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
56void File::WriteStringToFileOrDie(const std::string& str,
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
68bool 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
77bool 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
86std::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
92bool 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
107bool 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
121bool 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::
139 : stream_(stream) {
140}
141
142InputBuffer::InputBuffer(FILE* stream, size_t)
143 : stream_(stream) {
144}
145
147 if (stream_ != nullptr) {
148 fclose(stream_);
149 }
150}
151
152bool 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
167 int ret = fclose(stream_);
168 stream_ = nullptr;
169 return ret == 0;
170}
171
173// OutputBuffer::
175
177 : stream_(stream) {
178}
179
180OutputBuffer::OutputBuffer(FILE* stream, size_t)
181 : stream_(stream) {
182}
183
185 if (stream_ != nullptr) {
186 fclose(stream_);
187 }
188}
189
190void 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
#define TRUE
Definition: capi.h:51
#define BOOL
Definition: capi.h:50
#define ASSERT_HOST(x)
Definition: errcode.h:88
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:35
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:121
static FILE * OpenOrDie(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:46
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
Definition: fileio.cpp:86
static bool Delete(const char *pathname)
Definition: fileio.cpp:92
static bool Readable(const std::string &filename)
Definition: fileio.cpp:68
static bool ReadFileToString(const std::string &filename, std::string *out)
Definition: fileio.cpp:77
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
Definition: fileio.cpp:56
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:42
InputBuffer(FILE *stream)
Definition: fileio.cpp:138
bool Read(std::string *out)
Definition: fileio.cpp:152
void WriteString(const std::string &str)
Definition: fileio.cpp:190
OutputBuffer(FILE *stream)
Definition: fileio.cpp:176