NDEVR
API Documentation
File.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: File
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/String.h>
35#include <NDEVR/Time.h>
36#include <NDEVR/StringView.h>
37#include <NDEVR/FileCachedData.h>
38#include <NDEVR/InfoPipe.h>
39namespace NDEVR
40{
41 typedef AlocatingAlignedBuffer<File, 64> FileBuffer;
42 class InfoPipe;
43 class Time;
44 class UUID;
45 class StringView;
52 class File : public String
53 {
54 public:
70
81 public:
85 NDEVR_BASE_API File();
90 NDEVR_BASE_API File(const File& file);
96 NDEVR_BASE_API File(const String& resource_name, const String& path_id);
101 NDEVR_BASE_API File(File&& file) noexcept;
106 NDEVR_BASE_API explicit File(const String& full_path);
111 NDEVR_BASE_API File(const StringView& full_path);
116 NDEVR_BASE_API explicit File(String&& full_path);
120 NDEVR_BASE_API ~File();
125 NDEVR_BASE_API void setFolderName(const StringView& folder_name);
133 NDEVR_BASE_API bool copyTo(const File& cs, bool override_file, LogPtr log = LogPtr()) const;
142 NDEVR_BASE_API bool moveTo(File& file, bool override_file, LogPtr log = LogPtr(), bool is_source_temp = false);
148 NDEVR_BASE_API File& appendPath(const StringView& cs);
154 NDEVR_BASE_API File& appendPath(const String& cs);
160 NDEVR_BASE_API File& appendPath(const char* cs);
166 [[nodiscard]] NDEVR_BASE_API StringView getPath(uint01 file_parts) const;
172 NDEVR_BASE_API void setPath(const StringView& path, uint01 part);
176 NDEVR_BASE_API void removeInvalidChars();
181 [[nodiscard]] NDEVR_BASE_API bool exists() const;
186 [[nodiscard]] NDEVR_BASE_API bool cachedExist() const;
191 [[nodiscard]] NDEVR_BASE_API bool parentDirectoryExists() const;
196 [[nodiscard]] NDEVR_BASE_API bool isDirectory() const;
201 [[nodiscard]] NDEVR_BASE_API bool isDrive() const;
206 [[nodiscard]] NDEVR_BASE_API bool isHidden() const;
211 [[nodiscard]] NDEVR_BASE_API bool isOpen() const;
212
217 [[nodiscard]] NDEVR_BASE_API Buffer<uint01> getData();
222 [[nodiscard]] NDEVR_BASE_API HighCapacityBuffer<uint01> getDataExtended();
228 [[nodiscard]] NDEVR_BASE_API StringView getFolderName(bool trailing_mark = false) const;
233 [[nodiscard]] NDEVR_BASE_API String getDriveDescription() const;
234
239 [[nodiscard]] NDEVR_BASE_API Time getModifiedTime() const;
244 [[nodiscard]] NDEVR_BASE_API Time getCreationTime() const;
249 [[nodiscard]] NDEVR_BASE_API Time getAccessTime() const;
256 NDEVR_BASE_API void getFileTimes(Time& modified_time, Time& creation_time, Time& access_time) const;
261 NDEVR_BASE_API bool updateAccessedTime() const;
267 NDEVR_BASE_API bool updateAccessedTime(Time access_time) const;
268
274 NDEVR_BASE_API File& append(const StringView& s);
280 NDEVR_BASE_API File& append(const char& s);
285 NDEVR_BASE_API File getParentDirectory() const;
290 NDEVR_BASE_API File findNonExistingSuitableName() const;
294 NDEVR_BASE_API void expandEnvironmentalVars();
299 [[nodiscard]] NDEVR_BASE_API FileBuffer getChildren() const;
307 [[nodiscard]] NDEVR_BASE_API FileBuffer getChildrenMatching(const StringView& include_pattern, bool recursive = false, uint04 max_count = Constant<uint04>::Max) const;
315 [[nodiscard]] NDEVR_BASE_API FileBuffer getChildrenMatching(const Buffer<String>& include_patterns, bool recursive = false, uint04 max_count = Constant<uint04>::Max) const;
324 [[nodiscard]] NDEVR_BASE_API FileBuffer getChildrenMatching(const Buffer<String>& include_patterns, const Buffer<String>& exclude_patterns, bool recursive = false, uint04 max_count = Constant<uint04>::Max) const;
332 [[nodiscard]] NDEVR_BASE_API FileBuffer getChildrenMatching(const Buffer<StringView>& include_patterns, bool recursive = false, uint04 max_count = Constant<uint04>::Max) const;
341 [[nodiscard]] NDEVR_BASE_API FileBuffer getChildrenMatching(const Buffer<StringView>& include_patterns, const Buffer<StringView>& exclude_patterns, bool recursive = false, uint04 max_count = Constant<uint04>::Max) const;
346 [[nodiscard]] NDEVR_BASE_API FILE* filePtr() const;
353 NDEVR_BASE_API FILE* open(OpenMode mode, bool append = false);
357 NDEVR_BASE_API void removeOpenOwnership();
363 NDEVR_BASE_API String relativePathTo(const File& reference) const;
367 NDEVR_BASE_API void flush();
371 NDEVR_BASE_API void close();
377 NDEVR_BASE_API bool hasNullBytes(uint08 max_length = Constant<uint08>::Max);
382 NDEVR_BASE_API void makeReadOnly(bool read_only);
383
388 NDEVR_BASE_API void create(bool override_file) const;
393 NDEVR_BASE_API void deleteFile(LogPtr log = LogPtr());
394
398 NDEVR_BASE_API void throwIfNotExist() const;
399
404 [[nodiscard]] NDEVR_BASE_API uint08 fileSize() const;
405
410 [[nodiscard]] NDEVR_BASE_API UUID getUUID() const;
411
417 NDEVR_BASE_API File& operator=(const File& value);
418
424 NDEVR_BASE_API bool operator<<(const StringView& value);
430 NDEVR_BASE_API bool operator<<(const Buffer<uint01>& values);
436 NDEVR_BASE_API bool operator<<(const HighCapacityBuffer<uint01>& values);
442 NDEVR_BASE_API bool operator<<(const fltp04& value);
448 NDEVR_BASE_API bool operator<<(const fltp08& value);
449
455 NDEVR_BASE_API File& operator=(String& value);
461 NDEVR_BASE_API File& operator=(File&& value) noexcept;
462
466 NDEVR_BASE_API void setInternalVars();
467
472 [[nodiscard]] NDEVR_BASE_API File generateTempFile() const;
473
478 const String& pathID() const { return m_id; }
483 StringView pathNameID() const;
488 NDEVR_BASE_API bool isMTP() const;
493 NDEVR_BASE_API bool resolveMTPPath() const;
498 NDEVR_BASE_API bool resolveNameFromMTPPath();
502 NDEVR_BASE_API void invalidateCache();
506 NDEVR_BASE_API void updateCache() const;
511 NDEVR_BASE_API const FileCachedData& currentCache() const;
516 NDEVR_BASE_API void updateCache(const FileCachedData& cache);
521 NDEVR_BASE_API String toDisplayString();
526 NDEVR_BASE_API void setFromDisplayString(const StringView& display_string);
532 NDEVR_BASE_API uint08 availableDiskSpace(bool privelaged = true) const;
537 size_t operator()() const
538 {
539 return static_cast<size_t>(hash());
540 }
541 public:
546 [[nodiscard]] NDEVR_BASE_API static File TempFileFolder();
552 [[nodiscard]] NDEVR_BASE_API static String FileSizeToString(uint08 file_size);
557 [[nodiscard]] NDEVR_BASE_API static FileBuffer SystemDrives();
562 [[nodiscard]] NDEVR_BASE_API static FileBuffer MTPDevices();
568 [[nodiscard]] NDEVR_BASE_API static bool DoesFileExist(const String& name);
575 [[nodiscard]] NDEVR_BASE_API static bool typeSort(const File& i, const File& j);
582 [[nodiscard]] NDEVR_BASE_API static bool nameSort(const File& i, const File& j);
589 [[nodiscard]] NDEVR_BASE_API static bool accessSort(const File& i, const File& j);
596 [[nodiscard]] NDEVR_BASE_API static bool createSort(const File& i, const File& j);
602 [[nodiscard]] NDEVR_BASE_API static bool isPathSep(const char& path);
608 [[nodiscard]] NDEVR_BASE_API static bool isIDSep(const char& path);
609 public:
610#ifdef _WIN32
611 static constexpr char PathSep = '\\';
612 static constexpr StringView PathSepS = "\\";
613#else
614 static constexpr char PathSep = '/';
615 static constexpr StringView PathSepS = "/";
616#endif
617 static constexpr char ExtSep = '.';
618 static constexpr char DriveSep = ':';
619 static constexpr char IDSep = '|';
620 private:
625 void checkError(uint04 error) const;
626 protected:
628 mutable String m_id;
634 mutable bool m_id_valid;
635 };
636
640 class NDEVR_BASE_API FileSearch
641 {
642 public:
646 uint04 max_recursion_level = Constant<uint04>::Max;
652 protected:
659 void performSearch(const File& current_file, Buffer<File>& files, uint04 recursion_level) const;
660 };
661}
662
663namespace std//Define things to allow use within std libs
664{
668 template <>
669 struct hash<NDEVR::File>
670 {
676 std::size_t operator()(const File& s) const noexcept
677 {
678 return static_cast<size_t>(s.hash());
679 }
680 };
687 NDEVR_BASE_API istream& operator>>(istream& in, StringView& string);
688};
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
Logic for searching for files based on some criteria.
Definition File.h:641
uint04 max_recursion_level
Maximum directory depth for recursive searches.
Definition File.h:646
Buffer< File > excluded_list
Files or directories to exclude from results.
Definition File.h:645
void performSearch(const File &current_file, Buffer< File > &files, uint04 recursion_level) const
Recursively searches a directory for matching files.
Buffer< File > performSearch() const
Performs the file search and returns all matching files.
Buffer< File > files_recursively_to_search
Root files or directories to search within.
Definition File.h:643
Buffer< String > search_extensions
File extensions to match during the search.
Definition File.h:644
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
FILE * open(OpenMode mode, bool append=false)
Opens the file with the specified mode.
uint01 m_file_ext_start
Index into the path string where the file extension begins.
Definition File.h:633
OpenMode
Specifies the mode in which a file is opened.
Definition File.h:59
@ e_ascii_write_safe
Open for ASCII text writing with safety checks.
Definition File.h:66
@ e_binary_read_safe
Open for binary reading with safety checks.
Definition File.h:65
@ e_binary_write
Open for binary writing.
Definition File.h:61
@ e_ascii_write
Open for ASCII text writing.
Definition File.h:63
@ e_binary_write_safe
Open for binary writing with safety checks.
Definition File.h:67
@ e_undefined
Undefined or unset open mode.
Definition File.h:68
@ e_ascii_read
Open for ASCII text reading.
Definition File.h:62
@ e_ascii_read_safe
Open for ASCII text reading with safety checks.
Definition File.h:64
@ e_binary_read
Open for binary reading.
Definition File.h:60
void setInternalVars()
Recalculates internal cached path component indices from the current path string.
File & appendPath(const StringView &cs)
Appends a path component to this file path.
String getDriveDescription() const
Retrieves a human-readable description of the drive this file resides on.
void close()
Closes the file if it is open.
static bool isPathSep(const char &path)
Checks whether a character is a path separator.
static bool accessSort(const File &i, const File &j)
Comparison function for sorting files by last access time.
void expandEnvironmentalVars()
Expands any environment variables present in the file path.
uint08 availableDiskSpace(bool privelaged=true) const
Queries the available disk space on the drive containing this file.
String m_id
The path identifier string for this file.
Definition File.h:628
bool operator<<(const StringView &value)
Writes a string to the file.
File & appendPath(const char *cs)
Appends a path component to this file path.
bool isHidden() const
Checks whether this file is hidden.
bool resolveNameFromMTPPath()
Resolves the display name from an MTP device path.
bool isOpen() const
Checks whether this file is currently open.
static constexpr char ExtSep
File extension separator character.
Definition File.h:617
bool updateAccessedTime() const
Updates the last accessed time of the file to the current time.
File getParentDirectory() const
Gets the parent directory of this file.
void updateCache(const FileCachedData &cache)
Updates the cached file metadata with the provided data.
uint08 fileSize() const
Gets the size of the file in bytes.
Time getAccessTime() const
Gets the last access time of the file.
File(const File &file)
Copy constructor.
const String & pathID() const
Gets the path identifier string for this file.
Definition File.h:478
bool isMTP() const
Checks whether this file path refers to an MTP (Media Transfer Protocol) device.
FileBuffer getChildrenMatching(const Buffer< StringView > &include_patterns, const Buffer< StringView > &exclude_patterns, bool recursive=false, uint04 max_count=Constant< uint04 >::Max) const
Retrieves children matching include patterns while excluding others, using StringView patterns.
bool m_id_valid
Whether the cached path identifier is currently valid.
Definition File.h:634
static FileBuffer SystemDrives()
Retrieves all system drives.
static FileBuffer MTPDevices()
Retrieves all connected MTP (Media Transfer Protocol) devices.
uint01 m_path_start
Index into the path string where the directory path begins.
Definition File.h:631
FileCachedData m_cached_data
Cached file metadata such as size, times, and existence.
Definition File.h:627
static bool createSort(const File &i, const File &j)
Comparison function for sorting files by creation time.
void setFromDisplayString(const StringView &display_string)
Sets this file path from a user-friendly display string.
size_t operator()() const
Functor operator for use as a hash key in standard containers.
Definition File.h:537
static bool nameSort(const File &i, const File &j)
Comparison function for sorting files by name.
HighCapacityBuffer< uint01 > getDataExtended()
Reads the entire file contents into a high-capacity byte buffer for large files.
bool operator<<(const fltp08 &value)
Writes a 64-bit floating-point value to the file.
void updateCache() const
Updates the cached file metadata from the filesystem.
File(const String &full_path)
Constructs a File from a full path string.
File & operator=(const File &value)
Copy assignment operator.
File(File &&file) noexcept
Move constructor.
File & append(const char &s)
Appends a single character to the file path.
UUID getUUID() const
Gets a UUID that uniquely identifies this file on the filesystem.
void throwIfNotExist() const
Throws an exception if this file does not exist.
File & operator=(File &&value) noexcept
Move assignment operator.
FileBuffer getChildrenMatching(const Buffer< StringView > &include_patterns, bool recursive=false, uint04 max_count=Constant< uint04 >::Max) const
Retrieves children of this directory matching any of the given StringView patterns.
void setFolderName(const StringView &folder_name)
Sets the folder name portion of the file path.
void removeOpenOwnership()
Releases ownership of the open file handle without closing it.
Time getCreationTime() const
Gets the creation time of the file.
bool moveTo(File &file, bool override_file, LogPtr log=LogPtr(), bool is_source_temp=false)
Moves this file to a new location.
void makeReadOnly(bool read_only)
Sets or clears the read-only attribute on this file.
File & operator=(String &value)
Assignment operator from a String reference.
bool operator<<(const HighCapacityBuffer< uint01 > &values)
Writes a high-capacity byte buffer to the file.
static constexpr char DriveSep
Drive separator character (e.g., "C:").
Definition File.h:618
FileBuffer getChildrenMatching(const StringView &include_pattern, bool recursive=false, uint04 max_count=Constant< uint04 >::Max) const
Retrieves children of this directory matching a pattern.
bool resolveMTPPath() const
Resolves the actual filesystem path from an MTP device path.
static constexpr StringView PathSepS
Platform-specific path separator string.
Definition File.h:615
bool isDirectory() const
Checks whether this path refers to a directory.
bool copyTo(const File &cs, bool override_file, LogPtr log=LogPtr()) const
Copies this file to a destination.
void flush()
Flushes any buffered data to disk.
StringView getPath(uint01 file_parts) const
Retrieves a subset of the file path based on the specified parts.
FileBuffer getChildrenMatching(const Buffer< String > &include_patterns, const Buffer< String > &exclude_patterns, bool recursive=false, uint04 max_count=Constant< uint04 >::Max) const
Retrieves children matching include patterns while excluding others.
bool operator<<(const fltp04 &value)
Writes a 32-bit floating-point value to the file.
StringView pathNameID() const
Gets a combined path and name identifier for this file.
uint01 m_file_name_start
Index into the path string where the file name begins.
Definition File.h:632
FILE * filePtr() const
Returns the underlying C FILE pointer for this file.
void invalidateCache()
Invalidates the cached file metadata, forcing a refresh on next access.
bool operator<<(const Buffer< uint01 > &values)
Writes a byte buffer to the file.
const FileCachedData & currentCache() const
Returns the current cached file metadata.
void create(bool override_file) const
Creates the file or directory on disk.
String toDisplayString()
Converts this file path to a user-friendly display string.
static File TempFileFolder()
Gets the system temporary file folder.
String relativePathTo(const File &reference) const
Computes the relative path from this file to a reference file.
static bool isIDSep(const char &path)
Checks whether a character is an ID separator.
~File()
Destructor.
FILE * m_file_ptr
The underlying C file pointer, or nullptr if not open.
Definition File.h:629
bool cachedExist() const
Checks whether the file exists using cached data rather than querying the filesystem.
File & append(const StringView &s)
Appends a string to the file path.
static bool DoesFileExist(const String &name)
Checks whether a file with the given name exists.
static constexpr char IDSep
ID separator character used in path identifiers.
Definition File.h:619
void deleteFile(LogPtr log=LogPtr())
Deletes this file from disk.
StringView getFolderName(bool trailing_mark=false) const
Retrieves the folder name portion of the file path.
File()
Default constructor.
static constexpr char PathSep
Platform-specific path separator character.
Definition File.h:614
Time getModifiedTime() const
Gets the last modified time of the file.
void getFileTimes(Time &modified_time, Time &creation_time, Time &access_time) const
Retrieves all three file timestamps at once.
File(const String &resource_name, const String &path_id)
Constructs a File from a resource name and path identifier.
static String FileSizeToString(uint08 file_size)
Converts a file size in bytes to a human-readable string (e.g., "1.5 MB").
File findNonExistingSuitableName() const
Generates a unique file name based on this path that does not conflict with existing files.
FilePart
Bitmask flags representing parts of a file path.
Definition File.h:74
@ e_file_name
The file name without extension.
Definition File.h:77
@ e_drive
The drive component of the path (e.g., "C:").
Definition File.h:75
@ e_file_extension
The file extension (e.g., ".txt").
Definition File.h:78
@ e_dir_path
The directory component of the path.
Definition File.h:76
@ e_full_path
The complete file path including all components.
Definition File.h:79
File & appendPath(const String &cs)
Appends a path component to this file path.
bool updateAccessedTime(Time access_time) const
Updates the last accessed time of the file to a specific time.
bool hasNullBytes(uint08 max_length=Constant< uint08 >::Max)
Checks whether the file contains null bytes within the specified range.
bool exists() const
Checks whether this file or directory exists on disk.
void removeInvalidChars()
Removes characters from the path that are invalid for the current filesystem.
FileBuffer getChildren() const
Retrieves all direct children of this directory.
Buffer< uint01 > getData()
Reads the entire file contents into a byte buffer.
File generateTempFile() const
Generates a temporary file path derived from this file's directory.
File(String &&full_path)
Constructs a File by moving a path string.
bool isDrive() const
Checks whether this path refers to a drive root.
void setPath(const StringView &path, uint01 part)
Sets a specific part of the file path.
OpenMode m_open_mode
The current open mode of the file.
Definition File.h:630
bool parentDirectoryExists() const
Checks whether the parent directory of this file exists.
File(const StringView &full_path)
Constructs a File from a StringView of a full path.
FileBuffer getChildrenMatching(const Buffer< String > &include_patterns, bool recursive=false, uint04 max_count=Constant< uint04 >::Max) const
Retrieves children of this directory matching any of the given patterns.
static bool typeSort(const File &i, const File &j)
Comparison function for sorting files by type (extension).
A light-weight base class for Log that allows processes to update, without the need for additional in...
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
The core String View class for the NDEVR API.
Definition StringView.h:58
constexpr uint08 hash() const
Creates a simple, quick hash of the object.
Definition String.h:524
constexpr String() noexcept
Constructor used to create an empty String*.
Definition String.h:101
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:62
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer s...
Definition UUID.h:61
The primary namespace for the NDEVR SDK.
AlocatingAlignedBuffer< File, 64 > FileBuffer
Buffer type for storing File objects with 64-byte alignment.
Definition File.h:41
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
@ creation_time
Timestamp when the object was created.
@ modified_time
Timestamp when the object was last modified.
@ file
The source file path associated with this object.
@ name
The display name of the object.
STL namespace.
istream & operator>>(istream &in, StringView &string)
Stream extraction operator for reading into an StringView.
Data that is cached from a file into program memory to limit expensive file lookups.
Definition FileCache.h:22