ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
alib::files::File Class Reference

Description:

This class represents nodes in FTree instances. While class FTree is just a rather small wrapper around its base class StringTree, this class File is a wrapper around class Cursor. With that, instances of this class are very lightweight and contain only two pointers: One pointing to the FTree that an instance originates from, the second pointing to the node in the tree.

It is important to understand that this class has three interfaces.

  1. The direct interface as exposed with this class.
  2. Using operator-> the attached data instance of type FInfo is accessed.
  3. The method AsCursor casts an instance to the (otherwise protected) type Cursor of the underlying string tree.

To get a thorough understanding of why this split exists and what purpose which of the three interfaces serve, a basic understanding of container type class StringTree is very helpful. A similar design principle is implemented with class Variable of module ALib Variables. A technical explanation to why base class FTree::Cursor is protected there in the same fashion is given here.

See also
  • For a quick tutorial about using ALib files, consult the tutorial-style Programmer's Manual of camp ALib Files.
  • For this class, a pretty printer for the GNU debugger is provided.
  • Instances of this type are appendable to class AString. If done, the full path and file name is written to the target string.

Definition at line 640 of file ftree.inl.

Inheritance diagram for alib::files::File:

Public Type Index:

using ConstCursor = FTree::ConstCursor
 The constant version of type Cursor.
using Cursor = FTree::Cursor

Public Method Index:

 File ()=default
 Defaulted default constructor.
 File (const Cursor &cursor)
 File (FTree &pTree)
CursorAsCursor ()
const CursorAsCursor () const
strings::TAString< system::PathCharType > & AssembleRealPath (strings::TAString< system::PathCharType > &target, lang::Inclusion includeFilename) const
strings::TAString< system::PathCharType > & AssembleSymbolicPath (strings::TAString< system::PathCharType > &target, lang::Inclusion includeFilename) const
template<typename TCustom, typename... TArgs>
TCustom & AttachCustomData (TArgs &&... args)
template<typename TCustom>
void DeleteCustomData ()
system::PathString Extension () const
AStringFormat (Substring format, AString &target, lang::CurrentData targetData=lang::CurrentData::Keep, NumberFormat *numberFormat=nullptr) const
AStringFormatAccessRights (AString &target) const
template<typename TCustom>
TCustom & GetCustomData ()
FTreeGetFTree () const
const NStringGetGroupName () const
FInfoGetMutableFInfo ()
const NStringGetOwnerName () const
File GetSymbolicParent ()
const File GetSymbolicParent () const
bool HasCustomData () const
bool HasSymbolicParent ()
bool operator!= (const File &other) const
const FInfooperator-> () const
Fileoperator= (const Cursor &other)
bool operator== (const File &other) const
void SetSymbolicParent (File &symbolicParent, bool overwrite=false)
void SetSymbolicParent (FTree::CursorHandle handle, bool overwrite=false)
system::PathString Stem () const

Type Definition Details:

◆ ConstCursor

The constant version of type Cursor.

Definition at line 648 of file ftree.inl.

◆ Cursor

using alib::files::File::Cursor = FTree::Cursor

The base cursor type of the internal StringTree. This type is used to perform cursor operations on FTree instances.

Definition at line 645 of file ftree.inl.

Constructor(s) / Destructor Details:

◆ File() [1/2]

alib::files::File::File ( FTree & pTree)
inline

Constructor taking a file tree. After construction, this file will point to the root node "/" of the tree.

Parameters
pTreeThe tree to associate this file instance with.

Definition at line 661 of file ftree.inl.

◆ File() [2/2]

alib::files::File::File ( const Cursor & cursor)
inline

Constructs an instance of this type from its base type. This constructor is for advanced use when direct operations with class StringTree and its cursor and iterator types are performed.

Parameters
cursorThe StringTree cursor representing a file.

Definition at line 668 of file ftree.inl.

Method Details:

◆ AsCursor() [1/2]

Cursor & alib::files::File::AsCursor ( )
inline

This is an explicit cast operator to the protected base class.

Note
For details on the code design which makes this method necessary, consult the documentation of the same concept found with method Cursor & AsCursor() .
Returns
This instance cast 'down' to its protected base class.

Definition at line 708 of file ftree.inl.

◆ AsCursor() [2/2]

const Cursor & alib::files::File::AsCursor ( ) const
inline

const version of the cast operator to the protected base class.

Returns
This instance cast 'down' to its protected base class.

Definition at line 712 of file ftree.inl.

◆ AssembleRealPath()

