1 | //
|
---|
2 | // state_bin.cc
|
---|
3 | //
|
---|
4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
5 | //
|
---|
6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
7 | // Maintainer: LPS
|
---|
8 | //
|
---|
9 | // This file is part of the SC Toolkit.
|
---|
10 | //
|
---|
11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
12 | // it under the terms of the GNU Library General Public License as published by
|
---|
13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
14 | // any later version.
|
---|
15 | //
|
---|
16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | // GNU Library General Public License for more details.
|
---|
20 | //
|
---|
21 | // You should have received a copy of the GNU Library General Public License
|
---|
22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | //
|
---|
25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
26 | //
|
---|
27 |
|
---|
28 | #ifdef __GNUC__
|
---|
29 | #pragma implementation
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <scconfig.h>
|
---|
33 | #include <util/state/state_bin.h>
|
---|
34 |
|
---|
35 | using namespace std;
|
---|
36 | using namespace sc;
|
---|
37 |
|
---|
38 | #define DEBUG 0
|
---|
39 |
|
---|
40 | static ClassDesc StateOutBin_cd(
|
---|
41 | typeid(StateOutBin),"StateOutBin",1,"public StateOutFile");
|
---|
42 |
|
---|
43 | StateOutBin::StateOutBin() :
|
---|
44 | StateOutFile()
|
---|
45 | {
|
---|
46 | file_position_ = 0;
|
---|
47 | }
|
---|
48 |
|
---|
49 | StateOutBin::StateOutBin(ostream& s):
|
---|
50 | StateOutFile(s)
|
---|
51 | {
|
---|
52 | file_position_ = 0;
|
---|
53 | // needed here since only StateOutFile::open has been called so far
|
---|
54 | put_header();
|
---|
55 | }
|
---|
56 |
|
---|
57 | StateOutBin::StateOutBin(const char *path) :
|
---|
58 | StateOutFile(path)
|
---|
59 | {
|
---|
60 | file_position_ = 0;
|
---|
61 | // needed here since only StateOutFile::open has been called so far
|
---|
62 | put_header();
|
---|
63 | }
|
---|
64 |
|
---|
65 | StateOutBin::~StateOutBin()
|
---|
66 | {
|
---|
67 | // must close here since close() is overridden in this class
|
---|
68 | close();
|
---|
69 | }
|
---|
70 |
|
---|
71 | int
|
---|
72 | StateOutBin::open(const char *f)
|
---|
73 | {
|
---|
74 | int r = StateOutFile::open(f);
|
---|
75 | put_header();
|
---|
76 | return r;
|
---|
77 | }
|
---|
78 |
|
---|
79 | void
|
---|
80 | StateOutBin::close()
|
---|
81 | {
|
---|
82 | if (buf_ && use_directory()) {
|
---|
83 | int dir_loc = tell();
|
---|
84 | seek(dir_loc_loc_);
|
---|
85 | put_array_int(&dir_loc,1);
|
---|
86 | seek(dir_loc);
|
---|
87 | put_directory();
|
---|
88 | }
|
---|
89 |
|
---|
90 | StateOutFile::close();
|
---|
91 | }
|
---|
92 |
|
---|
93 | int
|
---|
94 | StateOutBin::tell()
|
---|
95 | {
|
---|
96 | return file_position_;
|
---|
97 | }
|
---|
98 |
|
---|
99 | void
|
---|
100 | StateOutBin::seek(int loc)
|
---|
101 | {
|
---|
102 | file_position_ = loc;
|
---|
103 | #if defined(HAVE_PUBSEEKOFF)
|
---|
104 | buf_->pubseekoff(loc,ios::beg,ios::out);
|
---|
105 | #elif defined(HAVE_SEEKOFF)
|
---|
106 | buf_->seekoff(loc,ios::beg,ios::out);
|
---|
107 | #endif
|
---|
108 | }
|
---|
109 |
|
---|
110 | int
|
---|
111 | StateOutBin::seekable()
|
---|
112 | {
|
---|
113 | #if defined(HAVE_PUBSEEKOFF) || defined(HAVE_SEEKOFF)
|
---|
114 | return 1;
|
---|
115 | #else
|
---|
116 | return 0;
|
---|
117 | #endif
|
---|
118 | }
|
---|
119 |
|
---|
120 | int
|
---|
121 | StateOutBin::use_directory()
|
---|
122 | {
|
---|
123 | return seekable();
|
---|
124 | }
|
---|
125 |
|
---|
126 | ////////////////////////////////////////////////////////////////
|
---|
127 |
|
---|
128 | static ClassDesc StateInBin_cd(typeid(StateInBin),
|
---|
129 | "StateInBin",1,"public StateInFile",
|
---|
130 | 0, create<StateInBin>);
|
---|
131 |
|
---|
132 | StateInBin::StateInBin() :
|
---|
133 | StateInFile()
|
---|
134 | {
|
---|
135 | file_position_ = 0;
|
---|
136 | }
|
---|
137 |
|
---|
138 | StateInBin::StateInBin(istream& s) :
|
---|
139 | StateInFile(s)
|
---|
140 | {
|
---|
141 | file_position_ = 0;
|
---|
142 | get_header();
|
---|
143 | find_and_get_directory();
|
---|
144 | }
|
---|
145 |
|
---|
146 | StateInBin::StateInBin(const char *path) :
|
---|
147 | StateInFile(path)
|
---|
148 | {
|
---|
149 | file_position_ = 0;
|
---|
150 | get_header();
|
---|
151 | find_and_get_directory();
|
---|
152 | }
|
---|
153 |
|
---|
154 | StateInBin::StateInBin(const Ref<KeyVal> &keyval)
|
---|
155 | {
|
---|
156 | char *path = keyval->pcharvalue("file");
|
---|
157 | if (!path) {
|
---|
158 | ExEnv::errn() << "StateInBin(const Ref<KeyVal>&): no path given" << endl;
|
---|
159 | }
|
---|
160 | open(path);
|
---|
161 | delete[] path;
|
---|
162 | }
|
---|
163 |
|
---|
164 | StateInBin::~StateInBin()
|
---|
165 | {
|
---|
166 | }
|
---|
167 |
|
---|
168 | int
|
---|
169 | StateInBin::open(const char *f)
|
---|
170 | {
|
---|
171 | file_position_ = 0;
|
---|
172 | int r = StateInFile::open(f);
|
---|
173 | get_header();
|
---|
174 | find_and_get_directory();
|
---|
175 | return r;
|
---|
176 | }
|
---|
177 |
|
---|
178 | int
|
---|
179 | StateInBin::tell()
|
---|
180 | {
|
---|
181 | return file_position_;
|
---|
182 | }
|
---|
183 |
|
---|
184 | void
|
---|
185 | StateInBin::seek(int loc)
|
---|
186 | {
|
---|
187 | file_position_ = loc;
|
---|
188 | #if defined(HAVE_PUBSEEKOFF)
|
---|
189 | buf_->pubseekoff(loc,ios::beg,ios::in);
|
---|
190 | #elif defined(HAVE_SEEKOFF)
|
---|
191 | buf_->seekoff(loc,ios::beg,ios::in);
|
---|
192 | #endif
|
---|
193 | }
|
---|
194 |
|
---|
195 | int
|
---|
196 | StateInBin::seekable()
|
---|
197 | {
|
---|
198 | #if defined(HAVE_PUBSEEKOFF) || defined(HAVE_SEEKOFF)
|
---|
199 | return 1;
|
---|
200 | #else
|
---|
201 | return 0;
|
---|
202 | #endif
|
---|
203 | }
|
---|
204 |
|
---|
205 | int
|
---|
206 | StateInBin::use_directory()
|
---|
207 | {
|
---|
208 | return seekable();
|
---|
209 | }
|
---|
210 |
|
---|
211 | ////////////////////////////////////////////////////////////////
|
---|
212 |
|
---|
213 | int StateOutBin::put_array_void(const void*p,int size)
|
---|
214 | {
|
---|
215 | if (buf_->sputn((const char *)p,size) != size) {
|
---|
216 | ExEnv::errn() << "StateOutBin::put_array_void: failed" << endl;
|
---|
217 | abort();
|
---|
218 | }
|
---|
219 | file_position_ += size;
|
---|
220 | return size;
|
---|
221 | }
|
---|
222 |
|
---|
223 | int StateInBin::get_array_void(void*p,int size)
|
---|
224 | {
|
---|
225 | if (buf_->sgetn((char*)p,size) != size) {
|
---|
226 | ExEnv::errn() << "StateInBin::get_array_void: failed" << endl;
|
---|
227 | abort();
|
---|
228 | }
|
---|
229 | #if DEBUG
|
---|
230 | ExEnv::outn() << "Read " << size << " bytes: ";
|
---|
231 | for (int i=0; i<size; i++) {
|
---|
232 | ExEnv::outn() << ((unsigned char*)p)[i];
|
---|
233 | }
|
---|
234 | ExEnv::outn() << endl;
|
---|
235 | #endif
|
---|
236 | file_position_ += size;
|
---|
237 | return size;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /////////////////////////////////////////////////////////////////////////////
|
---|
241 |
|
---|
242 | // Local Variables:
|
---|
243 | // mode: c++
|
---|
244 | // c-file-style: "CLJ"
|
---|
245 | // End:
|
---|