feisty meow concerns codebase 2.140
file_time.cpp
Go to the documentation of this file.
1/*****************************************************************************\
2* *
3* Name : file_time *
4* Author : Chris Koeritz *
5* *
6*******************************************************************************
7* Copyright (c) 1992-$now By Author. This program is free software; you can *
8* redistribute it and/or modify it under the terms of the GNU General Public *
9* License as published by the Free Software Foundation; either version 2 of *
10* the License or (at your option) any later version. This is online at: *
11* http://www.fsf.org/copyleft/gpl.html *
12* Please send any updates to: fred@gruntose.com *
13\*****************************************************************************/
14
15#include "file_time.h"
16
17#include <basis/astring.h>
18#include <basis/byte_array.h>
19#include <basis/functions.h>
20#include <basis/guards.h>
21#include <basis/astring.h>
23
24#include <stdio.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#ifdef __UNIX__
28 #include <utime.h>
29#endif
30#ifdef __WIN32__
31 #include <sys/utime.h>
32#endif
33
34#undef LOG
35#define LOG(to_print) printf("%s::%s: %s\n", static_class_name(), func, astring(to_print).s())
36
37using namespace basis;
38
39namespace filesystem {
40
41file_time::file_time() : _when(0) { reset(""); }
42
43file_time::file_time(FILE *the_FILE) : _when(0) { reset(the_FILE); }
44
45file_time::file_time(const time_t &t) : _when(t) {}
46
48: _when(0)
49{
50 FILE *ptr = fopen(filename.s(), "r");
51 if (ptr) {
52 reset(ptr);
53 fclose(ptr);
54 }
55}
56
58
60{
61 // prepare the access time and modified time to match our stamp.
62 utimbuf held_time;
63 held_time.actime = raw();
64 held_time.modtime = raw();
65 // stuff our timestamp on the file.
66 return !utime(filename.s(), &held_time);
67}
68
69void file_time::reset(const time_t &t) { _when = t; }
70
72{
73 FILE *ptr = fopen(filename.s(), "r");
74 if (ptr) {
75 reset(ptr);
76 fclose(ptr);
77 }
78}
79
80void file_time::reset(FILE *the_FILE_in)
81{
82 FUNCDEF("reset");
83 _when = 0;
84 FILE *the_file = (FILE *)the_FILE_in;
85 if (!the_file) {
86 return;
87 }
88 struct stat stat_buffer;
89 if (fstat(fileno(the_file), &stat_buffer) < 0) {
90 LOG("stat failure on file");
91 }
92 _when = stat_buffer.st_mtime;
93}
94
95int file_time::compare(const file_time &b) const
96{
97 if (_when > b._when) return 1;
98 else if (_when == b._when) return 0;
99 else return -1;
100}
101
102bool file_time::less_than(const orderable &ft2) const
103{
104 const file_time *cast = dynamic_cast<const file_time *>(&ft2);
105 if (!cast) return false;
106 return bool(compare(*cast) < 0);
107}
108
109bool file_time::equal_to(const equalizable &ft2) const
110{
111 const file_time *cast = dynamic_cast<const file_time *>(&ft2);
112 if (!cast) return false;
113 return bool(compare(*cast) == 0);
114}
115
117{
118 time_string.assign(basis::astring(class_name()) + basis::a_sprintf("@%x", _when));
119}
120
122{
123 tm *now = localtime(&_when); // convert to time round hereparts.
124 time_string.assign(a_sprintf("%d.%02d.%02d %02d:%02d:%02d",
125 now->tm_year+1900, now->tm_mon+1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec));
126}
127
128// magic value unfortunately, but this is the length of a packed string with 10 characters.
129// we do this because the size of 2^32 in decimal requires that many characters, and we also
130// want to just have a fixed length chunk for this. we are not worried about larger pieces
131// than that because we cannot conveniently handle them anyhow.
132int file_time::packed_size() const { return 11; }
133
134void file_time::pack(byte_array &packed_form) const
135{ a_sprintf("%010d", _when).pack(packed_form); }
136
138{
139 astring tempo;
140 if (!tempo.unpack(packed_form)) return false;
141 _when = tempo.convert(0L);
142 return true;
143}
144
145} //namespace
146
#define stat
Definition Xos2defs.h:41
#define fileno
Definition Xos2defs.h:27
#define fstat
Definition Xos2defs.h:28
#define LOG(s)
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
void pack(byte_array &target) const
stores this string in the "target". it can later be unpacked again.
Definition astring.cpp:964
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
Definition astring.h:113
int convert(int default_value) const
Converts the string into a corresponding integer.
Definition astring.cpp:760
bool unpack(byte_array &source)
retrieves a string (packed with pack()) from "source" into this string.
Definition astring.cpp:967
Defines the base class for all string processing objects in hoople.
Definition base_string.h:28
virtual base_string & assign(const base_string &s)=0
Sets the contents of this string to "s".
A very common template for a dynamic array of bytes.
Definition byte_array.h:36
Base class for object that can tell itself apart from other instances.
Definition contracts.h:44
A base for objects that can be alphabetically (lexicographically) ordered.
Definition contracts.h:57
virtual const char * class_name() const =0
Returns the bare name of this class as a constant character pointer.
bool set_time(const basis::astring &filename)
sets the time for the the "filename" to the currently held time.
Definition file_time.cpp:59
virtual bool equal_to(const basis::equalizable &ft2) const
virtual bool less_than(const basis::orderable &ft2) const
virtual int packed_size() const
virtual void pack(basis::byte_array &packed_form) const
virtual void text_form(basis::base_string &time_string) const
returns a definitive but sorta ugly version of the file's time.
void reset(FILE *the_FILE)
reacquires the time from a different FILE than constructed with.
Definition file_time.cpp:80
time_t raw() const
provides the OS version of the file's timestamp.
Definition file_time.h:71
virtual void readable_text_form(basis::base_string &time_string) const
sets "time_string" to a human readable form of the file's time.
virtual bool unpack(basis::byte_array &packed_form)
file_time()
sets up a bogus file_time object.
Definition file_time.cpp:41
Provides operations commonly needed on file names.
Definition filename.h:64
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
A platform independent way to obtain the timestamp of a file.