ulxr_file_resource.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                   ulxr_call.h  -  create xml-rpc calling data
00003                              -------------------
00004     begin                : Sun May 1 2005
00005     copyright            : (C) 2002-2007 by Ewald Arnold
00006     email                : ulxmlrpcpp@ewald-arnold.de
00007 
00008     $Id: ulxr_file_resource.cpp 940 2006-12-30 18:22:05Z ewald-arnold $
00009 
00010  ***************************************************************************/
00011 
00012 /**************************************************************************
00013  *
00014  * This program is free software; you can redistribute it and/or modify
00015  * it under the terms of the GNU Lesser General Public License as
00016  * published by the Free Software Foundation; either version 2 of the License,
00017  * or (at your option) any later version.
00018  *
00019  * This program is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU Lesser General Public License
00025  * along with this program; if not, write to the Free Software
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00027  *
00028  ***************************************************************************/
00029 
00030 //#define ULXR_SHOW_TRACE
00031 //#define ULXR_DEBUG_OUTPUT
00032 
00033 #define ULXR_NEED_EXPORTS
00034 #include <ulxmlrpcpp/ulxmlrpcpp.h>  // always first header
00035 
00036 #ifdef _MSC_VER
00037 # include <io.h>
00038 #endif
00039 
00040 #include <cstdio>
00041 
00042 #include <ulxmlrpcpp/ulxr_file_resource.h>
00043 
00044 
00045 namespace ulxr {
00046 
00047 
00048 ULXR_API_IMPL0 FileResource::FileResource(const CppString &res_name,
00049                                        const CppString &in_file_name,
00050                                        bool do_read)
00051  : CachedResource(res_name)
00052  , filename(in_file_name)
00053  , error(false)
00054  , opened(false)
00055 {
00056   open(do_read);
00057 }
00058 
00059 
00060 ULXR_API_IMPL0 FileResource::~FileResource()
00061 {
00062   close();
00063 }
00064 
00065 
00066 ULXR_API_IMPL(CppString) FileResource::getFilename() const
00067 {
00068   return filename;
00069 }
00070 
00071 
00072 ULXR_API_IMPL(void) FileResource::open()
00073 {
00074   open(true);
00075 }
00076 
00077 
00078 ULXR_API_IMPL(void) FileResource::open(bool do_read)
00079 {
00080   ULXR_TRACE(ULXR_PCHAR("FileResource::open ") << filename);
00081   if (opened)
00082     return;
00083 
00084   reset();
00085   if (do_read)
00086   {
00087     ULXR_TRACE(ULXR_PCHAR("FileResource::open do_read"));
00088 #ifdef __unix__
00089     if (0 == ulxr_access(getLatin1(getFilename()).c_str(), F_OK)) // file exists?
00090 #else
00091     if (0 == ulxr_access(getLatin1(getFilename()).c_str(), 0))
00092 #endif
00093     {
00094       ulxr_FILE *ifs = ulxr_fopen (getLatin1(filename).c_str(), "rb");
00095       char buffer [2000];
00096       if (ifs != 0)
00097       {
00098         while (!ulxr_feof(ifs) && !error)
00099         {
00100           size_t readed = ulxr_fread(buffer, 1, sizeof(buffer), ifs);
00101           if (ulxr_ferror(ifs))
00102             error = true;
00103 
00104           write(buffer, readed);
00105         }
00106         ulxr_fclose (ifs);
00107       }
00108     }
00109   }
00110 
00111   opened = true;
00112 }
00113 
00114 
00115 ULXR_API_IMPL(void) FileResource::close()
00116 {
00117   ULXR_TRACE(ULXR_PCHAR("FileResource::close ") << filename);
00118   if (!opened)
00119     return;
00120 
00121 //  size_t written;
00122   const std::string dat = data();
00123   const unsigned len = dat.length();
00124   ulxr_FILE *ifs = ulxr_fopen (getLatin1(filename).c_str(), "wb");
00125   if (ifs != 0)
00126   {
00127     if (len != 0)
00128     {
00129       /* written = */ ulxr_fwrite(dat.data(), 1, len, ifs);
00130       if (ulxr_ferror(ifs))
00131         error = true;
00132     }
00133     ulxr_fclose (ifs);
00134   }
00135   else
00136     error = true;
00137 
00138   opened = false;
00139   CachedResource::clear();
00140 }
00141 
00142 
00143 ULXR_API_IMPL(void) FileResource::reset()
00144 {
00145   ULXR_TRACE(ULXR_PCHAR("FileResource::reset ") << filename);
00146   CachedResource::reset();
00147   error = false;
00148 }
00149 
00150 
00151 ULXR_API_IMPL(void) FileResource::clear()
00152 {
00153   ULXR_TRACE(ULXR_PCHAR("FileResource::clear ") << filename);
00154   CachedResource::clear();
00155   opened = false;
00156 
00157 #ifdef __unix__
00158   if (0 == ulxr_access(getLatin1(getFilename()).c_str(), F_OK))
00159 #else
00160   if (0 == ulxr_access(getLatin1(getFilename()).c_str(), 0))
00161 #endif
00162     if (0 != ulxr_remove(getLatin1(getFilename()).c_str()))
00163       error = true;
00164 }
00165 
00166 
00167 ULXR_API_IMPL(bool) FileResource::good()
00168 {
00169   ULXR_TRACE(ULXR_PCHAR("FileResource::good ") << error);
00170   return !error;
00171 }
00172 
00173 
00174 };  // namespace ulxr

Generated on Sun Aug 19 20:08:57 2007 for ulxmlrpcpp by  doxygen 1.5.1