Skip to content
/ dotenv Public
forked from harshfeudal/dotenv

A simple C++ dotenv file reader for reading .env file in simplest way

License

Notifications You must be signed in to change notification settings

dhtson/dotenv

 
 

Repository files navigation

Dotenv for C++

dotenv

This library support to load environment variables from a .env file and read them like what .json does but more simple!

Build status Release GitHub issues GitHub license
Discord GitHub stars GitHub forks

📝 About this repository

  • This project is owned by @harshfeudal. You can join my Discord server here!
  • This library can be used for many different projects, currently, this library is enough to make a C++ Discord bot using D++ for safety token reader (you can use .gitignore to ignore .env file).

🚨 Usage

  1. Download ONLY dotenv folder (make sure that it contains dotenv.h inside). Simple right?
  2. Include it in your project like this:

main.cpp

#include <dotenv/dotenv.h>
#include <iostream>

int main() {
    // Load your .env file
	dotenv::load(".env");

    // Read your .env variable
    char* my_var;
    size_t envValueSize;
    _dupenv_s(&my_var, &envValueSize, "MY_VAR");

    // Read it
    std::cout << "The variable is: " << my_var << std::endl;

    // Remember to free it
    free(my_var);
}
  • Some notice update: You can also use std::getenv("YOUR_ENV_VAR"). However, some compiler like MSVC doesn't allow you to use that, and they recommend you to replace as _dupenv_s. You can also use other methods if you find a new one! If you are not using MSVC, you can do like below (recommend):
#include <dotenv/dotenv.h>
#include <iostream>
int main() {
    // Load your .env file
	dotenv::load(".env");

    // Read your .env variable
    const char* my_var = std::getenv("MY_VAR");

    // Read it
    std::cout << "The variable is: " << my_var << std::endl;
}

... and compare it to your .env file:

.env

MY_VAR="Hello World!"

Done! Simple right (I think)?

✨ Library features

  1. Somehow you really want to skip line (or do whatever you want) like this:
The sentence is:
"Hello World"
    - Hello 2023 -

... so, with this library feature, to do this, you can just do like this in your .env file:

SENTENCE="\n\"Hello World\"\n\t- Hello 2023 -"

and it will work as expected!

  1. Or, you really want to make a comment like this:
TOKEN="WW91ckRpc2NvcmRCb3RUb2tlbg=="    # Your Discord bot token

With this library features, your comment will be ignored after the # mark! Comments begin where a # exists, so if your value contains a # please wrap it in quotes.

  1. This library just include only a header file (dotenv.h)!

  2. Very similar with what Javascript does; The expansion engine roughly has the following rules:

    • $KEY will expand any env with the name KEY
    • ${KEY} will expand any env with the name KEY
    • \$KEY will escape the $KEY rather than expand
    • ${KEY:-default} will first attempt to expand any env with the name KEY. If not one, then it will return default
  3. If you decide to make an empty variable like this:

EMPTY=

... so remember to have a space like this:

EMPTY= # A space after the '=' symbol

👷 Support platform

  • Microsoft Windows 10 x64/x86
  • Visual Studio platform x64/x86
  • C++ 17 ISO/IEC 14882

🤝 Support me

  • Please leave me a star if you like it. Thank you very much!
  • You can probably help me develop this project too by DMing me on my Discord profile.

💎 Contribution and Pull Request

I'd probably glad to see if you have anything new to help and support me. To do that, please make one and before you do that, just make sure you've tested the code. I won't bite you if you do it wrong, but just make sure that you have to test it clearly before I merge it.

📚 Download

You can download the latest version here.

- © 2023 Harshfeudal. All rights reserved. -

About

A simple C++ dotenv file reader for reading .env file in simplest way

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 68.4%
  • Shell 21.1%
  • CMake 7.0%
  • Lua 3.5%