UnbufferedFileByteStream.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: UnbufferedFileByteStream.hpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 #include "UnbufferedFileByteStream.hpp" 00045 00046 #ifndef WIN32 00047 #include <sys/file.h> 00048 #endif 00049 00050 #ifndef WINCE 00051 #include <errno.h> 00052 #endif 00053 00054 UnbufferedFileByteStream::UnbufferedFileByteStream(const char* url, bool forReading) : ByteStream(url,forReading) { 00055 io = NULL; 00056 } 00057 00058 00059 bool UnbufferedFileByteStream::open() { 00060 if (io) { //already open.... 00061 dprintf_err("UnbufferedFileByteStream::open: %s already open!\n",url); 00062 return true; 00063 } 00064 00065 if (readOnly) 00066 io = fopen(url,"rb"); 00067 else { 00068 io = fopen(url,"w"); 00069 #ifndef WIN32 00070 if (io) { 00071 //make sure that we are the only writer system-wide! 00072 int val = flock(fileno(io),LOCK_EX | LOCK_NB); 00073 if (val == -1) 00074 perror("UnbufferedFileByteStream::open flock out-file"); 00075 //ret = ((val == 0) || (errno == ENOLCK)); //eg. NFS might not support flocks 00076 if (errno == EWOULDBLOCK) { 00077 dprintf_err("MuxDemuxIO::open(): FATAL: lock on %s is held by other process! \n", url); 00078 ::exit(1); 00079 } 00080 } 00081 #endif 00082 } 00083 00084 if (!io) { 00085 dprintf_err("UnbufferedFileByteStream::open: file not found: %s!\n",url); 00086 return false; 00087 } else { 00088 return true; 00089 } 00090 } 00091 00092 00093 bool UnbufferedFileByteStream::close() { 00094 if (!io) 00095 return false; 00096 else 00097 return fclose(io); 00098 } 00099 00100 int UnbufferedFileByteStream::getChar() { 00101 if (!io) 00102 return false; 00103 00104 return fgetc(io); 00105 } 00106 00107 00108 int UnbufferedFileByteStream::read(void *ptr, int size) { 00109 if (!io || !readOnly) 00110 return -1; 00111 00112 int ret = fread(ptr,size,1,io); 00113 00114 //NOTE: ignores any ferrors, just assume eof... 00115 if ( (ret == 0) && (feof(io) || ferror(io)) ) 00116 //return -1; 00117 return 0; 00118 00119 return size; 00120 } 00121 00122 00123 int UnbufferedFileByteStream::write(void *ptr, int size){ 00124 if (!io|| readOnly) 00125 return -1; 00126 00127 int ret = fwrite(ptr,size,1,io); 00128 fflush(io); 00129 00130 //assure that all bytes where written! 00131 if ( (ret < size) || feof(io) || ferror(io) ) 00132 return -1; 00133 00134 return size; 00135 } 00136 00137 00138 int UnbufferedFileByteStream::seek(u64 pos) { 00139 if (!io) 00140 return -1; 00141 00142 return fseek(io, pos, SEEK_SET); 00143 } 00144 00145 00146 u64 UnbufferedFileByteStream::tell() { 00147 if (!io) 00148 return 0; 00149 00150 return ftell(io); 00151 }