Debian Codegraph @

nsprpub/pr/src/cplus/rcfileio.cpp

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 ** Class implementation for normal and special file I/O (ref: prio.h)
8 */
9
10 #include "rcfileio.h"
11
12 #include <string.h>
13
14 RCFileIO::RCFileIO(): RCIO(RCIO::file) { }
15
16 RCFileIO::~RCFileIO() {
17 if (NULL != fd) {
18 (void)Close();
19 }
20 }
21
22 PRInt64 RCFileIO::Available()
23 {
24 return fd->methods->available(fd);
25 }
26
27 PRStatus RCFileIO::Close()
28 {
29 PRStatus rv = fd->methods->close(fd);
30 fd = NULL;
31 return rv;
32 }
33
34 PRStatus RCFileIO::Delete(const char* filename) {
35 return PR_Delete(filename);
36 }
37
38 PRStatus RCFileIO::FileInfo(RCFileInfo* info) const
39 {
40 return fd->methods->fileInfo64(fd, &info->info);
41 }
42
43 PRStatus RCFileIO::FileInfo(const char *name, RCFileInfo* info)
44 {
45 return PR_GetFileInfo64(name, &info->info);
46 }
47
48 PRStatus RCFileIO::Fsync()
49 {
50 return fd->methods->fsync(fd);
51 }
52
53 PRStatus RCFileIO::Open(const char *filename, PRIntn flags, PRIntn mode)
54 {
55 fd = PR_Open(filename, flags, mode);
56 return (NULL == fd) ? PR_FAILURE : PR_SUCCESS;
57 } /* RCFileIO::Open */
58
59 PRInt32 RCFileIO::Read(void *buf, PRSize amount)
60 {
61 return fd->methods->read(fd, buf, amount);
62 }
63
64 PRInt64 RCFileIO::Seek(PRInt64 offset, RCIO::Whence how)
65 {
66 PRSeekWhence whence;
67 switch (how)
68 {
69 case RCFileIO::set: whence = PR_SEEK_SET; break;
70 case RCFileIO::current: whence = PR_SEEK_CUR; break;
71 case RCFileIO::end: whence = PR_SEEK_END; break;
72 default: whence = (PRSeekWhence)-1;
73 }
74 return fd->methods->seek64(fd, offset, whence);
75 } /* RCFileIO::Seek */
76
77 PRInt32 RCFileIO::Write(const void *buf, PRSize amount)
78 {
79 return fd->methods->write(fd, buf, amount);
80 }
81
82 PRInt32 RCFileIO::Writev(
83 const PRIOVec *iov, PRSize size, const RCInterval& timeout)
84 {
85 return fd->methods->writev(fd, iov, size, timeout);
86 }
87
88 RCIO *RCFileIO::GetSpecialFile(RCFileIO::SpecialFile special)
89 {
90 PRFileDesc* fd;
91 PRSpecialFD which;
92 RCFileIO* spec = NULL;
93
94 switch (special)
95 {
96 case RCFileIO::input: which = PR_StandardInput; break;
97 case RCFileIO::output: which = PR_StandardOutput; break;
98 case RCFileIO::error: which = PR_StandardError; break;
99 default: which = (PRSpecialFD)-1;
100 }
101 fd = PR_GetSpecialFD(which);
102 if (NULL != fd)
103 {
104 spec = new RCFileIO();
105 if (NULL != spec) {
106 spec->fd = fd;
107 }
108 }
109 return spec;
110 } /* RCFileIO::GetSpecialFile */
111
112
113 /*
114 ** The following methods have been made non-virtual and private. These
115 ** default implementations are intended to NEVER be called. They
116 ** are not valid for this type of I/O class (normal and special file).
117 */
118 PRStatus RCFileIO::Connect(const RCNetAddr&, const RCInterval&)
119 {
120 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
121 return PR_FAILURE;
122 }
123
124 PRStatus RCFileIO::GetLocalName(RCNetAddr*) const
125 {
126 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
127 return PR_FAILURE;
128 }
129
130 PRStatus RCFileIO::GetPeerName(RCNetAddr*) const
131 {
132 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
133 return PR_FAILURE;
134 }
135
136 PRStatus RCFileIO::GetSocketOption(PRSocketOptionData*) const
137 {
138 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
139 return PR_FAILURE;
140 }
141
142 PRStatus RCFileIO::Listen(PRIntn)
143 {
144 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
145 return PR_FAILURE;
146 }
147
148 PRInt16 RCFileIO::Poll(PRInt16, PRInt16*)
149 {
150 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
151 return 0;
152 }
153
154 PRInt32 RCFileIO::Recv(void*, PRSize, PRIntn, const RCInterval&)
155 {
156 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
157 return -1;
158 }
159
160 PRInt32 RCFileIO::Recvfrom(void*, PRSize, PRIntn, RCNetAddr*, const RCInterval&)
161 {
162 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
163 return -1;
164 }
165
166 PRInt32 RCFileIO::Send(
167 const void*, PRSize, PRIntn, const RCInterval&)
168 {
169 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
170 return -1;
171 }
172
173 PRInt32 RCFileIO::Sendto(
174 const void*, PRSize, PRIntn, const RCNetAddr&, const RCInterval&)
175 {
176 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
177 return -1;
178 }
179
180 RCIO* RCFileIO::Accept(RCNetAddr*, const RCInterval&)
181 {
182 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
183 return NULL;
184 }
185
186 PRStatus RCFileIO::Bind(const RCNetAddr&)
187 {
188 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
189 return PR_FAILURE;
190 }
191
192 PRInt32 RCFileIO::AcceptRead(
193 RCIO**, RCNetAddr**, void*, PRSize, const RCInterval&)
194 {
195 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
196 return -1;
197 }
198
199 PRStatus RCFileIO::SetSocketOption(const PRSocketOptionData*)
200 {
201 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
202 return PR_FAILURE;
203 }
204
205 PRStatus RCFileIO::Shutdown(RCIO::ShutdownHow)
206 {
207 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
208 return PR_FAILURE;
209 }
210
211 PRInt32 RCFileIO::TransmitFile(
212 RCIO*, const void*, PRSize, RCIO::FileDisposition, const RCInterval&)
213 {
214 PR_SetError(PR_INVALID_METHOD_ERROR, 0);
215 return -1;
216 }
217
218 /*
219 ** Class implementation for file information object (ref: prio.h)
220 */
221
222 RCFileInfo::~RCFileInfo() { }
223
224 RCFileInfo::RCFileInfo(const RCFileInfo& her): RCBase()
225 {
226 info = her.info; /* RCFileInfo::RCFileInfo */
227 }
228
229 RCTime RCFileInfo::CreationTime() const {
230 return RCTime(info.creationTime);
231 }
232
233 RCTime RCFileInfo::ModifyTime() const {
234 return RCTime(info.modifyTime);
235 }
236
237 RCFileInfo::FileType RCFileInfo::Type() const
238 {
239 RCFileInfo::FileType type;
240 switch (info.type)
241 {
242 case PR_FILE_FILE: type = RCFileInfo::file; break;
243 case PR_FILE_DIRECTORY: type = RCFileInfo::directory; break;
244 default: type = RCFileInfo::other;
245 }
246 return type;
247 } /* RCFileInfo::Type */