"Fossies" - the Fresh Open Source Software Archive

Member "OpenSP-1.5.2/lib/LiteralStorage.cxx" (21 Jul 2005, 2621 Bytes) of package /linux/misc/old/OpenSP-1.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "LiteralStorage.cxx" see the Fossies "Dox" file reference documentation.

    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 
   47 LiteralStorageManager::LiteralStorageManager(const char *type)
   48 : type_(type)
   49 {
   50 }
   51 
   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 
   63 const InputCodingSystem *LiteralStorageManager::requiredCodingSystem() const
   64 {
   65   static MemoryInputCodingSystem cs;
   66   return &cs;
   67 }
   68 
   69 Boolean LiteralStorageManager::requiresCr() const
   70 {
   71   return 1;
   72 }
   73 
   74 const char *LiteralStorageManager::type() const
   75 {
   76   return type_;
   77 }
   78 
   79 Boolean LiteralStorageManager::inheritable() const
   80 {
   81   return 0;
   82 }
   83 
   84 LiteralStorageObject::LiteralStorageObject(const StringC &str)
   85 : str_(str), nBytesRead_(0)
   86 {
   87 }
   88 
   89 Boolean LiteralStorageObject::rewind(Messenger &)
   90 {
   91   nBytesRead_ = 0;
   92   return 1;
   93 }
   94 
   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 
  108 Decoder *MemoryInputCodingSystem::makeDecoder() const
  109 {
  110   return new MemoryDecoder;
  111 }
  112 
  113 MemoryDecoder::MemoryDecoder()
  114 : Decoder(sizeof(Char))
  115 {
  116 }
  117 
  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