source: icXML/icXML-devel/src/xercesc/util/FileManagers/PosixFileMgr.cpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 6.3 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (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 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: PosixFileMgr.cpp 673975 2008-07-04 09:23:56Z borisk $
20 */
21
22#include <stdio.h>
23#include <unistd.h>
24#include <limits.h>
25
26#include <xercesc/util/FileManagers/PosixFileMgr.hpp>
27
28#include <icxercesc/util/PlatformUtils.hpp>
29#include <xercesc/util/Janitor.hpp>
30#include <xercesc/util/RuntimeException.hpp>
31#include <xercesc/util/PanicHandler.hpp>
32#include <icxercesc/util/XMLString.hpp>
33
34
35XERCES_CPP_NAMESPACE_BEGIN
36
37
38PosixFileMgr::PosixFileMgr()
39{
40}
41
42
43PosixFileMgr::~PosixFileMgr()
44{
45}
46
47
48FileHandle
49PosixFileMgr::fileOpen(const XMLCh* path, bool toWrite, MemoryManager* const manager)
50{
51    const char* tmpFileName = XMLString::transcode(path, manager);
52    ArrayJanitor<char> janText((char*)tmpFileName, manager);
53    return fileOpen(tmpFileName, toWrite, manager);
54}
55
56
57FileHandle
58PosixFileMgr::fileOpen(const char* path, bool toWrite, MemoryManager* const /*manager*/)
59{
60    const char* perms = (toWrite) ? "wb" : "rb";
61    FileHandle result = (FileHandle)fopen(path , perms);
62    return result;
63}
64
65
66FileHandle
67PosixFileMgr::openStdIn(MemoryManager* const /*manager*/)
68{
69    return (FileHandle)fdopen(dup(0), "rb");
70}
71
72
73void
74PosixFileMgr::fileClose(FileHandle f, MemoryManager* const manager)
75{
76    if (!f)
77                ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, manager);
78
79    if (fclose((FILE*)f))
80        ThrowXMLwithMemMgr(XMLPlatformUtilsException,
81                 XMLExcepts::File_CouldNotCloseFile, manager);
82}
83
84
85void
86PosixFileMgr::fileReset(FileHandle f, MemoryManager* const manager)
87{
88    if (!f)
89                ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, manager);
90
91    // Seek to the start of the file
92    if (fseek((FILE*)f, 0, SEEK_SET))
93        ThrowXMLwithMemMgr(XMLPlatformUtilsException,
94                 XMLExcepts::File_CouldNotResetFile, manager);
95}
96
97
98XMLFilePos
99PosixFileMgr::curPos(FileHandle f, MemoryManager* const manager)
100{
101    if (!f)
102                ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, manager);
103                 
104    long curPos = ftell((FILE*)f);
105       
106    if (curPos == -1)
107        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotGetSize, manager);
108
109    return (XMLFilePos)curPos;
110}
111
112
113XMLFilePos
114PosixFileMgr::fileSize(FileHandle f, MemoryManager* const manager)
115{
116    if (!f)
117                ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, manager);
118               
119    // Get the current position
120    long curPos = ftell((FILE*)f);
121    if (curPos == -1)
122        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotGetCurPos, manager);
123
124    // Seek to the end and save that value for return
125    if (fseek((FILE*)f, 0, SEEK_END))
126        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotSeekToEnd, manager);
127
128    long retVal = ftell((FILE*)f);
129    if (retVal == -1)
130        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotSeekToEnd, manager);
131
132    // And put the pointer back
133    if (fseek((FILE*)f, curPos, SEEK_SET))
134        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotSeekToPos, manager);
135
136    return (XMLFilePos)retVal;
137}
138
139
140XMLSize_t
141PosixFileMgr::fileRead(FileHandle f, XMLSize_t byteCount, XMLByte* buffer, MemoryManager* const manager)
142{
143    if (!f || !buffer)
144                ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, manager);
145               
146    XMLSize_t bytesRead = 0;
147        if (byteCount > 0)
148        {
149        bytesRead = fread((void*)buffer, 1, byteCount, (FILE*)f);
150
151                if (ferror((FILE*)f))
152                        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotReadFromFile, manager);
153        }
154       
155    return bytesRead;
156}
157
158
159void
160PosixFileMgr::fileWrite(FileHandle f, XMLSize_t byteCount, const XMLByte* buffer, MemoryManager* const manager)
161{
162    if (!f || !buffer)
163                ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero, manager);
164
165    while (byteCount > 0)
166    {
167        XMLSize_t bytesWritten = fwrite(buffer, sizeof(XMLByte), byteCount, (FILE*)f);
168
169        if (ferror((FILE*)f))
170                        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotWriteToFile, manager);
171
172                buffer          += bytesWritten;
173                byteCount       -= bytesWritten;
174    }
175}
176
177
178XMLCh*
179PosixFileMgr::getFullPath(const XMLCh* const srcPath, MemoryManager* const manager)
180{
181    //
182    //  NOTE: The path provided has always already been opened successfully,
183    //  so we know that its not some pathological freaky path. It comes in
184    //  in native format, and goes out as Unicode always
185    //
186    char* newSrc = XMLString::transcode(srcPath, manager);
187    ArrayJanitor<char> janText(newSrc, manager);
188
189    // Use a local buffer that is big enough for the largest legal path
190    char absPath[PATH_MAX + 1];
191   
192    // get the absolute path
193    if (!realpath(newSrc, absPath))
194                ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotGetBasePathName, manager);
195
196    return XMLString::transcode(absPath, manager);
197}
198
199
200XMLCh*
201PosixFileMgr::getCurrentDirectory(MemoryManager* const manager)
202{
203    char dirBuf[PATH_MAX + 2];
204    char *curDir = getcwd(&dirBuf[0], PATH_MAX + 1);
205
206    if (!curDir)
207        ThrowXMLwithMemMgr(XMLPlatformUtilsException,
208                 XMLExcepts::File_CouldNotGetBasePathName, manager);
209
210    return XMLString::transcode(curDir, manager);
211}
212
213
214bool
215PosixFileMgr::isRelative(const XMLCh* const toCheck, MemoryManager* const /*manager*/)
216{
217    // Check for pathological case of empty path
218    if (!toCheck || !toCheck[0])
219        return false;
220
221    //
222    //  If it starts with a slash, then it cannot be relative.
223    //
224    return toCheck[0] != XMLCh('/');
225}
226
227
228XERCES_CPP_NAMESPACE_END
229
Note: See TracBrowser for help on using the repository browser.