tesseract  4.00.00dev
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  // Sets the value of the swap flag, so that FReadEndian does the right thing.
65  void set_swap(bool value) { swap_ = value; }
66 
67  // Reads a line like fgets. Returns NULL on EOF, otherwise buffer.
68  // Reads at most buffer_size bytes, including '\0' terminator, even if
69  // the line is longer. Does nothing if buffer_size <= 0.
70  // To use fscanf use FGets and sscanf.
71  char* FGets(char* buffer, int buffer_size);
72  // Replicates fread, followed by a swap of the bytes if needed, returning the
73  // number of items read. If swap_ is true then the count items will each have
74  // size bytes reversed.
75  int FReadEndian(void* buffer, int size, int count);
76  // Replicates fread, returning the number of items read.
77  int FRead(void* buffer, int size, int count);
78  // Resets the TFile as if it has been Opened, but nothing read.
79  // Only allowed while reading!
80  void Rewind();
81 
82  // Open for writing. Either supply a non-NULL data with OpenWrite before
83  // calling FWrite, (no close required), or supply a NULL data to OpenWrite
84  // and call CloseWrite to write to a file after the FWrites.
85  void OpenWrite(GenericVector<char>* data);
86  bool CloseWrite(const STRING& filename, FileWriter writer);
87 
88  // Replicates fwrite, returning the number of items written.
89  // To use fprintf, use snprintf and FWrite.
90  int FWrite(const void* buffer, int size, int count);
91 
92  private:
93  // The number of bytes used so far.
94  int offset_;
95  // The buffered data from the file.
96  GenericVector<char>* data_;
97  // True if the data_ pointer is owned by *this.
98  bool data_is_owned_;
99  // True if the TFile is open for writing.
100  bool is_writing_;
101  // True if bytes need to be swapped in FReadEndian.
102  bool swap_;
103 };
104 
105 } // namespace tesseract.
106 
107 #endif
int64_t inT64
Definition: host.h:40
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
voidpf void uLong size
Definition: ioapi.h:39
bool CloseWrite(const STRING &filename, FileWriter writer)
Definition: serialis.cpp:140
int FReadEndian(void *buffer, int size, int count)
Definition: serialis.cpp:97
void OpenWrite(GenericVector< char > *data)
Definition: serialis.cpp:125
Definition: strngs.h:45
void set_swap(bool value)
Definition: serialis.h:65
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:148
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
const char * filename
Definition: ioapi.h:38
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:38
char * FGets(char *buffer, int buffer_size)
Definition: serialis.cpp:86
int count(LIST var_list)
Definition: oldlist.cpp:103
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:108