~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/lib/std/shl/FileInfo.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2011-03-16 21:31:18 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110316213118-gk4k3ez3e5d2huna
Tags: 2.0.0-1
* QA upload.
* New upstream release
* Debian source format is 3.0 (quilt)
* Fix debhelper-but-no-misc-depends
* Fix ancient-standards-version
* Fix package-contains-linda-override
* debhelper compatibility is 7
* Fix dh-clean-k-is-deprecated

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ---------------------------------------------------------------------------
 
2
// - FileInfo.cpp                                                            -
 
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.                  -
 
7
// -                                                                         -
 
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
// ---------------------------------------------------------------------------
 
16
 
 
17
#include "Vector.hpp"
 
18
#include "Integer.hpp"
 
19
#include "FileInfo.hpp"
 
20
#include "QuarkZone.hpp"
 
21
#include "Exception.hpp"
 
22
#include "csio.hpp"
 
23
 
 
24
namespace afnix {
 
25
 
 
26
  // -------------------------------------------------------------------------
 
27
  // - class section                                                         -
 
28
  // -------------------------------------------------------------------------
 
29
 
 
30
  // create a new file information by name
 
31
 
 
32
  FileInfo::FileInfo (const String& name) {
 
33
    d_name = name;
 
34
    update ();
 
35
  }
 
36
 
 
37
  // return the class name
 
38
 
 
39
  String FileInfo::repr (void) const {
 
40
    return "FileInfo";
 
41
  }
 
42
 
 
43
  // update the file information structure
 
44
 
 
45
  void FileInfo::update (void) {
 
46
    wrlock ();
 
47
    try {
 
48
      // check the file name
 
49
      if (d_name.isnil () == true) {
 
50
        throw Exception ("name-error", "nil input file name for update");
 
51
      }
 
52
      // get the file information
 
53
      char*    fname = d_name.tochar ();
 
54
      s_finfo* finfo = c_finfo (fname);
 
55
      // clean and check
 
56
      delete [] fname;
 
57
      if (finfo == nilp) {
 
58
        throw Exception ("open-error", "cannot get file information", d_name);
 
59
      }
 
60
      // update the information structure
 
61
      d_size  = finfo->d_size;
 
62
      d_mtime = finfo->d_mtime;
 
63
      // clean the structure
 
64
      delete finfo;
 
65
      unlock ();
 
66
    } catch (...) {
 
67
      unlock ();
 
68
      throw;
 
69
    }
 
70
  }
 
71
 
 
72
  // get the file name
 
73
 
 
74
  String FileInfo::getname (void) const {
 
75
    rdlock ();
 
76
    try {
 
77
      String result = d_name;
 
78
      unlock ();
 
79
      return result;
 
80
    } catch (...) {
 
81
      unlock ();
 
82
      throw;
 
83
    }
 
84
  }
 
85
 
 
86
  // return the input file size
 
87
 
 
88
  t_long FileInfo::length (void) const {
 
89
    rdlock ();
 
90
    try {
 
91
      t_long result = d_size;
 
92
      unlock ();
 
93
      return result;
 
94
    } catch (...) {
 
95
      unlock ();
 
96
      throw;
 
97
    }
 
98
  }
 
99
 
 
100
  // return the file modification time
 
101
 
 
102
  t_long FileInfo::mtime (void) const {
 
103
    rdlock ();
 
104
    try {
 
105
      t_long result = d_mtime;
 
106
      unlock ();
 
107
      return result;
 
108
    } catch (...) {
 
109
      unlock ();
 
110
      throw;
 
111
    }
 
112
  }
 
113
 
 
114
  // -------------------------------------------------------------------------
 
115
  // - object section                                                        -
 
116
  // -------------------------------------------------------------------------
 
117
 
 
118
  // the quark zone
 
119
  static const long QUARK_ZONE_LENGTH = 3;
 
120
  static QuarkZone  zone (QUARK_ZONE_LENGTH);
 
121
 
 
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");
 
126
 
 
127
  // create a new object in a generic way
 
128
 
 
129
  Object* FileInfo::mknew (Vector* argv) {
 
130
    long argc = (argv == nilp) ? 0 : argv->length ();
 
131
    // check for 1 argument
 
132
    if (argc == 1) {
 
133
      String name = argv->getstring (0);
 
134
      return new FileInfo (name);
 
135
    }
 
136
    throw Exception ("argument-error", 
 
137
                     "invalid arguments with with file information"); 
 
138
  }
 
139
 
 
140
  // return true if the given quark is defined
 
141
 
 
142
  bool FileInfo::isquark (const long quark, const bool hflg) const {
 
143
    rdlock ();
 
144
    if (zone.exists (quark) == true) {
 
145
      unlock ();
 
146
      return true;
 
147
    }
 
148
    // check the nameable class
 
149
    bool result = hflg ? Nameable::isquark (quark, hflg) : false;
 
150
    unlock ();
 
151
    return result;
 
152
  }
 
153
 
 
154
  // apply this object with a set of arguments and a quark
 
155
 
 
156
  Object* FileInfo::apply (Runnable* robj, Nameset* nset, const long quark,
 
157
                           Vector* argv) {
 
158
 
 
159
    // get the number of arguments
 
160
    long argc = (argv == nilp) ? 0 : argv->length ();
 
161
 
 
162
    // dispatch 0 argument
 
163
    if (argc == 0) {
 
164
      if (quark == QUARK_MTIME)  return new Integer (mtime  ());
 
165
      if (quark == QUARK_LENGTH) return new Integer (length ());
 
166
      if (quark == QUARK_UPDATE) {
 
167
        update ();
 
168
        return nilp;
 
169
      }
 
170
    }
 
171
    // call the nameable class
 
172
    return Nameable::apply (robj, nset, quark, argv);
 
173
  }
 
174
}