TAO_NS_FlatFileStream Class Reference

#include <Flat_File_Persistence.h>

Inheritance diagram for TAO_NS_FlatFileStream:

Inheritance graph
[legend]
Collaboration diagram for TAO_NS_FlatFileStream:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 TAO_NS_FlatFileStream (const ACE_CString &file, const char *mode)
virtual ~TAO_NS_FlatFileStream ()
virtual void remove ()
 Remove a file by name (file is not open).

virtual int exists ()
 Check if a file exists on disk (file is not open).

virtual int open ()
 Open a file (the remaining methods below all require an open file).

virtual int close ()
 Close an open file.

virtual int flock (int whence, int start, int len)
 Acquire a file lock.

virtual int funlock (int whence, int start, int len)
 Release a file lock.

virtual time_t last_changed (void)
 Returns the last time an open file was changed.

virtual TAO_Storable_Baseoperator<< (const TAO_NS_Persistence_Header &header)
 Write a header to disk.

virtual TAO_Storable_Baseoperator>> (TAO_NS_Persistence_Header &header)
 Read a header from disk.

virtual TAO_Storable_Baseoperator<< (const TAO_NS_Persistence_Record &record)
 Write a record to disk.

virtual TAO_Storable_Baseoperator>> (TAO_NS_Persistence_Record &record)
 Read a record from disk.

virtual TAO_Storable_Baseoperator<< (const TAO_NS_Persistence_Global &global)
 Write the global data to disk.

virtual TAO_Storable_Baseoperator>> (TAO_NS_Persistence_Global &global)
 Read the global data from disk.


Private Attributes

ACE_OS::ace_flock_t filelock_
FILE * fl_
ACE_CString file_
ACE_CString mode_

Constructor & Destructor Documentation

TAO_BEGIN_VERSIONED_NAMESPACE_DECL TAO_NS_FlatFileStream::TAO_NS_FlatFileStream const ACE_CString file,
const char *  mode
 

Definition at line 15 of file Flat_File_Persistence.cpp.

References ACE_TRACE.

00017   : fl_ (0)
00018 {
00019   ACE_TRACE("TAO_NS_FlatFileStream");
00020   file_ = file;
00021   mode_ = mode;
00022 }

TAO_NS_FlatFileStream::~TAO_NS_FlatFileStream  )  [virtual]
 

Definition at line 24 of file Flat_File_Persistence.cpp.

References ACE_TRACE, close(), and fl_.

00025 {
00026   ACE_TRACE("~TAO_NS_FlatFileStream");
00027   if ( fl_ != 0 )
00028     this->close();
00029 }


Member Function Documentation

int TAO_NS_FlatFileStream::close  )  [virtual]
 

Close an open file.

Implements TAO_Storable_Base.

Definition at line 88 of file Flat_File_Persistence.cpp.

References ACE_TRACE, ACE_OS::fclose(), ACE_OS::fflush(), filelock_, fl_, and ACE_OS::flock_destroy().

Referenced by ~TAO_NS_FlatFileStream().

00089 {
00090   ACE_TRACE("close");
00091   ACE_OS::fflush(fl_);
00092 #ifndef ACE_WIN32
00093   ACE_OS::flock_destroy (&filelock_, 0);
00094 #endif
00095   ACE_OS::fclose (fl_);  // even though flock_destroy closes the handle
00096                          // we still need to destroy the FILE*
00097 
00098   fl_ = 0;
00099   return 0;
00100 }

int TAO_NS_FlatFileStream::exists  )  [virtual]
 

Check if a file exists on disk (file is not open).

Implements TAO_Storable_Base.

Definition at line 39 of file Flat_File_Persistence.cpp.

References ACE_OS::access(), ACE_TRACE, and F_OK.

00040 {
00041   ACE_TRACE("exists");
00042   // We could check the mode for this file, but for now just check exists
00043   return ! ACE_OS::access(file_.c_str(), F_OK);
00044 }

