All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
serialis.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: serialis.h (Formerly serialmac.h)
3  * Description: Inline routines and macros for serialisation functions
4  * Author: Phil Cheatle
5  * Created: Tue Oct 08 08:33:12 BST 1991
6  *
7  * (C) Copyright 1990, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #ifndef SERIALIS_H
21 #define SERIALIS_H
22 
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include "host.h"
27 
28 template <typename T> class GenericVector;
29 class STRING;
30 
31 /***********************************************************************
32  QUOTE_IT MACRO DEFINITION
33  ===========================
34 Replace <parm> with "<parm>". <parm> may be an arbitrary number of tokens
35 ***********************************************************************/
36 
37 #define QUOTE_IT( parm ) #parm
38 
39 namespace tesseract {
40 
41 // Function to read a GenericVector<char> from a whole file.
42 // Returns false on failure.
43 typedef bool (*FileReader)(const STRING& filename, GenericVector<char>* data);
44 // Function to write a GenericVector<char> to a whole file.
45 // Returns false on failure.
46 typedef bool (*FileWriter)(const GenericVector<char>& data,
47  const STRING& filename);
48 
49 // Simple file class.
50 // Allows for portable file input from memory and from foreign file systems.
51 class TFile {
52  public:
53  TFile();
54  ~TFile();
55 
56  // All the Open methods load the whole file into memory for reading.
57  // Opens a file with a supplied reader, or NULL to use the default.
58  // Note that mixed read/write is not supported.
59  bool Open(const STRING& filename, FileReader reader);
60  // From an existing memory buffer.
61  bool Open(const char* data, int size);
62  // From an open file and an end offset.
63  bool Open(FILE* fp, inT64 end_offset);
64 
65  // Reads a line like fgets. Returns NULL on EOF, otherwise buffer.
66  // Reads at most buffer_size bytes, including '\0' terminator, even if
67  // the line is longer. Does nothing if buffer_size <= 0.
68  // To use fscanf use FGets and sscanf.
69  char* FGets(char* buffer, int buffer_size);
70  // Replicates fread, returning the number of items read.
71  int FRead(void* buffer, int size, int count);
72  // Resets the TFile as if it has been Opened, but nothing read.
73  // Only allowed while reading!
74  void Rewind();
75 
76  // Open for writing. Either supply a non-NULL data with OpenWrite before
77  // calling FWrite, (no close required), or supply a NULL data to OpenWrite
78  // and call CloseWrite to write to a file after the FWrites.
79  void OpenWrite(GenericVector<char>* data);
80  bool CloseWrite(const STRING& filename, FileWriter writer);
81 
82  // Replicates fwrite, returning the number of items written.
83  // To use fprintf, use snprintf and FWrite.
84  int FWrite(const void* buffer, int size, int count);
85 
86  private:
87  // The number of bytes used so far.
88  int offset_;
89  // The buffered data from the file.
90  GenericVector<char>* data_;
91  // True if the data_ pointer is owned by *this.
92  bool data_is_owned_;
93  // True if the TFile is open for writing.
94  bool is_writing_;
95 };
96 
97 } // namespace tesseract.
98 
99 #endif
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
bool CloseWrite(const STRING &filename, FileWriter writer)
Definition: serialis.cpp:123
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:35
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:131
int count(LIST var_list)
Definition: oldlist.cpp:108
char * FGets(char *buffer, int buffer_size)
Definition: serialis.cpp:80
Definition: strngs.h:44
void OpenWrite(GenericVector< char > *data)
Definition: serialis.cpp:109
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:91
long long int inT64
Definition: host.h:108