lllivefile.cpp

Go to the documentation of this file.
00001 
00031 #include "linden_common.h"
00032 
00033 #include "lllivefile.h"
00034 #include "llframetimer.h"
00035 #include "lltimer.h"
00036 
00037 class LLLiveFile::Impl
00038 {
00039 public:
00040         Impl(const std::string &filename, const F32 refresh_period);
00041         ~Impl();
00042         
00043         bool check();
00044         
00045         
00046         bool mForceCheck;
00047         F32 mRefreshPeriod;
00048         LLFrameTimer mRefreshTimer;
00049 
00050         std::string mFilename;
00051         time_t mLastModTime;
00052         bool mLastExists;
00053         
00054         LLEventTimer* mEventTimer;
00055 };
00056 
00057 LLLiveFile::Impl::Impl(const std::string &filename, const F32 refresh_period)
00058         : mForceCheck(true),
00059         mRefreshPeriod(refresh_period),
00060         mFilename(filename),
00061         mLastModTime(0),
00062         mLastExists(false),
00063         mEventTimer(NULL)
00064 {
00065 }
00066 
00067 LLLiveFile::Impl::~Impl()
00068 {
00069         delete mEventTimer;
00070 }
00071 
00072 LLLiveFile::LLLiveFile(const std::string &filename, const F32 refresh_period)
00073         : impl(* new Impl(filename, refresh_period))
00074 {
00075 }
00076 
00077 LLLiveFile::~LLLiveFile()
00078 {
00079         delete &impl;
00080 }
00081 
00082 
00083 bool LLLiveFile::Impl::check()
00084 {
00085         if (!mForceCheck && mRefreshTimer.getElapsedTimeF32() < mRefreshPeriod)
00086         {
00087                 // Skip the check if not enough time has elapsed and we're not
00088                 // forcing a check of the file
00089                 return false;
00090         }
00091         mForceCheck = false;
00092         mRefreshTimer.reset();
00093 
00094         // Stat the file to see if it exists and when it was last modified.
00095         llstat stat_data;
00096         int res = LLFile::stat(mFilename.c_str(), &stat_data);
00097 
00098         if (res)
00099         {
00100                 // Couldn't stat the file, that means it doesn't exist or is
00101                 // broken somehow.  Clear flags and return.
00102                 if (mLastExists)
00103                 {
00104                         mLastExists = false;
00105                         return true;    // no longer existing is a change!
00106                 }
00107                 return false;
00108         }
00109 
00110         // The file exists, decide if we want to load it.
00111         if (mLastExists)
00112         {
00113                 // The file existed last time, don't read it if it hasn't changed since
00114                 // last time.
00115                 if (stat_data.st_mtime <= mLastModTime)
00116                 {
00117                         return false;
00118                 }
00119         }
00120 
00121         // We want to read the file.  Update status info for the file.
00122         mLastExists = true;
00123         mLastModTime = stat_data.st_mtime;
00124         
00125         return true;
00126 }
00127 
00128 bool LLLiveFile::checkAndReload()
00129 {
00130         bool changed = impl.check();
00131         if (changed)
00132         {
00133                 loadFile();
00134         }
00135         return changed;
00136 }
00137 
00138 std::string LLLiveFile::filename() const
00139 {
00140         return impl.mFilename;
00141 }
00142 
00143 namespace
00144 {
00145         class LiveFileEventTimer : public LLEventTimer
00146         {
00147         public:
00148                 LiveFileEventTimer(LLLiveFile& f, F32 refresh)
00149                         : LLEventTimer(refresh), mLiveFile(f)
00150                         { }
00151                         
00152                 BOOL tick()
00153                 { 
00154                         mLiveFile.checkAndReload(); 
00155                         return FALSE;
00156                 }
00157         
00158         private:
00159                 LLLiveFile& mLiveFile;
00160         };
00161         
00162 }
00163 
00164 void LLLiveFile::addToEventTimer()
00165 {
00166         impl.mEventTimer = new LiveFileEventTimer(*this, impl.mRefreshPeriod);
00167 }
00168 

Generated on Thu Jul 1 06:08:49 2010 for Second Life Viewer by  doxygen 1.4.7