From 66463236cc3b40649eecc87e1a51d4a225aa9061 Mon Sep 17 00:00:00 2001 From: josh Date: Mon, 12 Jan 2026 11:28:31 -0600 Subject: [PATCH] first commit --- .gitignore | 3 +++ Makefile | 31 +++++++++++++++++++++++++++++++ README.md | 35 +++++++++++++++++++++++++++++++++++ include/env.hpp | 15 +++++++++++++++ main.cpp | 24 ++++++++++++++++++++++++ src/env.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 include/env.hpp create mode 100644 main.cpp create mode 100644 src/env.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..355ad87 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +app +libenv.a +.env diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3073564 --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +TARGET = app +CXX = g++ -std=c++23 -ggdb -Wall -Wextra -pedantic -O0 +INC = -I./include +LIB_PATH = +LIBS = +HEADERS = include/env.hpp +SOURCES = src/env.cpp \ + main.cpp +STATIC = libenv.a + +all: $(TARGET) + +$(TARGET): $(HEADERS) $(SOURCES) + @$(CXX) $(INC) $(LIB_PATH) $(SOURCES) -o $(TARGET) $(LIBS) + +$(STATIC): include/env.hpp src/env.cpp + @g++ -std=c++23 -I./include -c src/env.cpp -o env.o + @ar rcs $(STATIC) env.o + +.PHONY: clean run + +clean: + @rm -rf $(TARGET) + @rm -rf $(STATIC) + +run: $(TARGET) + -@./$(TARGET) + @rm -rf $(TARGET) + +lib: $(STATIC) + @rm -rf env.o diff --git a/README.md b/README.md new file mode 100644 index 0000000..2294354 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# Env Reader + +This is a library to read .env files. + +## Usage +- `make lib` + +```cpp + +#include +#include +#include + +#include "env.hpp" + +using std::unexpected; +using std::expected; +using std::println; +using std::string; + +int main() { + Env env; + env.loadEnv(""); + expected result = env.get("FIRST"); + if (!result.has_value()) { + println("{}", result.error()); + return 1; + } + + println("{}", result.value()); + + return 0; +} + +``` diff --git a/include/env.hpp b/include/env.hpp new file mode 100644 index 0000000..d07d2e9 --- /dev/null +++ b/include/env.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include + +class Env { + public: + Env(); + void loadEnv(const std::string& cwd); + std::expected get(const std::string& key); + + private: + std::map map_; +}; diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..f9e9553 --- /dev/null +++ b/main.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +#include "env.hpp" + +using std::unexpected; +using std::expected; +using std::println; +using std::string; + +int main() { + Env env; + env.loadEnv(""); + expected result = env.get("FIRST"); + if (!result.has_value()) { + println("{}", result.error()); + return 1; + } + + println("{}", result.value()); + + return 0; +} diff --git a/src/env.cpp b/src/env.cpp new file mode 100644 index 0000000..c47ac9f --- /dev/null +++ b/src/env.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; + +using std::unexpected; +using std::expected; +using std::string; +using std::map; + +#include "env.hpp" + +Env::Env() { +} + +void Env::loadEnv(const string& cwd) { + string filename = ".env"; + if (!cwd.empty()) { + filename = (fs::path(cwd) / ".env").string(); + } + + std::ifstream file(filename); + string line; + string::size_type pos; + while (std::getline(file, line)) { + pos = line.find_first_of("="); + string key = line.substr(0, pos); + string value = line.substr(pos + 1); + if (value.starts_with("'")) { + value = value.substr(1); + } + if (value.ends_with("'")) { + value.pop_back(); + } + map_[key] = value; + } +} + +expected Env::get(const string& key) { + if (map_.contains(key)) { + return map_[key]; + } + + return unexpected("Key does not exist"); +}