strings::TAString< system::PathCharType > & alib::files::File::AssembleRealPath ( strings::TAString< system::PathCharType > & target,
lang::Inclusion includeFilename ) const
inline

Appends the file's real path to the given target string. The result excludes a trailing separation character. If this file represents the root folder of the file tree, nothing is written to target.

See also
Sibling method AssembleSymbolicPath.
Parameters
targetThe string buffer to append the path to.
includeFilenameDenotes whether the filename should be included or if the path to the parent directory is requested.
Returns
The given AString to allow concatenated operations.

Definition at line 796 of file ftree.inl.

◆ AssembleSymbolicPath()

strings::TAString< system::PathCharType > & alib::files::File::AssembleSymbolicPath ( strings::TAString< system::PathCharType > & target,
lang::Inclusion includeFilename ) const

Returns the symbolic path to this file. The result excludes a trailing separation character. If this file represents the root folder of the file tree, nothing is written to target.

See also
Sibling method AssembleRealPath.
Parameters
targetThe string buffer to append the path to.
includeFilenameDenotes whether the filename should be included or if the path to the parent directory is requested.
Returns
The given AString to allow concatenated operations.

Definition at line 50 of file file.cpp.

◆ AttachCustomData()

template<typename TCustom, typename... TArgs>
TCustom & alib::files::File::AttachCustomData ( TArgs &&... args)
inline

Allocates a custom object attached to this file using the PoolAllocator of the FTree.

See also
Methods GetCustomData, HasCustomData, DeleteCustomData, and void DeleteAllCustomData() .
Template Parameters
TCustomThe type of custom data associated to the FTree that this file belongs to.
TArgsTypes of the variadic arguments args that construct TCustom.
Parameters
argsVariadic arguments forwarded to the constructor of TCustom.
Returns
The custom data record.

Definition at line 864 of file ftree.inl.

◆ DeleteCustomData()

template<typename TCustom>
void alib::files::File::DeleteCustomData ( )
inline

Destructs and deletes the custom data attached to this file. With debug-compilations it is asserted that HasCustomData() returns true and that TCustom is the same as set.

See also
Methods AttachCustomData, GetCustomData, HasCustomData, and void DeleteAllCustomData() .
Template Parameters
TCustomThe object type to optionally store in tree nodes.

Definition at line 880 of file ftree.inl.

◆ Extension()

system::PathString alib::files::File::Extension ( ) const
inline

Returns the file extension, which is the substring behind the last period '.' character which is not located at the start of the name. (A filename like ".profile" is not treated to have an extension).

Returns
The extension found in the filename. An empty string if none is found.

Definition at line 781 of file ftree.inl.

◆ Format()

AString & alib::files::File::Format ( Substring format,
AString & target,
lang::CurrentData targetData = lang::CurrentData::Keep,
NumberFormat * numberFormat = nullptr ) const

Writes formatted information on this file to the given string buffer target. Within the pattern string format, different symbols are interpreted as tokens. Spaces between tokens are written as given. Strings within the format text that should not be interpreted as tokens may be given in single quotes. Two consecutive single quotes will be replaced to one single quote.

Tokens are defined in lower case letters. If given with upper case letters, the generated string is converted to upper case letters.

This method supports the following tokens:

