All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 void File::ReadFileToStringOrDie(const string& filename, string* out) {
84  ASSERT_HOST_MSG(ReadFileToString(filename, out),
85  "Failed to read file: %s\n", filename.c_str());
86 }
87 
88 
89 string File::JoinPath(const string& prefix, const string& suffix) {
90  return (!prefix.size() || prefix[prefix.size() - 1] == '/') ?
91  prefix + suffix : prefix + "/" + suffix;
92 }
93 
94 bool File::Delete(const char* pathname) {
95  const int status = unlink(pathname);
96  if (status != 0) {
97  tprintf("ERROR: Unable to delete file %s\n", pathname);
98  return false;
99  }
100  return true;
101 }
102 
103 #ifdef _WIN32
104 bool File::DeleteMatchingFiles(const char* pattern) {
105  WIN32_FIND_DATA data;
106  BOOL result = TRUE;
107  HANDLE handle = FindFirstFile(pattern, &data);
108  bool all_deleted = true;
109  if (handle != INVALID_HANDLE_VALUE) {
110  for (; result; result = FindNextFile(handle, &data)) {
111  all_deleted &= File::Delete(data.cFileName);
112  }
113  FindClose(handle);
114  }
115  return all_deleted;
116 }
117 #else
118 bool File::DeleteMatchingFiles(const char* pattern) {
119  glob_t pglob;
120  char **paths;
121  bool all_deleted = true;
122  if (glob(pattern, 0, NULL, &pglob) == 0) {
123  for (paths = pglob.gl_pathv; *paths != NULL; paths++) {
124  all_deleted &= File::Delete(*paths);
125  }
126  globfree(&pglob);
127  }
128  return all_deleted;
129 }
130 #endif
131 
133 // InputBuffer::
136  : stream_(stream) {
137  fseek(stream_, 0, SEEK_END);
138  filesize_ = ftell(stream_);
139  fseek(stream_, 0, SEEK_SET);
140 }
141 
142 InputBuffer::InputBuffer(FILE* stream, size_t)
143  : stream_(stream) {
144  fseek(stream_, 0, SEEK_END);
145  filesize_ = ftell(stream_);
146  fseek(stream_, 0, SEEK_SET);
147 }
148 
150  if (stream_ != NULL) {
151  fclose(stream_);
152  }
153 }
154 
155 bool InputBuffer::Read(string* out) {
156  char buf[BUFSIZ + 1];
157  int l;
158  while ((l = fread(buf, 1, BUFSIZ, stream_)) > 0) {
159  if (ferror(stream_)) {
160  clearerr(stream_);
161  return false;
162  }
163  buf[l] = 0;
164  out->append(buf);
165  }
166  return true;
167 }
168 
170  int ret = fclose(stream_);
171  stream_ = NULL;
172  return ret == 0;
173 }
174 
176 // OutputBuffer::
178 
180  : stream_(stream) {
181 }
182 
183 OutputBuffer::OutputBuffer(FILE* stream, size_t)
184  : stream_(stream) {
185 }
186 
188  if (stream_ != NULL) {
189  fclose(stream_);
190  }
191 }
192 
193 void OutputBuffer::WriteString(const string& str) {
194  fputs(str.c_str(), stream_);
195 }
196 
198  int ret = fclose(stream_);
199  stream_ = NULL;
200  return ret == 0;
201 }
202 
203 } // namespace tesseract
static bool ReadFileToString(const string &filename, string *out)
Definition: fileio.cpp:73
#define BOOL
Definition: capi.h:27
void WriteString(const string &str)
Definition: fileio.cpp:193
#define tprintf(...)
Definition: tprintf.h:31
static bool Readable(const string &filename)
Definition: fileio.cpp:64
static string JoinPath(const string &prefix, const string &suffix)
Definition: fileio.cpp:89
static bool Delete(const char *pathname)
Definition: fileio.cpp:94
static FILE * OpenOrDie(const string &filename, const string &mode)
Definition: fileio.cpp:43
CMD_EVENTS mode
Definition: pgedit.cpp:116
static void WriteStringToFileOrDie(const string &str, const string &filename)
Definition: fileio.cpp:53
#define ASSERT_HOST_MSG(x, msg...)
Definition: errcode.h:98
#define ASSERT_HOST(x)
Definition: errcode.h:84
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:118
static FILE * Open(const string &filename, const string &mode)
Definition: fileio.cpp:39
bool Read(string *out)
Definition: fileio.cpp:155
OutputBuffer(FILE *stream)
Definition: fileio.cpp:179
#define TRUE
Definition: capi.h:28
#define NULL
Definition: host.h:144
static void ReadFileToStringOrDie(const string &filename, string *out)
Definition: fileio.cpp:83
InputBuffer(FILE *stream)
Definition: fileio.cpp:135