File: StdioStorage.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 (156 lines) | stat: -rw-r--r-- 3,172 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 1994, 1995 James Clark
// See the file COPYING for copying permission.

#ifdef __GNUG__
#pragma implementation
#endif

#include "splib.h"
#include "StdioStorage.h"
#include "Message.h"
#include "types.h"
#include "ErrnoMessageArg.h"
#include "StringOf.h"
#include "StringC.h"
#include "CodingSystem.h"

#include "StdioStorageMessages.h"

#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <errno.h>

#ifndef SEEK_SET
#define SEEK_SET 0
#endif

#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif

class StdioStorageObject : public StorageObject {
public:
  StdioStorageObject(FILE *fp, const StringC &filename);
  ~StdioStorageObject();
  Boolean read(char *buf, size_t bufSize, Messenger &mgr, size_t &nread);
  Boolean rewind(Messenger &mgr);
  size_t getBlockSize() const;
private:
  enum ErrorIndex {
    invalidErrorIndex,
    fopenFailed,
    readError,
    seekError
  };
  void error(Messenger &mgr, const MessageType2 &, int err);

  FILE *fp_;
  StringC filename_;
  String<char> filenameBytes_;
};

StdioStorageManager::StdioStorageManager(const char *type,
					 const CharsetInfo *filenameCharset,
					 const OutputCodingSystem *filenameCodingSystem)
: IdStorageManager(filenameCharset),
  type_(type),
  filenameCodingSystem_(filenameCodingSystem)
{
}

StorageObject *StdioStorageManager::makeStorageObject(const StringC &str,
						      const StringC &,
						      Boolean,
						      Boolean,
						      Messenger &mgr,
						      StringC &filename)
{
  filename = str;
  String<char> filenameBytes = filenameCodingSystem_->convertOut(filename);
  errno = 0;
  FILE *fp = fopen(filenameBytes.data(), "r");
  if (!fp) {
    ParentLocationMessenger(mgr).message(StdioStorageMessages::openFailed,
					 StringMessageArg(filename),
					 ErrnoMessageArg(errno));

    return 0;
  }
  return new StdioStorageObject(fp, filename);
}

const char *StdioStorageManager::type() const
{
  return type_;
}

StdioStorageObject::StdioStorageObject(FILE *fp, const StringC &filename)
: fp_(fp), filename_(filename)
{
}

StdioStorageObject::~StdioStorageObject()
{
  if (fp_) {
    fclose(fp_);
    fp_ = 0;
  }
}

Boolean StdioStorageObject::rewind(Messenger &mgr)
{
  if (fp_) {
    errno = 0;
    if (fseek(fp_, 0L, SEEK_SET) < 0) {
      error(mgr, StdioStorageMessages::seekFailed, errno);
      return 0;
    }
    return 1;
  }
  return 1;
}

size_t StdioStorageObject::getBlockSize() const
{
  return BUFSIZ;
}

Boolean StdioStorageObject::read(char *buf, size_t bufSize, Messenger &mgr,
				 size_t &nread)
{
  if (!fp_)
    return 0;
  errno = 0;
  size_t n = 0;
  FILE *fp = fp_;
  while (n < bufSize) {
    int c = getc(fp);
    if (c == EOF) {
      if (ferror(fp)) {
	error(mgr, StdioStorageMessages::readFailed, errno);
	(void)fclose(fp);
	return 0;
      }
      fclose(fp);
      fp_ = 0;
      break;
    }
    buf[n++] = c;
  }
  nread = n;
  return n > 0;
}

void StdioStorageObject::error(Messenger &mgr,
			       const MessageType2 &msg,
			       int err)
{
  ParentLocationMessenger(mgr).message(msg,
				       StringMessageArg(filename_),
				       ErrnoMessageArg(err));
}

#ifdef SP_NAMESPACE
}
#endif