Back to index

lightning-sunbird  0.9+nobinonly
rcfileio.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** Class implementation for normal and special file I/O (ref: prio.h)
00040 */
00041 
00042 #include "rcfileio.h"
00043 
00044 #include <string.h>
00045 
00046 RCFileIO::RCFileIO(): RCIO(RCIO::file) { }
00047 
00048 RCFileIO::~RCFileIO() { if (NULL != fd) (void)Close(); }
00049 
00050 PRInt64 RCFileIO::Available()
00051     { return fd->methods->available(fd); }
00052 
00053 PRStatus RCFileIO::Close()
00054     { PRStatus rv = fd->methods->close(fd); fd = NULL; return rv; }
00055 
00056 PRStatus RCFileIO::Delete(const char* filename) { return PR_Delete(filename); }
00057 
00058 PRStatus RCFileIO::FileInfo(RCFileInfo* info) const
00059     { return fd->methods->fileInfo64(fd, &info->info); }
00060 
00061 PRStatus RCFileIO::FileInfo(const char *name, RCFileInfo* info)
00062     { return PR_GetFileInfo64(name, &info->info); }
00063 
00064 PRStatus RCFileIO::Fsync()
00065     { return fd->methods->fsync(fd); }
00066 
00067 PRStatus RCFileIO::Open(const char *filename, PRIntn flags, PRIntn mode)
00068 {
00069     fd = PR_Open(filename, flags, mode);
00070     return (NULL == fd) ? PR_FAILURE : PR_SUCCESS;
00071 }  /* RCFileIO::Open */
00072 
00073 PRInt32 RCFileIO::Read(void *buf, PRSize amount)
00074     { return fd->methods->read(fd, buf, amount); }
00075 
00076 PRInt64 RCFileIO::Seek(PRInt64 offset, RCIO::Whence how)
00077 {
00078     PRSeekWhence whence;
00079     switch (how)
00080     {
00081         case RCFileIO::set: whence = PR_SEEK_SET; break;
00082         case RCFileIO::current: whence = PR_SEEK_CUR; break;
00083         case RCFileIO::end: whence = PR_SEEK_END; break;
00084         default: whence = (PRSeekWhence)-1;
00085     }
00086     return fd->methods->seek64(fd, offset, whence);
00087 }  /* RCFileIO::Seek */
00088 
00089 PRInt32 RCFileIO::Write(const void *buf, PRSize amount)
00090     { return fd->methods->write(fd, buf, amount); }
00091 
00092 PRInt32 RCFileIO::Writev(
00093     const PRIOVec *iov, PRSize size, const RCInterval& timeout)
00094     { return fd->methods->writev(fd, iov, size, timeout); }
00095 
00096 RCIO *RCFileIO::GetSpecialFile(RCFileIO::SpecialFile special)
00097 {
00098     PRFileDesc* fd;
00099     PRSpecialFD which;
00100     RCFileIO* spec = NULL;
00101 
00102     switch (special)
00103     {
00104         case RCFileIO::input: which = PR_StandardInput; break;
00105         case RCFileIO::output: which = PR_StandardOutput; break;
00106         case RCFileIO::error: which = PR_StandardError; break;
00107         default: which = (PRSpecialFD)-1;
00108     }
00109     fd = PR_GetSpecialFD(which);
00110     if (NULL != fd)
00111     {
00112         spec = new RCFileIO();
00113         if (NULL != spec) spec->fd = fd;
00114     }
00115     return spec;
00116 }  /* RCFileIO::GetSpecialFile */
00117 
00118 
00119 /*
00120 ** The following methods have been made non-virtual and private. These
00121 ** default implementations are intended to NEVER be called. They
00122 ** are not valid for this type of I/O class (normal and special file).
00123 */
00124 PRStatus RCFileIO::Connect(const RCNetAddr&, const RCInterval&)
00125 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00126 
00127 PRStatus RCFileIO::GetLocalName(RCNetAddr*) const
00128 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00129 
00130 PRStatus RCFileIO::GetPeerName(RCNetAddr*) const
00131 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00132 
00133 PRStatus RCFileIO::GetSocketOption(PRSocketOptionData*) const
00134 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00135 
00136 PRStatus RCFileIO::Listen(PRIntn)
00137 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00138 
00139 PRInt16 RCFileIO::Poll(PRInt16, PRInt16*)
00140 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return 0; }
00141 
00142 PRInt32 RCFileIO::Recv(void*, PRSize, PRIntn, const RCInterval&)
00143 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
00144 
00145 PRInt32 RCFileIO::Recvfrom(void*, PRSize, PRIntn, RCNetAddr*, const RCInterval&)
00146 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
00147 
00148 PRInt32 RCFileIO::Send(
00149     const void*, PRSize, PRIntn, const RCInterval&)
00150 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
00151 
00152 PRInt32 RCFileIO::Sendto(
00153     const void*, PRSize, PRIntn, const RCNetAddr&, const RCInterval&)
00154 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
00155 
00156 RCIO* RCFileIO::Accept(RCNetAddr*, const RCInterval&)
00157 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return NULL; }
00158 
00159 PRStatus RCFileIO::Bind(const RCNetAddr&)
00160 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00161 
00162 PRInt32 RCFileIO::AcceptRead(
00163     RCIO**, RCNetAddr**, void*, PRSize, const RCInterval&)
00164 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
00165 
00166 PRStatus RCFileIO::SetSocketOption(const PRSocketOptionData*)
00167 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00168 
00169 PRStatus RCFileIO::Shutdown(RCIO::ShutdownHow)
00170 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00171 
00172 PRInt32 RCFileIO::TransmitFile(
00173     RCIO*, const void*, PRSize, RCIO::FileDisposition, const RCInterval&)
00174 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
00175 
00176 /*
00177 ** Class implementation for file information object (ref: prio.h)
00178 */
00179 
00180 RCFileInfo::~RCFileInfo() { }
00181 
00182 RCFileInfo::RCFileInfo(const RCFileInfo& her): RCBase()
00183     { info = her.info; }  /* RCFileInfo::RCFileInfo */
00184 
00185 RCTime RCFileInfo::CreationTime() const { return RCTime(info.creationTime); }
00186 
00187 RCTime RCFileInfo::ModifyTime() const { return RCTime(info.modifyTime); }
00188 
00189 RCFileInfo::FileType RCFileInfo::Type() const
00190 {
00191     RCFileInfo::FileType type;
00192     switch (info.type)
00193     {
00194         case PR_FILE_FILE: type = RCFileInfo::file; break;
00195         case PR_FILE_DIRECTORY: type = RCFileInfo::directory; break;
00196         default: type = RCFileInfo::other;
00197     }
00198     return type;
00199 }  /* RCFileInfo::Type */