first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / filesystem / file_info.h
1 #ifndef FILE_INFO_CLASS
2 #define FILE_INFO_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : file_info                                                         *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 1993-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 #include "filename.h"
19 #include "file_time.h"
20
21 #include <basis/definitions.h>
22 #include <basis/enhance_cpp.h>
23
24 namespace filesystem {
25
26 //! Encapsulates some measures and calculations based on a file's contents.
27
28 class file_info : public filename 
29 {
30 public:
31   //! this enum encapsulates how files may be compared.
32   enum file_similarity {
33     EQUAL_NAME = 0,         // we assume name equality is pre-eminent and always required.
34     EQUAL_CHECKSUM = 0x1,   // the files have the same checksum, however computed.
35     EQUAL_TIMESTAMP = 0x2,  // the files have exactly equal timestamps.
36     EQUAL_FILESIZE = 0x4,   // the files have the same sizes.
37     EQUAL_CHECKSUM_TIMESTAMP_FILESIZE = EQUAL_CHECKSUM & EQUAL_TIMESTAMP & EQUAL_FILESIZE
38   };
39
40   double _file_size;  //!< the size of the file.
41   file_time _time;  //!< the file's access time.
42   int _checksum;  //!< the checksum for the file.
43
44   file_info();  //!< blank constructor.
45
46   file_info(const filename &to_copy, double file_size, 
47          const file_time &time = file_time(), int checksum = 0);
48     //!< to get the real file size, timestamp and checksum, invoke the calculate method.
49
50   file_info(const file_info &to_copy);
51
52   virtual ~file_info();
53
54   DEFINE_CLASS_NAME("file_info");
55
56   file_info &operator = (const file_info &to_copy);
57
58   basis::astring text_form() const;
59
60   bool calculate(const basis::astring &prefix, bool just_size_n_time,
61         int checksum_edge = 1 * basis::KILOBYTE);
62     //!< fills in the correct file size and checksum information for this file.
63     /*!< note that the file must exist for this to work.  if "just_size_n_time"
64     is true, then the checksum is not calculated.  if the "prefix" is not
65     empty, then it is used as a directory path that needs to be added to
66     the filename to make it completely valid.  this is common if the
67     filename is stored relative to a path.  the "checksum_edge" is used for
68     checksum calculations; only 2 * checksum_edge bytes will be factored in,
69     each part from the head and tail ends of the file. */
70
71   const basis::astring &secondary() const;
72     //!< observes the alternate form of the name.
73   void secondary(const basis::astring &new_sec);
74     //!< accesses the alternate form of the name.
75
76   const basis::byte_array &attachment() const;
77     //!< returns the chunk of data optionally attached to the file's info.
78     /*!< this supports extending the file's record with extra data that might
79     be needed during processing. */
80   void attachment(const basis::byte_array &new_attachment);
81     //!< sets the optional chunk of data hooked up to the file's info.
82
83   // standard streaming operations.
84   virtual int packed_size() const;
85   virtual void pack(basis::byte_array &packed_form) const;
86   virtual bool unpack(basis::byte_array &packed_form);
87
88 private:
89   basis::astring c_secondary;  //!< alternate filename for the main one.
90   basis::byte_array c_attachment;  //!< extra information, if needed.
91 };
92
93 } //namespace.
94
95 #endif
96