tesseract 4.1.1
Loading...
Searching...
No Matches
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 *
6 * (C) Copyright 1990, Hewlett-Packard Ltd.
7 ** Licensed under the Apache License, Version 2.0 (the "License");
8 ** you may not use this file except in compliance with the License.
9 ** You may obtain a copy of the License at
10 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 *
17 **********************************************************************/
18
19#ifndef SERIALIS_H
20#define SERIALIS_H
21
22#include <cstdint> // uint8_t
23#include <cstdio>
24#include <cstdlib>
25#include <cstring>
26
27template <typename T>
28class GenericVector;
29class STRING;
30
31/***********************************************************************
32 QUOTE_IT MACRO DEFINITION
33 ===========================
34Replace <parm> with "<parm>". <parm> may be an arbitrary number of tokens
35***********************************************************************/
36
37#define QUOTE_IT(parm) #parm
38
39namespace tesseract {
40
41// Return number of elements of an array.
42template <typename T, size_t N>
43constexpr 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.
49using FileReader = bool (*)(const STRING&, GenericVector<char>*);
50// Function to write a GenericVector<char> to a whole file.
51// Returns false on failure.
52using FileWriter = bool (*)(const GenericVector<char>&, const STRING&);
53
54// Deserialize data from file.
55bool DeSerialize(FILE* fp, char* data, size_t n = 1);
56bool DeSerialize(FILE* fp, float* data, size_t n = 1);
57bool DeSerialize(FILE* fp, int8_t* data, size_t n = 1);
58bool DeSerialize(FILE* fp, int16_t* data, size_t n = 1);
59bool DeSerialize(FILE* fp, int32_t* data, size_t n = 1);
60bool DeSerialize(FILE* fp, uint8_t* data, size_t n = 1);
61bool DeSerialize(FILE* fp, uint16_t* data, size_t n = 1);
62bool DeSerialize(FILE* fp, uint32_t* data, size_t n = 1);
63
64// Serialize data to file.
65bool Serialize(FILE* fp, const char* data, size_t n = 1);
66bool Serialize(FILE* fp, const float* data, size_t n = 1);
67bool Serialize(FILE* fp, const int8_t* data, size_t n = 1);
68bool Serialize(FILE* fp, const int16_t* data, size_t n = 1);
69bool Serialize(FILE* fp, const int32_t* data, size_t n = 1);
70bool Serialize(FILE* fp, const uint8_t* data, size_t n = 1);
71bool Serialize(FILE* fp, const uint16_t* data, size_t n = 1);
72bool Serialize(FILE* fp, const uint32_t* data, size_t n = 1);
73
74// Simple file class.
75// Allows for portable file input from memory and from foreign file systems.
76class TFile {
77 public:
78 TFile();
79 ~TFile();
80
81 // All the Open methods load the whole file into memory for reading.
82 // Opens a file with a supplied reader, or nullptr to use the default.
83 // Note that mixed read/write is not supported.
84 bool Open(const STRING& filename, FileReader reader);
85 // From an existing memory buffer.
86 bool Open(const char* data, int size);
87 // From an open file and an end offset.
88 bool Open(FILE* fp, int64_t end_offset);
89 // Sets the value of the swap flag, so that FReadEndian does the right thing.
90 void set_swap(bool value) {
91 swap_ = value;
92 }
93
94 // Deserialize data.
95 bool DeSerialize(char* data, size_t count = 1);
96 bool DeSerialize(double* data, size_t count = 1);
97 bool DeSerialize(float* data, size_t count = 1);
98 bool DeSerialize(int8_t* data, size_t count = 1);
99 bool DeSerialize(int16_t* data, size_t count = 1);
100 bool DeSerialize(int32_t* data, size_t count = 1);
101 bool DeSerialize(int64_t* data, size_t count = 1);
102 bool DeSerialize(uint8_t* data, size_t count = 1);
103 bool DeSerialize(uint16_t* data, size_t count = 1);
104 bool DeSerialize(uint32_t* data, size_t count = 1);
105 bool DeSerialize(uint64_t* data, size_t count = 1);
106
107 // Serialize data.
108 bool Serialize(const char* data, size_t count = 1);
109 bool Serialize(const double* data, size_t count = 1);
110 bool Serialize(const float* data, size_t count = 1);
111 bool Serialize(const int8_t* data, size_t count = 1);
112 bool Serialize(const int16_t* data, size_t count = 1);
113 bool Serialize(const int32_t* data, size_t count = 1);
114 bool Serialize(const int64_t* data, size_t count = 1);
115 bool Serialize(const uint8_t* data, size_t count = 1);
116 bool Serialize(const uint16_t* data, size_t count = 1);
117 bool Serialize(const uint32_t* data, size_t count = 1);
118 bool Serialize(const uint64_t* data, size_t count = 1);
119
120 // Skip data.
121 bool Skip(size_t count);
122
123 // Reads a line like fgets. Returns nullptr on EOF, otherwise buffer.
124 // Reads at most buffer_size bytes, including '\0' terminator, even if
125 // the line is longer. Does nothing if buffer_size <= 0.
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.
151 GenericVector<char>* data_;
152 // True if the data_ pointer is owned by *this.
153 bool data_is_owned_;
154 // True if the TFile is open for writing.
155 bool is_writing_;
156 // True if bytes need to be swapped in FReadEndian.
157 bool swap_;
158};
159
160} // namespace tesseract.
161
162#endif
int count(LIST var_list)
Definition: oldlist.cpp:95
bool(*)(const STRING &, GenericVector< char > *) FileReader
Definition: serialis.h:49
constexpr size_t countof(T const (&)[N]) noexcept
Definition: serialis.h:43
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:28
bool(*)(const GenericVector< char > &, const STRING &) FileWriter
Definition: serialis.h:52
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:60
void OpenWrite(GenericVector< char > *data)
Definition: serialis.cpp:296
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:319
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:260
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:271
void set_swap(bool value)
Definition: serialis.h:90
char * FGets(char *buffer, int buffer_size)
Definition: serialis.cpp:249
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:197
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:148
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:104
bool Skip(size_t count)
Definition: serialis.cpp:192
bool CloseWrite(const STRING &filename, FileWriter writer)
Definition: serialis.cpp:311
Definition: strngs.h:45