/* * CDE - Common Desktop Environment * * Copyright (c) 1993-2012, The Open Group. All rights reserved. * * These libraries and programs are free software; you can * redistribute them and/or modify them under the terms of the GNU * Lesser General Public License as published by the Free Software * Foundation; either version 2 of the License, or (at your option) * any later version. * * These libraries and programs are distributed in the hope that * they will be useful, but WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public * License along with these libraries and programs; if not, write * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth * Floor, Boston, MA 02110-1301 USA */ /* $XConsortium: LiteralStorage.C /main/1 1996/07/29 16:56:08 cde-hp $ */ // Copyright (c) 1996 James Clark // See the file COPYING for copying permission. #ifdef __GNUG__ #pragma implementation #endif #include "splib.h" #include "LiteralStorage.h" #include "CodingSystem.h" #include #ifdef DECLARE_MEMMOVE extern "C" { void *memmove(void *, const void *, size_t); } #endif #ifdef SP_NAMESPACE namespace SP_NAMESPACE { #endif class LiteralStorageObject : public StorageObject { public: LiteralStorageObject(const StringC &); Boolean read(char *buf, size_t bufSize, Messenger &, size_t &nread); Boolean rewind(Messenger &); private: LiteralStorageObject(const LiteralStorageObject &); // undefined void operator=(const LiteralStorageObject &); // undefined StringC str_; size_t nBytesRead_; }; class MemoryInputCodingSystem : public InputCodingSystem { public: Decoder *makeDecoder() const; }; class MemoryDecoder : public Decoder { public: MemoryDecoder(); size_t decode(Char *, const char *, size_t, const char **); }; LiteralStorageManager::LiteralStorageManager(const char *type) : type_(type) { } StorageObject *LiteralStorageManager::makeStorageObject(const StringC &id, const StringC &, Boolean, Boolean, Messenger &, StringC &foundId) { foundId = id; return new LiteralStorageObject(id); } const InputCodingSystem *LiteralStorageManager::requiredCodingSystem() const { static MemoryInputCodingSystem cs; return &cs; } Boolean LiteralStorageManager::requiresCr() const { return 1; } const char *LiteralStorageManager::type() const { return type_; } Boolean LiteralStorageManager::inheritable() const { return 0; } LiteralStorageObject::LiteralStorageObject(const StringC &str) : str_(str), nBytesRead_(0) { } Boolean LiteralStorageObject::rewind(Messenger &) { nBytesRead_ = 0; return 1; } Boolean LiteralStorageObject::read(char *buf, size_t bufSize, Messenger &, size_t &nread) { if (nBytesRead_ >= str_.size()*sizeof(Char)) return 0; nread = str_.size()*sizeof(Char) - nBytesRead_; if (nread > bufSize) nread = bufSize; memcpy(buf, (char *)str_.data() + nBytesRead_, nread); nBytesRead_ += nread; return 1; } Decoder *MemoryInputCodingSystem::makeDecoder() const { return new MemoryDecoder; } MemoryDecoder::MemoryDecoder() : Decoder(sizeof(Char)) { } size_t MemoryDecoder::decode(Char *to, const char *from, size_t fromLen, const char **rest) { size_t nChars = fromLen/sizeof(Char); *rest = from + nChars*sizeof(Char); if (from != (char *)to) memmove(to, from, nChars*sizeof(Char)); return nChars; } #ifdef SP_NAMESPACE } #endif