icedb  version 0.5.1
Snow particle scattering database API
Functions | Variables
error_context.cpp File Reference
#include "../icedb/defs.h"
#include "../icedb/util.h"
#include <string.h>
#include <stdlib.h>
#include "../icedb/error_context.h"
Include dependency graph for error_context.cpp:

Go to the source code of this file.

Functions

struct ICEDB_error_contexterror_context_create_impl (int code, const char *file, int line, const char *fsig)
 
struct ICEDB_error_contexterror_context_copy (const struct ICEDB_error_context *c)
 
void error_context_append (struct ICEDB_error_context *c, size_t sz, const char *data)
 
void error_context_append_str (struct ICEDB_error_context *c, const char *data)
 
void error_context_add_string (struct ICEDB_error_context *c, size_t var_sz, const char *var_name, size_t val_sz, const char *var_val)
 
void error_context_add_string2 (struct ICEDB_error_context *c, const char *var_name, const char *var_val)
 
void error_context_widen (struct ICEDB_error_context *c, size_t numNewSpaces)
 

Variables

ICEDB_SYMBOL_PRIVATE ICEDB_THREAD_LOCAL ICEDB_error_context__ICEDB_LOCAL_THREAD_error_context = NULL
 
DL_ICEDB ICEDB_error_context_create_impl_f ICEDB_error_context_create_impl = error_context_create_impl
 
DL_ICEDB ICEDB_error_context_copy_f ICEDB_error_context_copy = error_context_copy
 
DL_ICEDB ICEDB_error_context_appendA_f ICEDB_error_context_appendA = error_context_append
 
DL_ICEDB ICEDB_error_context_append_strA_f ICEDB_error_context_append_strA = error_context_append_str
 
DL_ICEDB ICEDB_error_context_add_stringA_f ICEDB_error_context_add_stringA = error_context_add_string
 
DL_ICEDB ICEDB_error_context_add_string2A_f ICEDB_error_context_add_string2A = error_context_add_string2
 
DL_ICEDB ICEDB_error_context_widen_f ICEDB_error_context_widen = error_context_widen
 
DL_ICEDB const struct ICEDB_error_context_container_ftable ICEDB_ct_error_context
 

Function Documentation

◆ error_context_add_string()

void error_context_add_string ( struct ICEDB_error_context c,
size_t  var_sz,
const char *  var_name,
size_t  val_sz,
const char *  var_val 
)

Definition at line 87 of file error_context.cpp.

References ICEDB_COMPAT_strdup_s(), ICEDB_error_context_widen, ICEDB_error_context::max_num_var_fields, ICEDB_error_context::num_var_fields, ICEDB_error_context_var_val::val, ICEDB_error_context::var_vals, and ICEDB_error_context_var_val::varname.

88 {
91  c->var_vals[c->num_var_fields].varname = ICEDB_COMPAT_strdup_s(var_name, var_sz);
92  c->var_vals[c->num_var_fields].val = ICEDB_COMPAT_strdup_s(var_val, val_sz);
93  c->num_var_fields++;
94 }
DL_ICEDB ICEDB_error_context_widen_f ICEDB_error_context_widen
ICEDB_error_context_var_val * var_vals
Definition: error_context.h:26
ICEDB_SYMBOL_SHARED char * ICEDB_COMPAT_strdup_s(const char *src, size_t srcSz)
Definition: util.cpp:42
Here is the call graph for this function:

◆ error_context_add_string2()

void error_context_add_string2 ( struct ICEDB_error_context c,
const char *  var_name,
const char *  var_val 
)

Definition at line 97 of file error_context.cpp.

References ICEDB_error_context_add_string.

98 {
99 #if defined(__STDC_LIB_EXT1__) || defined(__STDC_SECURE_LIB__)
100  ICEDB_error_context_add_string(c, (size_t)strnlen_s(var_name, UINT16_MAX), var_name, (size_t)strnlen_s(var_val, UINT16_MAX), var_val);
101 #else
102  ICEDB_error_context_add_string(c, strnlen(var_name, UINT16_MAX), var_name, strnlen(var_val, UINT16_MAX), var_val);
103 #endif
104 }
#define ICEDB_error_context_add_string
Definition: error_context.h:62

◆ error_context_append()

void error_context_append ( struct ICEDB_error_context c,
size_t  sz,
const char *  data 
)

