tesseract v5.3.3.20231005
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 <cerrno>
27#include <cstdio>
28#include <cstdlib>
29#include <string>
30
31#include "errcode.h"
32#include "fileio.h"
33#include "host.h" // includes windows.h for BOOL, ...
34#include "tprintf.h"
35
36namespace tesseract {
37
39// File::
41FILE *File::Open(const std::string &filename, const std::string &mode) {
42 return fopen(filename.c_str(), mode.c_str());
43}
44
45FILE *File::OpenOrDie(const std::string &filename, 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': %s\n", filename.c_str(), mode.c_str(),
49 strerror(errno));
50 }
51 return stream;
52}
53
54void File::WriteStringToFileOrDie(const std::string &str, const std::string &filename) {
55 FILE *stream = fopen(filename.c_str(), "wb");
56 if (stream == nullptr) {
57 tprintf("Unable to open '%s' for writing: %s\n", filename.c_str(), strerror(errno));
58 return;
59 }
60 fputs(str.c_str(), stream);
61 ASSERT_HOST(fclose(stream) == 0);
62}
63
64bool 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
73bool File::ReadFileToString(const std::string &filename, std::string *out) {
74 FILE *stream = File::Open(filename.c_str(), "rb");
75 if (stream == nullptr) {
76 return false;
77 }
78 InputBuffer in(stream);
79 *out = "";
80 in.Read(out);
81 return in.CloseFile();
82}
83
84std::string File::JoinPath(const std::string &prefix, const std::string &suffix) {
85 return (prefix.empty() || prefix[prefix.size() - 1] == '/') ? prefix + suffix
86 : prefix + "/" + suffix;
87}
88
89bool 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$: %s\n", pathname, strerror(errno));
97 return false;
98 }
99 return true;
100}
101
102#ifdef _WIN32
103bool 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
117bool 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::
134InputBuffer::InputBuffer(FILE *stream) : stream_(stream) {}
135
136InputBuffer::InputBuffer(FILE *stream, size_t) : stream_(stream) {}
137
139 if (stream_ != nullptr) {
140 fclose(stream_);
141 }
142}
143
144bool InputBuffer::Read(std::string *out) {
145 char buf[BUFSIZ + 1];
146 int l;
147 while ((l = fread(buf, 1, BUFSIZ, stream_)) > 0) {
148 if (ferror(stream_)) {
149 clearerr(stream_);
150 return false;
151 }
152 buf[l] = 0;
153 out->append(buf);
154 }
155 return true;
156}
157
159 int ret = fclose(stream_);
160 stream_ = nullptr;
161 return ret == 0;
162}
163
165// OutputBuffer::
167
168OutputBuffer::OutputBuffer(FILE *stream) : stream_(stream) {}
169
170OutputBuffer::OutputBuffer(FILE *stream, size_t) : stream_(stream) {}
171
173 if (stream_ != nullptr) {
174 fclose(stream_);
175 }
176}
177
178void OutputBuffer::WriteString(const std::string &str) {
179 fputs(str.c_str(), stream_);
180}
181
183 int ret = fclose(stream_);
184 stream_ = nullptr;
185 return ret == 0;
186}
187
188} // namespace tesseract
#define TRUE
Definition: capi.h:38
#define BOOL
Definition: capi.h:37
#define ASSERT_HOST(x)
Definition: errcode.h:54
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:117
static FILE * OpenOrDie(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:45
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
Definition: fileio.cpp:84
static bool Delete(const char *pathname)
Definition: fileio.cpp:89
static bool Readable(const std::string &filename)
Definition: fileio.cpp:64
static bool ReadFileToString(const std::string &filename, std::string *out)
Definition: fileio.cpp:73
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
Definition: fileio.cpp:54
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:41
InputBuffer(FILE *stream)
Definition: fileio.cpp:134
bool Read(std::string *out)
Definition: fileio.cpp:144
void WriteString(const std::string &str)
Definition: fileio.cpp:178
OutputBuffer(FILE *stream)
Definition: fileio.cpp:168