tesseract
3.03
|
00001 /********************************************************************** 00002 * File: fileio.h 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 #ifndef TESSERACT_TRAINING_FILEIO_H_ 00018 #define TESSERACT_TRAINING_FILEIO_H_ 00019 00020 #include <stddef.h> 00021 #include <cstdio> 00022 #include <string> 00023 00024 #ifdef USE_STD_NAMESPACE 00025 using std::string; 00026 #endif 00027 00028 namespace tesseract { 00029 00030 // A class to manipulate FILE*s. 00031 class File { 00032 public: 00033 // Try to open the file 'filename' in mode 'mode'. 00034 // Stop the program if it cannot open it. 00035 static FILE* OpenOrDie(const string& filename, const string& mode); 00036 static FILE* Open(const string& filename, const string& mode); 00037 00038 // Try to open the file 'filename' and to write 'str' in it. 00039 // Stop the program if it fails. 00040 static void WriteStringToFileOrDie(const string& str, const string& filename); 00041 00042 // Return true if the file 'filename' is readable. 00043 static bool Readable(const string& filename); 00044 00045 static void ReadFileToStringOrDie(const string& filename, string* out); 00046 static bool ReadFileToString(const string& filename, string* out); 00047 00048 // Helper methods 00049 00050 // Concatenate file paths removing any extra intervening '/' symbols. 00051 static string JoinPath(const string& prefix, const string& suffix); 00052 // Delete a filename or all filenames matching a glob pattern. 00053 static bool Delete(const char* pathname); 00054 static bool DeleteMatchingFiles(const char* pattern); 00055 }; 00056 00057 // A class to manipulate Files for reading. 00058 class InputBuffer { 00059 public: 00060 explicit InputBuffer(FILE* stream); 00061 // 'size' is ignored. 00062 InputBuffer(FILE* stream, size_t size); 00063 00064 ~InputBuffer(); 00065 00066 // Read data until end-of-file or a \n is read. 00067 // The data is stored in '*out', excluding the \n if present. 00068 // Return false if an error occurs or at end-of-file, true otherwise. 00069 bool ReadLine(string* out); 00070 00071 // Close the FILE* used by InputBuffer. 00072 // Return false if an error occurs, true otherwise. 00073 bool CloseFile(); 00074 00075 private: 00076 FILE* stream_; 00077 int filesize_; 00078 }; 00079 00080 // A class to manipulate Files for writing. 00081 class OutputBuffer { 00082 public: 00083 explicit OutputBuffer(FILE* stream); 00084 // 'size' is ignored. 00085 OutputBuffer(FILE* stream, size_t size); 00086 00087 ~OutputBuffer(); 00088 00089 // Write string 'str' to the open FILE*. 00090 void WriteString(const string& str); 00091 00092 // Close the FILE* used by InputBuffer. 00093 // Return false if an error occurs, true otherwise. 00094 bool CloseFile(); 00095 00096 private: 00097 FILE* stream_; 00098 }; 00099 00100 } // namespace tesseract 00101 #endif // TESSERACT_TRAINING_FILEIO_H_