|
htils 1
A small set of utilities for C programming.
|
#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <htils/assert.h>#include <htils/darray.h>#include <htils/dotenv.h>#include <htils/file.h>#include <htils/path.h>Data Structures | |
| struct | dotenv_dir_t |
| A minimal directory iterator over a single directory's entries. More... | |
Typedefs | |
| typedef struct dotenv_dir_t | dotenv_dir_t |
| A minimal directory iterator over a single directory's entries. | |
Functions | |
| static b32 | dotenv_dir_open (dotenv_dir_t *dir, arena_t *arena, const string *path) |
| Open a directory for iteration. | |
| static b32 | dotenv_dir_next (dotenv_dir_t *dir, const cstr **name, b32 *is_dir) |
| Advance the iterator to the next directory entry. | |
| static void | dotenv_dir_close (dotenv_dir_t *dir) |
| Close a directory iterator. | |
| static b32 | dotenv_set_env (const cstr *key, const cstr *value) |
| Set an environment variable, overwriting any existing value. | |
| static b32 | is_env_file (const cstr *path_name) |
| Check if a path is a .env file. | |
| static string * | find_first_env_file (arena_t *arena, const string *path) |
| Find the first .env file in a directory. | |
| static void | to_upper (string_slice str_slice) |
| Convert a string to uppercase. | |
| static void | trim_quotes (string_slice *str) |
| Trim quotes from a string. | |
| static b32 | parse_line (arena_t *arena, string *line, string **key, string **value) |
| Parse a line of a .env file. | |
| i32 | htils_dotenv_load (arena_t *arena, const string *path) |
| Load environment variables from a .env file. | |
| typedef struct dotenv_dir_t dotenv_dir_t |
A minimal directory iterator over a single directory's entries.
Wraps the platform directory-enumeration API so the dotenv crawler can iterate entries without platform-specific code at each call site.
|
static |
Close a directory iterator.
| dir | The iterator to close. |
dir must be valid and cannot be null.
|
static |
Advance the iterator to the next directory entry.
| dir | The iterator to advance. |
| name | Overwritten with the entry's name, valid until the next call. |
| is_dir | Overwritten with whether the entry is a directory. |
dir, name, and is_dir must be valid and cannot be null.
|
static |
Open a directory for iteration.
| dir | The iterator to initialize. |
| arena | The arena to allocate from. |
| path | The directory to open. |
dir and path must be valid and cannot be null.Set an environment variable, overwriting any existing value.
| key | The environment variable name. |
| value | The value to assign. |
key and value must be valid and cannot be null.Find the first .env file in a directory.
Crawling through a directory and returns the path of the first known .env file.
| arena | The arena to allocate from. |
| path | The path to find the .env file in. |
arena and path must be valid and cannot be null.Load environment variables from a .env file.
Parses KEY=VALUE lines and sets each as a process environment variable with setenv() (overwriting). path may be the .env file itself, or a directory containing one, in which case the first .env found is used.
| arena | The arena to allocate from. |
| path | The .env file, or a directory to search for one. |
path and arena must be valid and not null.path must exist.Check if a path is a .env file.
By first retrieving its extension, and then verifying that the extension is .env.
| path_name | The path name to check. |
Parse a line of a .env file.
By checking if the line is empty, a comment, or a new_line before trimming whitespace, making sure the key is upper case, and trimming quotes from the value, then setting the key and line to the parsed values.
| arena | The arena to allocate from. |
| line | The line to parse. |
| key | The key of the line. |
| value | The value of the line. |
arena, line, must be valid and cannot be null.key and value must be valid pointers that you can overwrite.
|
static |
Convert a string to uppercase.
By iterating through the string, and converting each character to its uppercase counterpart.
| str_slice | The string to convert. |
|
static |
Trim quotes from a string.
By checking if the first and last characters are quotes, and then trims and memmoves as needed.
| str | The string to trim. |
str must be valid and cannot be null.