comparison mupdf-source/thirdparty/tesseract/src/training/unicharset/fileio.cpp @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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
36 namespace tesseract {
37
38 ///////////////////////////////////////////////////////////////////////////////
39 // File::
40 ///////////////////////////////////////////////////////////////////////////////
41 FILE *File::Open(const std::string &filename, const std::string &mode) {
42 return fopen(filename.c_str(), mode.c_str());
43 }
44
45 FILE *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
54 void 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
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, "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
84 std::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
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$: %s\n", pathname, strerror(errno));
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
131 ///////////////////////////////////////////////////////////////////////////////
132 // InputBuffer::
133 ///////////////////////////////////////////////////////////////////////////////
134 InputBuffer::InputBuffer(FILE *stream) : stream_(stream) {}
135
136 InputBuffer::InputBuffer(FILE *stream, size_t) : stream_(stream) {}
137
138 InputBuffer::~InputBuffer() {
139 if (stream_ != nullptr) {
140 fclose(stream_);
141 }
142 }
143
144 bool 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
158 bool InputBuffer::CloseFile() {
159 int ret = fclose(stream_);
160 stream_ = nullptr;
161 return ret == 0;
162 }
163
164 ///////////////////////////////////////////////////////////////////////////////
165 // OutputBuffer::
166 ///////////////////////////////////////////////////////////////////////////////
167
168 OutputBuffer::OutputBuffer(FILE *stream) : stream_(stream) {}
169
170 OutputBuffer::OutputBuffer(FILE *stream, size_t) : stream_(stream) {}
171
172 OutputBuffer::~OutputBuffer() {
173 if (stream_ != nullptr) {
174 fclose(stream_);
175 }
176 }
177
178 void OutputBuffer::WriteString(const std::string &str) {
179 fputs(str.c_str(), stream_);
180 }
181
182 bool OutputBuffer::CloseFile() {
183 int ret = fclose(stream_);
184 stream_ = nullptr;
185 return ret == 0;
186 }
187
188 } // namespace tesseract