1
// ---------------------------------------------------------------------------
3
// - standard object library - file info class implementation -
4
// ---------------------------------------------------------------------------
5
// - This program is free software; you can redistribute it and/or modify -
6
// - it provided that this copyright notice is kept intact. -
8
// - This program is distributed in the hope that it will be useful, but -
9
// - without any warranty; without even the implied warranty of -
10
// - merchantability or fitness for a particular purpose. In no event shall -
11
// - the copyright holder be liable for any direct, indirect, incidental or -
12
// - special damages arising in any way out of the use of this software. -
13
// ---------------------------------------------------------------------------
14
// - copyright (c) 1999-2011 amaury darsch -
15
// ---------------------------------------------------------------------------
18
#include "Integer.hpp"
19
#include "FileInfo.hpp"
20
#include "QuarkZone.hpp"
21
#include "Exception.hpp"
26
// -------------------------------------------------------------------------
28
// -------------------------------------------------------------------------
30
// create a new file information by name
32
FileInfo::FileInfo (const String& name) {
37
// return the class name
39
String FileInfo::repr (void) const {
43
// update the file information structure
45
void FileInfo::update (void) {
48
// check the file name
49
if (d_name.isnil () == true) {
50
throw Exception ("name-error", "nil input file name for update");
52
// get the file information
53
char* fname = d_name.tochar ();
54
s_finfo* finfo = c_finfo (fname);
58
throw Exception ("open-error", "cannot get file information", d_name);
60
// update the information structure
61
d_size = finfo->d_size;
62
d_mtime = finfo->d_mtime;
63
// clean the structure
74
String FileInfo::getname (void) const {
77
String result = d_name;
86
// return the input file size
88
t_long FileInfo::length (void) const {
91
t_long result = d_size;
100
// return the file modification time
102
t_long FileInfo::mtime (void) const {
105
t_long result = d_mtime;
114
// -------------------------------------------------------------------------
115
// - object section -
116
// -------------------------------------------------------------------------
119
static const long QUARK_ZONE_LENGTH = 3;
120
static QuarkZone zone (QUARK_ZONE_LENGTH);
122
// the object supported quarks
123
static const long QUARK_MTIME = zone.intern ("get-modification-time");
124
static const long QUARK_LENGTH = zone.intern ("length");
125
static const long QUARK_UPDATE = zone.intern ("update");
127
// create a new object in a generic way
129
Object* FileInfo::mknew (Vector* argv) {
130
long argc = (argv == nilp) ? 0 : argv->length ();
131
// check for 1 argument
133
String name = argv->getstring (0);
134
return new FileInfo (name);
136
throw Exception ("argument-error",
137
"invalid arguments with with file information");
140
// return true if the given quark is defined
142
bool FileInfo::isquark (const long quark, const bool hflg) const {
144
if (zone.exists (quark) == true) {
148
// check the nameable class
149
bool result = hflg ? Nameable::isquark (quark, hflg) : false;
154
// apply this object with a set of arguments and a quark
156
Object* FileInfo::apply (Runnable* robj, Nameset* nset, const long quark,
159
// get the number of arguments
160
long argc = (argv == nilp) ? 0 : argv->length ();
162
// dispatch 0 argument
164
if (quark == QUARK_MTIME) return new Integer (mtime ());
165
if (quark == QUARK_LENGTH) return new Integer (length ());
166
if (quark == QUARK_UPDATE) {
171
// call the nameable class
172
return Nameable::apply (robj, nset, quark, argv);