|
C-Menu 0.2.9
A User Interface Toolkit
|
string manipulation, file handling, and error reporting. More...
Functions | |
| size_t | trim (char *s) |
| Trims leading and trailing spaces from string s in place. | |
| size_t | ssnprintf (char *buf, size_t buf_size, const char *format,...) |
| ssnprintf was designed to be a safer alternative to snprintf. | |
| int | str_to_args (char **argv, char *arg_str, int max_args) |
| Converts a string into an array of argument strings. | |
| void | destroy_argv (int argc, char **argv) |
| Deallocates memory allocated for argument strings in argv. | |
| bool | str_to_lower (char *s) |
| Converts a string to lowercase. | |
| bool | str_to_upper (char *s) |
| Converts a string to uppercase. | |
| size_t | strnz__cpy (char *d, const char *s, size_t max_len) |
| safer alternative to strncpy | |
| size_t | strnz__cat (char *d, const char *s, size_t max_len) |
| safer alternative to strncat | |
| size_t | strz (char *s) |
| Terminates string at new line or carriage return. | |
| size_t | strnz (char *s, size_t max_len) |
| terminates string at New Line, Carriage Return, or max_len | |
| size_t | strnlf (char *s, size_t max_len) |
| terminates string with line feed | |
| char * | strnz_dup (char *s, size_t l) |
| Allocates memory for and duplicates string s up to length l or until line feed or carriage return. | |
| bool | str_subc (char *d, char *s, char ReplaceChr, char *Withstr, int l) |
| Replaces "ReplaceChr" in "s" with "Withstr" in "d" won't copy more than "l" bytes to "d" Replaces all occurrences of a character in a string with another string, copying the result to a destination buffer. | |
| bool | strnfill (char *s, char c, int n) |
| Fills string s with character c n. | |
| bool | strip_quotes (char *s) |
| removes leading and trailing double quotes if present | |
| bool | stripz_quotes (char *s) |
| removes leading and trailing double quotes if present | |
| bool | chrep (char *s, char old_chr, char new_chr) |
| Replaces all occurrences of old_chr in s with new_chr in place. | |
| int | a_toi (char *s, bool *a_toi_error) |
| a safer alternative to atoi() for converting ASCII strings to integers. | |
| size_t | strip_ansi (char *d, char *s) |
| Strips ANSI SGR escape sequences (ending in 'm') from string s to d. | |
| bool | normalize_file_spec (char *fs) |
| replace backslashes with forward lashes | |
| bool | file_spec_path (char *fp, char *fs) |
| extracts the path component of a file specification | |
| bool | file_spec_name (char *fn, char *fs) |
| extracts the file name component of a file specification | |
| double | str_to_double (char *s) |
| converts string to double | |
| bool | expand_tilde (char *path, int path_maxlen) |
| Replace Leading Tilde With Home Directory. | |
| bool | trim_path (char *dir) |
| Trims trailing spaces and slashes from directory path in place. | |
| bool | trim_ext (char *buf, char *filename) |
| trims the file extension from "filename" and copies the result to "buf" | |
| bool | base_name (char *buf, char *path) |
| Returns the base name of a file specification. | |
| bool | dir_name (char *buf, char *path) |
| Returns the directory name of a file specification. | |
| bool | verify_dir (char *spec, int imode) |
| Verifies that the directory specified by "spec" exists and is accessible with the permissions specified by "imode". | |
| bool | verify_file (char *in_spec, int imode) |
| Verifies that the file specified by "in_spec" exists and is accessible with the permissions specified by "imode". | |
| bool | locate_file_in_path (char *file_spec, char *file_name) |
| Locates a file in the system PATH. | |
| bool | lf_find (const char *base_path, const char *re, const char *ere, int max_depth, int flags) |
| Find files in a directory matching a regular expression. | |
| bool | lf_process (const char *base_path, regex_t *compiled_re, regex_t *compiled_ere, int depth, int max_depth, int flags) |
| logic for lf_find() | |
| bool | mk_dir (char *dir) |
| If directory doesn't exist, make it. | |
| size_t | canonicalize_file_spec (char *spec) |
| Removes quotes and trims at first space. | |
| bool | is_directory (const char *path) |
| Checks if the given path is a directory. | |
| bool | is_valid_regex (const char *pattern) |
| Checks if the given regular expression pattern is valid. | |
| char * | rep_substring (const char *org_s, const char *tgt_s, const char *rep_s) |
| Replace all occurrences of "tgt_s" in "org_s" with "rep_s". | |
string manipulation, file handling, and error reporting.
These functions provide common operations such as trimming strings, converting case, safely copying and concatenating strings, verifying file and directory access, and locating files in the system PATH. They are designed to be robust and handle edge cases gracefully, making them useful for a wide range of applications.
| int a_toi | ( | char * | s, |
| bool * | a_toi_error ) |
a safer alternative to atoi() for converting ASCII strings to integers.
| s | is the input string |
| a_toi_error | is a pointer to a boolean that will be set to true if an error occurs during conversion, or false if the conversion is successful. |
Definition at line 505 of file futil.c.
Referenced by parse_ansi_str().
| bool base_name | ( | char * | buf, |
| char * | path ) |
Returns the base name of a file specification.
| buf | - buffer to receive result |
| path | - file specification |
Definition at line 775 of file futil.c.
Referenced by build_prompt(), exec_objects(), form_process(), init_form_files(), init_pick_files(), main(), and view_init_input().
| size_t canonicalize_file_spec | ( | char * | spec | ) |
Removes quotes and trims at first space.
| spec | - file specification to canonicalize |
Definition at line 1216 of file futil.c.
References strnz__cpy().
Referenced by locate_file_in_path(), verify_file(), and verify_spec_arg().
| bool chrep | ( | char * | s, |
| char | old_chr, | ||
| char | new_chr ) |
Replaces all occurrences of old_chr in s with new_chr in place.
| s | - string to modify |
| old_chr | - character to replace |
| new_chr | - character to insert |
Definition at line 486 of file futil.c.
Referenced by parse_menu_description().
| void destroy_argv | ( | int | argc, |
| char ** | argv ) |
Deallocates memory allocated for argument strings in argv.
| argc | - count of allocated vectors in argv |
| argv | - array of pointers to arguments |
Definition at line 221 of file futil.c.
Referenced by destroy_init(), destroy_view(), display_pick_help(), enter_file_spec(), exec_objects(), form_engine(), form_process(), init_pick(), menu_cmd_processor(), view_display_help(), and view_init_input().
| bool dir_name | ( | char * | buf, |
| char * | path ) |
Returns the directory name of a file specification.
| buf | - buffer to receive result |
| path | - file specification |
Definition at line 801 of file futil.c.
References strnz__cpy().
| bool expand_tilde | ( | char * | path, |
| int | path_maxlen ) |
Replace Leading Tilde With Home Directory.
| path | - path to expand |
| path_maxlen | - maximum length of path |
Definition at line 684 of file futil.c.
References strnz__cat(), and strnz__cpy().
Referenced by enter_file_spec(), mapp_initialization(), mk_dir(), parse_config(), verify_dir(), verify_file(), verify_spec_arg(), and view_init_input().
| bool file_spec_name | ( | char * | fn, |
| char * | fs ) |
extracts the file name component of a file specification
| fn | - name component to return |
| fs | - full file specification |
Definition at line 605 of file futil.c.
| bool file_spec_path | ( | char * | fp, |
| char * | fs ) |
extracts the path component of a file specification
| fp | - path component to return |
| fs | - full file specification |
Definition at line 578 of file futil.c.
| bool is_directory | ( | const char * | path | ) |
Checks if the given path is a directory.
| path | - path to check |
Definition at line 1245 of file futil.c.
Referenced by main().
| bool is_valid_regex | ( | const char * | pattern | ) |
Checks if the given regular expression pattern is valid.
| pattern | - regular expression pattern to check |
Definition at line 1256 of file futil.c.
Referenced by main().
| bool lf_find | ( | const char * | base_path, |
| const char * | re, | ||
| const char * | ere, | ||
| int | max_depth, | ||
| int | flags ) |
Find files in a directory matching a regular expression.
| base_path | directory to search |
| ere | regular expression match to exclude |
| re | regular expression match to include |
| max_depth | depth of directories to scan |
| flags | search flags |
Definition at line 977 of file futil.c.
References display_error(), em0, em1, em2, LF_EXC_REGEX, LF_ICASE, lf_process(), LF_REGEX, and ssnprintf().
Referenced by main().
| bool lf_process | ( | const char * | base_path, |
| regex_t * | compiled_re, | ||
| regex_t * | compiled_ere, | ||
| int | depth, | ||
| int | max_depth, | ||
| int | flags ) |
logic for lf_find()
| base_path | directory to search |
| compiled_ere | compiled regular expression to exclude |
| compiled_re | compiled regular expression to include |
| depth | recursion counter |
| max_depth | how deep to descend into the directory structure |
| flags |
LF_ALL = 1, List all files including hidden files
LF_ICASE = 2, Ignore case in search
LF_EXC_REGEX = 4, Exclude files matching regular expression
LF_REGEX = 8, Include files matching regular expression
LF_BLK = 256,
LF_CHR = 512,
LF_DIR = 1024,
LF_FIFO = 2048,
LF_LNK = 4096,
LF_REG = 8192,
LF_SOCK = 16384,
LF_UNKNOWN = 32768,
Include Exclude
---------- ----------
LF_BLK 1 0 00000001 7 11111110 block device
LF_CHR 2 1 00000010 6 11111101 character device
LF_DIR 4 2 00000100 5 11111011 directory
LF_FIFO 8 3 00001000 4 11110111 named pipe
LF_LNK 16 4 00010000 3 11101111 link
LF_REG 32 5 00100000 2 11011111 regular file
LF_SOCK 64 6 01000000 1 10111111 socket
LF_UNKNOWN 128 7 10000000 0 01111111 unknown
bool s_blk;
bool s_chr;
bool s_dir;
bool s_fifo;
bool s_lnk;
bool s_reg;
bool s_sock;
bool s_unknown;
Definition at line 1063 of file futil.c.
References FT_BLK, FT_CHR, FT_DIR, FT_FIFO, FT_LNK, FT_REG, FT_SOCK, FT_UNKNOWN, LF_EXC_REGEX, LF_HIDE, lf_process(), LF_REGEX, Perror(), ssnprintf(), strnz__cat(), and strnz__cpy().
Referenced by lf_find(), and lf_process().
| bool locate_file_in_path | ( | char * | file_spec, |
| char * | file_name ) |
Locates a file in the system PATH.
| file_spec | - buffer to receive located file specification |
| file_name | - name of file to locate |
Definition at line 939 of file futil.c.
References canonicalize_file_spec(), strnz__cat(), and strnz__cpy().
Referenced by init_form_files(), init_pick_files(), and verify_spec_arg().
| bool mk_dir | ( | char * | dir | ) |
If directory doesn't exist, make it.
| dir | directory name |
Directory does not exist and unable to create
Definition at line 1195 of file futil.c.
References display_error(), em0, em1, em2, expand_tilde(), ssnprintf(), strnz__cat(), strnz__cpy(), and verify_dir().
Referenced by enter_file_spec().
| bool normalize_file_spec | ( | char * | fs | ) |
replace backslashes with forward lashes
| fs | - file specification to normalize |
Definition at line 561 of file futil.c.
| char * rep_substring | ( | const char * | org_s, |
| const char * | tgt_s, | ||
| const char * | rep_s ) |
Replace all occurrences of "tgt_s" in "org_s" with "rep_s".
| org_s | - original string |
| tgt_s | - target substring to replace |
| rep_s | - replacement substring |
Definition at line 1292 of file futil.c.
References strnz__cpy().
Referenced by exec_objects().
| size_t ssnprintf | ( | char * | buf, |
| size_t | buf_size, | ||
| const char * | format, | ||
| ... ) |
ssnprintf was designed to be a safer alternative to snprintf.
It ensures that the buffer is not overflowed by taking the buffer size as a parameter and using vsnprintf internally. It also returns the number of characters that would have been written if enough space had been available, allowing the caller to detect truncation. This function is particularly useful in situations where the formatted string may exceed the buffer size, as it prevents buffer overflows and provides a way to handle such cases gracefully.
| buf | - buffer to receive formatted string |
| buf_size | - size of buffer |
| format | - printf-style format string |
| ... | - arguments |
Definition at line 147 of file futil.c.
Referenced by action_disposition(), answer_yn(), display_error(), display_line(), enter_file_spec(), fork_exec(), form_desc_error(), form_parse_desc(), form_process(), form_write(), get_clr_pair(), handle_signal(), init_view_boxwin(), init_view_full_screen(), lf_find(), lf_process(), lp(), mapp_initialization(), menu_engine(), mk_dir(), new_init(), new_pick(), new_view(), open_pick_win(), parse_menu_description(), Perror(), search(), set_chyron_key(), set_chyron_key_cp(), signal_handler(), verify_dir(), verify_file(), view_cmd_processor(), view_init_input(), wait_continue(), wait_mk_win(), waitpid_with_timeout(), and write_view_buffer().
| bool str_subc | ( | char * | d, |
| char * | s, | ||
| char | ReplaceChr, | ||
| char * | Withstr, | ||
| int | l ) |
Replaces "ReplaceChr" in "s" with "Withstr" in "d" won't copy more than "l" bytes to "d" Replaces all occurrences of a character in a string with another string, copying the result to a destination buffer.
| d | - destination string |
| s | - source string |
| ReplaceChr | - character to replace |
| Withstr | - string to insert |
| l | - maximum length to copy |
This function ensures that the total length of the resulting string does not exceed the specified limit, and that the result is null-terminated. This function is useful for simple string substitutions where you want to replace a single character with a longer string, such as replacing spaces with underscores or tabs with spaces.
Definition at line 415 of file futil.c.
Referenced by view_cmd_processor().
| int str_to_args | ( | char ** | argv, |
| char * | arg_str, | ||
| int | max_args ) |
Converts a string into an array of argument strings.
| argv | - array of pointers to arguments |
| arg_str | - string containing arguments |
| max_args | - maximum number of arguments to parse |
Definition at line 167 of file futil.c.
Referenced by enter_file_spec(), exec_objects(), form_process(), init_pick(), menu_cmd_processor(), and view_init_input().
| double str_to_double | ( | char * | s | ) |
converts string to double
| s | - string to convert |
Definition at line 637 of file futil.c.
Referenced by parse_config().
| bool str_to_lower | ( | char * | s | ) |
Converts a string to lowercase.
| s | - string to convert |
Definition at line 233 of file futil.c.
Referenced by clr_name_to_idx(), and form_parse_desc().
| bool str_to_upper | ( | char * | s | ) |
Converts a string to uppercase.
| s | - string to convert |
Definition at line 247 of file futil.c.
| size_t strip_ansi | ( | char * | d, |
| char * | s ) |
Strips ANSI SGR escape sequences (ending in 'm') from string s to d.
| d | Destination string |
| s | Source string |
Definition at line 537 of file futil.c.
Referenced by main(), and write_view_buffer().
| bool strip_quotes | ( | char * | s | ) |
removes leading and trailing double quotes if present
| s | - string to strip quotes from |
Definition at line 454 of file futil.c.
Referenced by init_form_files(), and init_view_files().
| bool stripz_quotes | ( | char * | s | ) |
removes leading and trailing double quotes if present
| s | - string to strip quotes from |
Definition at line 469 of file futil.c.
Referenced by verify_spec_arg().
| bool strnfill | ( | char * | s, |
| char | c, | ||
| int | n ) |
Fills string s with character c n.
| s | - string to fill |
| c | - character to fill with |
| n | - number of characters to fill |
Definition at line 440 of file futil.c.
Referenced by form_display_fields().
| size_t strnlf | ( | char * | s, |
| size_t | max_len ) |
terminates string with line feed
| s | string to terminate |
| max_len | maximum length to scan |
Definition at line 358 of file futil.c.
Referenced by write_view_buffer().
| size_t strnz | ( | char * | s, |
| size_t | max_len ) |
terminates string at New Line, Carriage Return, or max_len
| s | string to terminate |
| max_len | - maximum length to scan |
Definition at line 340 of file futil.c.
Referenced by answer_yn(), display_error(), enter_file_spec(), form_display_fields(), form_display_screen(), form_fmt_field(), and pick_engine().
| size_t strnz__cat | ( | char * | d, |
| const char * | s, | ||
| size_t | max_len ) |
safer alternative to strncat
| d | - destination string |
| s | - source string |
| max_len | - maximum length to copy |
Definition at line 298 of file futil.c.
Referenced by build_prompt(), derive_file_spec(), display_pick_help(), enter_file_spec(), exec_objects(), expand_tilde(), file_spec_parts(), form_display_screen(), form_engine(), form_exec_cmd(), form_parse_desc(), form_process(), form_read_data(), form_write(), init_form(), init_menu_files(), init_pick(), init_view_files(), lf_process(), locate_file_in_path(), menu_cmd_processor(), mk_dir(), open_curses(), output_objects(), parse_menu_description(), popup_ckeys(), search(), verify_spec_arg(), view_cmd_processor(), view_display_help(), view_init_input(), whence(), write_config(), and write_view_buffer().
| size_t strnz__cpy | ( | char * | d, |
| const char * | s, | ||
| size_t | max_len ) |
safer alternative to strncpy
copies string s to d, ensuring that the total length of d does not exceed max_len, and that the resulting string is null-terminated. It also treats newline and carriage return characters as string terminators, preventing them from being included in the result. This is particularly useful when copying user input or file data, where embedded newlines could cause issues.
| d | - destination string |
| s | - source string |
| max_len | - maximum length to copy |
Definition at line 269 of file futil.c.
Referenced by answer_yn(), build_prompt(), canonicalize_file_spec(), derive_file_spec(), dir_name(), display_error(), display_pick_help(), display_prompt(), enter_file_spec(), exec_objects(), expand_tilde(), file_spec_parts(), form_desc_error(), form_display_screen(), form_engine(), form_exec_cmd(), form_fmt_field(), form_parse_desc(), form_process(), form_read_data(), form_write(), get_cmd_arg(), handle_signal(), init_form(), init_form_files(), init_menu_files(), init_pick(), init_pick_files(), init_view_boxwin(), init_view_files(), lf_process(), locate_file_in_path(), main(), mapp_initialization(), menu_cmd_processor(), mk_dir(), new_form(), open_curses(), output_objects(), parse_config(), parse_menu_description(), Perror(), popup_ckeys(), rep_substring(), save_object(), search(), verify_dir(), verify_file(), verify_spec_arg(), view_cmd_processor(), view_display_help(), view_file(), view_init_input(), whence(), write_config(), and write_view_buffer().
| char * strnz_dup | ( | char * | s, |
| size_t | l ) |
Allocates memory for and duplicates string s up to length l or until line feed or carriage return.
| s | - string to duplicate |
| l | - maximum length to copy |
Definition at line 379 of file futil.c.
Referenced by new_view().
| size_t strz | ( | char * | s | ) |
Terminates string at new line or carriage return.
| s | string to terminate |
Definition at line 322 of file futil.c.
| size_t trim | ( | char * | s | ) |
Trims leading and trailing spaces from string s in place.
| s | - string to trim |
Definition at line 118 of file futil.c.
Referenced by form_fmt_field(), form_parse_desc(), left_justify(), menu_cmd_processor(), parse_menu_description(), and right_justify().
| bool trim_ext | ( | char * | buf, |
| char * | filename ) |
trims the file extension from "filename" and copies the result to "buf"
| buf | - buffer to receive result |
| filename | - filename to trim |
Definition at line 741 of file futil.c.
| bool trim_path | ( | char * | dir | ) |
Trims trailing spaces and slashes from directory path in place.
| dir | - directory path to trim |
Definition at line 713 of file futil.c.
Referenced by derive_file_spec().
| bool verify_dir | ( | char * | spec, |
| int | imode ) |
Verifies that the directory specified by "spec" exists and is accessible with the permissions specified by "imode".
| spec | - directory specification |
| imode | - access mode F_OK - existence R_OK - read W_OK - Write X_OK - Execute S_WCOK - Write or Create S_QUIET - Suppress Error Messages |
Definition at line 841 of file futil.c.
References display_error(), em0, em1, em2, em3, expand_tilde(), fn, src_line, src_name, ssnprintf(), and strnz__cpy().
Referenced by mapp_initialization(), mk_dir(), and verify_spec_arg().
| bool verify_file | ( | char * | in_spec, |
| int | imode ) |
Verifies that the file specified by "in_spec" exists and is accessible with the permissions specified by "imode".
| in_spec | - directory specification |
| imode | - access mode F_OK - existence R_OK - read W_OK - Write X_OK - Execute S_WCOK - Write or Create S_QUIET - Suppress Error Messages |
Definition at line 892 of file futil.c.
References canonicalize_file_spec(), display_error(), em0, em1, em2, em3, expand_tilde(), fn, src_line, src_name, ssnprintf(), and strnz__cpy().
Referenced by verify_spec_arg().