htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
dotenv.c File Reference
#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 stringfind_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 Documentation

◆ dotenv_dir_t

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.

Function Documentation

◆ dotenv_dir_close()

void dotenv_dir_close ( dotenv_dir_t * dir)
static

Close a directory iterator.

Parameters
dirThe iterator to close.
Precondition
dir must be valid and cannot be null.

◆ dotenv_dir_next()

b32 dotenv_dir_next ( dotenv_dir_t * dir,
const cstr ** name,
b32 * is_dir )
static

Advance the iterator to the next directory entry.

Parameters
dirThe iterator to advance.
nameOverwritten with the entry's name, valid until the next call.
is_dirOverwritten with whether the entry is a directory.
Precondition
dir, name, and is_dir must be valid and cannot be null.
Returns
True if an entry was read, false once the directory is exhausted.

◆ dotenv_dir_open()

b32 dotenv_dir_open ( dotenv_dir_t * dir,
arena_t * arena,
const string * path )
static

Open a directory for iteration.

Parameters
dirThe iterator to initialize.
arenaThe arena to allocate from.
pathThe directory to open.
Precondition
dir and path must be valid and cannot be null.
Returns
True if the directory was opened, false if it wasn't.

◆ dotenv_set_env()

b32 dotenv_set_env ( const cstr * key,
const cstr * value )
static

Set an environment variable, overwriting any existing value.

Parameters
keyThe environment variable name.
valueThe value to assign.
Precondition
key and value must be valid and cannot be null.
Returns
True if the variable was set, false if it wasn't.

◆ find_first_env_file()

string * find_first_env_file ( arena_t * arena,
const string * path )
static

Find the first .env file in a directory.

Crawling through a directory and returns the path of the first known .env file.

Parameters
arenaThe arena to allocate from.
pathThe path to find the .env file in.
Precondition
arena and path must be valid and cannot be null.
Returns
The path to the .env file as string, or null if none was found.

◆ htils_dotenv_load()

i32 htils_dotenv_load ( arena_t * arena,
const string * path )

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.

Parameters
arenaThe arena to allocate from.
pathThe .env file, or a directory to search for one.
Precondition
  • path and arena must be valid and not null.
  • path must exist.
Returns
The number of variables loaded, or -1 on failure.

◆ is_env_file()

b32 is_env_file ( const cstr * path_name)
static

Check if a path is a .env file.

By first retrieving its extension, and then verifying that the extension is .env.

Parameters
path_nameThe path name to check.
Returns
True if the path is a .env file, false if it isn't.

◆ parse_line()

b32 parse_line ( arena_t * arena,
string * line,
string ** key,
string ** value )
static

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.

Parameters
arenaThe arena to allocate from.
lineThe line to parse.
keyThe key of the line.
valueThe value of the line.
Precondition
  • arena, line, must be valid and cannot be null.
  • key and value must be valid pointers that you can overwrite.
Returns
True if the line was parsed, false if it wasn't.

◆ to_upper()

void to_upper ( string_slice str_slice)
static

Convert a string to uppercase.

By iterating through the string, and converting each character to its uppercase counterpart.

Parameters
str_sliceThe string to convert.

◆ trim_quotes()

void trim_quotes ( string_slice * str)
static

Trim quotes from a string.

By checking if the first and last characters are quotes, and then trims and memmoves as needed.

Parameters
strThe string to trim.
Precondition
str must be valid and cannot be null.