Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / src / cplus / rcfileio.cpp
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
4  *
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/
9  *
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.
14  *
15  * The Original Code is the Netscape Portable Runtime (NSPR).
16  *
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.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 /*
39 ** Class implementation for normal and special file I/O (ref: prio.h)
40 */
41
42 #include "rcfileio.h"
43
44 #include <string.h>
45
46 RCFileIO::RCFileIO(): RCIO(RCIO::file) { }
47
48 RCFileIO::~RCFileIO() { if (NULL != fd) (void)Close(); }
49
50 PRInt64 RCFileIO::Available()
51     { return fd->methods->available(fd); }
52
53 PRStatus RCFileIO::Close()
54     { PRStatus rv = fd->methods->close(fd); fd = NULL; return rv; }
55
56 PRStatus RCFileIO::Delete(const char* filename) { return PR_Delete(filename); }
57
58 PRStatus RCFileIO::FileInfo(RCFileInfo* info) const
59     { return fd->methods->fileInfo64(fd, &info->info); }
60
61 PRStatus RCFileIO::FileInfo(const char *name, RCFileInfo* info)
62     { return PR_GetFileInfo64(name, &info->info); }
63
64 PRStatus RCFileIO::Fsync()
65     { return fd->methods->fsync(fd); }
66
67 PRStatus RCFileIO::Open(const char *filename, PRIntn flags, PRIntn mode)
68 {
69     fd = PR_Open(filename, flags, mode);
70     return (NULL == fd) ? PR_FAILURE : PR_SUCCESS;
71 }  /* RCFileIO::Open */
72
73 PRInt32 RCFileIO::Read(void *buf, PRSize amount)
74     { return fd->methods->read(fd, buf, amount); }
75
76 PRInt64 RCFileIO::Seek(PRInt64 offset, RCIO::Whence how)
77 {
78     PRSeekWhence whence;
79     switch (how)
80     {
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;
85     }
86     return fd->methods->seek64(fd, offset, whence);
87 }  /* RCFileIO::Seek */
88
89 PRInt32 RCFileIO::Write(const void *buf, PRSize amount)
90     { return fd->methods->write(fd, buf, amount); }
91
92 PRInt32 RCFileIO::Writev(
93     const PRIOVec *iov, PRSize size, const RCInterval& timeout)
94     { return fd->methods->writev(fd, iov, size, timeout); }
95
96 RCIO *RCFileIO::GetSpecialFile(RCFileIO::SpecialFile special)
97 {
98     PRFileDesc* fd;
99     PRSpecialFD which;
100     RCFileIO* spec = NULL;
101
102     switch (special)
103     {
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;
108     }
109     fd = PR_GetSpecialFD(which);
110     if (NULL != fd)
111     {
112         spec = new RCFileIO();
113         if (NULL != spec) spec->fd = fd;
114     }
115     return spec;
116 }  /* RCFileIO::GetSpecialFile */
117
118
119 /*
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).
123 */
124 PRStatus RCFileIO::Connect(const RCNetAddr&, const RCInterval&)
125 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
126
127 PRStatus RCFileIO::GetLocalName(RCNetAddr*) const
128 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
129
130 PRStatus RCFileIO::GetPeerName(RCNetAddr*) const
131 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
132
133 PRStatus RCFileIO::GetSocketOption(PRSocketOptionData*) const
134 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
135
136 PRStatus RCFileIO::Listen(PRIntn)
137 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
138
139 PRInt16 RCFileIO::Poll(PRInt16, PRInt16*)
140 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return 0; }
141
142 PRInt32 RCFileIO::Recv(void*, PRSize, PRIntn, const RCInterval&)
143 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
144
145 PRInt32 RCFileIO::Recvfrom(void*, PRSize, PRIntn, RCNetAddr*, const RCInterval&)
146 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
147
148 PRInt32 RCFileIO::Send(
149     const void*, PRSize, PRIntn, const RCInterval&)
150 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
151
152 PRInt32 RCFileIO::Sendto(
153     const void*, PRSize, PRIntn, const RCNetAddr&, const RCInterval&)
154 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
155
156 RCIO* RCFileIO::Accept(RCNetAddr*, const RCInterval&)
157 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return NULL; }
158
159 PRStatus RCFileIO::Bind(const RCNetAddr&)
160 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
161
162 PRInt32 RCFileIO::AcceptRead(
163     RCIO**, RCNetAddr**, void*, PRSize, const RCInterval&)
164 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
165
166 PRStatus RCFileIO::SetSocketOption(const PRSocketOptionData*)
167 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
168
169 PRStatus RCFileIO::Shutdown(RCIO::ShutdownHow)
170 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
171
172 PRInt32 RCFileIO::TransmitFile(
173     RCIO*, const void*, PRSize, RCIO::FileDisposition, const RCInterval&)
174 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return -1; }
175
176 /*
177 ** Class implementation for file information object (ref: prio.h)
178 */
179
180 RCFileInfo::~RCFileInfo() { }
181
182 RCFileInfo::RCFileInfo(const RCFileInfo& her): RCBase()
183     { info = her.info; }  /* RCFileInfo::RCFileInfo */
184
185 RCTime RCFileInfo::CreationTime() const { return RCTime(info.creationTime); }
186
187 RCTime RCFileInfo::ModifyTime() const { return RCTime(info.modifyTime); }
188
189 RCFileInfo::FileType RCFileInfo::Type() const
190 {
191     RCFileInfo::FileType type;
192     switch (info.type)
193     {
194         case PR_FILE_FILE: type = RCFileInfo::file; break;
195         case PR_FILE_DIRECTORY: type = RCFileInfo::directory; break;
196         default: type = RCFileInfo::other;
197     }
198     return type;
199 }  /* RCFileInfo::Type */