Definition at line 57 of file error_context.cpp.

References ICEDB_COMPAT_strncpy_s(), ICEDB_COMPAT_strnlen_s, ICEDB_free(), ICEDB_malloc(), ICEDB_error_context::message_size, ICEDB_error_context::message_size_alloced, and ICEDB_error_context::message_text.

58 {
59  if (data[sz] != '\0' && data[sz + 1] == '\0') sz++; // Null character check
60  const size_t min_alloc_size_inc = 256;
61  if (c->message_size + sz >= c->message_size_alloced) {
62  size_t newszinc = (sz> min_alloc_size_inc) ? sz : min_alloc_size_inc;
63  size_t newsz = c->message_size_alloced + newszinc;
64  char* newtext = (char*)ICEDB_malloc(newsz);
65  if (c->message_text) {
68  }
69  c->message_text = newtext;
70  c->message_size_alloced = newsz;
71  }
72  ICEDB_COMPAT_strncpy_s(c->message_text+ c->message_size-1, //c->message_size always includes the null character
74  data, sz);
76  (c->message_size_alloced < UINT16_MAX-1) ? c->message_size_alloced : UINT16_MAX-1)+1; // -1,+1 because message_size includes the null character.
77 }
ICEDB_SYMBOL_SHARED void ICEDB_free(void *obj)
Free memory region. Should not be double-freed.
Definition: util.cpp:97
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
ICEDB_SYMBOL_SHARED void * ICEDB_malloc(size_t numBytes)
Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted...
Definition: util.cpp:90
#define ICEDB_COMPAT_strnlen_s
Definition: util.h:44
Here is the call graph for this function:

◆ error_context_append_str()

void error_context_append_str ( struct ICEDB_error_context c,
const char *  data 
)

Definition at line 80 of file error_context.cpp.

References ICEDB_error_context_append.

81 {
82  size_t sz = (size_t) strnlen(data, UINT16_MAX);
83  ICEDB_error_context_append(c, sz+1, data);
84 }
#define ICEDB_error_context_append
Definition: error_context.h:60

◆ error_context_copy()

struct ICEDB_error_context* error_context_copy ( const struct ICEDB_error_context c)

Definition at line 40 of file error_context.cpp.

References ICEDB_error_context::code, ICEDB_COMPAT_strncpy_s(), ICEDB_error_context_add_string2, ICEDB_error_context_create, ICEDB_malloc(), ICEDB_error_context::message_size, ICEDB_error_context::message_size_alloced, ICEDB_error_context::message_text, ICEDB_error_context::num_var_fields, ICEDB_error_context_var_val::val, ICEDB_error_context::var_vals, and ICEDB_error_context_var_val::varname.

41 {
42  if (!c) return NULL;
44  res->code = c->code;
45  res->message_size = c->message_size;
49 
50  for (int i = 0; i < c->num_var_fields; ++i)
52 
53  return res;
54 }
ICEDB_error_code code
Definition: error_context.h:20
ICEDB_error_context_var_val * var_vals
Definition: error_context.h:26
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
ICEDB_SYMBOL_SHARED void * ICEDB_malloc(size_t numBytes)
Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted...
Definition: util.cpp:90
#define ICEDB_error_context_add_string2
Definition: error_context.h:63
#define ICEDB_error_context_create(x)
Definition: error_context.h:35
Here is the call graph for this function:

◆ error_context_create_impl()

struct ICEDB_error_context* error_context_create_impl ( int  code,
const char *  file,
int  line,
const char *  fsig 
)

Definition at line 10 of file error_context.cpp.

References __ICEDB_LOCAL_THREAD_error_context, ICEDB_error_context::code, ICEDB_COMPAT_strncpy_s(), ICEDB_COMPAT_strnlen_s, ICEDB_DEBUG_RAISE_EXCEPTION, ICEDB_error_context_add_string2, ICEDB_error_context_widen, ICEDB_ERRORCODES_MAP, ICEDB_free(), ICEDB_malloc(), ICEDB_error_context::max_num_var_fields, ICEDB_error_context::message_size, ICEDB_error_context::message_size_alloced, ICEDB_error_context::message_text, ICEDB_error_context::num_var_fields, and ICEDB_error_context::var_vals.