Token
Description
a Invokes FormatAccessRights.
b In case this node has a symbolic parent, prints " <- symbolicparent". The symbolic parent is always an absolute path.
dm{DATEFORMAT} The modification date of this file. This token is optionally followed by a "DATEFORMAT" string given in curly braces. For specification information, see AString & Format(Substring, AString&, lang::CurrentData) const.
db{DATEFORMAT} Same as 'dm', but uses the creation date of this file.
dc{DATEFORMAT} Same as 'dm', but uses the change date of this file.
da{DATEFORMAT} Same as 'dm', but uses the date of last access to this file.
fx Prints 'm' if IsCrossingFS returns true, '-' otherwise.
fa Prints 'a' if IsArtificialFS returns true, '-' otherwise.
gi[{width[,alignment]}] The ID of the user group of the file.
gn[{width[,alignment]}] The name of the user group of the file.
h The number of hard links pointing to this file.
l In case of Symbolic links, prints " -> linktarget". If the linktarget is a relative path, then the absolute path is appended in round brackets.
na The name of the file.
ns The stem of the file.
ne The extension of the file.
np The symbolic path to the file, excluding the file name and a trailing separation character.
nf The symbolic path to the file, including the file name and excluding a trailing separation character.
nr The real path to the file, excluding the file name and a trailing separation character.
nx The real path to the file, including the file name and excluding a trailing separation character.
oi[{width[,alignment]}] The ID of the owner of the file.
on[{width[,alignment]}] The name of the owner of the file.
q The scan state printed as resourced with enum ScanStates.
rd Recursively counted subfolders.
rf Recursively counted files.
re Recursively counted access errors.
rb Recursively counted broken links.
qqq The scan ScanStates, encoded in three characters. The conversion is resourced with enum ScanStates3Letters.
s[{unit}] The size of the file. See the explanation below.
t The type, encoded in a single character. The conversion is resourced with enum TypeNames1Letter.
tt The type, encoded in two characters. The conversion is resourced with enum TypeNames2Letters.
ttt The type, encoded in three characters. The conversion is resourced with enum TypeNames3Letters.
tttt The type, as a full word. The conversion is resourced with enum Types.
Fields and Alignment
Any of the tokens above may be followed by {width[,Alignment]}. In words: a pair of curly braces that contains an integral value specifying a field width and, optionally and separated by a comma, an alignment specifier. (The optional alignment specifier is parsed using enum records.) If so, a corresponding field, using spaces as padding character, is printed.
Printing sizes:
Token "s[(unit)]" is used to print file sizes. The optional unit string in curly braces may have one the following values:
  • IEC: Chooses IEC standard with automatic detection of an appropriate magnitude. The unit of the magnitude found ("B", "KiB", "MiB", "GiB",...) is added to the output. This is the default if the optional unit-partis omitted.
  • SI: Chooses SI standard with automatic detection of an appropriate magnitude. The unit of the magnitude found ("B", "kB", "MB", "GB",...) is added to the output.
  • One of the more than 20 possible entity names of either IEC or SI standard. In this case, the unit is not included after the number, because this way it can be optionally added to the format string by using a pair of single quotes '.
With the two automatic modes IEC and SI, the namespace function FormatByteSize is used.
For formatting the file size numbers, this method retrieves formatting hints with NumberFormat & GetNumberFormat() . With that, the details of the format can be specified "per FTree". Manipulations of this object before invoking this method, allows specifying output widths, group characters, decimal separation character, and so forth.
Printing owner and group:
For printing owner and group names, those have to be queried from the OS. To increase performance, the resolver utility instance received with const OwnerAndGroupResolver & GetOGResolver() const is used. The use of this instance has to be protected against racing conditions in multithreaded applications. This means if two threads invoke this method on File object that belong to the same FTree, a locking mechanism has to be used, to avoid undefined behavior. (For example, by using the class Lock.)
Sample
As a sample, the following format string mimics the output of GNU/Linux console command ls -l:
     "ta h on gn s dm nal"
See also
This method is invoked by FFormat_File, which is an implementation of box-function FFormat. With hat, objects of this type can be used as arguments for FormatterPythonStyle. The format specifier passed to this method has to be placed behind the colon in the placeholder field, as in "{:FORMATSPEC}". If no format string is given in the placeholder, the string "ta h on gn s dm nal" is used, which is resourced in camp FILES under key "FFMT".
Parameters
formatThe format pattern string.
targetA reference to an AString that gets the result of the format processing appended.
targetDataIf CurrentData::Keep (the default) the string is appended to target. if CurrentData::Clear, target is cleared.
numberFormatThe number format specification to use. Defaults to nullptr which chooses static TNumberFormat Computational.
Returns
target (for convenience).

Definition at line 140 of file file.cpp.

◆ FormatAccessRights()

AString & alib::files::File::FormatAccessRights ( AString & target) const

Writes the permission flags to the given target string in the same format as GNU/Linux command 'ls -l' does.

Parameters
targetThe target string to write into.
Returns
The given target to allow concatenated calls.

Definition at line 99 of file file.cpp.

◆ GetCustomData()

template<typename TCustom>
TCustom & alib::files::File::GetCustomData ( )
inline

Retrieves a custom data object. With debug-compilations it is asserted that HasCustomData() returns true and that TCustom is the same as set.

Template Parameters
TCustomThe type of custom data requested. In case no data was previously attached, yet, the constructor of this type is called on the new allocated memory.
See also
Methods AttachCustomData, HasCustomData, DeleteCustomData, and void DeleteAllCustomData() .
Returns
The custom data record.

Definition at line 843 of file ftree.inl.

◆ GetFTree()

FTree & alib::files::File::GetFTree ( ) const
inline

Returns a reference to the file tree that this file resides in.

Returns
The associated file tree instance.

Definition at line 653 of file ftree.inl.

◆ GetGroupName()

