PCL
FileInfo.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.6.5
6 // ----------------------------------------------------------------------------
7 // pcl/FileInfo.h - Released 2024-01-13T15:47:58Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_FileInfo_h
53 #define __PCL_FileInfo_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Flags.h>
61 #include <pcl/String.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
107 namespace FileAttribute
108 {
109  enum mask_type
110  {
111  /*
112  * File type.
113  */
114  Block = 0x00000001, // Block special
115  Character = 0x00000002, // Character special
116  FIFO = 0x00000004, // FIFO special
117  Regular = 0x00000008, // Regular file
118  Directory = 0x00000010, // Directory
119  SymbolicLink = 0x00000020, // Symbolic link
120  Socket = 0x00000040, // Socket
121  FileType = 0x000000FF,
122 
123  /*
124  * File attributes.
125  * These are Windows-exclusive, except ReadOnly and Hidden, which we
126  * emulate on UNIX and Linux.
127  */
128  Archive = 0x00001000, // File is archived
129  Compressed = 0x00002000, // File is compressed
130  Encrypted = 0x00004000, // File is encrypted
131  Hidden = 0x00008000, // File is hidden
132  ReadOnly = 0x00010000, // File is read-only
133  System = 0x00020000, // File is a system file
134  Temporary = 0x00040000, // File is a temporary file
135  FileAttributes = 0x000FF000,
136 
137  /*
138  * File permissions.
139  */
140  Read = 0x00100000, // Owner can read
141  Write = 0x00200000, // Owner can write
142  Execute = 0x00400000, // Owner can execute/search
143  ReadGroup = 0x01000000, // Group can read
144  WriteGroup = 0x02000000, // Group can write
145  ExecuteGroup = 0x04000000, // Group can execute/search
146  ReadOthers = 0x10000000, // Others can read
147  WriteOthers = 0x20000000, // Others can write
148  ExecuteOthers = 0x40000000, // Others can execute/search
149  FilePermissions= 0xFFF00000
150  };
151 }
152 
158 using FileAttributes = Flags<FileAttribute::mask_type>;
159 
160 // ----------------------------------------------------------------------------
161 
171 struct FileTime
172 {
173  uint16 year : 16;
174  uint8 month : 4;
175  uint8 day : 5;
176  uint8 hour : 5;
177  int : 2;
178  uint8 minute : 6;
179  uint8 second : 6;
180  int : 4;
182  int : 6;
183 
187  bool operator ==( const FileTime& t ) const
188  {
189  return second == t.second && minute == t.minute && hour == t.hour &&
190  day == t.day && month == t.month && year == t.year &&
192  }
193 
197  bool operator <( const FileTime& t ) const
198  {
199  if ( year != t.year )
200  return year < t.year;
201  if ( month != t.month )
202  return month < t.month;
203  if ( day != t.day )
204  return day < t.day;
205  if ( hour != t.hour )
206  return hour < t.hour;
207  if ( minute != t.minute )
208  return minute < t.minute;
209  if ( second != t.second )
210  return second < t.second;
211  if ( milliseconds != t.milliseconds )
212  return milliseconds < t.milliseconds;
213  return false;
214  }
215 
220  double ToJD() const;
221 
226  double DaysSinceEpoch() const
227  {
228  return ToJD() - 2440587.5;
229  }
230 
235  double SecondsSinceEpoch() const
236  {
237  return 86400*DaysSinceEpoch();
238  }
239 };
240 
241 // ----------------------------------------------------------------------------
242 
250 class PCL_CLASS FileInfo
251 {
252 public:
253 
259  {
260  Clear();
261  }
262 
267  FileInfo( const String& path )
268  {
269  Refresh( path );
270  }
271 
275  FileInfo( const FileInfo& ) = default;
276 
280  virtual ~FileInfo()
281  {
282  }
283 
287  FileInfo& operator =( const FileInfo& ) = default;
288 
292  const String& Path() const
293  {
294  return m_path;
295  }
296 
304  String Drive() const;
305 
319  String Directory() const;
320 
332  String Name() const;
333 
345  String Extension() const;
346 
350  String Suffix() const
351  {
352  return Extension();
353  }
354 
365  String CompleteSuffix() const;
366 
371  String NameAndExtension() const;
372 
377  {
378  return NameAndExtension();
379  }
380 
384  bool Exists() const
385  {
386  return m_exists;
387  }
388 
393  const FileAttributes& Attributes() const
394  {
395  return m_attributes;
396  }
397 
401  bool IsDirectory() const
402  {
403  return m_attributes.IsFlagSet( FileAttribute::Directory );
404  }
405 
409  bool IsFile() const
410  {
411  return m_attributes.IsFlagSet( FileAttribute::Regular );
412  }
413 
420  bool IsSymbolicLink() const
421  {
422  return m_attributes.IsFlagSet( FileAttribute::SymbolicLink );
423  }
424 
432  String SymbolicLinkTarget() const;
433 
442  bool IsHidden() const
443  {
444  return m_attributes.IsFlagSet( FileAttribute::Hidden );
445  }
446 
451  bool IsReadable() const
452  {
453  return m_readable;
454  }
455 
460  bool IsWritable() const
461  {
462  return m_writable;
463  }
464 
477  bool IsExecutable() const
478  {
479  return m_executable;
480  }
481 
490  fsize_type Size() const
491  {
492  return m_size;
493  }
494 
502  int NumberOfHardLinks() const
503  {
504  return m_numberOfHardLinks;
505  }
506 
514  int UserId() const
515  {
516  return m_userId;
517  }
518 
526  int GroupId() const
527  {
528  return m_groupId;
529  }
530 
535  const FileTime& TimeCreated() const
536  {
537  return m_created;
538  }
539 
544  const FileTime& LastAccessed() const
545  {
546  return m_lastAccessed;
547  }
548 
553  const FileTime& LastModified() const
554  {
555  return m_lastModified;
556  }
557 
561  void Refresh();
562 
566  void Refresh( const String& path );
567 
572  void Clear();
573 
574 private:
575 
576  String m_path; // full file or directory path
577  FileAttributes m_attributes; // item attributes
578  fsize_type m_size; // file size in bytes
579  int m_numberOfHardLinks; // number of existing hard links to this file
580  int m_userId; // user id of the file owner
581  int m_groupId; // group id of the file owner
582  FileTime m_created; // creation time
583  FileTime m_lastAccessed; // time of last access
584  FileTime m_lastModified; // time of last change
585  bool m_exists; // item exists
586  bool m_readable; // caller has read permission
587  bool m_writable; // caller has write permission
588  bool m_executable; // caller has execute permission
589 
590  // Clears everything but *preserves* m_path
591  void ClearInfo();
592 };
593 
599 using FileInfoList = Array<FileInfo>;
600 
601 // ----------------------------------------------------------------------------
602 
603 } // pcl
604 
605 #endif // __PCL_FileInfo_h
606 
607 // ----------------------------------------------------------------------------
608 // EOF pcl/FileInfo.h - Released 2024-01-13T15:47:58Z
pcl::FileInfo::Size
fsize_type Size() const
Definition: FileInfo.h:490
pcl::FileInfo::TimeCreated
const FileTime & TimeCreated() const
Definition: FileInfo.h:535
pcl
PCL root namespace.
Definition: AbstractImage.h:76
pcl::FileTime
File date and time.
Definition: FileInfo.h:171
pcl::FileInfo::GroupId
int GroupId() const
Definition: FileInfo.h:526
FileInfoList
A dynamic array of FileInfo structures.
pcl::String
Unicode (UTF-16) string.
Definition: String.h:8112
pcl::FileInfo::IsFile
bool IsFile() const
Definition: FileInfo.h:409
pcl::FileTime::SecondsSinceEpoch
double SecondsSinceEpoch() const
Definition: FileInfo.h:235
pcl::FileTime::second
uint8 second
Second [0,59].
Definition: FileInfo.h:179
pcl::uint16
unsigned short uint16
Definition: Defs.h:657
pcl::FileTime::day
uint8 day
Day [1,31].
Definition: FileInfo.h:175
pcl::FileTime::minute
uint8 minute
Minute [0,59].
Definition: FileInfo.h:178
FileAttributes
A combination of file type, attribute and access mode flags.
pcl::FileInfo::NameAndSuffix
String NameAndSuffix() const
Definition: FileInfo.h:376
pcl::FileInfo::LastAccessed
const FileTime & LastAccessed() const
Definition: FileInfo.h:544
pcl::FileTime::ToJD
double ToJD() const
pcl::FileInfo::~FileInfo
virtual ~FileInfo()
Definition: FileInfo.h:280
pcl::FileInfo::IsReadable
bool IsReadable() const
Definition: FileInfo.h:451
pcl::Flags< FileAttribute::mask_type >
pcl::uint8
unsigned char uint8
Definition: Defs.h:645
pcl::FileInfo::LastModified
const FileTime & LastModified() const
Definition: FileInfo.h:553
pcl::FileTime::milliseconds
uint16 milliseconds
Milliseconds [0,999].
Definition: FileInfo.h:181
pcl::FileInfo::IsWritable
bool IsWritable() const
Definition: FileInfo.h:460
pcl::FileInfo::IsDirectory
bool IsDirectory() const
Definition: FileInfo.h:401
pcl::FileInfo::FileInfo
FileInfo(const String &path)
Definition: FileInfo.h:267
pcl::FileInfo
Platform-independent information on file and directory items.
Definition: FileInfo.h:250
pcl::FileInfo::Path
const String & Path() const
Definition: FileInfo.h:292
pcl::FileTime::hour
uint8 hour
Hour [0,23].
Definition: FileInfo.h:176
pcl::fsize_type
int64 fsize_type
Definition: Defs.h:1188
pcl::ColorSpace::Name
String Name(int colorSpace)
String.h
pcl::FileTime::DaysSinceEpoch
double DaysSinceEpoch() const
Definition: FileInfo.h:226
pcl::FileInfo::IsExecutable
bool IsExecutable() const
Definition: FileInfo.h:477
Flags.h
pcl::FileInfo::Exists
bool Exists() const
Definition: FileInfo.h:384
pcl::FileInfo::IsSymbolicLink
bool IsSymbolicLink() const
Definition: FileInfo.h:420
pcl::FileTime::year
uint16 year
Year.
Definition: FileInfo.h:173
pcl::FileTime::operator==
bool operator==(const FileTime &t) const
Definition: FileInfo.h:187
Defs.h
pcl::FileInfo::UserId
int UserId() const
Definition: FileInfo.h:514
pcl::FileInfo::FileInfo
FileInfo()
Definition: FileInfo.h:258
pcl::FileTime::operator<
bool operator<(const FileTime &t) const
Definition: FileInfo.h:197
pcl::FileInfo::NumberOfHardLinks
int NumberOfHardLinks() const
Definition: FileInfo.h:502
pcl::FileInfo::IsHidden
bool IsHidden() const
Definition: FileInfo.h:442
pcl::FileTime::month
uint8 month
Month [1,12].
Definition: FileInfo.h:174
pcl::FileInfo::Suffix
String Suffix() const
Definition: FileInfo.h:350
pcl::FileInfo::Attributes
const FileAttributes & Attributes() const
Definition: FileInfo.h:393