11 {
13  if (!res) ICEDB_DEBUG_RAISE_EXCEPTION();
14  res->code = code;
15  res->message_size_alloced = (size_t) (sizeof(char)*(1 + ICEDB_COMPAT_strnlen_s(ICEDB_ERRORCODES_MAP[code],UINT16_MAX-1)));
16  res->message_text = (char*)ICEDB_malloc(sizeof(char)*res->message_size_alloced);
18  ICEDB_ERRORCODES_MAP[code], (size_t)sizeof(char)*(1+ ICEDB_COMPAT_strnlen_s(ICEDB_ERRORCODES_MAP[code], UINT16_MAX - 1)));
19  // Note that the null character is counted here, by my convention. message_size should NEVER equal zero.
20  res->message_size = (size_t) (sizeof(char)*(1 + ICEDB_COMPAT_strnlen_s(res->message_text, UINT16_MAX - 1)));
21  res->num_var_fields = 0;
22  res->max_num_var_fields = 0;
23  res->var_vals = NULL;
25  ICEDB_error_context_add_string2(res, "file", file);
26  ICEDB_error_context_add_string2(res, "fsig", fsig);
27 
28  const int slinesz = 50;
29  char sline[slinesz];
30  snprintf(sline, slinesz, "%d", line);
31 
32  ICEDB_error_context_add_string2(res, "line", sline);
33 
36  return res;
37 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
ICEDB_SYMBOL_SHARED void ICEDB_free(void *obj)
Free memory region. Should not be double-freed.
Definition: util.cpp:97
DL_ICEDB ICEDB_error_context_widen_f ICEDB_error_context_widen
ICEDB_error_code code
Definition: error_context.h:20
ICEDB_error_context_var_val * var_vals
Definition: error_context.h:26
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
ICEDB_SYMBOL_SHARED void * ICEDB_malloc(size_t numBytes)
Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted...
Definition: util.cpp:90
ICEDB_SYMBOL_PRIVATE ICEDB_THREAD_LOCAL ICEDB_error_context * __ICEDB_LOCAL_THREAD_error_context
ICEDB_SYMBOL_PRIVATE const char * ICEDB_ERRORCODES_MAP[ICEDB_ERRORCODES_TOTAL]
Definition: errorCodes.cpp:3
#define ICEDB_error_context_add_string2
Definition: error_context.h:63
#define ICEDB_COMPAT_strnlen_s
Definition: util.h:44
Here is the call graph for this function:

◆ error_context_widen()

void error_context_widen ( struct ICEDB_error_context c,
size_t  numNewSpaces 
)

Definition at line 107 of file error_context.cpp.

References ICEDB_COMPAT_strncpy_s(), ICEDB_free(), ICEDB_malloc(), ICEDB_error_context::max_num_var_fields, ICEDB_error_context_var_val::val, ICEDB_error_context::var_vals, and ICEDB_error_context_var_val::varname.

108 {
110  (numNewSpaces + c->max_num_var_fields) * (sizeof (ICEDB_error_context)));
111 
112  for (size_t i = 0; i < numNewSpaces; ++i) {
113  newvals[i + c->max_num_var_fields].val = NULL;
114  newvals[i + c->max_num_var_fields].varname = NULL;
115  }
116  if (c->var_vals) {
117  for (size_t i = 0; i < c->max_num_var_fields; ++i) {
118  size_t szval = sizeof(char) * strlen(c->var_vals[i].val);
119  newvals[i].val = (char*)ICEDB_malloc(szval);
120  ICEDB_COMPAT_strncpy_s(newvals[i].val, szval, c->var_vals[i].val, szval);
121 
122  size_t szname = sizeof(char) * strlen(c->var_vals[i].varname);
123  newvals[i].varname = (char*)ICEDB_malloc(szname);
124  ICEDB_COMPAT_strncpy_s(newvals[i].varname, szname, c->var_vals[i].varname, szname);
125  }
126  ICEDB_free(c->var_vals);
127  }
128  c->var_vals = newvals;
129  c->max_num_var_fields += numNewSpaces;
130 }
ICEDB_SYMBOL_SHARED void ICEDB_free(void *obj)
Free memory region. Should not be double-freed.
Definition: util.cpp:97
ICEDB_error_context_var_val * var_vals
Definition: error_context.h:26
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
ICEDB_SYMBOL_SHARED void * ICEDB_malloc(size_t numBytes)
Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted...
Definition: util.cpp:90
Here is the call graph for this function: