LiteralStorage.C 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 libraries 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. #ifdef __GNUG__
  27. #pragma implementation
  28. #endif
  29. #include "splib.h"
  30. #include "LiteralStorage.h"
  31. #include "CodingSystem.h"
  32. #include <string.h>
  33. #ifdef DECLARE_MEMMOVE
  34. extern "C" {
  35. void *memmove(void *, const void *, size_t);
  36. }
  37. #endif
  38. #ifdef SP_NAMESPACE
  39. namespace SP_NAMESPACE {
  40. #endif
  41. class LiteralStorageObject : public StorageObject {
  42. public:
  43. LiteralStorageObject(const StringC &);
  44. Boolean read(char *buf, size_t bufSize, Messenger &, size_t &nread);
  45. Boolean rewind(Messenger &);
  46. private:
  47. LiteralStorageObject(const LiteralStorageObject &); // undefined
  48. void operator=(const LiteralStorageObject &); // undefined
  49. StringC str_;
  50. size_t nBytesRead_;
  51. };
  52. class MemoryInputCodingSystem : public InputCodingSystem {
  53. public:
  54. Decoder *makeDecoder() const;
  55. };
  56. class MemoryDecoder : public Decoder {
  57. public:
  58. MemoryDecoder();
  59. size_t decode(Char *, const char *, size_t, const char **);
  60. };
  61. LiteralStorageManager::LiteralStorageManager(const char *type)
  62. : type_(type)
  63. {
  64. }
  65. StorageObject *LiteralStorageManager::makeStorageObject(const StringC &id,
  66. const StringC &,
  67. Boolean,
  68. Boolean,
  69. Messenger &,
  70. StringC &foundId)
  71. {
  72. foundId = id;
  73. return new LiteralStorageObject(id);
  74. }
  75. const InputCodingSystem *LiteralStorageManager::requiredCodingSystem() const
  76. {
  77. static MemoryInputCodingSystem cs;
  78. return &cs;
  79. }
  80. Boolean LiteralStorageManager::requiresCr() const
  81. {
  82. return 1;
  83. }
  84. const char *LiteralStorageManager::type() const
  85. {
  86. return type_;
  87. }
  88. Boolean LiteralStorageManager::inheritable() const
  89. {
  90. return 0;
  91. }
  92. LiteralStorageObject::LiteralStorageObject(const StringC &str)
  93. : str_(str), nBytesRead_(0)
  94. {
  95. }
  96. Boolean LiteralStorageObject::rewind(Messenger &)
  97. {
  98. nBytesRead_ = 0;
  99. return 1;
  100. }
  101. Boolean LiteralStorageObject::read(char *buf, size_t bufSize,
  102. Messenger &, size_t &nread)
  103. {
  104. if (nBytesRead_ >= str_.size()*sizeof(Char))
  105. return 0;
  106. nread = str_.size()*sizeof(Char) - nBytesRead_;
  107. if (nread > bufSize)
  108. nread = bufSize;
  109. memcpy(buf, (char *)str_.data() + nBytesRead_, nread);
  110. nBytesRead_ += nread;
  111. return 1;
  112. }
  113. Decoder *MemoryInputCodingSystem::makeDecoder() const
  114. {
  115. return new MemoryDecoder;
  116. }
  117. MemoryDecoder::MemoryDecoder()
  118. : Decoder(sizeof(Char))
  119. {
  120. }
  121. size_t MemoryDecoder::decode(Char *to, const char *from, size_t fromLen,
  122. const char **rest)
  123. {
  124. size_t nChars = fromLen/sizeof(Char);
  125. *rest = from + nChars*sizeof(Char);
  126. if (from != (char *)to)
  127. memmove(to, from, nChars*sizeof(Char));
  128. return nChars;
  129. }
  130. #ifdef SP_NAMESPACE
  131. }
  132. #endif