|
tesseract 3.04.01
|
00001 /********************************************************************** 00002 * File: fileio.cpp 00003 * Description: File I/O utilities. 00004 * Author: Samuel Charron 00005 * Created: Tuesday, July 9, 2013 00006 * 00007 * (C) Copyright 2013, Google Inc. 00008 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 00009 * use this file except in compliance with the License. You may obtain a copy 00010 * of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 00011 * by applicable law or agreed to in writing, software distributed under the 00012 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 00013 * OF ANY KIND, either express or implied. See the License for the specific 00014 * language governing permissions and limitations under the License. 00015 * 00016 **********************************************************************/ 00017 #ifdef _WIN32 00018 #include <windows.h> 00019 #ifndef unlink 00020 #include <io.h> 00021 #endif 00022 #else 00023 #include <glob.h> 00024 #include <unistd.h> 00025 #endif 00026 00027 #include <stdlib.h> 00028 #include <cstdio> 00029 #include <string> 00030 00031 #include "fileio.h" 00032 #include "tprintf.h" 00033 00034 namespace tesseract { 00035 00037 // File:: 00039 FILE* File::Open(const string& filename, const string& mode) { 00040 return fopen(filename.c_str(), mode.c_str()); 00041 } 00042 00043 FILE* File::OpenOrDie(const string& filename, 00044 const string& mode) { 00045 FILE* stream = fopen(filename.c_str(), mode.c_str()); 00046 if (stream == NULL) { 00047 tprintf("Unable to open '%s' in mode '%s'\n", filename.c_str(), 00048 mode.c_str()); 00049 } 00050 return stream; 00051 } 00052 00053 void File::WriteStringToFileOrDie(const string& str, 00054 const string& filename) { 00055 FILE* stream = fopen(filename.c_str(), "wb"); 00056 if (stream == NULL) { 00057 tprintf("Unable to open '%s' for writing\n", filename.c_str()); 00058 return; 00059 } 00060 fputs(str.c_str(), stream); 00061 ASSERT_HOST(fclose(stream) == 0); 00062 } 00063 00064 bool File::Readable(const string& filename) { 00065 FILE* stream = fopen(filename.c_str(), "rb"); 00066 if (stream == NULL) { 00067 return false; 00068 } 00069 fclose(stream); 00070 return true; 00071 } 00072 00073 bool File::ReadFileToString(const string& filename, string* out) { 00074 FILE* stream = File::Open(filename.c_str(), "rb"); 00075 if (stream == NULL) 00076 return false; 00077 InputBuffer in(stream); 00078 *out = ""; 00079 in.Read(out); 00080 return in.CloseFile(); 00081 } 00082 00083 void File::ReadFileToStringOrDie(const string& filename, string* out) { 00084 ASSERT_HOST_MSG(ReadFileToString(filename, out), 00085 "Failed to read file: %s\n", filename.c_str()); 00086 } 00087 00088 00089 string File::JoinPath(const string& prefix, const string& suffix) { 00090 return (!prefix.size() || prefix[prefix.size() - 1] == '/') ? 00091 prefix + suffix : prefix + "/" + suffix; 00092 } 00093 00094 bool File::Delete(const char* pathname) { 00095 const int status = unlink(pathname); 00096 if (status != 0) { 00097 tprintf("ERROR: Unable to delete file %s\n", pathname); 00098 return false; 00099 } 00100 return true; 00101 } 00102 00103 #ifdef _WIN32 00104 bool File::DeleteMatchingFiles(const char* pattern) { 00105 WIN32_FIND_DATA data; 00106 BOOL result = TRUE; 00107 HANDLE handle = FindFirstFile(pattern, &data); 00108 bool all_deleted = true; 00109 if (handle != INVALID_HANDLE_VALUE) { 00110 for (; result; result = FindNextFile(handle, &data)) { 00111 all_deleted &= File::Delete(data.cFileName); 00112 } 00113 FindClose(handle); 00114 } 00115 return all_deleted; 00116 } 00117 #else 00118 bool File::DeleteMatchingFiles(const char* pattern) { 00119 glob_t pglob; 00120 char **paths; 00121 bool all_deleted = true; 00122 if (glob(pattern, 0, NULL, &pglob) == 0) { 00123 for (paths = pglob.gl_pathv; *paths != NULL; paths++) { 00124 all_deleted &= File::Delete(*paths); 00125 } 00126 globfree(&pglob); 00127 } 00128 return all_deleted; 00129 } 00130 #endif 00131 00133 // InputBuffer:: 00135 InputBuffer::InputBuffer(FILE* stream) 00136 : stream_(stream) { 00137 fseek(stream_, 0, SEEK_END); 00138 filesize_ = ftell(stream_); 00139 fseek(stream_, 0, SEEK_SET); 00140 } 00141 00142 InputBuffer::InputBuffer(FILE* stream, size_t) 00143 : stream_(stream) { 00144 fseek(stream_, 0, SEEK_END); 00145 filesize_ = ftell(stream_); 00146 fseek(stream_, 0, SEEK_SET); 00147 } 00148 00149 InputBuffer::~InputBuffer() { 00150 if (stream_ != NULL) { 00151 fclose(stream_); 00152 } 00153 } 00154 00155 bool InputBuffer::Read(string* out) { 00156 char buf[BUFSIZ + 1]; 00157 int l; 00158 while ((l = fread(buf, 1, BUFSIZ, stream_)) > 0) { 00159 if (ferror(stream_)) { 00160 clearerr(stream_); 00161 return false; 00162 } 00163 buf[l] = 0; 00164 out->append(buf); 00165 } 00166 return true; 00167 } 00168 00169 bool InputBuffer::CloseFile() { 00170 int ret = fclose(stream_); 00171 stream_ = NULL; 00172 return ret == 0; 00173 } 00174 00176 // OutputBuffer:: 00178 00179 OutputBuffer::OutputBuffer(FILE* stream) 00180 : stream_(stream) { 00181 } 00182 00183 OutputBuffer::OutputBuffer(FILE* stream, size_t) 00184 : stream_(stream) { 00185 } 00186 00187 OutputBuffer::~OutputBuffer() { 00188 if (stream_ != NULL) { 00189 fclose(stream_); 00190 } 00191 } 00192 00193 void OutputBuffer::WriteString(const string& str) { 00194 fputs(str.c_str(), stream_); 00195 } 00196 00197 bool OutputBuffer::CloseFile() { 00198 int ret = fclose(stream_); 00199 stream_ = NULL; 00200 return ret == 0; 00201 } 00202 00203 } // namespace tesseract