-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
Pablo RM edited this page Sep 22, 2025
·
3 revisions
Reads a complete line from the file stream, automatically handling memory allocation.
Parameters:
-
f- File stream to read from
Returns:
- Malloc-allocated buffer containing the line (without newline)
-
NULLon error/EOF
Notes:
- Caller is responsible for freeing the returned buffer
- Cross-platform compatible (uses
fgetsinternally)
Example:
FILE *fp = fopen("example.txt", "r");
char *line = afgets(fp);
if (line) {
printf("Line: %s\n", line);
free(line);
}
fclose(fp);Reads the entire contents of a file into memory.
Parameters:
-
f- File stream to read from -
out_len- Pointer to store the length of the read data (can be NULL)
Returns:
- Malloc-allocated buffer containing file contents
-
NULLon error
Notes:
- Caller is responsible for freeing the returned buffer
- File position is preserved after operation
Example:
FILE *fp = fopen("data.txt", "r");
size_t len;
char *content = afread(fp, &len);
if (content) {
printf("File size: %zu bytes\n", len);
printf("Content: %s\n", content);
free(content);
}
fclose(fp);Joins multiple strings with a separator.
Parameters:
-
parts- Array of strings to join -
nparts- Number of strings in the array -
sep- Separator string (can be NULL for no separator)
Returns:
- Malloc-allocated buffer containing the joined string
-
NULLon memory allocation failure
Example:
const char *words[] = {"Hello", "beautiful", "world"};
char *sentence = strjoin(words, 3, " ");
printf("%s\n", sentence); // "Hello beautiful world"
free(sentence);Variable argument version of strjoin.
Parameters:
-
sep- Separator string -
...- Variable number of string arguments, terminated by NULL
Returns:
- Malloc-allocated buffer containing the joined string
-
NULLon memory allocation failure
Example:
char *path = strjoin_va("/", "usr", "local", "bin", NULL);
printf("%s\n", path); // "/usr/local/bin"
free(path);Trims whitespace from both ends of a string.
Parameters:
-
s- Input string
Returns:
- Malloc-allocated buffer containing the trimmed string
-
NULLif input is NULL or memory allocation fails
Notes:
- Trims spaces, tabs, newlines, and carriage returns
- Original string is not modified
Example:
char *trimmed = strtrim(" Hello World \n");
printf("'%s'\n", trimmed); // "Hello World"
free(trimmed);Trims whitespace from both ends of a string in-place.
Parameters:
-
s- String to modify (must be mutable)
Notes:
- Modifies the input string directly
- No memory allocation/deallocation
- Safe to call with NULL (no-op)
Example:
char buffer[] = " Hello World \n";
strtrim_inplace(buffer);
printf("'%s'\n", buffer); // "Hello World"Adds two struct timeval values.
Parameters:
-
a,b- Time values to add
Returns:
- Result of addition with proper carry handling
Example:
struct timeval t1 = {1, 500000}; // 1.5 seconds
struct timeval t2 = {2, 600000}; // 2.6 seconds
struct timeval sum = timeval_add(t1, t2); // 4.1 secondsSubtracts two struct timeval values.
Parameters:
-
a,b- Time values (a - b)
Returns:
- Result of subtraction with proper borrow handling
Example:
struct timeval t1 = {5, 200000}; // 5.2 seconds
struct timeval t2 = {2, 100000}; // 2.1 seconds
struct timeval diff = timeval_sub(t1, t2); // 3.1 secondsConverts a struct timeval to seconds as a double.
Parameters:
-
t- Time value to convert
Returns:
- Time in seconds with microsecond precision
Example:
struct timeval t = {3, 500000}; // 3.5 seconds
double seconds = timeval_to_seconds(t);
printf("%.6f seconds\n", seconds); // 3.500000 seconds