int TAO_NS_FlatFileStream::flock int  whence,
int  start,
int  len
[virtual]
 

Acquire a file lock.

Implements TAO_Storable_Base.

Definition at line 103 of file Flat_File_Persistence.cpp.

References ACE_TRACE, filelock_, ACE_OS::flock_rdlock(), ACE_OS::flock_wrlock(), and ACE_OS::strcmp().

00104 {
00105   ACE_TRACE("flock");
00106 #if defined (ACE_WIN32)
00107   ACE_UNUSED_ARG (whence);
00108   ACE_UNUSED_ARG (start);
00109   ACE_UNUSED_ARG (len);
00110 #else
00111   if( ACE_OS::strcmp(mode_.c_str(), "r") == 0 )
00112     ACE_OS::flock_rdlock(&filelock_, whence, start, len);
00113   else
00114     ACE_OS::flock_wrlock(&filelock_, whence, start, len);
00115 #endif
00116   return 0;
00117 }

int TAO_NS_FlatFileStream::funlock int  whence,
int  start,
int  len
[virtual]
 

Release a file lock.

Implements TAO_Storable_Base.

Definition at line 120 of file Flat_File_Persistence.cpp.

References ACE_TRACE, filelock_, and ACE_OS::flock_unlock().

00121 {
00122   ACE_TRACE("funlock");
00123 #if defined (ACE_WIN32)
00124   ACE_UNUSED_ARG (whence);
00125   ACE_UNUSED_ARG (start);
00126   ACE_UNUSED_ARG (len);
00127 #else
00128   ACE_OS::flock_unlock(&filelock_, whence, start, len);
00129 #endif
00130   return 0;
00131 }

time_t TAO_NS_FlatFileStream::last_changed void   )  [virtual]
 

Returns the last time an open file was changed.

Implements TAO_Storable_Base.

Definition at line 134 of file Flat_File_Persistence.cpp.

References ACE_stat, ACE_TRACE, filelock_, ACE_OS::fstat(), and ACE_OS::ace_flock_t::handle_.

00135 {
00136   ACE_TRACE("TAO_NS_FlatFileStream::last_changed");
00137   ACE_stat st;
00138   ACE_OS::fstat(filelock_.handle_, &st);
00139 #if !defined (ACE_HAS_WINCE)
00140   return st.st_mtime;
00141 #else
00142   return st.st_mtime.sec ();
00143 #endif /* ACE_HAS_WINCE */
00144 }

int TAO_NS_FlatFileStream::open  )  [virtual]
 

Open a file (the remaining methods below all require an open file).

Implements TAO_Storable_Base.

Definition at line 47 of file Flat_File_Persistence.cpp.

References ACE_ERROR_RETURN, ACE_TEXT_CHAR_TO_TCHAR, ACE_TRACE, ACE_OS::fdopen(), filelock_, fl_, ACE_OS::flock_init(), ACE_OS::ace_flock_t::handle_, LM_ERROR, ACE_OS::open(), and ACE_OS::strerror().

