File: LiteralStorage.cxx

package info (click to toggle)
opensp 1.5.2-10
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,856 kB
  • sloc: cpp: 65,784; ansic: 17,124; sh: 13,693; xml: 2,704; makefile: 910; perl: 561; yacc: 288; sed: 16
file content (130 lines) | stat: -rw-r--r-- 2,621 bytes parent folder | download | duplicates (22)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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 <string.h>

#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