tesseract  3.05.02
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 #include <windows.h>
19 #ifndef unlink
20 #include <io.h>
21 #endif
22 #else
23 #include <glob.h>
24 #include <unistd.h>
25 #endif
26 
27 #include <stdlib.h>
28 #include <cstdio>
29 #include <string>
30 
31 #include "fileio.h"
32 #include "tprintf.h"
33 
34 namespace tesseract {
35 
37 // File::
39 FILE* File::Open(const string& filename, const string& mode) {
40  return fopen(filename.c_str(), mode.c_str());
41 }
42 
43 FILE* File::OpenOrDie(const string& filename,
44  const string& mode) {
45  FILE* stream = fopen(filename.c_str(), mode.c_str());
46  if (stream == NULL) {
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 string& str,
54  const string& filename) {
55  FILE* stream = fopen(filename.c_str(), "wb");
56  if (stream == NULL) {
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 string& filename) {
65  FILE* stream = fopen(filename.c_str(), "rb");
66  if (stream == NULL) {
67  return false;
68  }
69  fclose(stream);
70  return true;
71 }
72 
73 bool File::ReadFileToString(const string& filename, string* out) {
74  FILE* stream = File::Open(filename.c_str(), "rb");
75  if (stream == NULL)
76  return false;
77  InputBuffer in(stream);
78  *out = "";
79  in.Read(out);
80  return in.CloseFile();
81 }
82 
83 string File::JoinPath(const string& prefix, const string& suffix) {
84  return (prefix.empty() || prefix[prefix.size() - 1] == '/')
85  ? prefix + suffix
86  : prefix + "/" + suffix;
87 }
88 
89 bool File::Delete(const char* pathname) {
90  const int status = unlink(pathname);
91  if (status != 0) {
92  tprintf("ERROR: Unable to delete file %s\n", pathname);
93  return false;
94  }
95  return true;
96 }
97 
98 #ifdef _WIN32
99 bool File::DeleteMatchingFiles(const char* pattern) {
100  WIN32_FIND_DATA data;
101  BOOL result = TRUE;
102  HANDLE handle = FindFirstFile(pattern, &data);
103  bool all_deleted = true;
104  if (handle != INVALID_HANDLE_VALUE) {
105  for (; result; result = FindNextFile(handle, &data)) {
106  all_deleted &= File::Delete(data.cFileName);
107  }
108  FindClose(handle);
109  }
110  return all_deleted;
111 }
112 #else
113 bool File::DeleteMatchingFiles(const char* pattern) {
114  glob_t pglob;
115  char **paths;
116  bool all_deleted = true;
117  if (glob(pattern, 0, NULL, &pglob) == 0) {
118  for (paths = pglob.gl_pathv; *paths != NULL; paths++) {
119  all_deleted &= File::Delete(*paths);
120  }
121  globfree(&pglob);
122  }
123  return all_deleted;
124 }
125 #endif
126 
128 // InputBuffer::
131  : stream_(stream) {
132  fseek(stream_, 0, SEEK_END);
133  filesize_ = ftell(stream_);
134  fseek(stream_, 0, SEEK_SET);
135 }
136 
137 InputBuffer::InputBuffer(FILE* stream, size_t)
138  : stream_(stream) {
139  fseek(stream_, 0, SEEK_END);
140  filesize_ = ftell(stream_);
141  fseek(stream_, 0, SEEK_SET);
142 }
143 
145  if (stream_ != NULL) {
146  fclose(stream_);
147  }
148 }
149 
150 bool InputBuffer::Read(string* out) {
151  char buf[BUFSIZ + 1];
152  int l;
153  while ((l = fread(buf, 1, BUFSIZ, stream_)) > 0) {
154  if (ferror(stream_)) {
155  clearerr(stream_);
156  return false;
157  }
158  buf[l] = 0;
159  out->append(buf);
160  }
161  return true;
162 }
163 
165  int ret = fclose(stream_);
166  stream_ = NULL;
167  return ret == 0;
168 }
169 
171 // OutputBuffer::
173 
175  : stream_(stream) {
176 }
177 
178 OutputBuffer::OutputBuffer(FILE* stream, size_t)
179  : stream_(stream) {
180 }
181 
183  if (stream_ != NULL) {
184  fclose(stream_);
185  }
186 }
187 
188 void OutputBuffer::WriteString(const string& str) {
189  fputs(str.c_str(), stream_);
190 }
191 
193  int ret = fclose(stream_);
194  stream_ = NULL;
195  return ret == 0;
196 }
197 
198 } // namespace tesseract
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:113
#define TRUE
Definition: capi.h:45
#define BOOL
Definition: capi.h:44
OutputBuffer(FILE *stream)
Definition: fileio.cpp:174
static bool Readable(const string &filename)
Definition: fileio.cpp:64
static FILE * OpenOrDie(const string &filename, const string &mode)
Definition: fileio.cpp:43
CMD_EVENTS mode
Definition: pgedit.cpp:116
InputBuffer(FILE *stream)
Definition: fileio.cpp:130
static FILE * Open(const string &filename, const string &mode)
Definition: fileio.cpp:39
#define tprintf(...)
Definition: tprintf.h:31
static bool ReadFileToString(const string &filename, string *out)
Definition: fileio.cpp:73
void WriteString(const string &str)
Definition: fileio.cpp:188
static string JoinPath(const string &prefix, const string &suffix)
Definition: fileio.cpp:83
static bool Delete(const char *pathname)
Definition: fileio.cpp:89
bool Read(string *out)
Definition: fileio.cpp:150
#define ASSERT_HOST(x)
Definition: errcode.h:84
static void WriteStringToFileOrDie(const string &str, const string &filename)
Definition: fileio.cpp:53