00048 {
00049   ACE_TRACE("open");
00050   // For now, three flags exist "r", "w",  and "c"
00051   int flags = 0;
00052   const char *fdmode = 0;
00053   if( strchr(mode_.c_str(), 'r') )
00054     if( strchr(mode_.c_str(), 'w') )
00055       flags = O_RDWR, fdmode = "r+";
00056     else
00057       flags = O_RDONLY, fdmode = "r";
00058   else
00059     flags = O_WRONLY, fdmode = "w";
00060   if( strchr(mode_.c_str(), 'c') )
00061     flags |= O_CREAT;
00062 #ifndef ACE_WIN32
00063   if( ACE_OS::flock_init (&filelock_, flags, ACE_TEXT_CHAR_TO_TCHAR(file_.c_str()), 0666) != 0 )
00064     ACE_ERROR_RETURN ((LM_ERROR,
00065                        "Cannot open file %s for mode %s: (%d) %s\n",
00066                        file_.c_str(), mode_.c_str(),
00067                        errno, ACE_OS::strerror(errno)),
00068                       -1);
00069 #else
00070   if( (filelock_.handle_= ACE_OS::open (ACE_TEXT_CHAR_TO_TCHAR(file_.c_str()), flags, 0)) == ACE_INVALID_HANDLE )
00071     ACE_ERROR_RETURN ((LM_ERROR,
00072                        "Cannot open file %s for mode %s: (%d) %s\n",
00073                        file_.c_str(), mode_.c_str(),
00074                        errno, ACE_OS::strerror(errno)),
00075                       -1);
00076 #endif
00077   this->fl_ = ACE_OS::fdopen(filelock_.handle_, ACE_TEXT_CHAR_TO_TCHAR(fdmode));
00078   if (this->fl_ == 0)
00079     ACE_ERROR_RETURN ((LM_ERROR,
00080                        "Cannot fdopen file %s for mode %s: (%d) %s\n",
00081                        file_.c_str(), mode_.c_str(),
00082                        errno, ACE_OS::strerror(errno)),
00083                       -1);
00084   return 0;
00085 }

TAO_Storable_Base & TAO_NS_FlatFileStream::operator<< const TAO_NS_Persistence_Global global  )  [virtual]
 

Write the global data to disk.

Implements TAO_Storable_Base.

Definition at line 314 of file Flat_File_Persistence.cpp.

References ACE_TRACE, TAO_NS_Persistence_Global::counter(), ACE_OS::fflush(), ACE_OS::fprintf(), and ACE_OS::rewind().

00316 {
00317   ACE_TRACE("TAO_NS_FlatFileStream::operator <<");
00318   ACE_OS::rewind(this->fl_);
00319   ACE_OS::fprintf(this->fl_, "%d\n", global.counter());
00320   ACE_OS::fflush(this->fl_);
00321 
00322   return *this;
00323 }

TAO_Storable_Base & TAO_NS_FlatFileStream::operator<< const TAO_NS_Persistence_Record record  )  [virtual]
 

Write a record to disk.

Implements TAO_Storable_Base.

Definition at line 194 of file Flat_File_Persistence.cpp.

References ACE_CString, ACE_SIZE_T_FORMAT_SPECIFIER, ACE_TEXT, ACE_TRACE, ACE_OS::fflush(), ACE_OS::fprintf(), TAO_NS_Persistence_Record::id(), TAO_NS_Persistence_Record::kind(), TAO_NS_Persistence_Record::ref(), and TAO_NS_Persistence_Record::type().

00196 {
00197   ACE_TRACE("TAO_NS_FlatFileStream::operator <<");
00198   TAO_NS_Persistence_Record::Record_Type type = record.type();
00199   ACE_OS::fprintf(this->fl_, "%d\n", type);
00200 
00201   ACE_CString id = record.id();
00202   ACE_OS::fprintf(this->fl_, ACE_SIZE_T_FORMAT_SPECIFIER ACE_TEXT("\n%s\n"),
00203                   id.length(), id.c_str());
00204 
00205   ACE_CString kind = record.kind();
00206   ACE_OS::fprintf(this->fl_, ACE_SIZE_T_FORMAT_SPECIFIER ACE_TEXT ("\n%s\n"),
00207                   kind.length(), kind.c_str());
00208 
00209   ACE_CString ref = record.ref();
00210   ACE_OS::fprintf(this->fl_, ACE_SIZE_T_FORMAT_SPECIFIER ACE_TEXT ("\n%s\n"),
00211                   ref.length(), ref.c_str());
00212 
00213   ACE_OS::fflush(this->fl_);
00214 
00215   return *this;
00216 }

TAO_Storable_Base & TAO_NS_FlatFileStream::operator<< const TAO_NS_Persistence_Header header  )  [virtual]
 

Write a header to disk.

Implements TAO_Storable_Base.

Definition at line 147 of file Flat_File_Persistence.cpp.

