latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*-------------------------------------------------------------------- 00025 * project ....: LTI-Lib: Image Processing and Computer Vision Library 00026 * file .......: ltiStreamAccess.h 00027 * authors ....: Jochen Wickel 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 11.11.2002 00030 * revisions ..: $Id: ltiGetStreamFd.h,v 1.9 2006/02/08 12:53:32 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_GET_STREAM_FD_H_ 00034 #define _LTI_GET_STREAM_FD_H_ 00035 00036 #include "ltiConfig.h" 00037 #include "ltiFunctor.h" 00038 00039 #if defined(HAVE_STREAMBUF_FD) 00040 #include <iostream> 00041 #include <fstream> 00042 #else 00043 #include <istream> 00044 #include <ostream> 00045 #endif 00046 00047 #include <cstdio> 00048 00049 namespace lti { 00050 /** 00051 * This functor can be used to access the file descriptor that is 00052 * used within a stream object. Its primary purpose is to 00053 * use it on machines or implementations of the iostreams that do 00054 * not offer legal methods of obtaining it. 00055 * 00056 * This class is strongly system and compiler dependend 00057 */ 00058 class getStreamFd : public functor { 00059 public: 00060 /** 00061 * the parameters for the class streamAccess 00062 */ 00063 class parameters : public functor::parameters { 00064 public: 00065 /** 00066 * default constructor 00067 */ 00068 parameters(); 00069 00070 /** 00071 * copy constructor 00072 * @param other the parameters object to be copied 00073 */ 00074 parameters(const parameters& other); 00075 00076 /** 00077 * destructor 00078 */ 00079 ~parameters(); 00080 00081 /** 00082 * returns name of this type 00083 */ 00084 const char* getTypeName() const; 00085 00086 /** 00087 * copy the contents of a parameters object 00088 * @param other the parameters object to be copied 00089 * @return a reference to this parameters object 00090 */ 00091 parameters& copy(const parameters& other); 00092 00093 /** 00094 * copy the contents of a parameters object 00095 * @param other the parameters object to be copied 00096 * @return a reference to this parameters object 00097 */ 00098 parameters& operator=(const parameters& other); 00099 00100 00101 /** 00102 * returns a pointer to a clone of the parameters 00103 */ 00104 virtual functor::parameters* clone() const; 00105 00106 /** 00107 * write the parameters in the given ioHandler 00108 * @param handler the ioHandler to be used 00109 * @param complete if true (the default) the enclosing begin/end will 00110 * be also written, otherwise only the data block will be written. 00111 * @return true if write was successful 00112 */ 00113 virtual bool write(ioHandler& handler,const bool complete=true) const; 00114 00115 /** 00116 * read the parameters from the given ioHandler 00117 * @param handler the ioHandler to be used 00118 * @param complete if true (the default) the enclosing begin/end will 00119 * be also written, otherwise only the data block will be written. 00120 * @return true if write was successful 00121 */ 00122 virtual bool read(ioHandler& handler,const bool complete=true); 00123 00124 # ifdef _LTI_MSC_6 00125 /** 00126 * this function is required by MSVC only, as a workaround for a 00127 * very awful bug, which exists since MSVC V.4.0, and still by 00128 * V.6.0 with all bugfixes (so called "service packs") remains 00129 * there... This method is also public due to another bug, so please 00130 * NEVER EVER call this method directly: use read() instead 00131 */ 00132 bool readMS(ioHandler& handler,const bool complete=true); 00133 00134 /** 00135 * this function is required by MSVC only, as a workaround for a 00136 * very awful bug, which exists since MSVC V.4.0, and still by 00137 * V.6.0 with all bugfixes (so called "service packs") remains 00138 * there... This method is also public due to another bug, so please 00139 * NEVER EVER call this method directly: use write() instead 00140 */ 00141 bool writeMS(ioHandler& handler,const bool complete=true) const; 00142 # endif 00143 00144 // ------------------------------------------------ 00145 // the parameters 00146 // ------------------------------------------------ 00147 00148 /** 00149 * If this parameter is true, the file descriptor is duplicated 00150 * before it is returned. This is primarily relevant for creating 00151 * new ANSI C "FILE" structures. 00152 * Default value is false. 00153 */ 00154 bool duplicateFd; 00155 00156 }; 00157 00158 /** 00159 * default constructor 00160 */ 00161 getStreamFd(); 00162 00163 /** 00164 * Construct a functor using the given parameters 00165 */ 00166 getStreamFd(const parameters& par); 00167 00168 /** 00169 * copy constructor 00170 * @param other the object to be copied 00171 */ 00172 getStreamFd(const getStreamFd& other); 00173 00174 /** 00175 * destructor 00176 */ 00177 virtual ~getStreamFd(); 00178 00179 /** 00180 * returns the name of this type ("getStreamFd") 00181 */ 00182 virtual const char* getTypeName() const; 00183 00184 /** 00185 * copy data of "other" functor. 00186 * @param other the functor to be copied 00187 * @return a reference to this functor object 00188 */ 00189 getStreamFd& copy(const getStreamFd& other); 00190 00191 /** 00192 * alias for copy member 00193 * @param other the functor to be copied 00194 * @return a reference to this functor object 00195 */ 00196 getStreamFd& operator=(const getStreamFd& other); 00197 00198 /** 00199 * returns a pointer to a clone of this functor. 00200 */ 00201 virtual functor* clone() const; 00202 00203 /** 00204 * returns used parameters 00205 */ 00206 const parameters& getParameters() const; 00207 00208 /** 00209 * Retrieve the file descriptor associated with the given stream. 00210 * @param ins ifstream whose file descriptor is to be returned. 00211 * @param fd will receive the file descriptor, or -1 if it could not 00212 * be determined. 00213 * @return true if the method succeeded, or false otherwise. 00214 */ 00215 bool apply(std::ifstream& ins, int& fd) const; 00216 00217 /** 00218 * Retrieve the file descriptor associated with the given stream. 00219 * @param ins ofstream whose file descriptor is to be returned. 00220 * @param fd will receive the file descriptor, or -1 if it could not 00221 * be determined. 00222 * @return true if the method succeeded, or false otherwise. 00223 */ 00224 bool apply(std::ofstream& ins, int& fd) const; 00225 00226 /** 00227 * Retrieve the file descriptor associated with the given stream. 00228 * @param ins fstream whose file descriptor is to be returned. 00229 * @param fd will receive the file descriptor, or -1 if it could not 00230 * be determined. 00231 * @return true if the method succeeded, or false otherwise. 00232 */ 00233 bool apply(std::fstream& ins, int& fd) const; 00234 00235 /** 00236 * Creates a new output stream that wraps around the given 00237 * file descriptor. If the duplicateFd parameter is true, 00238 * the file descriptor will be duplicated, and the duplicated 00239 * fd will be closed when the stream is closed. Otherwise, 00240 * the file descriptor will not be duplicated and not be closed. 00241 * @param fd the file descriptor 00242 * @param buffered a flag that tells if the stream should be 00243 * buffered (default) or not. 00244 */ 00245 std::ofstream* newOutputStream(int fd, bool buffered=true) const; 00246 00247 /** 00248 * Creates a new input stream that wraps around the given 00249 * file descriptor. If the duplicateFd parameter is true, 00250 * the file descriptor will be duplicated, and the duplicated 00251 * fd will be closed when the stream is closed. Otherwise, 00252 * the file descriptor will not be duplicated and not be closed. 00253 * @param fd the file descriptor 00254 * @param buffered a flag that tells if the stream should be 00255 * buffered (default) or not. 00256 */ 00257 std::ifstream* newInputStream(int fd, bool buffered=true) const; 00258 00259 /** 00260 * Create a new FILE structure from the given output stream. 00261 * WARNING: It is strongly advisable to use this method 00262 * only with the duplicateFd parameter set to true. 00263 * The returned FILE file must be closed with fclose. 00264 * @param ins ofstream for which a new FILE structure is to 00265 * be created. 00266 * @param f will receive the pointer to the newly created FILE 00267 * structure. 00268 * @return true if the file could be successfully opened, 00269 * false otherwise. 00270 */ 00271 bool apply(std::ofstream& ins, FILE*& f) const; 00272 00273 /** 00274 * Create a new FILE structure from the given input stream. 00275 * WARNING: It is strongly advisable to use this method 00276 * only with the duplicateFd parameter set to true. 00277 * The returned FILE file must be closed with fclose. 00278 * @param ins ofstream for which a new FILE structure is to 00279 * be created. 00280 * @param f will receive the pointer to the newly created FILE 00281 * structure. 00282 * @return true if the file could be successfully opened, 00283 * false otherwise. 00284 */ 00285 bool apply(std::ifstream& ins, FILE*& f) const; 00286 00287 /** 00288 * Retrieve the file descriptor associated with the given stream. 00289 * This method ignores the value of the duplicateFd parameter; 00290 * the file descriptor is never duplicated. 00291 * @param ins ifstream whose file descriptor is to be returned. 00292 * @return the file descriptor, or -1 if none could be determined. 00293 */ 00294 int getfd(std::ifstream& ins) const; 00295 00296 /** 00297 * Retrieve the file descriptor associated with the given stream. 00298 * This method ignores the value of the duplicateFd parameter; 00299 * the file descriptor is never duplicated. 00300 * @param ins ifstream whose file descriptor is to be returned. 00301 * @return the file descriptor, or -1 if none could be determined. 00302 */ 00303 int getfd(std::ofstream& ins) const; 00304 00305 /** 00306 * Retrieve the file descriptor associated with the given stream. 00307 * This method ignores the value of the duplicateFd parameter; 00308 * the file descriptor is never duplicated. 00309 * @param ins ifstream whose file descriptor is to be returned. 00310 * @return the file descriptor, or -1 if none could be determined. 00311 */ 00312 int getfd(std::fstream& ins) const; 00313 00314 00315 /** 00316 * Sets the duplicateFd parameter according to the given value. 00317 */ 00318 void setDuplicate(bool d=true); 00319 00320 }; 00321 } 00322 00323 #endif