All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
serialis.cpp
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 #include "serialis.h"
21 #include <stdio.h>
22 #include "genericvector.h"
23 
24 namespace tesseract {
25 
27  : offset_(0), data_(NULL), data_is_owned_(false), is_writing_(false) {
28 }
29 
31  if (data_is_owned_)
32  delete data_;
33 }
34 
35 bool TFile::Open(const STRING& filename, FileReader reader) {
36  if (!data_is_owned_) {
37  data_ = new GenericVector<char>;
38  data_is_owned_ = true;
39  }
40  offset_ = 0;
41  is_writing_ = false;
42  if (reader == NULL)
43  return LoadDataFromFile(filename, data_);
44  else
45  return (*reader)(filename, data_);
46 }
47 
48 bool TFile::Open(const char* data, int size) {
49  offset_ = 0;
50  if (!data_is_owned_) {
51  data_ = new GenericVector<char>;
52  data_is_owned_ = true;
53  }
54  is_writing_ = false;
55  data_->init_to_size(size, 0);
56  memcpy(&(*data_)[0], data, size);
57  return true;
58 }
59 
60 bool TFile::Open(FILE* fp, inT64 end_offset) {
61  offset_ = 0;
62  inT64 current_pos = ftell(fp);
63  if (end_offset < 0) {
64  if (fseek(fp, 0, SEEK_END))
65  return false;
66  end_offset = ftell(fp);
67  if (fseek(fp, current_pos, SEEK_SET))
68  return false;
69  }
70  int size = end_offset - current_pos;
71  is_writing_ = false;
72  if (!data_is_owned_) {
73  data_ = new GenericVector<char>;
74  data_is_owned_ = true;
75  }
76  data_->init_to_size(size, 0);
77  return static_cast<int>(fread(&(*data_)[0], 1, size, fp)) == size;
78 }
79 
80 char* TFile::FGets(char* buffer, int buffer_size) {
81  ASSERT_HOST(!is_writing_);
82  int size = 0;
83  while (size + 1 < buffer_size && offset_ < data_->size()) {
84  buffer[size++] = (*data_)[offset_++];
85  if ((*data_)[offset_ - 1] == '\n') break;
86  }
87  if (size < buffer_size) buffer[size] = '\0';
88  return size > 0 ? buffer : NULL;
89 }
90 
91 int TFile::FRead(void* buffer, int size, int count) {
92  ASSERT_HOST(!is_writing_);
93  int required_size = size * count;
94  if (required_size <= 0) return 0;
95  char* char_buffer = reinterpret_cast<char*>(buffer);
96  if (data_->size() - offset_ < required_size)
97  required_size = data_->size() - offset_;
98  if (required_size > 0)
99  memcpy(char_buffer, &(*data_)[offset_], required_size);
100  offset_ += required_size;
101  return required_size / size;
102 }
103 
105  ASSERT_HOST(!is_writing_);
106  offset_ = 0;
107 }
108 
110  offset_ = 0;
111  if (data != NULL) {
112  if (data_is_owned_) delete data_;
113  data_ = data;
114  data_is_owned_ = false;
115  } else if (!data_is_owned_) {
116  data_ = new GenericVector<char>;
117  data_is_owned_ = true;
118  }
119  is_writing_ = true;
120  data_->truncate(0);
121 }
122 
124  ASSERT_HOST(is_writing_);
125  if (writer == NULL)
126  return SaveDataToFile(*data_, filename);
127  else
128  return (*writer)(*data_, filename);
129 }
130 
131 int TFile::FWrite(const void* buffer, int size, int count) {
132  ASSERT_HOST(is_writing_);
133  int total = size * count;
134  if (total <= 0) return 0;
135  const char* buf = reinterpret_cast<const char*>(buffer);
136  // This isn't very efficient, but memory is so fast compared to disk
137  // that it is relatively unimportant, and very simple.
138  for (int i = 0; i < total; ++i)
139  data_->push_back(buf[i]);
140  return count;
141 }
142 
143 
144 } // namespace tesseract.
145 
int size() const
Definition: genericvector.h:72
void truncate(int size)
int push_back(T object)
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)
#define ASSERT_HOST(x)
Definition: errcode.h:84
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
void init_to_size(int size, T t)
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
#define NULL
Definition: host.h:144
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:91
long long int inT64
Definition: host.h:108