tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
serialis.h
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 <cstdlib>
24 #include <cstring>
25 #include <cstdio>
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 // Return number of elements of an array.
42 template <typename T, size_t N>
43 constexpr size_t countof(T const (&)[N]) noexcept {
44  return N;
45 }
46 
47 // Function to read a GenericVector<char> from a whole file.
48 // Returns false on failure.
49 typedef bool (*FileReader)(const STRING& filename, GenericVector<char>* data);
50 // Function to write a GenericVector<char> to a whole file.
51 // Returns false on failure.
52 typedef bool (*FileWriter)(const GenericVector<char>& data,
53  const STRING& filename);
54 
55 // Deserialize data from file.
56 bool DeSerialize(FILE* fp, char* data, size_t n = 1);
57 bool DeSerialize(FILE* fp, float* data, size_t n = 1);
58 bool DeSerialize(FILE* fp, int8_t* data, size_t n = 1);
59 bool DeSerialize(FILE* fp, int16_t* data, size_t n = 1);
60 bool DeSerialize(FILE* fp, int32_t* data, size_t n = 1);
61 bool DeSerialize(FILE* fp, uint8_t* data, size_t n = 1);
62 bool DeSerialize(FILE* fp, uint16_t* data, size_t n = 1);
63 bool DeSerialize(FILE* fp, uint32_t* data, size_t n = 1);
64 
65 // Serialize data to file.
66 bool Serialize(FILE* fp, const char* data, size_t n = 1);
67 bool Serialize(FILE* fp, const float* data, size_t n = 1);
68 bool Serialize(FILE* fp, const int8_t* data, size_t n = 1);
69 bool Serialize(FILE* fp, const int16_t* data, size_t n = 1);
70 bool Serialize(FILE* fp, const int32_t* data, size_t n = 1);
71 bool Serialize(FILE* fp, const uint8_t* data, size_t n = 1);
72 bool Serialize(FILE* fp, const uint16_t* data, size_t n = 1);
73 bool Serialize(FILE* fp, const uint32_t* data, size_t n = 1);
74 
75 // Simple file class.
76 // Allows for portable file input from memory and from foreign file systems.
77 class TFile {
78  public:
79  TFile();
80  ~TFile();
81 
82  // All the Open methods load the whole file into memory for reading.
83  // Opens a file with a supplied reader, or nullptr to use the default.
84  // Note that mixed read/write is not supported.
85  bool Open(const STRING& filename, FileReader reader);
86  // From an existing memory buffer.
87  bool Open(const char* data, int size);
88  // From an open file and an end offset.
89  bool Open(FILE* fp, int64_t end_offset);
90  // Sets the value of the swap flag, so that FReadEndian does the right thing.
91  void set_swap(bool value) { swap_ = value; }
92 
93  // Deserialize data.
94  bool DeSerialize(char* data, size_t count = 1);
95  bool DeSerialize(double* data, size_t count = 1);
96  bool DeSerialize(float* data, size_t count = 1);
97  bool DeSerialize(int8_t* data, size_t count = 1);
98  bool DeSerialize(int16_t* data, size_t count = 1);
99  bool DeSerialize(int32_t* data, size_t count = 1);
100  bool DeSerialize(int64_t* data, size_t count = 1);
101  bool DeSerialize(uint8_t* data, size_t count = 1);
102  bool DeSerialize(uint16_t* data, size_t count = 1);
103  bool DeSerialize(uint32_t* data, size_t count = 1);
104  bool DeSerialize(uint64_t* data, size_t count = 1);
105 
106  // Serialize data.
107  bool Serialize(const char* data, size_t count = 1);
108  bool Serialize(const double* data, size_t count = 1);
109  bool Serialize(const float* data, size_t count = 1);
110  bool Serialize(const int8_t* data, size_t count = 1);
111  bool Serialize(const int16_t* data, size_t count = 1);
112  bool Serialize(const int32_t* data, size_t count = 1);
113  bool Serialize(const int64_t* data, size_t count = 1);
114  bool Serialize(const uint8_t* data, size_t count = 1);
115  bool Serialize(const uint16_t* data, size_t count = 1);
116  bool Serialize(const uint32_t* data, size_t count = 1);
117  bool Serialize(const uint64_t* data, size_t count = 1);
118 
119  // Skip data.
120  bool Skip(size_t count);
121 
122  // Reads a line like fgets. Returns nullptr on EOF, otherwise buffer.
123  // Reads at most buffer_size bytes, including '\0' terminator, even if
124  // the line is longer. Does nothing if buffer_size <= 0.
125  // To use fscanf use FGets and sscanf.
126  char* FGets(char* buffer, int buffer_size);
127  // Replicates fread, followed by a swap of the bytes if needed, returning the
128  // number of items read. If swap_ is true then the count items will each have
129  // size bytes reversed.
130  int FReadEndian(void* buffer, size_t size, int count);
131  // Replicates fread, returning the number of items read.
132  int FRead(void* buffer, size_t size, int count);
133  // Resets the TFile as if it has been Opened, but nothing read.
134  // Only allowed while reading!
135  void Rewind();
136 
137  // Open for writing. Either supply a non-nullptr data with OpenWrite before
138  // calling FWrite, (no close required), or supply a nullptr data to OpenWrite
139  // and call CloseWrite to write to a file after the FWrites.
140  void OpenWrite(GenericVector<char>* data);
141  bool CloseWrite(const STRING& filename, FileWriter writer);
142 
143  // Replicates fwrite, returning the number of items written.
144  // To use fprintf, use snprintf and FWrite.
145  int FWrite(const void* buffer, size_t size, int count);
146 
147  private:
148  // The number of bytes used so far.
149  int offset_;
150  // The buffered data from the file.
152  // True if the data_ pointer is owned by *this.
154  // True if the TFile is open for writing.
156  // True if bytes need to be swapped in FReadEndian.
157  bool swap_;
158 };
159 
160 } // namespace tesseract.
161 
162 #endif
~TFile()
Definition: serialis.cpp:98
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:196
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
Definition: genericvector.h:360
Definition: serialis.h:77
Definition: baseapi.cpp:94
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
bool swap_
Definition: serialis.h:157
bool is_writing_
Definition: serialis.h:155
void Rewind()
Definition: serialis.cpp:290
void set_swap(bool value)
Definition: serialis.h:91
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
Definition: genericvector.h:363
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:103
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
Definition: baseapi.h:37
Definition: strngs.h:45
bool CloseWrite(const STRING &filename, FileWriter writer)
Definition: serialis.cpp:310
int offset_
Definition: serialis.h:149
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
bool data_is_owned_
Definition: serialis.h:153
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:147
TFile()
Definition: serialis.cpp:91
constexpr size_t countof(T const (&)[N]) noexcept
Definition: serialis.h:43
GenericVector< char > * data_
Definition: serialis.h:151
bool Skip(size_t count)
Definition: serialis.cpp:191
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:27
char * FGets(char *buffer, int buffer_size)
Definition: serialis.cpp:248
void OpenWrite(GenericVector< char > *data)
Definition: serialis.cpp:295