htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
dotenv.c File Reference
#include <ctype.h>
#include <dirent.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>
#include <htils/string.h>

Functions

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 a dotenv file, or a path with a .env file within it.
 

Function Documentation

◆ find_first_env_file()

static 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 return 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 a dotenv file, or a path with a .env file within it.

Parameters
pathThe path where a .env file is located or the explicit relative or complete path to the .env file
arenaThe arena to allocate from.
Precondition
  • path, and arena must be valid, and not null.
  • path must be a valid path.
Returns
amount of variables loaded, -1 on failure.

◆ is_env_file()

static b32 is_env_file ( const cstr path_name)
static

Check if a path is a .env file.

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

Parameters
arenaThe arena to allocate from.
pathThe path to check.
Precondition
arena and path must be valid and cannot be null.
Returns
True if the path is a .env file, false if it isn't.

◆ parse_line()

static 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()

static void to_upper ( string_slice  str_slice)
static

Convert a string to uppercase.

By iterating through the string, and converting each character to it's uppercase counterpart.

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

◆ trim_quotes()

static 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.