1 // Copyright (c) 1996 James Clark
2 // See the file COPYING for copying permission.
3
4 #ifdef __GNUG__
5 #pragma implementation
6 #endif
7
8 #include "splib.h"
9 #include "LiteralStorage.h"
10 #include "CodingSystem.h"
11 #include <string.h>
12
13 #ifdef DECLARE_MEMMOVE
14 extern "C" {
15 void *memmove(void *, const void *, size_t);
16 }
17 #endif
18
19 #ifdef SP_NAMESPACE
20 namespace SP_NAMESPACE {
21 #endif
22
23 class LiteralStorageObject : public StorageObject {
24 public:
25 LiteralStorageObject(const StringC &);
26 Boolean read(char *buf, size_t bufSize, Messenger &, size_t &nread);
27 Boolean rewind(Messenger &);
28 private:
29 LiteralStorageObject(const LiteralStorageObject &); // undefined
30 void operator=(const LiteralStorageObject &); // undefined
31
32 StringC str_;
33 size_t nBytesRead_;
34 };
35
36 class MemoryInputCodingSystem : public InputCodingSystem {
37 public:
38 Decoder *makeDecoder() const;
39 };
40
41 class MemoryDecoder : public Decoder {
42 public:
43 MemoryDecoder();
44 size_t decode(Char *, const char *, size_t, const char **);
45 };
46
LiteralStorageManager(const char * type)47 LiteralStorageManager::LiteralStorageManager(const char *type)
48 : type_(type)
49 {
50 }
51
makeStorageObject(const StringC & id,const StringC &,Boolean,Boolean,Messenger &,StringC & foundId)52 StorageObject *LiteralStorageManager::makeStorageObject(const StringC &id,
53 const StringC &,
54 Boolean,
55 Boolean,
56 Messenger &,
57 StringC &foundId)
58 {
59 foundId = id;
60 return new LiteralStorageObject(id);
61 }
62
requiredCodingSystem() const63 const InputCodingSystem *LiteralStorageManager::requiredCodingSystem() const
64 {
65 static MemoryInputCodingSystem cs;
66 return &cs;
67 }
68
requiresCr() const69 Boolean LiteralStorageManager::requiresCr() const
70 {
71 return 1;
72 }
73
type() const74 const char *LiteralStorageManager::type() const
75 {
76 return type_;
77 }
78
inheritable() const79 Boolean LiteralStorageManager::inheritable() const
80 {
81 return 0;
82 }
83
LiteralStorageObject(const StringC & str)84 LiteralStorageObject::LiteralStorageObject(const StringC &str)
85 : str_(str), nBytesRead_(0)
86 {
87 }
88
rewind(Messenger &)89 Boolean LiteralStorageObject::rewind(Messenger &)
90 {
91 nBytesRead_ = 0;
92 return 1;
93 }
94
read(char * buf,size_t bufSize,Messenger &,size_t & nread)95 Boolean LiteralStorageObject::read(char *buf, size_t bufSize,
96 Messenger &, size_t &nread)
97 {
98 if (nBytesRead_ >= str_.size()*sizeof(Char))
99 return 0;
100 nread = str_.size()*sizeof(Char) - nBytesRead_;
101 if (nread > bufSize)
102 nread = bufSize;
103 memcpy(buf, (char *)str_.data() + nBytesRead_, nread);
104 nBytesRead_ += nread;
105 return 1;
106 }
107
makeDecoder() const108 Decoder *MemoryInputCodingSystem::makeDecoder() const
109 {
110 return new MemoryDecoder;
111 }
112
MemoryDecoder()113 MemoryDecoder::MemoryDecoder()
114 : Decoder(sizeof(Char))
115 {
116 }
117
decode(Char * to,const char * from,size_t fromLen,const char ** rest)118 size_t MemoryDecoder::decode(Char *to, const char *from, size_t fromLen,
119 const char **rest)
120 {
121 size_t nChars = fromLen/sizeof(Char);
122 *rest = from + nChars*sizeof(Char);
123 if (from != (char *)to)
124 memmove(to, from, nChars*sizeof(Char));
125 return nChars;
126 }
127
128 #ifdef SP_NAMESPACE
129 }
130 #endif
131