tesseract
3.03
|
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 string temp; 00080 while (in.ReadLine(&temp)) { 00081 *out += temp; 00082 *out += '\n'; 00083 } 00084 return in.CloseFile(); 00085 } 00086 00087 void File::ReadFileToStringOrDie(const string& filename, string* out) { 00088 ASSERT_HOST(ReadFileToString(filename, out)); 00089 } 00090 00091 00092 string File::JoinPath(const string& prefix, const string& suffix) { 00093 return (!prefix.size() || prefix[prefix.size() - 1] == '/') ? 00094 prefix + suffix : prefix + "/" + suffix; 00095 } 00096 00097 bool File::Delete(const char* pathname) { 00098 const int status = unlink(pathname); 00099 if (status != 0) { 00100 tprintf("ERROR: Unable to delete file %s\n", pathname); 00101 return false; 00102 } 00103 return true; 00104 } 00105 00106 #ifdef _WIN32 00107 bool File::DeleteMatchingFiles(const char* pattern) { 00108 WIN32_FIND_DATA data; 00109 BOOL result = TRUE; 00110 HANDLE handle = FindFirstFile(pattern, &data); 00111 bool all_deleted = true; 00112 if (handle != INVALID_HANDLE_VALUE) { 00113 for (; result; result = FindNextFile(handle, &data)) { 00114 all_deleted &= File::Delete(data.cFileName); 00115 } 00116 FindClose(handle); 00117 } 00118 return all_deleted; 00119 } 00120 #else 00121 bool File::DeleteMatchingFiles(const char* pattern) { 00122 glob_t pglob; 00123 char **paths; 00124 bool all_deleted = true; 00125 if (glob(pattern, 0, NULL, &pglob) == 0) { 00126 for (paths = pglob.gl_pathv; *paths != NULL; paths++) { 00127 all_deleted &= File::Delete(*paths); 00128 } 00129 globfree(&pglob); 00130 } 00131 return all_deleted; 00132 } 00133 #endif 00134 00136 // InputBuffer:: 00138 InputBuffer::InputBuffer(FILE* stream) 00139 : stream_(stream) { 00140 fseek(stream_, 0, SEEK_END); 00141 filesize_ = ftell(stream_); 00142 fseek(stream_, 0, SEEK_SET); 00143 } 00144 00145 InputBuffer::InputBuffer(FILE* stream, size_t) 00146 : stream_(stream) { 00147 fseek(stream_, 0, SEEK_END); 00148 filesize_ = ftell(stream_); 00149 fseek(stream_, 0, SEEK_SET); 00150 } 00151 00152 InputBuffer::~InputBuffer() { 00153 if (stream_ != NULL) { 00154 fclose(stream_); 00155 } 00156 } 00157 00158 bool InputBuffer::ReadLine(string* out) { 00159 ASSERT_HOST(stream_ != NULL); 00160 char* line = NULL; 00161 int len = -1; 00162 #ifdef _WIN32 00163 char line_buf[BUFSIZ]; 00164 if ((line = fgets(line_buf, BUFSIZ, stream_)) != NULL) { 00165 len = strlen(line); 00166 if (line_buf[0] != '\0' && line_buf[len - 1] == '\n') 00167 line_buf[len - 1] = '\0'; 00168 } else { 00169 return false; 00170 } 00171 *out = string(line); 00172 #else 00173 size_t line_size; 00174 len = getline(&line, &line_size, stream_); 00175 if (len < 0) { 00176 return false; 00177 } 00178 00179 if (len >= 1 && line[len - 1] == '\n') 00180 line[len - 1] = '\0'; 00181 *out = string(line); 00182 free(line); 00183 #endif // _WIN32 00184 return true; 00185 } 00186 00187 bool InputBuffer::CloseFile() { 00188 int ret = fclose(stream_); 00189 stream_ = NULL; 00190 return ret == 0; 00191 } 00192 00194 // OutputBuffer:: 00196 00197 OutputBuffer::OutputBuffer(FILE* stream) 00198 : stream_(stream) { 00199 } 00200 00201 OutputBuffer::OutputBuffer(FILE* stream, size_t) 00202 : stream_(stream) { 00203 } 00204 00205 OutputBuffer::~OutputBuffer() { 00206 if (stream_ != NULL) { 00207 fclose(stream_); 00208 } 00209 } 00210 00211 void OutputBuffer::WriteString(const string& str) { 00212 fputs(str.c_str(), stream_); 00213 } 00214 00215 bool OutputBuffer::CloseFile() { 00216 int ret = fclose(stream_); 00217 stream_ = NULL; 00218 return ret == 0; 00219 } 00220 00221 } // namespace tesseract