Merge branch 'master' of https://git.code.sf.net/p/cdesktopenv/code
[oweals/cde.git] / cde / programs / nsgmls / LiteralStorage.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 librararies 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: LiteralStorage.C /main/1 1996/07/29 16:56:08 cde-hp $ */
24 // Copyright (c) 1996 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 "LiteralStorage.h"
33 #include "CodingSystem.h"
34 #include <string.h>
35
36 #ifdef DECLARE_MEMMOVE
37 extern "C" {
38   void *memmove(void *, const void *, size_t);
39 }
40 #endif
41
42 #ifdef SP_NAMESPACE
43 namespace SP_NAMESPACE {
44 #endif
45
46 class LiteralStorageObject : public StorageObject {
47 public:
48   LiteralStorageObject(const StringC &);
49   Boolean read(char *buf, size_t bufSize, Messenger &, size_t &nread);
50   Boolean rewind(Messenger &);
51 private:
52   LiteralStorageObject(const LiteralStorageObject &);   // undefined
53   void operator=(const LiteralStorageObject &); // undefined
54
55   StringC str_;
56   size_t nBytesRead_;
57 };
58
59 class MemoryInputCodingSystem : public InputCodingSystem {
60 public:
61   Decoder *makeDecoder() const;
62 };
63
64 class MemoryDecoder : public Decoder {
65 public:
66   MemoryDecoder();
67   size_t decode(Char *, const char *, size_t, const char **);
68 };
69
70 LiteralStorageManager::LiteralStorageManager(const char *type)
71 : type_(type)
72 {
73 }
74
75 StorageObject *LiteralStorageManager::makeStorageObject(const StringC &id,
76                                                         const StringC &,
77                                                         Boolean,
78                                                         Boolean,
79                                                         Messenger &,
80                                                         StringC &foundId)
81 {
82   foundId = id;
83   return new LiteralStorageObject(id);
84 }
85
86 const InputCodingSystem *LiteralStorageManager::requiredCodingSystem() const
87 {
88   static MemoryInputCodingSystem cs;
89   return &cs;
90 }
91
92 Boolean LiteralStorageManager::requiresCr() const
93 {
94   return 1;
95 }
96
97 const char *LiteralStorageManager::type() const
98 {
99   return type_;
100 }
101
102 Boolean LiteralStorageManager::inheritable() const
103 {
104   return 0;
105 }
106
107 LiteralStorageObject::LiteralStorageObject(const StringC &str)
108 : str_(str), nBytesRead_(0)
109 {
110 }
111
112 Boolean LiteralStorageObject::rewind(Messenger &)
113 {
114   nBytesRead_ = 0;
115   return 1;
116 }
117
118 Boolean LiteralStorageObject::read(char *buf, size_t bufSize,
119                                    Messenger &, size_t &nread)
120 {
121   if (nBytesRead_ >= str_.size()*sizeof(Char))
122     return 0;
123   nread = str_.size()*sizeof(Char) - nBytesRead_;
124   if (nread > bufSize)
125     nread = bufSize;
126   memcpy(buf, (char *)str_.data() + nBytesRead_, nread);
127   nBytesRead_ += nread;
128   return 1;
129 }
130
131 Decoder *MemoryInputCodingSystem::makeDecoder() const
132 {
133   return new MemoryDecoder;
134 }
135
136 MemoryDecoder::MemoryDecoder()
137 : Decoder(sizeof(Char))
138 {
139 }
140
141 size_t MemoryDecoder::decode(Char *to, const char *from, size_t fromLen,
142                              const char **rest)
143 {
144   size_t nChars = fromLen/sizeof(Char);
145   *rest = from + nChars*sizeof(Char);
146   if (from != (char *)to)
147     memmove(to, from, nChars*sizeof(Char));
148   return nChars;
149 }
150
151 #ifdef SP_NAMESPACE
152 }
153 #endif