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
00035 #include "OW_config.h"
00036 #include "OW_TempFileEnumerationImplBase.hpp"
00037 #include "OW_EnumerationException.hpp"
00038 #include "OW_File.hpp"
00039 #include "OW_FileSystem.hpp"
00040
00041 namespace OW_NAMESPACE
00042 {
00043
00044 namespace
00045 {
00046 const UInt32 TEMPFILE_ENUMERATION_SIG = 0x4f57454e;
00047 }
00048
00049 TempFileEnumerationImplBase::TempFileEnumerationImplBase()
00050 : m_size(0), m_Data()
00051 {
00052 UInt32 enumSig = TEMPFILE_ENUMERATION_SIG;
00053 m_Data.write(reinterpret_cast<const char*>(&enumSig), sizeof(enumSig));
00054 if (!m_Data.good())
00055 {
00056 OW_THROW(EnumerationException, "Failed to write signature to "
00057 "enumeration tempfile.");
00058 }
00059
00060
00061 UInt32 tmpSig;
00062 m_Data.read(reinterpret_cast<char*>(&tmpSig), sizeof(tmpSig));
00063 if (!m_Data.good())
00064 {
00065 OW_THROW(EnumerationException, "Failed to read signature from "
00066 "enumeration tempfile.");
00067 }
00068 }
00069
00070 TempFileEnumerationImplBase::TempFileEnumerationImplBase(String const& filename)
00071 : m_size(readSize(filename)), m_Data(filename)
00072 {
00073
00074
00075 UInt32 tmpSig;
00076 m_Data.read(reinterpret_cast<char*>(&tmpSig), sizeof(tmpSig));
00077 if (!m_Data.good())
00078 {
00079 OW_THROW(EnumerationException, "Failed to read signature of "
00080 "enumeration tempfile.");
00081 }
00082 if (tmpSig != TEMPFILE_ENUMERATION_SIG)
00083 {
00084 OW_THROW(EnumerationException, "Signature of enumeration tempfile is not valid.");
00085 }
00086 }
00087 TempFileEnumerationImplBase::~TempFileEnumerationImplBase()
00088 {
00089 }
00090 bool
00091 TempFileEnumerationImplBase::hasMoreElements() const
00092 {
00093
00094 return m_size > 0;
00095 }
00096 size_t
00097 TempFileEnumerationImplBase::numberOfElements() const
00098 {
00099 return m_size;
00100 }
00101 void
00102 TempFileEnumerationImplBase::clear()
00103 {
00104 m_size = 0;
00105 m_Data.reset();
00106 }
00107 String
00108 TempFileEnumerationImplBase::releaseFile()
00109 {
00110
00111
00112 m_Data.write(reinterpret_cast<char*>(&m_size), sizeof(m_size));
00113 if (!m_Data.good())
00114 {
00115 OW_THROW(EnumerationException, "Failed to write size to "
00116 "enumeration tempfile.");
00117 }
00118 String rval = m_Data.releaseFile();
00119 clear();
00120 return rval;
00121 }
00122
00123 bool
00124 TempFileEnumerationImplBase::usingTempFile() const
00125 {
00126 return m_Data.usingTempFile();
00127 }
00128
00129 size_t
00130 TempFileEnumerationImplBase::readSize(String const& filename)
00131 {
00132 size_t size;
00133
00134 File f = FileSystem::openFile(filename);
00135 if (!f)
00136 {
00137 OW_THROW(EnumerationException, "Failed to open file");
00138 }
00139
00140
00141 UInt32 fileSig;
00142 if (f.read(reinterpret_cast<char*>(&fileSig), sizeof(fileSig)) != sizeof(fileSig))
00143 {
00144 OW_THROW(EnumerationException, "Failure to read enumeration "
00145 "signature");
00146 }
00147 if (fileSig != TEMPFILE_ENUMERATION_SIG)
00148 {
00149 OW_THROW(EnumerationException, "Attempted to construct an "
00150 "enumeration from a file that does not have the correct "
00151 "signature");
00152 }
00153
00154 off_t whence = f.seek(-static_cast<off_t>(sizeof(size)), SEEK_END);
00155 if (whence == -1)
00156 {
00157 OW_THROW(EnumerationException, "Failure to seek");
00158 }
00159 if (f.read(reinterpret_cast<char*>(&size), sizeof(size), whence) != sizeof(size))
00160 {
00161 OW_THROW(EnumerationException, "Failure to read enumeration "
00162 "size");
00163 }
00164 if (f.close() == -1)
00165 {
00166 OW_THROW(EnumerationException, "Failure to close enumeration "
00167 "file");
00168 }
00169 return size;
00170 }
00171
00172 void
00173 TempFileEnumerationImplBase::throwIfEmpty() const
00174 {
00175 if (!hasMoreElements())
00176 {
00177 OW_THROW (EnumerationException, "Attempt to Extract from empty Enum");
00178 }
00179 }
00180
00181
00182 }
00183
00184
00185