Skip to content

Cross platform C header for obtaining paths to user configuration files and directories

License

Notifications You must be signed in to change notification settings

LonghronShen/cfgpath

 
 

Repository files navigation

cfgpath.h: Cross platform methods for obtaining paths to configuration files

Copyright (C) 2013 Adam Nielsen [email protected]

Copyright (C) 2019 LonghronShen [email protected]


This is a simple header file that provides a handful of functions to obtain paths to configuration files, regardless of the operating system the application is running under.

It requires no dependencies beyond each platform's standard API.

This code is placed in the public domain. You are free to use it for any purpose. If you add new platform support, please contribute a patch!

Basic use:

#include <cfgpath/cfgpath.h>

char cfgdir[MAX_PATH];
get_user_config_file(cfgdir, sizeof(cfgdir), "myapp");
if (cfgdir[0] == 0)
{
  printf("Unable to find home directory.\n");
  return 1;
}
printf("Saving configuration file to %s\n", cfgdir);

If you are using C++, you can also use it like this:

#include <exception>
#include <cfgpath/cfgpath.h>

auto config_folder_path = libcfgpath::cfgpath::get_folder_path(libcfgpath::known_spefial_folder::CONFIG, "myapp");

if (config_folder_path == "")
{
 throw std::exception("Unable to find config directory.");
}

To integrate it into your own project:

  • Just copy the include folder into your own project.

  • Or clone the project as a submodule of your project

    1. Add the include folder as an 'additional include directory' of your project;

    2. If you are using CMake, you can use add_subdirectory to add the cloned submodule folder as an externl dependency, and link your project with the cfgpath library. Of course, you can also choose to use the header only mode by setting the CFGPATH_HEADER_ONLY variable.

All the other files are for testing to make sure it works correctly, so you don't need them unless you intend to make changes and send me a patch.

Supported platforms are currently:

  • Linux
  • Mac OS X
  • Windows

Patches adding support for more platforms would be greatly appreciated.

About

Cross platform C header for obtaining paths to user configuration files and directories

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 81.1%
  • C++ 9.6%
  • CMake 8.2%
  • Shell 1.1%