filetime.cpp

Go to the documentation of this file.
00001 #ifndef FILETIME_IMPLEMENTATION_FILE
00002 #define FILETIME_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : file_time                                                         *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "filetime.h"
00019 
00020 #include <basis/byte_array.h>
00021 #include <basis/function.h>
00022 #include <basis/guards.h>
00023 #include <basis/istring.h>
00024 #include <basis/log_base.h>
00025 
00026 #undef LOG
00027 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00028 
00029 file_time::file_time(FILE *the_file)
00030 : _when(0)
00031 { reset(the_file); }
00032 
00033 file_time::file_time(const time_t &t)
00034 : _when(t)
00035 {}
00036 
00037 file_time::file_time(const istring &filename)
00038 : _when(0)
00039 {
00040   FILE *ptr = fopen(filename.s(), "r");
00041   if (ptr) {
00042     reset(ptr);
00043     fclose(ptr);
00044   }
00045 }
00046 
00047 file_time::~file_time() {}
00048 
00049 void file_time::reset(const time_t &t) { _when = t; }
00050 
00051 void file_time::reset(const istring &filename)
00052 {
00053   FILE *ptr = fopen(filename.s(), "r");
00054   if (ptr) {
00055     reset(ptr);
00056     fclose(ptr);
00057   }
00058 }
00059 
00060 void file_time::reset(FILE *the_file)
00061 {
00062   FUNCDEF("reset");
00063   _when = 0;
00064   if (!the_file) {
00065     return;
00066   }
00067   struct stat stat_buffer;
00068   if (fstat(fileno(the_file), &stat_buffer) < 0) {
00069     LOG("stat failure on file");
00070   }
00071   _when = stat_buffer.st_mtime;
00072 }
00073 
00074 int file_time::compare(const file_time &b) const
00075 {
00076   if (_when > b._when) return 1;
00077   else if (_when == b._when) return 0;
00078   else return -1;
00079 }
00080 
00081 bool file_time::operator < (const file_time &ft2) const
00082 { return bool(compare(ft2) < 0); }
00083 
00084 bool file_time::operator > (const file_time &ft2) const
00085 { return bool(compare(ft2) > 0); }
00086 
00087 bool file_time::operator <= (const file_time &ft2) const
00088 { return bool(compare(ft2) <= 0); }
00089 
00090 bool file_time::operator >= (const file_time &ft2) const
00091 { return bool(compare(ft2) >= 0); }
00092 
00093 bool file_time::operator != (const file_time &ft2) const
00094 { return bool(compare(ft2) != 0); }
00095 
00096 bool file_time::operator == (const file_time &ft2) const
00097 { return bool(compare(ft2) == 0); }
00098 
00099 // we are picking a constant sized 8 bytes regardless of the real size
00100 // of the time_t.  this will hopefully be larger than the required size
00101 // for a really long time.
00102 void file_time::pack(byte_array &packed_form) const
00103 {
00104 //printf("pack, time curr = %ld\r\n", _when);
00105 
00106   byte_array to_add;
00107   time_t temp = _when;
00108   // pack in the part that's actually used, pushing to the right from posn 0.
00109   for (int i = 0; i < int(sizeof(time_t)); i++) {
00110     byte current = byte(temp % 256);
00111     temp /= 256;
00112     to_add.insert(0, 1);
00113     to_add[0] = current;
00114   }
00115   // pack extra bytes in for what's not used.
00116   for (int i = sizeof(time_t); i < 8; i++) {
00117     to_add.insert(0, 1);
00118     to_add[0] = 0;
00119   }
00120   packed_form += to_add;
00121 
00122 
00123 //istring to_print;
00124 //byte_format::text_dump(to_print, packed_form);
00125 //printf("filetime pack\r\n%s", to_print.s());
00126 
00127 
00128 }
00129 
00130 bool file_time::unpack(byte_array &packed_form)
00131 {
00132 
00133 //istring to_print;
00134 //byte_format::text_dump(to_print, packed_form);
00135 //printf("filetime unpack\r\n%s", to_print.s());
00136 
00137   if (packed_form.length() < 8) return false;
00138   time_t temp = 0;
00139   int unused = 8 - sizeof(time_t);
00140   packed_form.zap(0, unused - 1);
00141   for (int i = 0; i < packed_form.length(); i++) {
00142 //printf("current byte = %d\r\n", byte(packed_form[i]));
00143     temp *= 256;  // rotate up what's already stored.
00144     temp += packed_form[i];
00145   }
00146   // toss out the rest of the portion we've consumed.
00147   packed_form.zap(0, sizeof(time_t));
00148   _when = temp;
00149 
00150 //printf("unpack, time curr = %ld\r\n", _when);
00151 
00152   return true;
00153 }
00154 
00155 
00156 #endif //FILETIME_IMPLEMENTATION_FILE
00157 

Generated on Thu Nov 20 04:29:03 2008 for HOOPLE Libraries by  doxygen 1.5.1