mirror of
https://github.com/NixOS/nix
synced 2025-07-06 05:01:48 +02:00
C API: reformat according to proposed clang-format file
This commit is contained in:
parent
91e53de7d3
commit
e1bb799da9
13 changed files with 1115 additions and 1078 deletions
|
@ -7,139 +7,145 @@
|
|||
#include <cxxabi.h>
|
||||
#include <typeinfo>
|
||||
|
||||
nix_c_context *nix_c_context_create() { return new nix_c_context(); }
|
||||
nix_c_context * nix_c_context_create()
|
||||
{
|
||||
return new nix_c_context();
|
||||
}
|
||||
|
||||
void nix_c_context_free(nix_c_context *context) { delete context; }
|
||||
void nix_c_context_free(nix_c_context * context)
|
||||
{
|
||||
delete context;
|
||||
}
|
||||
|
||||
nix_err nix_context_error(nix_c_context *context) {
|
||||
if (context == nullptr) {
|
||||
throw;
|
||||
}
|
||||
try {
|
||||
throw;
|
||||
} catch (nix::Error &e) {
|
||||
/* Storing this exception is annoying, take what we need here */
|
||||
context->last_err = e.what();
|
||||
context->info = e.info();
|
||||
int status;
|
||||
const char *demangled =
|
||||
abi::__cxa_demangle(typeid(e).name(), 0, 0, &status);
|
||||
if (demangled) {
|
||||
context->name = demangled;
|
||||
// todo: free(demangled);
|
||||
} else {
|
||||
context->name = typeid(e).name();
|
||||
nix_err nix_context_error(nix_c_context * context)
|
||||
{
|
||||
if (context == nullptr) {
|
||||
throw;
|
||||
}
|
||||
context->last_err_code = NIX_ERR_NIX_ERROR;
|
||||
return context->last_err_code;
|
||||
} catch (const std::exception &e) {
|
||||
context->last_err = e.what();
|
||||
context->last_err_code = NIX_ERR_UNKNOWN;
|
||||
return context->last_err_code;
|
||||
}
|
||||
// unreachable
|
||||
try {
|
||||
throw;
|
||||
} catch (nix::Error & e) {
|
||||
/* Storing this exception is annoying, take what we need here */
|
||||
context->last_err = e.what();
|
||||
context->info = e.info();
|
||||
int status;
|
||||
const char * demangled = abi::__cxa_demangle(typeid(e).name(), 0, 0, &status);
|
||||
if (demangled) {
|
||||
context->name = demangled;
|
||||
// todo: free(demangled);
|
||||
} else {
|
||||
context->name = typeid(e).name();
|
||||
}
|
||||
context->last_err_code = NIX_ERR_NIX_ERROR;
|
||||
return context->last_err_code;
|
||||
} catch (const std::exception & e) {
|
||||
context->last_err = e.what();
|
||||
context->last_err_code = NIX_ERR_UNKNOWN;
|
||||
return context->last_err_code;
|
||||
}
|
||||
// unreachable
|
||||
}
|
||||
|
||||
nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg) {
|
||||
if (context == nullptr) {
|
||||
// todo last_err_code
|
||||
throw nix::Error("Nix C api error: %s", msg);
|
||||
}
|
||||
context->last_err_code = err;
|
||||
context->last_err = msg;
|
||||
return err;
|
||||
nix_err nix_set_err_msg(nix_c_context * context, nix_err err, const char * msg)
|
||||
{
|
||||
if (context == nullptr) {
|
||||
// todo last_err_code
|
||||
throw nix::Error("Nix C api error: %s", msg);
|
||||
}
|
||||
context->last_err_code = err;
|
||||
context->last_err = msg;
|
||||
return err;
|
||||
}
|
||||
|
||||
const char *nix_version_get() { return PACKAGE_VERSION; }
|
||||
const char * nix_version_get()
|
||||
{
|
||||
return PACKAGE_VERSION;
|
||||
}
|
||||
|
||||
// Implementations
|
||||
nix_err nix_setting_get(nix_c_context *context, const char *key, char *value,
|
||||
int n) {
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
std::map<std::string, nix::AbstractConfig::SettingInfo> settings;
|
||||
nix::globalConfig.getSettings(settings);
|
||||
if (settings.contains(key))
|
||||
return nix_export_std_string(context, settings[key].value, value, n);
|
||||
else {
|
||||
return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found");
|
||||
nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
std::map<std::string, nix::AbstractConfig::SettingInfo> settings;
|
||||
nix::globalConfig.getSettings(settings);
|
||||
if (settings.contains(key))
|
||||
return nix_export_std_string(context, settings[key].value, value, n);
|
||||
else {
|
||||
return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
nix_err nix_setting_set(nix_c_context *context, const char *key,
|
||||
const char *value) {
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
if (nix::globalConfig.set(key, value))
|
||||
return NIX_OK;
|
||||
else {
|
||||
return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found");
|
||||
}
|
||||
nix_err nix_setting_set(nix_c_context * context, const char * key, const char * value)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
if (nix::globalConfig.set(key, value))
|
||||
return NIX_OK;
|
||||
else {
|
||||
return nix_set_err_msg(context, NIX_ERR_KEY, "Setting not found");
|
||||
}
|
||||
}
|
||||
|
||||
nix_err nix_libutil_init(nix_c_context *context) {
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
nix::initLibUtil();
|
||||
return NIX_OK;
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
nix_err nix_libutil_init(nix_c_context * context)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
nix::initLibUtil();
|
||||
return NIX_OK;
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
const char *nix_err_msg(nix_c_context *context,
|
||||
const nix_c_context *read_context, unsigned int *n) {
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
if (read_context->last_err) {
|
||||
if (n)
|
||||
*n = read_context->last_err->size();
|
||||
return read_context->last_err->c_str();
|
||||
}
|
||||
nix_set_err_msg(context, NIX_ERR_UNKNOWN, "No error message");
|
||||
return nullptr;
|
||||
const char * nix_err_msg(nix_c_context * context, const nix_c_context * read_context, unsigned int * n)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
if (read_context->last_err) {
|
||||
if (n)
|
||||
*n = read_context->last_err->size();
|
||||
return read_context->last_err->c_str();
|
||||
}
|
||||
nix_set_err_msg(context, NIX_ERR_UNKNOWN, "No error message");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context,
|
||||
char *value, int n) {
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
if (read_context->last_err_code != NIX_ERR_NIX_ERROR) {
|
||||
return nix_set_err_msg(context, NIX_ERR_UNKNOWN,
|
||||
"Last error was not a nix error");
|
||||
}
|
||||
return nix_export_std_string(context, read_context->name, value, n);
|
||||
nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
if (read_context->last_err_code != NIX_ERR_NIX_ERROR) {
|
||||
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error");
|
||||
}
|
||||
return nix_export_std_string(context, read_context->name, value, n);
|
||||
}
|
||||
|
||||
nix_err nix_err_info_msg(nix_c_context *context,
|
||||
const nix_c_context *read_context, char *value,
|
||||
int n) {
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
if (read_context->last_err_code != NIX_ERR_NIX_ERROR) {
|
||||
return nix_set_err_msg(context, NIX_ERR_UNKNOWN,
|
||||
"Last error was not a nix error");
|
||||
}
|
||||
return nix_export_std_string(context, read_context->info->msg.str(), value,
|
||||
n);
|
||||
nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
if (read_context->last_err_code != NIX_ERR_NIX_ERROR) {
|
||||
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error");
|
||||
}
|
||||
return nix_export_std_string(context, read_context->info->msg.str(), value, n);
|
||||
}
|
||||
|
||||
nix_err nix_err_code(const nix_c_context *read_context) {
|
||||
return read_context->last_err_code;
|
||||
nix_err nix_err_code(const nix_c_context * read_context)
|
||||
{
|
||||
return read_context->last_err_code;
|
||||
}
|
||||
|
||||
// internal
|
||||
nix_err nix_export_std_string(nix_c_context *context,
|
||||
const std::string_view str, char *dest,
|
||||
unsigned int n) {
|
||||
size_t i = str.copy(dest, n - 1);
|
||||
dest[i] = 0;
|
||||
if (i == n - 1) {
|
||||
return nix_set_err_msg(context, NIX_ERR_OVERFLOW,
|
||||
"Provided buffer too short");
|
||||
} else
|
||||
return NIX_OK;
|
||||
nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n)
|
||||
{
|
||||
size_t i = str.copy(dest, n - 1);
|
||||
dest[i] = 0;
|
||||
if (i == n - 1) {
|
||||
return nix_set_err_msg(context, NIX_ERR_OVERFLOW, "Provided buffer too short");
|
||||
} else
|
||||
return NIX_OK;
|
||||
}
|
||||
|
|
|
@ -127,12 +127,12 @@ typedef struct nix_c_context nix_c_context;
|
|||
* @return allocated nix_c_context, owned by the caller. Free using
|
||||
* `nix_c_context_free`.
|
||||
*/
|
||||
nix_c_context *nix_c_context_create();
|
||||
nix_c_context * nix_c_context_create();
|
||||
/**
|
||||
* @brief Free a nix_c_context. Does not fail.
|
||||
* @param[out] context The context to free, mandatory.
|
||||
*/
|
||||
void nix_c_context_free(nix_c_context *context);
|
||||
void nix_c_context_free(nix_c_context * context);
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
@ -147,7 +147,7 @@ void nix_c_context_free(nix_c_context *context);
|
|||
* @return NIX_OK if the initialization is successful, or an error code
|
||||
* otherwise.
|
||||
*/
|
||||
nix_err nix_libutil_init(nix_c_context *context);
|
||||
nix_err nix_libutil_init(nix_c_context * context);
|
||||
|
||||
/** @defgroup settings
|
||||
* @{
|
||||
|
@ -167,8 +167,7 @@ nix_err nix_libutil_init(nix_c_context *context);
|
|||
* provided buffer is too short, or NIX_OK if the setting was retrieved
|
||||
* successfully.
|
||||
*/
|
||||
nix_err nix_setting_get(nix_c_context *context, const char *key, char *value,
|
||||
int n);
|
||||
nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n);
|
||||
|
||||
/**
|
||||
* @brief Sets a setting in the nix global configuration.
|
||||
|
@ -184,8 +183,7 @@ nix_err nix_setting_get(nix_c_context *context, const char *key, char *value,
|
|||
* @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was
|
||||
* set successfully.
|
||||
*/
|
||||
nix_err nix_setting_set(nix_c_context *context, const char *key,
|
||||
const char *value);
|
||||
nix_err nix_setting_set(nix_c_context * context, const char * key, const char * value);
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
@ -198,7 +196,7 @@ nix_err nix_setting_set(nix_c_context *context, const char *key,
|
|||
* Does not fail.
|
||||
* @return A static string representing the version of the nix library.
|
||||
*/
|
||||
const char *nix_version_get();
|
||||
const char * nix_version_get();
|
||||
|
||||
/** @addtogroup errors
|
||||
* @{
|
||||
|
@ -217,8 +215,7 @@ const char *nix_version_get();
|
|||
* @return nullptr if no error message was ever set,
|
||||
* a borrowed pointer to the error message otherwise.
|
||||
*/
|
||||
const char *nix_err_msg(nix_c_context *context, const nix_c_context *ctx,
|
||||
unsigned int *n);
|
||||
const char * nix_err_msg(nix_c_context * context, const nix_c_context * ctx, unsigned int * n);
|
||||
|
||||
/**
|
||||
* @brief Retrieves the error message from errorInfo in a context.
|
||||
|
@ -235,8 +232,7 @@ const char *nix_err_msg(nix_c_context *context, const nix_c_context *ctx,
|
|||
* @param[in] n Maximum size of the returned string.
|
||||
* @return NIX_OK if there were no errors, an error code otherwise.
|
||||
*/
|
||||
nix_err nix_err_info_msg(nix_c_context *context,
|
||||
const nix_c_context *read_context, char *value, int n);
|
||||
nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n);
|
||||
|
||||
/**
|
||||
* @brief Retrieves the error name from a context.
|
||||
|
@ -253,8 +249,7 @@ nix_err nix_err_info_msg(nix_c_context *context,
|
|||
* @param[in] n Maximum size of the returned string.
|
||||
* @return NIX_OK if there were no errors, an error code otherwise.
|
||||
*/
|
||||
nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context,
|
||||
char *value, int n);
|
||||
nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n);
|
||||
|
||||
/**
|
||||
* @brief Retrieves the most recent error code from a nix_c_context
|
||||
|
@ -266,7 +261,7 @@ nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context,
|
|||
* @param[in] read_context the context to retrieve the error message from
|
||||
* @return most recent error code stored in the context.
|
||||
*/
|
||||
nix_err nix_err_code(const nix_c_context *read_context);
|
||||
nix_err nix_err_code(const nix_c_context * read_context);
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
|
|
@ -7,14 +7,15 @@
|
|||
#include "error.hh"
|
||||
#include "nix_api_util.h"
|
||||
|
||||
struct nix_c_context {
|
||||
nix_err last_err_code = NIX_OK;
|
||||
std::optional<std::string> last_err = {};
|
||||
std::optional<nix::ErrorInfo> info = {};
|
||||
std::string name = "";
|
||||
struct nix_c_context
|
||||
{
|
||||
nix_err last_err_code = NIX_OK;
|
||||
std::optional<std::string> last_err = {};
|
||||
std::optional<nix::ErrorInfo> info = {};
|
||||
std::string name = "";
|
||||
};
|
||||
|
||||
nix_err nix_context_error(nix_c_context *context);
|
||||
nix_err nix_context_error(nix_c_context * context);
|
||||
|
||||
/**
|
||||
* Internal use only.
|
||||
|
@ -26,7 +27,7 @@ nix_err nix_context_error(nix_c_context *context);
|
|||
* @param msg The error message to set.
|
||||
* @returns the error code set
|
||||
*/
|
||||
nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg);
|
||||
nix_err nix_set_err_msg(nix_c_context * context, nix_err err, const char * msg);
|
||||
|
||||
/**
|
||||
* Internal use only.
|
||||
|
@ -40,21 +41,21 @@ nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg);
|
|||
* @return NIX_OK if there were no errors, NIX_ERR_OVERFLOW if the string length
|
||||
* exceeds `n`.
|
||||
*/
|
||||
nix_err nix_export_std_string(nix_c_context *context,
|
||||
const std::string_view str, char *dest,
|
||||
unsigned int n);
|
||||
nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n);
|
||||
|
||||
#define NIXC_CATCH_ERRS \
|
||||
catch (...) { \
|
||||
return nix_context_error(context); \
|
||||
} \
|
||||
return NIX_OK;
|
||||
#define NIXC_CATCH_ERRS \
|
||||
catch (...) \
|
||||
{ \
|
||||
return nix_context_error(context); \
|
||||
} \
|
||||
return NIX_OK;
|
||||
|
||||
#define NIXC_CATCH_ERRS_RES(def) \
|
||||
catch (...) { \
|
||||
nix_context_error(context); \
|
||||
return def; \
|
||||
}
|
||||
#define NIXC_CATCH_ERRS_RES(def) \
|
||||
catch (...) \
|
||||
{ \
|
||||
nix_context_error(context); \
|
||||
return def; \
|
||||
}
|
||||
#define NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_RES(nullptr)
|
||||
|
||||
#endif // NIX_API_UTIL_INTERNAL_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue