libamxc  1.10.3
C Generic Data Containers
amxc_utils.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <amxc/amxc_variant.h>
#include <amxc/amxc_string.h>
#include <amxc/amxc_utils.h>
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Typedefs

typedef void(* amxc_string_replace_cb_t) (amxc_string_t *const string, size_t pos, size_t length, const char *txt, const void *priv)
 

Functions

static void amxc_string_resolve_replace_env (amxc_string_t *const string, size_t pos, size_t length, const char *txt, UNUSED const void *priv)
 
static void amxc_string_resolve_replace_var (amxc_string_t *const string, size_t pos, size_t length, const char *txt, const void *priv)
 
static int amxc_string_generic_resolve (amxc_string_t *const string, const char *start_string, const char *end_string, amxc_string_replace_cb_t fn, const void *priv)
 
int amxc_string_resolve_env (amxc_string_t *const string)
 Resolves environment variables. More...
 
int amxc_string_resolve_var (amxc_string_t *const string, const amxc_var_t *const data)
 Resolves variant path variables. More...
 
int amxc_string_resolve_esc (amxc_string_t *const string)
 Resolves escaped characters in a string. More...
 
int amxc_string_esc (amxc_string_t *const string)
 Add escape characters to a string. More...
 
int amxc_string_resolve (amxc_string_t *const string, const amxc_var_t *const data)
 Resolves variant paths and environment variables. More...
 
int amxc_string_set_resolved (amxc_string_t *string, const char *text, const amxc_var_t *const data)
 Sets the resolved string. More...
 
int amxc_string_new_resolved (amxc_string_t **string, const char *text, const amxc_var_t *const data)
 Sets the resolved string. More...
 
amxc_llist_it_tamxc_llist_add_string (amxc_llist_t *const llist, const char *text)
 Adds a string (char*) to a linked list of amxc_string_t structures. More...
 
void amxc_string_list_it_free (amxc_llist_it_t *it)
 Helper function to delete an item in a linked list. More...
 

Typedef Documentation

◆ amxc_string_replace_cb_t

typedef void(* amxc_string_replace_cb_t) (amxc_string_t *const string, size_t pos, size_t length, const char *txt, const void *priv)

Definition at line 66 of file amxc_utils.c.

Function Documentation

◆ amxc_string_generic_resolve()

static int amxc_string_generic_resolve ( amxc_string_t *const  string,
const char *  start_string,
const char *  end_string,
amxc_string_replace_cb_t  fn,
const void *  priv 
)
static

Definition at line 102 of file amxc_utils.c.

106  {
107  int retval = 0;
108  size_t start_length = strlen(start_string);
109  size_t end_length = strlen(end_string);
110  size_t start = 0;
111  size_t end = 0;
112 
113  for(size_t i = 0; i < string->last_used;) {
114  if(strncmp(string->buffer + i, start_string, start_length) != 0) {
115  i++;
116  continue;
117  }
118  if((i > 0) && (string->buffer[i - 1] == '\\')) {
119  i++;
120  continue;
121  }
122  start = i;
123  end = 0;
124  i++;
125  while(i + end_length <= string->last_used) {
126  if(strncmp(string->buffer + i, end_string, end_length) == 0) {
127  end = i + end_length;
128  string->buffer[i] = 0;
129  break;
130  }
131  i++;
132  }
133  if(i + end_length > string->last_used) {
134  break;
135  }
136  fn(string,
137  start,
138  (end - start),
139  string->buffer + start + start_length,
140  priv);
141  retval++;
142  i = start;
143  }
144 
145  return retval;
146 }
char * buffer
Definition: amxc_string.h:104
size_t last_used
Definition: amxc_string.h:106

◆ amxc_string_resolve_replace_env()

static void amxc_string_resolve_replace_env ( amxc_string_t *const  string,
size_t  pos,
size_t  length,
const char *  txt,
UNUSED const void *  priv 
)
static

Definition at line 72 of file amxc_utils.c.

76  {
77  char* value = getenv(txt);
78  amxc_string_remove_at(string, pos, length);
79  if(value) {
80  amxc_string_insert_at(string, pos, value, strlen(value));
81  }
82 }
AMXC_INLINE int amxc_string_insert_at(amxc_string_t *const string, const size_t pos, const char *text, size_t length)
Inserts a string of the given length into a string at a certain position.
Definition: amxc_string.h:1035
int amxc_string_remove_at(amxc_string_t *const string, const size_t pos, size_t length)
Removes part of the text in the string buffer.
Definition: amxc_string.c:314

◆ amxc_string_resolve_replace_var()

static void amxc_string_resolve_replace_var ( amxc_string_t *const  string,
size_t  pos,
size_t  length,
const char *  txt,
const void *  priv 
)
static

Definition at line 84 of file amxc_utils.c.

88  {
89  amxc_var_t* data = (amxc_var_t*) priv;
90  char* value = amxc_var_dyncast(cstring_t,
92  txt,
94  amxc_string_remove_at(string, pos, length);
95  if(value) {
96  amxc_string_insert_at(string, pos, value, strlen(value));
97  }
98  free(value);
99 
100 }
#define cstring_t
Convenience macro.
Definition: amxc_variant.h:584
#define AMXC_VAR_FLAG_DEFAULT
The default flag, do not copy, use variant as is.
Definition: amxc_variant.h:392
amxc_var_t * amxc_var_get_path(const amxc_var_t *const var, const char *const path, const int flags)
Retrieves the variant at the given path of a composite variant.
Definition: amxc_variant.c:573
#define amxc_var_dyncast(type, var)
Dynamic cast a variant to a certain type.
Definition: amxc_variant.h:678
The variant struct definition.
Definition: amxc_variant.h:861
char data[]