Qfile
The Qfile crate provides functionality for accessing a file by path, case-insensitive, including automatic detection, creation of a path with a new file or opening an existing file. It includes several submodules to handle different aspects of file handling, such as initialization, reading, and writing. The crate also defines some custom errors related to file operations.
qfile is not the rust version of qfile from the QT framework
Paths syntax
Unix format
let path1 = "File.txt";
let path2 = "./File.txt";
let path3 = "../../File.txt";
let path4 = Stringfrom;
Windows format
let path1 = "File.txt";
let path2 = ".\\File.txt";
let path3 = "..\\..\\File.txt";
let path4 = "D:\\Folder\\file.txt";
let path5 = r"D:\Folder\file.txt";
let path6 = Stringfrom;
Methods to read, write, get the right path are case insensitive
Paths finder
This is the method that kicks off the directory search. It takes in the search location, names of files to search for, excluded directories, whether or not to follow symbolic links, and a channel to send the results back on.
It uses the rayon crate to parallelize the search over multiple threads for better performance.
The algorithm first filters out the excluded directories, and then iterates through the remaining directories to find all the files that match the specified search criteria. If a match is found, the path of the file is sent to the Sender object.
use ;
use mpsc;
let = channel;
find_paths?;
for path in rx
Writing to a file
The method for writing to a file depends on the current context, case insensitive
- If the file exists - overwrites all the content with the new content
- If file does not exist - creates files and, if necessary, all parent folders specified in the path. After that writes the new content
Example (Sync Code)
use ;
// real path : myFolder/file.txt
let mut file = add_path?;
file.write_only_new?;
file.write_only_new?;
Example (Async code)
use ;
// real path : myFolder/file.txt
let mut file = async_add_path.await?;
file.lock.await.auto_write.await?;
file.lock.await.auto_write.await?;
Unix & Windows
- If the path exists, we work with the file
| Linux | Windows | |
|---|---|---|
| The path we specified: | folder1/FolDER2/file.TXT |
folder1\FolDER2\file.TXT |
| Real path : | ./Folder1/Folder2/file.txt |
.\Folder1\Folder2\file.txt |
| Result : | ./Folder1/Folder2/file.txt |
.\Folder1\Folder2\file.txt |
- If the file/path is not found, creates a new path with the file
| Linux | Windows | |
|---|---|---|
| The path we specified: | ./main_folder/folder_new/file.txt |
.\main_folder\folder_new\file.txt |
| Real path : | ./Main_Folder |
.\Main_Folder |
| Result : | ./Main_Folder/folder_new/file.txt |
.\Main_Folder\folder_new\file.txt |
- The Windows file system treats file and directory names as case insensitive.
file.txtandFILE.txtwill be treated as equivalent files (Although the path is case insensitive in windows (..\FOLDER\file.txt), you can return a case-sensitive path with :get_path_string()orget_path_buf()).
Reading a file
Method for reading the contents of a file (String), case insensitive
Example (Sync code)
use ;
// real path : myFolder/file.txt
let mut file = add_path?;
let text = file.read?;
println!;
Example (Aync code)
use ;
// real path : myFolder/file.txt
let mut file = async_add_path.await?;
file.lock.await.auto_write.await?;
file.lock.await.auto_write.await?;