|
tesseract 3.04.01
|
00001 /********************************************************************** 00002 * File: serialis.h (Formerly serialmac.h) 00003 * Description: Inline routines and macros for serialisation functions 00004 * Author: Phil Cheatle 00005 * Created: Tue Oct 08 08:33:12 BST 1991 00006 * 00007 * (C) Copyright 1990, Hewlett-Packard Ltd. 00008 ** Licensed under the Apache License, Version 2.0 (the "License"); 00009 ** you may not use this file except in compliance with the License. 00010 ** You may obtain a copy of the License at 00011 ** http://www.apache.org/licenses/LICENSE-2.0 00012 ** Unless required by applicable law or agreed to in writing, software 00013 ** distributed under the License is distributed on an "AS IS" BASIS, 00014 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 ** See the License for the specific language governing permissions and 00016 ** limitations under the License. 00017 * 00018 **********************************************************************/ 00019 00020 #ifndef SERIALIS_H 00021 #define SERIALIS_H 00022 00023 #include <stdlib.h> 00024 #include <string.h> 00025 #include <stdio.h> 00026 #include "host.h" 00027 00028 template <typename T> class GenericVector; 00029 class STRING; 00030 00031 /*********************************************************************** 00032 QUOTE_IT MACRO DEFINITION 00033 =========================== 00034 Replace <parm> with "<parm>". <parm> may be an arbitrary number of tokens 00035 ***********************************************************************/ 00036 00037 #define QUOTE_IT( parm ) #parm 00038 00039 namespace tesseract { 00040 00041 // Function to read a GenericVector<char> from a whole file. 00042 // Returns false on failure. 00043 typedef bool (*FileReader)(const STRING& filename, GenericVector<char>* data); 00044 // Function to write a GenericVector<char> to a whole file. 00045 // Returns false on failure. 00046 typedef bool (*FileWriter)(const GenericVector<char>& data, 00047 const STRING& filename); 00048 00049 // Simple file class. 00050 // Allows for portable file input from memory and from foreign file systems. 00051 class TFile { 00052 public: 00053 TFile(); 00054 ~TFile(); 00055 00056 // All the Open methods load the whole file into memory for reading. 00057 // Opens a file with a supplied reader, or NULL to use the default. 00058 // Note that mixed read/write is not supported. 00059 bool Open(const STRING& filename, FileReader reader); 00060 // From an existing memory buffer. 00061 bool Open(const char* data, int size); 00062 // From an open file and an end offset. 00063 bool Open(FILE* fp, inT64 end_offset); 00064 00065 // Reads a line like fgets. Returns NULL on EOF, otherwise buffer. 00066 // Reads at most buffer_size bytes, including '\0' terminator, even if 00067 // the line is longer. Does nothing if buffer_size <= 0. 00068 // To use fscanf use FGets and sscanf. 00069 char* FGets(char* buffer, int buffer_size); 00070 // Replicates fread, returning the number of items read. 00071 int FRead(void* buffer, int size, int count); 00072 // Resets the TFile as if it has been Opened, but nothing read. 00073 // Only allowed while reading! 00074 void Rewind(); 00075 00076 // Open for writing. Either supply a non-NULL data with OpenWrite before 00077 // calling FWrite, (no close required), or supply a NULL data to OpenWrite 00078 // and call CloseWrite to write to a file after the FWrites. 00079 void OpenWrite(GenericVector<char>* data); 00080 bool CloseWrite(const STRING& filename, FileWriter writer); 00081 00082 // Replicates fwrite, returning the number of items written. 00083 // To use fprintf, use snprintf and FWrite. 00084 int FWrite(const void* buffer, int size, int count); 00085 00086 private: 00087 // The number of bytes used so far. 00088 int offset_; 00089 // The buffered data from the file. 00090 GenericVector<char>* data_; 00091 // True if the data_ pointer is owned by *this. 00092 bool data_is_owned_; 00093 // True if the TFile is open for writing. 00094 bool is_writing_; 00095 }; 00096 00097 } // namespace tesseract. 00098 00099 #endif