#include <string.h>
#include <stdbool.h>
#include <limits.h>
#include <glib.h>
#include "str_functions.h"
Go to the source code of this file.
Defines | |
#define | DIRECTORY_SEPARATOR '/' |
DIRECTORY_SEPARATOR is the separator for splits the directories in paths. | |
#define | PATH_SEPARATOR ':' |
PATH_SEPARATOR is the separator for splits the paths in configuration files or environment variables. | |
File access mode | |
Access modes of a file or directory.
This is similar to | |
#define | FILE_MODE_NO_SYMLINK 1<<5 |
Test whether the file is not symbol link. | |
#define | FILE_MODE_EXIST 1<<4 |
Test for existence. | |
#define | FILE_MODE_DIRECTORY 1<<3 |
Test whether the 'file' is directory. | |
#define | FILE_MODE_READ 1<<2 |
Test for read permission. | |
#define | FILE_MODE_WRITE 1<<1 |
Test for write permission. | |
#define | FILE_MODE_EXECUTE 1 |
Test for execute permission. | |
Typedefs | |
typedef TaskStatus(* | ChooseFilenameFunc )(gchar *filename_buf, guint filename_len, StringList *extensions, guint access_mode_mask, const gchar *prompt, gpointer option) |
Prototype of callback function for choosing a filename. | |
Enumerations | |
enum | TaskStatus { TASK_RUNNING, TASK_CANCELED, TASK_FAILED, TASK_COMPLETED } |
An enumeration of task running status. More... | |
Functions | |
gboolean | isReadable (const gchar *filename) |
Whether the file is readable. | |
gboolean | isWritable (const gchar *filename) |
Whether the file is writable. | |
gboolean | filename_meets_accessMode (const gchar *filename, guint access_mode_mask) |
Whether the file meets the given access mode mask. | |
gchar * | filename_search_paths (const gchar *filename, const gchar *search_paths) |
Find a file from the search path. | |
gchar * | filename_search_paths_mode (const gchar *filename, const gchar *search_paths, guint access_mode_mask) |
Find a file with specified access mode from the search path. | |
gchar * | filename_choose (const gchar *filename_default, guint filename_len, StringList *extensions, guint access_mode_mask, const gchar *prompt, gpointer option, ChooseFilenameFunc callback) |
Choose a suitable filename if default one is not. | |
StringList * | lsDir (const gchar *dir, const gchar *globStr, guint access_mode_mask, gboolean keepPath) |
List files which meet the given pattern and access modes in a directory. | |
StringList * | lsDir_append (StringList *sList, const gchar *dir, const gchar *globStr, guint access_mode_mask, gboolean keepPath) |
Append the files which meet the given pattern and access modes in a directory to the existing list. | |
StringList * | path_split (const gchar *path) |
Split a composite path string by path separator. | |
gchar * | path_concat (gchar *dest, const gchar *src, gsize destSize) |
Concatenate two path strings. | |
gchar * | truepath (const gchar *path, gchar *resolved_path) |
Return the canonicalized absolute pathname. |
#define DIRECTORY_SEPARATOR '/' |
If WIN32 is defined, DIRECTORY_SEPARATOR is '\', otherwise '/' is used as DIRECTORY_SEPARATOR.
#define PATH_SEPARATOR ':' |
If WIN32 is defined, PATH_SEPARATOR is ';', otherwise ':' is used as PATH_SEPARATOR.
typedef TaskStatus(* ChooseFilenameFunc)(gchar *filename_buf, guint filename_len, StringList *extensions, guint access_mode_mask, const gchar *prompt, gpointer option) |
This callback function should provide an UI for user select a filename. It should return:
TASK_COMPLETED
if a suitable file is chosen, and the chosen filename is in filename_buf.TASK_CANCELED
if user canceled the operation.TAKS_FAILED
if user selected an unsuitable file, or encounter I/O error, the selected file should be in filename_buf Parameter extensions for available extension filename, assign NULL if no need to have a specific extension. Use access_modes_mask to filter the files that has suitable access mode and permission.
Value in prompt is to be show in UI, such as dialog title or textual prompt in CLI. Use option to pass other data to the callback function.
filename_buf | Buffer that holds the filename, the present content will be default filename. | |
filename_len | pre-selected filename. Can be NULL. | |
extensions | acceptable file extensions. NULL for don't care. | |
access_mode_mask | the required access mode mask defined in File access mode.. | |
prompt | the string to be shown in UI, usually shown as dialog title for GUI. | |
option | Other custom option. |
TASK_COMPLETED | if a suitable file is chosen, and the chosen filename is in filename_buf. | |
TASK_CANCELED | if user canceled the operation. | |
TAKS_FAILED | if user selected an unsuitable file, or encounter I/O error, the selected file should be in filename_buf |
enum TaskStatus |
This enumeration lists the task running status. It can be used as function return values, or as concurrent process running status.
gchar* filename_choose | ( | const gchar * | filename_default, | |
guint | filename_len, | |||
StringList * | extensions, | |||
guint | access_mode_mask, | |||
const gchar * | prompt, | |||
gpointer | option, | |||
ChooseFilenameFunc | callback | |||
) |
This function returns either a newly allocated filename string that meets the access_mode_mask, or NULL if user cancel the process.
This function firstly try the filename given by a non-NULL filename_default, if it does not meet access_mode_make, then it will keep calling callback() to open an UI for user input until:
filename_default | The default filename to be check, NULL or "" if no default filename. | |
filename_len | pre-selected filename. Can be NULL. | |
extensions | acceptable file extensions. NULL for don't care. | |
access_mode_mask | the required access mode mask defined in File access mode.. | |
prompt | the string to be shown in UI, usually shown as dialog title for GUI. | |
option | Other custom option. | |
callback | a ChooseFilenameFunc callback that open a UI for filename input. |
gboolean filename_meets_accessMode | ( | const gchar * | filename, | |
guint | access_mode_mask | |||
) |
This function checks whether the filename meets the requirement specified as access_mode_mask. It returns TRUE if all file access mode are meet; FALSE otherwise.
Note that if only FILE_MODE_WRITE
is given to access_mode_mask, this function returns TRUE even if the file does not exist, but can be created and written. Use FILE_MODE_EXIST|FILE_MODE_WRITE
the check the write permission of existing file.
filename | The filename to be checked. | |
access_mode_mask | the required access mode mask defined in File access mode.. |
gchar* filename_search_paths | ( | const gchar * | filename, | |
const gchar * | search_paths | |||
) |
This function searches the given file within the search_paths. It is essentially filename_search_paths_mode(filename, search_paths, FILE_MODE_EXIST)
.
filename | The filename to be located. | |
search_paths | The paths to be searched. |
gchar* filename_search_paths_mode | ( | const gchar * | filename, | |
const gchar * | search_paths, | |||
guint | access_mode_mask | |||
) |
This function searches the file within the search_paths, after the file is found, filename_meets_accessMode()
is called to check whether the file meets the required access_mode_mask.
This function return newly allocated string of the exact location of the file if the file is found and it match the requirement; otherwise NULL is returned.
filename | The filename to be located. | |
search_paths | The paths to be searched. | |
access_mode_mask | the required access mode mask defined in File access mode.. |
gboolean isReadable | ( | const gchar * | filename | ) |
filename | The path name of the file. |
gboolean isWritable | ( | const gchar * | filename | ) |
This function checks whether the file is writable, or can be created if filename does not exists.
filename | The path name of the file. |
StringList* lsDir | ( | const gchar * | dir, | |
const gchar * | globStr, | |||
guint | access_mode_mask, | |||
gboolean | keepPath | |||
) |
This function returns all files that match the pattern glob and the access mode access_mode_mask; NULL
if error occurred.
The pattern glob is matched in the same way as in shell.
Use stringList_free() to free the result StringList.
dir | Directory to be listed. | |
globStr | Glob/Shell pattern such as "*.*", use space to separate multiple patterns. | |
access_mode_mask | the required access mode mask defined in File access mode.. | |
keepPath | TRUE to concatenate dir to each string in the returned StringList; FALSE for the filename only (without path). |
StringList* lsDir_append | ( | StringList * | sList, | |
const gchar * | dir, | |||
const gchar * | globStr, | |||
guint | access_mode_mask, | |||
gboolean | keepPath | |||
) |
This function appends the files it found to the existing list sList, and return it, otherwise it is similar with lsDir().
sList | existing list. | |
dir | Directory to be listed. | |
globStr | Glob/Shell pattern such as "*.*", use space to separate multiple patterns. | |
access_mode_mask | the required access mode mask defined in File access mode.. | |
keepPath | TRUE to concatenate dir to each string in the returned StringList; FALSE for the filename only (without path). |
gchar* path_concat | ( | gchar * | dest, | |
const gchar * | src, | |||
gsize | destSize | |||
) |
This function concatenate to path strings, a DIRECTORY_SEPARATOR
is inserted between dest and src if dest does not ended with a DIRECTORY_SEPARATOR
. The result string will be returned.
dest | destination buffer, already containing one nul-terminated string | |
src | string to be appended to dest. | |
destSize | length of dest buffer in bytes (not length of existing string inside dest). |
StringList* path_split | ( | const gchar * | path | ) |
This function splits a string that has multiple paths (composite path string) separated by PATH_SEPARATOR
into individual paths. These paths will be returned as StringList
.
path | The composite path. |
gchar* truepath | ( | const gchar * | path, | |
gchar * | resolved_path | |||
) |
It works exactly the same with realpath(3), except this function can handle the path with ~, where realpath cannot.
path | The path to be resolved. | |
resolved_path | Buffer for holding the resolved_path. |