00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #define ULXR_NEED_EXPORTS
00034 #include <ulxmlrpcpp/ulxmlrpcpp.h>
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))
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
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 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 };