References ACE_TRACE, TAO_NS_Persistence_Header::destroyed(), ACE_OS::fflush(), ACE_OS::fprintf(), ACE_OS::rewind(), and TAO_NS_Persistence_Header::size().

00149 {
00150   ACE_TRACE("TAO_NS_FlatFileStream::operator <<");
00151   ACE_OS::rewind(this->fl_);
00152   ACE_OS::fprintf(this->fl_, "%d\n%d\n", header.size(), header.destroyed());
00153   ACE_OS::fflush(this->fl_);
00154 
00155   return *this;
00156 }

TAO_Storable_Base & TAO_NS_FlatFileStream::operator>> TAO_NS_Persistence_Global global  )  [virtual]
 

Read the global data from disk.

Implements TAO_Storable_Base.

Definition at line 326 of file Flat_File_Persistence.cpp.

References ACE_TRACE, TAO_NS_Persistence_Global::counter(), fl_, ACE_OS::rewind(), and TAO_Storable_Base::setstate().

00328 {
00329   ACE_TRACE("TAO_NS_FlatFileStream::operator >>");
00330   unsigned int counter = 0;
00331 
00332   ACE_OS::rewind(this->fl_);
00333   switch (fscanf(fl_, "%u\n", &counter))
00334     {
00335     case 0:
00336       this->setstate (badbit);
00337       return *this;
00338     case EOF:
00339       this->setstate (eofbit);
00340       return *this;
00341     }
00342   global.counter(counter);
00343 
00344   return *this;
00345 
00346 }

TAO_Storable_Base & TAO_NS_FlatFileStream::operator>> TAO_NS_Persistence_Record record  )  [virtual]
 

Read a record from disk.

Implements TAO_Storable_Base.

Definition at line 219 of file Flat_File_Persistence.cpp.

References ACE_CString, ACE_TEXT_CHAR_TO_TCHAR, ACE_TRACE, ACE_OS::fgets(), fl_, TAO_NS_Persistence_Record::id(), TAO_NS_Persistence_Record::kind(), TAO_NS_Persistence_Record::ref(), TAO_Storable_Base::setstate(), and TAO_NS_Persistence_Record::type().

00221 {
00222   ACE_TRACE("TAO_NS_FlatFileStream::operator >>");
00223   TAO_NS_Persistence_Record::Record_Type type;
00224   int temp_type_in;
00225   switch (fscanf(fl_, "%d\n", &temp_type_in))
00226     {
00227     case 0:
00228       this->setstate (badbit);
00229       return *this;
00230     case EOF:
00231       this->setstate (eofbit);
00232       return *this;
00233     }
00234   type = (TAO_NS_Persistence_Record::Record_Type) temp_type_in;
00235   record.type(type);
00236 
00237   int bufSize = 0;
00238 
00239   //id
00240   switch (fscanf(fl_, "%d\n", &bufSize))
00241     {
00242     case 0:
00243       this->setstate (badbit);
00244       return *this;
00245     case EOF:
00246       this->setstate (eofbit);
00247       return *this;
00248     }
00249   char *id = new char[bufSize+1];
00250   //char *id;
00251   //ACE_NEW_RETURN (id, char[bufSize+1], 1);
00252   if (ACE_OS::fgets(ACE_TEXT_CHAR_TO_TCHAR(id), bufSize+1, fl_) == 0 &&
00253       bufSize != 0)
00254     {
00255       this->setstate (badbit);
00256       return *this;
00257     }
00258   ACE_CString newId(id);
00259   record.id(newId);
00260   delete [] id;
00261 
00262   //kind
00263   switch (fscanf(fl_, "%d\n", &bufSize))
00264     {
00265     case 0:
00266       this->setstate (badbit);
00267       return *this;
00268     case EOF:
00269       this->setstate (eofbit);
00270       return *this;
00271     }
00272   char *kind = new char[bufSize+1];
00273   //char *kind;
00274   //ACE_NEW (kind, char[bufSize+1]);
00275   if (ACE_OS::fgets(ACE_TEXT_CHAR_TO_TCHAR(kind), bufSize+1, fl_) == 0 &&
00276       bufSize != 0)
00277     {
00278       this->setstate (badbit);
00279       return *this;
00280     }
00281   kind[bufSize] = '\0';
00282   ACE_CString newKind(kind);
00283   record.kind(newKind);
00284   delete [] kind;
00285 
00286    //ref
00287   switch (fscanf(fl_, "%d\n", &bufSize))
00288     {
00289     case 0:
00290       this->setstate (badbit);
00291       return *this;
00292     case EOF:
00293       this->setstate (eofbit);
00294       return *this;
00295     }
00296   char *ref = new char[bufSize+1];
00297   //char *ref;
00298   //ACE_NEW(ref, char[bufSize+1]);
00299   if (ACE_OS::fgets(ACE_TEXT_CHAR_TO_TCHAR(ref), bufSize+1, fl_) == 0 &&
00300       bufSize != 0)
00301     {
00302       this->setstate (badbit);
00303       return *this;
00304     }
00305   ACE_CString newRef(ref);
00306   record.ref(newRef);
00307   delete [] ref;
00308 
00309   return *this;
00310 
00311 }

