file_time.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : file_time                                                         *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include "file_time.h"
00016 
00017 #include <basis/astring.h>
00018 #include <basis/byte_array.h>
00019 #include <basis/functions.h>
00020 #include <basis/guards.h>
00021 #include <basis/astring.h>
00022 #include <structures/object_packers.h>
00023 
00024 #include <stdio.h>
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027 #ifdef __UNIX__
00028   #include <utime.h>
00029 #endif
00030 #ifdef __WIN32__
00031   #include <sys/utime.h>
00032 #endif
00033 
00034 #undef LOG
00035 #define LOG(to_print) printf("%s::%s: %s\n", static_class_name(), func, astring(to_print).s())
00036 
00037 using namespace basis;
00038 
00039 namespace filesystem {
00040 
00041 file_time::file_time() : _when(0) { reset(""); } 
00042 
00043 file_time::file_time(FILE *the_FILE) : _when(0) { reset(the_FILE); }
00044 
00045 file_time::file_time(const time_t &t) : _when(t) {}
00046 
00047 file_time::file_time(const astring &filename)
00048 : _when(0)
00049 {
00050   FILE *ptr = fopen(filename.s(), "r");
00051   if (ptr) {
00052     reset(ptr);
00053     fclose(ptr);
00054   }
00055 }
00056 
00057 file_time::~file_time() {}
00058 
00059 bool file_time::set_time(const basis::astring &filename)
00060 {
00061   // prepare the access time and modified time to match our stamp.
00062   utimbuf held_time;
00063   held_time.actime = raw();
00064   held_time.modtime = raw();
00065   // stuff our timestamp on the file.
00066   return !utime(filename.s(), &held_time);
00067 }
00068 
00069 void file_time::reset(const time_t &t) { _when = t; }
00070 
00071 void file_time::reset(const astring &filename)
00072 {
00073   FILE *ptr = fopen(filename.s(), "r");
00074   if (ptr) {
00075     reset(ptr);
00076     fclose(ptr);
00077   }
00078 }
00079 
00080 void file_time::reset(FILE *the_FILE_in)
00081 {
00082   FUNCDEF("reset");
00083   _when = 0;
00084   FILE *the_file = (FILE *)the_FILE_in;
00085   if (!the_file) {
00086     return;
00087   }
00088   struct stat stat_buffer;
00089   if (fstat(fileno(the_file), &stat_buffer) < 0) {
00090     LOG("stat failure on file");
00091   }
00092   _when = stat_buffer.st_mtime;
00093 }
00094 
00095 int file_time::compare(const file_time &b) const
00096 {
00097   if (_when > b._when) return 1;
00098   else if (_when == b._when) return 0;
00099   else return -1;
00100 }
00101 
00102 bool file_time::less_than(const orderable &ft2) const
00103 {
00104   const file_time *cast = dynamic_cast<const file_time *>(&ft2);
00105   if (!cast) return false;
00106   return bool(compare(*cast) < 0);
00107 }
00108 
00109 bool file_time::equal_to(const equalizable &ft2) const
00110 {
00111   const file_time *cast = dynamic_cast<const file_time *>(&ft2);
00112   if (!cast) return false;
00113   return bool(compare(*cast) == 0);
00114 }
00115 
00116 void file_time::text_form(basis::base_string &time_string) const
00117 {
00118   time_string.assign(basis::astring(class_name()) + basis::a_sprintf("@%x", _when));
00119 }
00120 
00121 void file_time::readable_text_form(basis::base_string &time_string) const
00122 {
00123   tm *now = localtime(&_when);  // convert to time round hereparts.
00124   time_string.assign(a_sprintf("%d.%02d.%02d %02d:%02d:%02d",
00125       now->tm_year+1900, now->tm_mon+1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec));
00126 }
00127 
00128 // magic value unfortunately, but this is the length of a packed string with 10 characters.
00129 // we do this because the size of 2^32 in decimal requires that many characters, and we also
00130 // want to just have a fixed length chunk for this.  we are not worried about larger pieces
00131 // than that because we cannot conveniently handle them anyhow.
00132 int file_time::packed_size() const { return 11; }
00133 
00134 void file_time::pack(byte_array &packed_form) const
00135 { a_sprintf("%010d", _when).pack(packed_form); }
00136 
00137 bool file_time::unpack(byte_array &packed_form)
00138 {
00139   astring tempo;
00140   if (!tempo.unpack(packed_form)) return false;
00141   _when = tempo.convert(0L);
00142   return true;
00143 }
00144 
00145 } //namespace
00146 
Generated on Sat Jan 28 04:22:22 2012 for hoople2 project by  doxygen 1.6.3