nspr: import 3.0 RC1 cutoff from CVS
[mozilla-nspr.git] / nsprpub / pr / src / cplus / rcfileio.cpp
blob7646c89401dc2efa1b844fbb5b2160a2eb6d310b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 ** Class implementation for normal and special file I/O (ref: prio.h)
42 #include "rcfileio.h"
44 #include <string.h>
46 RCFileIO::RCFileIO(): RCIO(RCIO::file) { }
48 RCFileIO::~RCFileIO() { if (NULL != fd) (void)Close(); }
50 PRInt64 RCFileIO::Available()
51 { return fd->methods->available(fd); }
53 PRStatus RCFileIO::Close()
54 { PRStatus rv = fd->methods->close(fd); fd = NULL; return rv; }
56 PRStatus RCFileIO::Delete(const char* filename) { return PR_Delete(filename); }
58 PRStatus RCFileIO::FileInfo(RCFileInfo* info) const
59 { return fd->methods->fileInfo64(fd, &info->info); }
61 PRStatus RCFileIO::FileInfo(const char *name, RCFileInfo* info)
62 { return PR_GetFileInfo64(name, &info->info); }
64 PRStatus RCFileIO::Fsync()
65 { return fd->methods->fsync(fd); }
67 PRStatus RCFileIO::Open(const char *filename, PRIntn flags, PRIntn mode)
69 fd = PR_Open(filename, flags, mode);
70 return (NULL == fd) ? PR_FAILURE : PR_SUCCESS;
71 } /* RCFileIO::Open */
73 PRInt32 RCFileIO::Read(void *buf, PRSize amount)
74 { return fd->methods->read(fd, buf, amount); }
76 PRInt64 RCFileIO::Seek(PRInt64 offset, RCIO::Whence how)
78 PRSeekWhence whence;
79 switch (how)
81 case RCFileIO::set: whence = PR_SEEK_SET; break;
82 case RCFileIO::current: whence = PR_SEEK_CUR; break;
83 case RCFileIO::end: whence = PR_SEEK_END; break;
84 default: whence = (PRSeekWhence)-1;
86 return fd->methods->seek64(fd, offset, whence);
87 } /* RCFileIO::Seek */
89 PRInt32 RCFileIO::Write(const void *buf, PRSize amount)
90 { return fd->methods->write(fd, buf, amount); }
92 PRInt32 RCFileIO::Writev(
93 const PRIOVec *iov, PRSize size, const RCInterval& timeout)
94 { return fd->methods->writev(fd, iov, size, timeout); }
96 RCIO *RCFileIO::GetSpecialFile(RCFileIO::SpecialFile special)
98 PRFileDesc* fd;
99 PRSpecialFD which;
100 RCFileIO* spec = NULL;
102 switch (special)
104 case RCFileIO::input: which = PR_StandardInput; break;
105 case RCFileIO::output: which = PR_StandardOutput; break;
106 case RCFileIO::error: which = PR_StandardError; break;
107 default: which = (PRSpecialFD)-1;
109 fd = PR_GetSpecialFD(which);
110 if (NULL != fd)
112 spec = new RCFileIO();
113 if (NULL != spec) spec->fd = fd;
115 return spec;
116 } /* RCFileIO::GetSpecialFile */
120 ** The following methods have been made non-virtual and private. These
121 ** default implementations are intended to NEVER be called. They
122 ** are not valid for this type of I/O class (normal and special file).
124 PRStatus RCFileIO::Connect(const RCNetAddr&, const RCInterval&)
125 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
127 PRStatus RCFileIO::GetLocalName(RCNetAddr*) const
128 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
130 PRStatus RCFileIO::GetPeerName(RCNetAddr*) const
131 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
133 PRStatus RCFileIO::GetSocketOption(PRSocketOptionData*) const
134 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
136 PRStatus RCFileIO::Listen(PRIntn)
137 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
139 PRInt16 RCFileIO::Poll(PRInt16, PRInt16*)
140 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return 0; }
142 PRInt32 RCFileIO::Recv(void*, PRSize, PRIntn, const RCInterval&)
143 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
145 PRInt32 RCFileIO::Recvfrom(void*, PRSize, PRIntn, RCNetAddr*, const RCInterval&)
146 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
148 PRInt32 RCFileIO::Send(
149 const void*, PRSize, PRIntn, const RCInterval&)
150 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
152 PRInt32 RCFileIO::Sendto(
153 const void*, PRSize, PRIntn, const RCNetAddr&, const RCInterval&)
154 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
156 RCIO* RCFileIO::Accept(RCNetAddr*, const RCInterval&)
157 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return NULL; }
159 PRStatus RCFileIO::Bind(const RCNetAddr&)
160 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
162 PRInt32 RCFileIO::AcceptRead(
163 RCIO**, RCNetAddr**, void*, PRSize, const RCInterval&)
164 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
166 PRStatus RCFileIO::SetSocketOption(const PRSocketOptionData*)
167 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
169 PRStatus RCFileIO::Shutdown(RCIO::ShutdownHow)
170 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
172 PRInt32 RCFileIO::TransmitFile(
173 RCIO*, const void*, PRSize, RCIO::FileDisposition, const RCInterval&)
174 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
177 ** Class implementation for file information object (ref: prio.h)
180 RCFileInfo::~RCFileInfo() { }
182 RCFileInfo::RCFileInfo(const RCFileInfo& her): RCBase()
183 { info = her.info; } /* RCFileInfo::RCFileInfo */
185 RCTime RCFileInfo::CreationTime() const { return RCTime(info.creationTime); }
187 RCTime RCFileInfo::ModifyTime() const { return RCTime(info.modifyTime); }
189 RCFileInfo::FileType RCFileInfo::Type() const
191 RCFileInfo::FileType type;
192 switch (info.type)
194 case PR_FILE_FILE: type = RCFileInfo::file; break;
195 case PR_FILE_DIRECTORY: type = RCFileInfo::directory; break;
196 default: type = RCFileInfo::other;
198 return type;
199 } /* RCFileInfo::Type */