1 // Copyright (c) 1996 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident "%Z%%M% %I% %E% SMI"
10 #include "LiteralStorage.h"
11 #include "CodingSystem.h"
14 #ifdef DECLARE_MEMMOVE
16 void *memmove(void *, const void *, size_t);
21 namespace SP_NAMESPACE
{
24 class LiteralStorageObject
: public StorageObject
{
26 LiteralStorageObject(const StringC
&);
27 Boolean
read(char *buf
, size_t bufSize
, Messenger
&, size_t &nread
);
28 Boolean
rewind(Messenger
&);
30 LiteralStorageObject(const LiteralStorageObject
&); // undefined
31 void operator=(const LiteralStorageObject
&); // undefined
37 class MemoryInputCodingSystem
: public InputCodingSystem
{
39 Decoder
*makeDecoder() const;
42 class MemoryDecoder
: public Decoder
{
45 size_t decode(Char
*, const char *, size_t, const char **);
48 LiteralStorageManager::LiteralStorageManager(const char *type
)
53 StorageObject
*LiteralStorageManager::makeStorageObject(const StringC
&id
,
61 return new LiteralStorageObject(id
);
64 const InputCodingSystem
*LiteralStorageManager::requiredCodingSystem() const
66 static MemoryInputCodingSystem cs
;
70 Boolean
LiteralStorageManager::requiresCr() const
75 const char *LiteralStorageManager::type() const
80 Boolean
LiteralStorageManager::inheritable() const
85 LiteralStorageObject::LiteralStorageObject(const StringC
&str
)
86 : str_(str
), nBytesRead_(0)
90 Boolean
LiteralStorageObject::rewind(Messenger
&)
96 Boolean
LiteralStorageObject::read(char *buf
, size_t bufSize
,
97 Messenger
&, size_t &nread
)
99 if (nBytesRead_
>= str_
.size()*sizeof(Char
))
101 nread
= str_
.size()*sizeof(Char
) - nBytesRead_
;
104 memcpy(buf
, (char *)str_
.data() + nBytesRead_
, nread
);
105 nBytesRead_
+= nread
;
109 Decoder
*MemoryInputCodingSystem::makeDecoder() const
111 return new MemoryDecoder
;
114 MemoryDecoder::MemoryDecoder()
115 : Decoder(sizeof(Char
))
119 size_t MemoryDecoder::decode(Char
*to
, const char *from
, size_t fromLen
,
122 size_t nChars
= fromLen
/sizeof(Char
);
123 *rest
= from
+ nChars
*sizeof(Char
);
124 if (from
!= (char *)to
)
125 memmove(to
, from
, nChars
*sizeof(Char
));