#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "core.h"
#include "log.h"
#include "relocation.h"
Go to the source code of this file.
|
|
#define | STASIS_SORT_ALPHA 1 << 0 |
| |
|
#define | STASIS_SORT_NUMERIC 1 << 1 |
| |
|
#define | STASIS_SORT_LEN_ASCENDING 1 << 2 |
| |
|
#define | STASIS_SORT_LEN_DESCENDING 1 << 3 |
| |
| #define | strdup_maybe(S) |
| |
|
|
char * | strdup_maybe_entry (const char *restrict s, struct ExecPoint ep, int exit_code) |
| |
| int | num_chars (const char *sptr, int ch) |
| |
| int | startswith (const char *sptr, const char *pattern) |
| |
| int | endswith (const char *sptr, const char *pattern) |
| |
| void | strchrdel (char *sptr, const char *chars) |
| |
| char ** | split (char *sptr, const char *delim, size_t max) |
| |
| char * | join (char **arr, const char *separator) |
| |
| char * | join_ex (char *separator,...) |
| |
| char * | substring_between (char *sptr, const char *delims) |
| |
| void | strsort (char **arr, unsigned int sort_mode) |
| |
| int | isrelational (char ch) |
| |
| void | print_banner (const char *s, int len) |
| |
| char * | strstr_array (char **arr, const char *str) |
| |
| char ** | strdeldup (char **arr) |
| |
| char * | lstrip (char *sptr) |
| |
| char * | strip (char *sptr) |
| |
| int | isempty (char *sptr) |
| |
| int | isquoted (char *sptr) |
| |
| char * | normalize_space (char *s) |
| |
| char ** | strdup_array (char **array) |
| |
| int | strcmp_array (const char **a, const char **b) |
| |
| int | isdigit_s (const char *s) |
| |
| char * | tolower_s (char *s) |
| |
| char * | to_short_version (const char *s) |
| |
|
void | unindent (char *s) |
| |
|
int | safe_strncpy (char *dst, const char *src, size_t dsize) |
| |
|
int | safe_strncat (char *dst, const char *src, size_t dsize) |
| |
◆ strdup_maybe
| #define strdup_maybe |
( |
| S | ) |
|
Value:strdup_maybe_entry((S), EXECPOINT, 1)
◆ endswith()
| int endswith |
( |
const char * | sptr, |
|
|
const char * | pattern ) |
Scan for pattern string at the end of sptr
- Parameters
-
| sptr | string to scan |
| pattern | string to search for |
- Returns
- 1 = found, 0 = not found / error
◆ isdigit_s()
| int isdigit_s |
( |
const char * | s | ) |
|
Determine whether a string is comprised of digits
- Parameters
-
- Returns
- 0=no, 1=yes
◆ isempty()
| int isempty |
( |
char * | sptr | ) |
|
Check if a given string is "visibly" empty
char visibly[100];
strcpy(visibly, "\t \t\n");
printf("string is 'empty'\n");
} else {
printf("string is not 'empty'\n");
}
int isempty(char *sptr)
Definition str.c:475
- Parameters
-
- Returns
- 0=not empty, 1=empty
◆ isquoted()
| int isquoted |
( |
char * | sptr | ) |
|
Determine if a string is encapsulated by quotes
- Parameters
-
- Returns
- 0=not quoted, 1=quoted
◆ isrelational()
| int isrelational |
( |
char | ch | ) |
|
Determine whether the input character is a relational operator Note: ~ is non-standard
- Parameters
-
- Returns
- 0=no, 1=yes
◆ join()
| char * join |
( |
char ** | arr, |
|
|
const char * | separator ) |
Create new a string from an array of strings
char *array[] = {
"this",
"is",
"a",
"test",
NULL,
}
char *test =
join(array,
" ");
char *test2 =
join(array,
"_");
char *test3 =
join(array,
", ");
free(test);
free(test2);
free(test3);
char * join(char **arr, const char *separator)
Definition str.c:158
- Parameters
-
| arr | |
| separator | characters to insert between elements in string |
- Returns
- new joined string
◆ join_ex()
| char * join_ex |
( |
char * | separator, |
|
|
| ... ) |
Join two or more strings by a separator string
- Parameters
-
- Returns
- string
◆ lstrip()
| char * lstrip |
( |
char * | sptr | ) |
|
Remove leading whitespace from a string
char input[100];
strcpy(input, " I had leading spaces");
char * lstrip(char *sptr)
Definition str.c:432
- Parameters
-
- Returns
- pointer to first non-whitespace character in string
◆ normalize_space()
| char * normalize_space |
( |
char * | s | ) |
|
Collapse whitespace in s. The string is modified in place.
- Parameters
-
- Returns
- pointer to
s
◆ num_chars()
| int num_chars |
( |
const char * | sptr, |
|
|
int | ch ) |
Determine how many times the character ch appears in sptr string
- Parameters
-
| sptr | string to scan |
| ch | character to find |
- Returns
- count of characters found
◆ print_banner()
| void print_banner |
( |
const char * | s, |
|
|
int | len ) |
Print characters in s, len times
- Parameters
-
◆ split()
| char ** split |
( |
char * | sptr, |
|
|
const char * | delim, |
|
|
size_t | max ) |
Split a string by every delimiter in delim string.
Callee should free memory using GENERIC_ARRAY_FREE()
- Parameters
-
| sptr | string to split |
| delim | characters to split on |
- Returns
- success=parts of string, failure=NULL
◆ startswith()
| int startswith |
( |
const char * | sptr, |
|
|
const char * | pattern ) |
Scan for pattern string at the beginning of sptr
- Parameters
-
| sptr | string to scan |
| pattern | string to search for |
- Returns
- 1 = found, 0 = not found / error
◆ strchrdel()
| void strchrdel |
( |
char * | sptr, |
|
|
const char * | chars ) |
Deletes any characters matching chars from sptr string
- Parameters
-
| sptr | string to be modified in-place |
| chars | a string containing characters (e.g. " \n" would delete whitespace and line feeds) |
◆ strcmp_array()
| int strcmp_array |
( |
const char ** | a, |
|
|
const char ** | b ) |
Compare an array of strings
const char *a[] = {
"I",
"like",
"computers."
};
const char *b[] = {
"I",
"like",
"cars."
};
printf("a and b are not equal\n");
} else {
printf("a and b are equal\n");
}
int strcmp_array(const char **a, const char **b)
Definition str.c:621
- Parameters
-
| a | pointer to array |
| b | poitner to array |
- Returns
- 0 on identical, non-zero for different
◆ strdeldup()
| char ** strdeldup |
( |
char ** | arr | ) |
|
Remove duplicate strings from an array of strings
- Parameters
-
- Returns
- success=array of unique strings, failure=NULL
◆ strdup_array()
| char ** strdup_array |
( |
char ** | array | ) |
|
Duplicate an array of strings
char **array_orig = calloc(10, sizeof(*orig));
orig[0] = strdup("one");
orig[1] = strdup("two");
orig[2] = strdup("three");
for (size_t i = 0; array_orig_copy[i] != NULL; i++) {
printf("array_orig[%zu] = '%s'\narray_orig_copy[%zu] = '%s'\n\n",
i, array_orig[i],
i, array_orig_copy[i]);
free(array_orig_copy[i]);
free(array_orig[i]);
}
free(array_orig_copy);
free(array_orig);
char ** strdup_array(char **array)
Definition str.c:592
- Parameters
-
- Returns
◆ strip()
| char * strip |
( |
char * | sptr | ) |
|
Strips trailing whitespace from a given string
char input[100];
strcpy(input, "I had trailing spaces ");
char * strip(char *sptr)
Definition str.c:451
- Parameters
-
- Returns
- truncated string
◆ strsort()
| void strsort |
( |
char ** | arr, |
|
|
unsigned int | sort_mode ) |
Sort an array of strings
- Parameters
-
| arr | a NULL terminated array of strings |
| sort_mode |
- STASIS_SORT_LEN_DESCENDING
- STASIS_SORT_LEN_ASCENDING
- STASIS_SORT_ALPHA
- STASIS_SORT_NUMERIC
|
◆ strstr_array()
| char * strstr_array |
( |
char ** | arr, |
|
|
const char * | str ) |
Search for string in an array of strings
- Parameters
-
| arr | array of strings |
| str | string to search for |
- Returns
- yes=
pointer to string, no=NULL, failure=NULL
◆ substring_between()
| char * substring_between |
( |
char * | sptr, |
|
|
const char * | delims ) |
Extract the string encapsulated by characters listed in delims
char *str = "this is [some data] in a string";
char * substring_between(char *sptr, const char *delims)
Definition str.c:243
- Parameters
-
| sptr | string to parse |
| delims | two characters surrounding a string |
- Returns
- success=text between delimiters, failure=NULL
◆ to_short_version()
| char * to_short_version |
( |
const char * | s | ) |
|
Reduce a version string to the major[minor] format used by Python
#include <stdio.h>
int main(int argc, char *argv[]) {
char python_version[] = "3.13.3"
if (!python_short_version) {
perror("unable to allocate memory for shortened python version");
return 1;
}
free(python_short_version);
return 0;
}
char * to_short_version(const char *s)
Definition str.c:672
- Parameters
-
- Returns
- the shortened version string
-
NULL on error
◆ tolower_s()
| char * tolower_s |
( |
char * | s | ) |
|
Convert input string to lowercase
char *str = strdup("HELLO WORLD!");
char * tolower_s(char *s)
Definition str.c:665
- Parameters
-
- Returns
- pointer to input string