/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4dbit8.ctr */ /* Copyright (C) 2015-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @file dk4dbit8.c The dk4dbit8 module. */ #line 147 "dk4dbit8.ctr" #include "dk4conf.h" #include "dk4str8.h" #include "dk4dbit8.h" #include "dk4maasz.h" #line 157 "dk4dbit8.ctr" /** Object for database traversal. */ typedef struct { void *obj; /**< Object for string traversal. */ dk4dbi_c8_traverse_fct_t *fct; /**< String traversal function. */ } dk4dbi_c8_traversal_object_t; int dk4dbi_c8_del( dk4_dbi_t *db, const char *k, dk4_er_t *erp ) { dk4_er_t er; /* Internal error report for size calc */ size_t ks = 0; /* Key size */ int back = 0; #line 180 "dk4dbit8.ctr" if ((NULL != db) && (NULL != k)) { dk4error_init(&er); ks = dk4ma_size_t_add(1, dk4str8_len(k), &er); if (DK4_E_NONE == er.ec) { back = dk4dbi_delete(db, k, ks, erp); } else { dk4error_copy(erp, &er); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 191 "dk4dbit8.ctr" return back; } int dk4dbi_c8_set( dk4_dbi_t *db, const char *k, const char *v, dk4_er_t *erp ) { dk4_er_t er; /* Internal error report for size calc */ size_t ks = 0; /* Key size */ size_t vs = 0; /* Value size */ int back = 0; #line 209 "dk4dbit8.ctr" if ((NULL != db) && (NULL != k)) { #line 210 "dk4dbit8.ctr" if (NULL != v) { #line 211 "dk4dbit8.ctr" dk4error_init(&er); ks = dk4ma_size_t_add(1, dk4str8_len(k), &er); vs = dk4ma_size_t_add(1, dk4str8_len(v), &er); if (DK4_E_NONE == er.ec) { #line 215 "dk4dbit8.ctr" back = dk4dbi_set(db, k, ks, v, vs, erp); } else { #line 217 "dk4dbit8.ctr" dk4error_copy(erp, &er); } } else { #line 220 "dk4dbit8.ctr" back = dk4dbi_c8_del(db, k, erp); } } else { #line 223 "dk4dbit8.ctr" dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 225 "dk4dbit8.ctr" return back; } int dk4dbi_c8_get( dk4_dbi_t *db, const char *k, char *vbuf, size_t vsz, dk4_er_t *erp ) { dk4_er_t er; /* Internal error report for size calc */ size_t ks = 0; /* Key size */ size_t ressz = 0; /* Result size */ int back = 0; #line 245 "dk4dbit8.ctr" if ((NULL != vbuf) && (0 < vsz)) { *vbuf = '\0'; } if ((NULL != db) && (NULL != k) && (NULL != vbuf) && (0 < vsz)) { dk4error_init(&er); #line 248 "dk4dbit8.ctr" ressz = vsz; ks = dk4ma_size_t_add(1, dk4str8_len(k), &er); if (DK4_E_NONE == er.ec) { #line 251 "dk4dbit8.ctr" if (0 != dk4dbi_get(db, k, ks, vbuf, &ressz, erp)) { #line 252 "dk4dbit8.ctr" if (0 < ressz) { #line 253 "dk4dbit8.ctr" if ('\0' == vbuf[ressz - 1]) { back = 1; #line 255 "dk4dbit8.ctr" } else { if (ressz < vsz) { vbuf[ressz] = '\0'; back = 1; #line 259 "dk4dbit8.ctr" } else { #line 260 "dk4dbit8.ctr" *vbuf = '\0'; dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); } } } else { #line 265 "dk4dbit8.ctr" *vbuf = '\0'; dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND); } } #if TRACE_DEBUG else { #line 271 "dk4dbit8.ctr" } #endif } else { #line 274 "dk4dbit8.ctr" dk4error_copy(erp, &er); } } else { #line 277 "dk4dbit8.ctr" dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 279 "dk4dbit8.ctr" return back; } /** Function to traverse a simple database. The function must check the kb, ks, vb and vs arguments for validity, they are passed as found by the backend functions (this means pointers may be NULL, sizes may be 0). @param Object modified during traversal, may be NULL. @param kb Key data. @param ks Key size. @param vb Value data. @param vs Value size. @return 1 on success, 0 on error (can continue), -1 on error (abort). */ static int dk4dbi_c8_internal_traverse( void *obj, const void *kb, size_t ks, const void *vb, size_t vs ) { dk4dbi_c8_traversal_object_t *myobj; /* Passed from dk4dbi_traverse() */ const char *pk; /* Key pointer */ const char *pv; /* Value pointer */ int back = 0; #line 310 "dk4dbit8.ctr" if ((NULL != obj) && (NULL != kb) && (NULL != vb) && (0 < ks) && (0 < vs)) { pk = (const char *)kb; pv = (const char *)vb; if (('\0' == pk[ks - 1]) && ('\0' == pv[vs - 1])) { myobj = (dk4dbi_c8_traversal_object_t *)obj; back = (*(myobj->fct))(myobj->obj, pk, pv); } } #line 318 "dk4dbit8.ctr" return back; } int dk4dbi_c8_traverse( dk4_dbi_t *db, void *obj, dk4dbi_c8_traverse_fct_t *fct ) { dk4dbi_c8_traversal_object_t myobj; /* Passed to dk4dbi_traverse */ int back = -1; #line 334 "dk4dbit8.ctr" if ((NULL != db) && (NULL != fct)) { myobj.obj = obj; myobj.fct = fct; back = dk4dbi_traverse(db, &myobj, dk4dbi_c8_internal_traverse); } #line 339 "dk4dbit8.ctr" return back; }