nsgmls: remove register keyword
[oweals/cde.git] / cde / programs / nsgmls / RewindStorageObject.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: RewindStorageObject.C /main/1 1996/07/29 17:02:59 cde-hp $ */
24 // Copyright (c) 1994, 1995 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifdef __GNUG__
28 #pragma implementation
29 #endif
30
31 #include "splib.h"
32 #include "RewindStorageObject.h"
33 #include "macros.h"
34
35 #ifdef SP_NAMESPACE
36 namespace SP_NAMESPACE {
37 #endif
38
39 RewindStorageObject::RewindStorageObject(Boolean mayRewind, Boolean canSeek)
40 : mayRewind_(mayRewind), canSeek_(canSeek),
41   savingBytes_(mayRewind && canSeek), readingSaved_(0),
42   nBytesRead_(0)
43 {
44 }
45
46 Boolean RewindStorageObject::rewind(Messenger &mgr)
47 {
48   ASSERT(mayRewind_);
49   if (canSeek_)
50     return seekToStart(mgr);
51   else {
52     readingSaved_ = 1;
53     nBytesRead_ = 0;
54     return 1;
55   }
56 }
57
58 void RewindStorageObject::unread(const char *s, size_t n)
59 {
60   savedBytes_.append(s, n);
61   if (!readingSaved_) {
62     readingSaved_ = 1;
63     nBytesRead_ = 0;
64   }
65 }
66
67 void RewindStorageObject::willNotRewind()
68 {
69   mayRewind_ = 0;
70   savingBytes_ = 0;
71   if (!readingSaved_) {
72     // Ensure that memory is released now.
73     String<char> tem;
74     tem.swap(savedBytes_);
75   }
76 }
77
78 Boolean RewindStorageObject::readSaved(char *buf, size_t bufSize,
79                                        size_t &nread)
80 {
81   if (!readingSaved_)
82     return 0;
83   if (nBytesRead_ >= savedBytes_.size()) {
84     if (!mayRewind_) {
85       // Ensure that memory is released now.
86       String<char> tem;
87       tem.swap(savedBytes_);
88     }
89     readingSaved_ = 0;
90     return 0;
91   }
92   nread = savedBytes_.size() - nBytesRead_;
93   if (nread > bufSize)
94     nread = bufSize;
95   memcpy(buf, savedBytes_.data() + nBytesRead_, nread);
96   nBytesRead_ += nread;
97   return 1;
98 }
99
100 #ifdef SP_NAMESPACE
101 }
102 #endif