const NString & alib::files::File::GetGroupName ( ) const
inline

Retrieves the file's group name.

Returns
The name of the group of the file.

Definition at line 826 of file ftree.inl.

◆ GetMutableFInfo()

FInfo & alib::files::File::GetMutableFInfo ( )
inline

Provides access to members of contained FInfo record. Note that const access is available with method operator->.
Changes to the values should be done with caution. Usually the values are only set when scanning files or using certain interface methods of this class.

Returns
A writable pointer to the embedded FInfo data.

Definition at line 702 of file ftree.inl.

◆ GetOwnerName()

const NString & alib::files::File::GetOwnerName ( ) const
inline

Retrieves the file's owner's name.

Returns
The name of the owner of the file.

Definition at line 822 of file ftree.inl.

◆ GetSymbolicParent() [1/2]

File alib::files::File::GetSymbolicParent ( )
inline

Returns the symbolic parent detected when scanning the file.

See also
Manual chapter 2.3 Real-Paths.
Returns
If present, the symbolic parent that resolved this file. Otherwise the "real" parent in the FTree.

Definition at line 744 of file ftree.inl.

◆ GetSymbolicParent() [2/2]

const File alib::files::File::GetSymbolicParent ( ) const
inline

const version of GetSymbolicParent.

Returns
If present, the symbolic parent that resolved this file. Otherwise the "real" parent in the FTree.

Definition at line 753 of file ftree.inl.

◆ HasCustomData()

bool alib::files::File::HasCustomData ( ) const
inline

Tests if custom data is attached to this file.

See also
Methods AttachCustomData, GetCustomData, DeleteCustomData, and void DeleteAllCustomData() .
Returns
true if custom data is attached to this file, false otherwise.

Definition at line 832 of file ftree.inl.

◆ HasSymbolicParent()

bool alib::files::File::HasSymbolicParent ( )
inline

Tests if this file has a symbolic parent.

See also
Manual chapter 2.3 Real-Paths.
Returns
true if a symbolic parent is set, false otherwise.

Definition at line 736 of file ftree.inl.

◆ operator!=()

bool alib::files::File::operator!= ( const File & other) const
inline

Comparison operator.

Parameters
otherThe object to compare ourselves to.
Returns
false if this and the given file are equal, true otherwise.

Definition at line 684 of file ftree.inl.

◆ operator->()

const FInfo * alib::files::File::operator-> ( ) const
inline

Provides const access to members of contained FInfo record. Note that access to a mutable version of the type is available with method GetMutableFInfo.

Returns
A non-writable pointer to the embedded FInfo data.

Definition at line 695 of file ftree.inl.

◆ operator=()

File & alib::files::File::operator= ( const Cursor & other)
inline

Sets this Cursor to point to the same file (node in the FTree) as the given other.

Parameters
otherThe node to let this file instance point to.
Returns
A reference to this.

Definition at line 690 of file ftree.inl.

◆ operator==()

bool alib::files::File::operator== ( const File & other) const
inline

Comparison operator.

Parameters
otherThe object to compare ourselves to.
Returns
true if this and the given cursor are equal, false otherwise.

Definition at line 675 of file ftree.inl.

◆ SetSymbolicParent() [1/2]

void alib::files::File::SetSymbolicParent ( File & symbolicParent,
bool overwrite = false )
inline

Sets the symbolic parent in case it is not already set, or overwrite is given.

Parameters
symbolicParentA symbolic link that targets this file.
overwriteAdvises setting the new value even if a different symbolic parent is already set. Defaults to false.
See also
Manual chapter 2.3 Real-Paths.

Definition at line 730 of file ftree.inl.

◆ SetSymbolicParent() [2/2]

void alib::files::File::SetSymbolicParent ( FTree::CursorHandle handle,
bool overwrite = false )
inline

Returns the symbolic parent detected when scanning the file.

See also
Manual chapter 2.3 Real-Paths.
Parameters
handleThe exported cursor to the symbolic link that targets this file.
overwriteAdvises setting the new value even if a different symbolic parent is already set. Defaults to false.

Definition at line 719 of file ftree.inl.

◆ Stem()

system::PathString alib::files::File::Stem ( ) const
inline

Returns the substring from the beginning of Name() up to (and not including) the last period '.' character which is not located at the start of the name. With that, edge cases are treated as follows:

  • A filename like "filename.ext.txt" -> "filename.ext"
  • A filename like ".profile" results to identity ".profile".
    Returns
    The filename excluding the Extension.

Definition at line 770 of file ftree.inl.


The documentation for this class was generated from the following files: