mirror of
https://github.com/NixOS/nix
synced 2025-06-28 09:31:16 +02:00
C API: nix_get_string now accepts a callback to return the value
This commit is contained in:
parent
74e4bc9b1d
commit
c75b143b6c
6 changed files with 65 additions and 35 deletions
|
@ -40,24 +40,37 @@ Nix expression `builtins.nixVersion`.
|
|||
#include <nix_api_expr.h>
|
||||
#include <nix_api_value.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// NOTE: This example lacks all error handling. Production code must check for
|
||||
// errors, as some return values will be undefined.
|
||||
int main() {
|
||||
nix_libexpr_init(NULL);
|
||||
|
||||
Store* store = nix_store_open(NULL, "dummy://", NULL);
|
||||
EvalState* state = nix_state_create(NULL, NULL, store); // empty search path (NIX_PATH)
|
||||
Value *value = nix_alloc_value(NULL, state);
|
||||
void my_get_string_cb(const char * start, unsigned int n, char ** user_data)
|
||||
{
|
||||
*user_data = strdup(start);
|
||||
}
|
||||
|
||||
nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value);
|
||||
nix_value_force(NULL, state, value);
|
||||
printf("Nix version: %s\n", nix_get_string(NULL, value));
|
||||
int main()
|
||||
{
|
||||
nix_libexpr_init(NULL);
|
||||
|
||||
nix_gc_decref(NULL, value);
|
||||
nix_state_free(state);
|
||||
nix_store_free(store);
|
||||
return 0;
|
||||
Store * store = nix_store_open(NULL, "dummy://", NULL);
|
||||
EvalState * state = nix_state_create(NULL, NULL, store); // empty search path (NIX_PATH)
|
||||
Value * value = nix_alloc_value(NULL, state);
|
||||
|
||||
nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value);
|
||||
nix_value_force(NULL, state, value);
|
||||
|
||||
char * version;
|
||||
nix_get_string(NULL, value, my_get_string_cb, version);
|
||||
printf("Nix version: %s\n", version);
|
||||
|
||||
free(version);
|
||||
nix_gc_decref(NULL, value);
|
||||
nix_state_free(state);
|
||||
nix_store_free(store);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue