Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

FilePStreamBuf.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // FilePStreamBuf.cc 00004 // 00005 // Copyright (C) 2004 Pascal Vincent 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: FilePStreamBuf.cc,v 1.1 2004/06/26 00:24:14 plearner Exp $ 00037 ******************************************************* */ 00038 00039 // Authors: Pascal Vincent 00040 00044 #include "FilePStreamBuf.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 string FilePStreamBuf::getFilePathFromURL(string fileurl) 00050 { 00051 return fileurl; // (for now. Will do sth more fancy later) 00052 } 00053 00054 FilePStreamBuf::FilePStreamBuf() 00055 : f(0) 00056 /* ### Initialise all fields to their default value */ 00057 { 00058 // ... 00059 00060 // ### You may or may not want to call build_() to finish building the object 00061 // build_(); 00062 } 00063 00064 FilePStreamBuf::~FilePStreamBuf() 00065 { 00066 if(f) 00067 fclose(f); 00068 } 00069 00070 00071 PLEARN_IMPLEMENT_OBJECT(FilePStreamBuf, "ONE LINE DESCRIPTION", "MULTI LINE\nHELP"); 00072 00073 void FilePStreamBuf::declareOptions(OptionList& ol) 00074 { 00075 declareOption(ol, "url", &FilePStreamBuf::url, OptionBase::buildoption, 00076 "Uniform resource location of file"); 00077 00078 declareOption(ol, "openmode", &FilePStreamBuf::openmode, OptionBase::buildoption, 00079 "Uniform resource location of file"); 00080 00081 // Now call the parent class' declareOptions 00082 inherited::declareOptions(ol); 00083 } 00084 00085 void FilePStreamBuf::build_() 00086 { 00087 string filepath = getFilePathFromURL(url); 00088 00089 is_random_accessible = true; 00090 if(openmode=="r") 00091 { 00092 is_readable = true; 00093 is_writable = false; 00094 } 00095 else if(openmode=="w" || openmode=="a") 00096 { 00097 is_readable = false; 00098 is_writable = true; 00099 } 00100 else if(openmode=="r+" || openmode=="w+" || openmode=="a+") 00101 { 00102 is_readable = true; 00103 is_writable = true; 00104 } 00105 else 00106 PLERROR("In FilePStreamBuf::build Invalid openmode. Must be one of r, w, a, r+, w+, a+. The 'b' character is not used to denote 'binary mode'. All files are opened in binary mode anyway."); 00107 00108 string binaryopenmode = openmode+'b'; // binary for windows 00109 f = fopen(filepath.c_str(), binaryopenmode.c_str()); 00110 if(!f) 00111 PLERROR("Failed to open file %s in mode %s",filepath.c_str(), openmode.c_str()); 00112 } 00113 00114 // ### Nothing to add here, simply calls build_ 00115 void FilePStreamBuf::build() 00116 { 00117 inherited::build(); 00118 build_(); 00119 } 00120 00121 FilePStreamBuf::streamsize FilePStreamBuf::read_(char* p, streamsize n) 00122 { 00123 return fread(p, 1, n, f); 00124 } 00125 00127 void FilePStreamBuf::write_(const char* p, streamsize n) 00128 { 00129 streamsize nwritten = fwrite(p, 1, n, f); 00130 if(nwritten!=n) 00131 PLERROR("In FilePStreamBuf::write_ failed to write the requested number of bytes"); 00132 fflush(f); 00133 } 00134 00135 void FilePStreamBuf::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00136 { 00137 inherited::makeDeepCopyFromShallowCopy(copies); 00138 // deepCopyField(trainvec, copies); 00139 f = 0; 00140 build(); 00141 } 00142 00143 } // end of namespace PLearn

Generated on Tue Aug 17 15:52:51 2004 for PLearn by doxygen 1.3.7