TAO_Storable_Base & TAO_NS_FlatFileStream::operator>> TAO_NS_Persistence_Header header  )  [virtual]
 

Read a header from disk.

Implements TAO_Storable_Base.

Definition at line 159 of file Flat_File_Persistence.cpp.

References ACE_TRACE, TAO_NS_Persistence_Header::destroyed(), fl_, ACE_OS::rewind(), TAO_Storable_Base::setstate(), and TAO_NS_Persistence_Header::size().

00161 {
00162   ACE_TRACE("TAO_NS_FlatFileStream::operator >>");
00163   unsigned int size;
00164   int destroyed;
00165 
00166   ACE_OS::rewind(this->fl_);
00167   switch (fscanf(fl_, "%u\n", &size))
00168     {
00169     case 0:
00170       this->setstate (badbit);
00171       return *this;
00172     case EOF:
00173       this->setstate (eofbit);
00174       return *this;
00175     }
00176   header.size(size);
00177 
00178   switch (fscanf(fl_, "%d\n", &destroyed))
00179     {
00180     case 0:
00181       this->setstate (badbit);
00182       return *this;
00183     case EOF:
00184       this->setstate (eofbit);
00185       return *this;
00186     }
00187   header.destroyed(destroyed);
00188 
00189   return *this;
00190 
00191 }

void TAO_NS_FlatFileStream::remove  )  [virtual]
 

Remove a file by name (file is not open).

Implements TAO_Storable_Base.

Definition at line 32 of file Flat_File_Persistence.cpp.

References ACE_TEXT_CHAR_TO_TCHAR, ACE_TRACE, and ACE_OS::unlink().

00033 {
00034   ACE_TRACE("remove");
00035   ACE_OS::unlink(ACE_TEXT_CHAR_TO_TCHAR(file_.c_str()));
00036 }


Member Data Documentation

ACE_CString TAO_NS_FlatFileStream::file_ [private]
 

Definition at line 79 of file Flat_File_Persistence.h.

ACE_OS::ace_flock_t TAO_NS_FlatFileStream::filelock_ [private]
 

Definition at line 77 of file Flat_File_Persistence.h.

Referenced by close(), flock(), funlock(), last_changed(), and open().

FILE* TAO_NS_FlatFileStream::fl_ [private]
 

Definition at line 78 of file Flat_File_Persistence.h.

Referenced by close(), open(), operator>>(), and ~TAO_NS_FlatFileStream().

ACE_CString TAO_NS_FlatFileStream::mode_ [private]
 

Definition at line 80 of file Flat_File_Persistence.h.


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 13:57:42 2006 for TAO_CosNaming by doxygen 1.3.6