source: XMLIO_V2/external/src/POCO/Foundation.save/FileStream_POSIX.cpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1//
2// FileStream_POSIX.cpp
3//
4// $Id: //poco/1.3/Foundation/src/FileStream_POSIX.cpp#2 $
5//
6// Library: Foundation
7// Package: Streams
8// Module:  FileStream
9//
10// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// Permission is hereby granted, free of charge, to any person or organization
14// obtaining a copy of the software and accompanying documentation covered by
15// this license (the "Software") to use, reproduce, display, distribute,
16// execute, and transmit the Software, and to prepare derivative works of the
17// Software, and to permit third-parties to whom the Software is furnished to
18// do so, all subject to the following:
19//
20// The copyright notices in the Software and this entire statement, including
21// the above license grant, this restriction and the following disclaimer,
22// must be included in all copies of the Software, in whole or in part, and
23// all derivative works of the Software, unless such copies or derivative
24// works are solely in the form of machine-executable object code generated by
25// a source language processor.
26//
27// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33// DEALINGS IN THE SOFTWARE.
34//
35
36
37#include "Poco/FileStream.h"
38#include "Poco/File.h"
39#include "Poco/Exception.h"
40#include <sys/types.h>
41#include <sys/stat.h>
42#include <fcntl.h>
43#include <unistd.h>
44
45
46namespace Poco {
47
48
49FileStreamBuf::FileStreamBuf():
50        BufferedBidirectionalStreamBuf(BUFFER_SIZE, std::ios::in | std::ios::out),
51        _fd(-1),
52        _pos(0)
53{
54}
55
56
57FileStreamBuf::~FileStreamBuf()
58{
59        close();
60}
61
62
63void FileStreamBuf::open(const std::string& path, std::ios::openmode mode)
64{
65        poco_assert (_fd == -1);
66
67        _path = path;
68        setMode(mode);
69
70        int flags(0);
71        if (mode & std::ios::trunc)
72                flags |= O_TRUNC;
73        if (mode & std::ios::app)
74                flags |= O_APPEND;
75        if (mode & std::ios::out)
76                flags |= O_CREAT;
77        if ((mode & std::ios::in) && (mode & std::ios::out))
78                flags |= O_RDWR;
79        else if (mode & std::ios::in)
80                flags |= O_RDONLY;
81        else
82                flags |= O_WRONLY;
83                       
84        _fd = ::open(path.c_str(), flags, S_IRUSR | S_IWUSR | S_IRGRP);
85        if (_fd == -1)
86                File::handleLastError(_path);
87               
88        if ((mode & std::ios::app) || (mode & std::ios::ate))
89                seekoff(0, std::ios::end, mode);
90}
91
92
93int FileStreamBuf::readFromDevice(char* buffer, std::streamsize length)
94{
95        if (_fd == -1) return -1;
96       
97        if (getMode() & std::ios::out)
98                sync();
99       
100        int n = read(_fd, buffer, length);
101        if (n == -1)
102                File::handleLastError(_path);
103        _pos += n;
104        return n;
105}
106
107
108int FileStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
109{
110        if (_fd == -1) return -1;
111
112        int n = write(_fd, buffer, length);
113        if (n == -1)
114                File::handleLastError(_path);
115        _pos += n;
116        return n;
117}
118
119
120bool FileStreamBuf::close()
121{
122        bool success = true;
123        if (_fd != -1)
124        {
125                try
126                {
127                        sync();
128                }
129                catch (...)
130                {
131                        success = false;
132                }
133                ::close(_fd);
134                _fd = -1;
135        }
136        return success;
137}
138
139
140std::streampos FileStreamBuf::seekoff(std::streamoff off, std::ios::seekdir dir, std::ios::openmode mode)
141{
142        if (_fd == -1 || !(getMode() & mode)) 
143                return -1;
144
145        if (getMode() & std::ios::out)
146                sync();
147
148        std::streamoff adj;
149        if (mode & std::ios::in)
150                adj = static_cast<std::streamoff>(egptr() - gptr());
151        else
152                adj = 0;
153
154        resetBuffers();
155
156        int whence = SEEK_SET;
157        if (dir == std::ios::cur)
158        {
159                whence = SEEK_CUR;
160                off -= adj;
161        }
162        else if (dir == std::ios::end)
163        {
164                whence = SEEK_END;
165        }
166        _pos = lseek(_fd, off, whence);
167        return _pos;
168}
169
170
171std::streampos FileStreamBuf::seekpos(std::streampos pos, std::ios::openmode mode)
172{
173        if (_fd == -1 || !(getMode() & mode)) 
174                return -1;
175
176        if (getMode() & std::ios::out)
177                sync();
178
179        resetBuffers();
180
181        _pos = lseek(_fd, pos, SEEK_SET);
182        return _pos;
183}
184
185
186} // namespace Poco
Note: See TracBrowser for help on using the repository browser.