From: Dejan L. <dlo...@us...> - 2004-06-11 22:14:28
|
Update of /cvsroot/rtk/rtk/src/core/platform/linux In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10698/src/core/platform/linux Added Files: File.cpp Log Message: Linux FILE & IO TODO: add NOCTTY and NONBLOCK funcionality --- NEW FILE: File.cpp --- /** * * RTK * Fast and easy cross-platform GUI ToolKit. * * Copyright (C) 2001-200x RTK Development Team * * This library is free software; you can redistribute it and/or modify it * under the terms of the slightly modified (see the "EXCEPTION NOTICE" part * of RTK Library License) GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * and along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA . * * Also you should have received a copy of RTK Library License, if not please * write an e-mail to some of RTK authors (listed in file AUTHORS). * * Bug reports: bu...@rt... * Suggestions: rf...@rt... ***************************************************************************/ /** * $Source: /cvsroot/rtk/rtk/src/core/platform/linux/File.cpp,v $ ***** * Authors (chronological order): * Dejan Lekic, de...@rt... * Dejan Lozanovic, null§rtk.cx * Contributors (chronological order): * $fname $lname, $email ***** * T0D0 List: * - ***************************************************************************/ /** * @file src/core/platform/win32/File.cpp * Implementation of the File (IO) class. */ #include <rtk/File.h> #include <stdlib.h> //free namespace Rtk { #include <fcntl.h> #include <unistd.h> #include <sys/file.h> #ifndef INVALID_SET_FILE_POINTER #define INVALID_SET_FILE_POINTER 0xFFFFFFFF #endif #ifndef INVALID_HANDLE_VALUE #define INVALID_HANDLE_VALUE ((void*)-1) #endif #define FD *((int*)_handle) File::File() { _eos = false; _handle = INVALID_HANDLE_VALUE; SetFlags(CAN_SEEK|CAN_WRITE|CAN_READ); _mode = READ | WRITE; } File::File(const String &filename, int mode) { _eos = false; _handle = INVALID_HANDLE_VALUE; SetFlags(CAN_SEEK|CAN_WRITE|CAN_READ); _filename = filename; SetMode(mode); Open(); } File::File(FILE *file) { int val; uint flags=0; _eos = false; _handle = (void*)(fd=fileno(file)); val=fcntl(FD,F_GETFL,0); // access flags if (val & O_RDWR) { _flags = CAN_READ | CAN_WRITE; _mode = READ | WRITE; } else if (val & O_RDONLY) { _flags = CAN_READ; _mode = READ; } else if (val & O_WRONLY) { _flags = CAN_WRITE; _mode = WRITE; } if (lseek(FD, 0, SEEK_CUR) != -1) _flags |= CAN_SEEK; _flags |= EXTERN_HANDLE; if (val & O_APPEND) _mode |= APPEND; if (val & O_SYNC) _mode |= SYNC; } File::File(int fd) { int val; uint flags=0; _eos = false; _handle = (void*)(fd); val=fcntl(FD,F_GETFL,0); // access flags if (val & O_RDWR) { _flags = CAN_READ | CAN_WRITE; _mode = READ | WRITE; } else if (val & O_RDONLY) { _flags = CAN_READ; _mode = READ; } else if (val & O_WRONLY) { _flags = CAN_WRITE; _mode = WRITE; } if (lseek(FD, 0, SEEK_CUR) != -1) _flags |= CAN_SEEK; _flags |= EXTERN_HANDLE; if (val & O_APPEND) _mode |= APPEND; if (val & O_SYNC) _mode |= SYNC; } File::~File() { Close(); } void File::SetMode(int mode) { ClearFlag(CAN_READ | CAN_WRITE, _flags); if(mode & READ) _flags |= CAN_READ; if(mode & WRITE) _flags |= CAN_WRITE; _mode = mode; } bool File::Open() { const RCHAR *file = _filename.c_str(); if (_handle!=INVALID_HANDLE_VALUE) return false; int open_flags = 0; if(_mode & READ) { if (_mode & WRITE) open_flags |= O_RDWR; else open_flags |= O_RDONLY; } else if(_mode & WRITE) open_flags |= O_WRONLY; if(_mode & CREATE) open_flags |= O_CREAT; if(_mode & TRUNCATE) open_flags |= O_TRUNC; if(_mode & APPEND) open_flags |= O_APPEND; if(_mode & SYNC) open_flags |= O_SYNC; _handle = (void*) open(file, open_flags); else if(_mode & LOCKED) if (flock(FD, LOCK_EX|LOCK_NB) == -1) { close(FD); return false; } return IsOpen(); } bool File::Close() { int ret; // return value // Do not close external handle! if (_flags & EXTERN_HANDLE) return false; if (_handle==INVALID_HANDLE_VALUE) return false; //unlock the file if (_mode & LOCK) flock(FD, LOCK_UN); //close file if(close(FD)<0) return false; //remove temporary file if (_mode & TEMP) unlink(_filename.c_str()); _handle = INVALID_HANDLE_VALUE; return true; } bool File::IsOpen() const { return (_handle!=INVALID_HANDLE_VALUE); } int File::Read(void *buffer, int buffer_len) { if(!CanRead()) return false; if(_handle==INVALID_HANDLE_VALUE) return -1; int read_bytes = 0; if((read_bytes=read(_handle, buffer, buffer_len))<0) { _eos = (read_bytes == 0); return read_bytes; } _eos = true;//(GetLastError()==ERROR_HANDLE_EOF); return -1; } int File::Write(void *buffer, int buffer_len) { if(!CanWrite()) return false; if(_handle==INVALID_HANDLE_VALUE) return -1; int written_bytes = 0; _eos = false; if((writen_bytes=write(FD, buffer, buffer_len))<0) { return written_bytes; } return -1; } long File::Seek(long pos, IO::SeekMethod method) { if(_handle==INVALID_HANDLE_VALUE) return -1; if(!CanSeek()) return -1; int seek_method = SEEK_SET; switch(method) { case IO::END: seek_method = SEEK_END; break; case IO::CURRENT: seek_method = SEEK_CUR; break; default: break; }; return (long) lseek(i_handle, pos, seek_method); } long File::Tell() { if(_handle==INVALID_HANDLE_VALUE) return -1; if(!CanSeek()) return -1; return (long) lseek(i_handle, 0, SEEK_CUR); } bool File::Flush() { if(_handle==INVALID_HANDLE_VALUE) return false; return (fsync(FD)==0); } }; // Rtk |