File: RewindStorageObject.cxx

package info (click to toggle)
opensp 1.5.2-15.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,168 kB
  • sloc: cpp: 65,784; ansic: 17,124; sh: 11,193; xml: 2,704; makefile: 895; perl: 561; yacc: 288; sed: 16
file content (78 lines) | stat: -rw-r--r-- 1,560 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
// Copyright (c) 1994, 1995 James Clark
// See the file COPYING for copying permission.

#ifdef __GNUG__
#pragma implementation
#endif

#include "splib.h"
#include "RewindStorageObject.h"
#include "macros.h"

#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif

RewindStorageObject::RewindStorageObject(Boolean mayRewind, Boolean canSeek)
: mayRewind_(mayRewind), canSeek_(canSeek),
  savingBytes_(mayRewind && !canSeek), readingSaved_(0)
{
}

Boolean RewindStorageObject::rewind(Messenger &mgr)
{
  ASSERT(mayRewind_);
  if (canSeek_)
    return seekToStart(mgr);
  else {
    readingSaved_ = 1;
    nBytesRead_ = 0;
    return 1;
  }
}

void RewindStorageObject::unread(const char *s, size_t n)
{
  savedBytes_.append(s, n);
  if (!readingSaved_) {
    readingSaved_ = 1;
    nBytesRead_ = 0;
  }
}

void RewindStorageObject::willNotRewind()
{
  mayRewind_ = 0;
  savingBytes_ = 0;
  if (!readingSaved_) {
    // Ensure that memory is released now.
    String<char> tem;
    tem.swap(savedBytes_);
  }
}

Boolean RewindStorageObject::readSaved(char *buf, size_t bufSize,
				       size_t &nread)
{
  if (!readingSaved_)
    return 0;
  if (nBytesRead_ >= savedBytes_.size()) {
    if (!mayRewind_) {
      // Ensure that memory is released now.
      String<char> tem;
      tem.swap(savedBytes_);
    }
    readingSaved_ = 0;
    return 0;
  }
  nread = savedBytes_.size() - nBytesRead_;
  if (nread > bufSize)
    nread = bufSize;
  memcpy(buf, savedBytes_.data() + nBytesRead_, nread);
  nBytesRead_ += nread;
  return 1;
}

#ifdef SP_NAMESPACE
}
#endif