[Code formatting: Change indentation style to space](https://github.com/clicon/clixon/issues/379)
* Applies to all c/h/y/l/sh files and .editorconfig
This commit is contained in:
parent
a9d1ab006c
commit
d84c529ff1
376 changed files with 38147 additions and 38133 deletions
|
|
@ -98,7 +98,7 @@
|
|||
#include "clixon_err.h"
|
||||
#include "clixon_hash.h"
|
||||
|
||||
#define HASH_SIZE 1031 /* Number of hash buckets. Should be a prime */
|
||||
#define HASH_SIZE 1031 /* Number of hash buckets. Should be a prime */
|
||||
#define align4(s) (((s)/4)*4 + 4)
|
||||
|
||||
/*! A very simplistic algorithm to calculate a hash bucket index
|
||||
|
|
@ -109,7 +109,7 @@ hash_bucket(const char *str)
|
|||
uint32_t n = 0;
|
||||
|
||||
while(*str)
|
||||
n += (uint32_t)*str++;
|
||||
n += (uint32_t)*str++;
|
||||
return n % HASH_SIZE;
|
||||
}
|
||||
|
||||
|
|
@ -144,13 +144,13 @@ clicon_hash_free(clicon_hash_t *hash)
|
|||
int i;
|
||||
clicon_hash_t tmp;
|
||||
for (i = 0; i < HASH_SIZE; i++) {
|
||||
while (hash[i]) {
|
||||
tmp = hash[i];
|
||||
DELQ(tmp, hash[i], clicon_hash_t);
|
||||
free(tmp->h_key);
|
||||
free(tmp->h_val);
|
||||
free(tmp);
|
||||
}
|
||||
while (hash[i]) {
|
||||
tmp = hash[i];
|
||||
DELQ(tmp, hash[i], clicon_hash_t);
|
||||
free(tmp->h_key);
|
||||
free(tmp->h_val);
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
free(hash);
|
||||
return 0;
|
||||
|
|
@ -165,7 +165,7 @@ clicon_hash_free(clicon_hash_t *hash)
|
|||
*/
|
||||
clicon_hash_t
|
||||
clicon_hash_lookup(clicon_hash_t *hash,
|
||||
const char *key)
|
||||
const char *key)
|
||||
{
|
||||
uint32_t bkt;
|
||||
clicon_hash_t h;
|
||||
|
|
@ -173,11 +173,11 @@ clicon_hash_lookup(clicon_hash_t *hash,
|
|||
bkt = hash_bucket(key);
|
||||
h = hash[bkt];
|
||||
if (h) {
|
||||
do {
|
||||
if (!strcmp(h->h_key, key))
|
||||
return h;
|
||||
h = NEXTQ(clicon_hash_t, h);
|
||||
} while (h != hash[bkt]);
|
||||
do {
|
||||
if (!strcmp(h->h_key, key))
|
||||
return h;
|
||||
h = NEXTQ(clicon_hash_t, h);
|
||||
} while (h != hash[bkt]);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -191,21 +191,21 @@ clicon_hash_lookup(clicon_hash_t *hash,
|
|||
*/
|
||||
void *
|
||||
clicon_hash_value(clicon_hash_t *hash,
|
||||
const char *key,
|
||||
size_t *vlen)
|
||||
const char *key,
|
||||
size_t *vlen)
|
||||
{
|
||||
clicon_hash_t h;
|
||||
|
||||
if (hash == NULL){
|
||||
clicon_err(OE_UNIX, EINVAL, "hash is NULL");
|
||||
return NULL;
|
||||
clicon_err(OE_UNIX, EINVAL, "hash is NULL");
|
||||
return NULL;
|
||||
}
|
||||
h = clicon_hash_lookup(hash, key);
|
||||
if (h == NULL)
|
||||
return NULL; /* OK, key not found */
|
||||
return NULL; /* OK, key not found */
|
||||
|
||||
if (vlen)
|
||||
*vlen = h->h_vlen;
|
||||
*vlen = h->h_vlen;
|
||||
return h->h_val;
|
||||
}
|
||||
|
||||
|
|
@ -221,69 +221,69 @@ clicon_hash_value(clicon_hash_t *hash,
|
|||
*/
|
||||
clicon_hash_t
|
||||
clicon_hash_add(clicon_hash_t *hash,
|
||||
const char *key,
|
||||
void *val,
|
||||
size_t vlen)
|
||||
const char *key,
|
||||
void *val,
|
||||
size_t vlen)
|
||||
{
|
||||
void *newval = NULL;
|
||||
clicon_hash_t h;
|
||||
clicon_hash_t new = NULL;
|
||||
|
||||
if (hash == NULL){
|
||||
clicon_err(OE_UNIX, EINVAL, "hash is NULL");
|
||||
return NULL;
|
||||
clicon_err(OE_UNIX, EINVAL, "hash is NULL");
|
||||
return NULL;
|
||||
}
|
||||
/* Check NULL case */
|
||||
if ((val == NULL && vlen != 0) ||
|
||||
(val != NULL && vlen == 0)){
|
||||
clicon_err(OE_UNIX, EINVAL, "Mismatch in value and length, only one is zero");
|
||||
goto catch;
|
||||
(val != NULL && vlen == 0)){
|
||||
clicon_err(OE_UNIX, EINVAL, "Mismatch in value and length, only one is zero");
|
||||
goto catch;
|
||||
}
|
||||
/* If variable exist, don't allocate a new. just replace value */
|
||||
h = clicon_hash_lookup(hash, key);
|
||||
if (h == NULL) {
|
||||
if ((new = (clicon_hash_t)malloc(sizeof(*new))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "malloc");
|
||||
goto catch;
|
||||
}
|
||||
memset(new, 0, sizeof(*new));
|
||||
|
||||
new->h_key = strdup(key);
|
||||
if (new->h_key == NULL){
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
goto catch;
|
||||
}
|
||||
|
||||
h = new;
|
||||
if ((new = (clicon_hash_t)malloc(sizeof(*new))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "malloc");
|
||||
goto catch;
|
||||
}
|
||||
memset(new, 0, sizeof(*new));
|
||||
|
||||
new->h_key = strdup(key);
|
||||
if (new->h_key == NULL){
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
goto catch;
|
||||
}
|
||||
|
||||
h = new;
|
||||
}
|
||||
|
||||
if (vlen){
|
||||
/* Make copy of value. aligned */
|
||||
newval = malloc(align4(vlen+3));
|
||||
if (newval == NULL){
|
||||
clicon_err(OE_UNIX, errno, "malloc");
|
||||
goto catch;
|
||||
}
|
||||
memcpy(newval, val, vlen);
|
||||
/* Make copy of value. aligned */
|
||||
newval = malloc(align4(vlen+3));
|
||||
if (newval == NULL){
|
||||
clicon_err(OE_UNIX, errno, "malloc");
|
||||
goto catch;
|
||||
}
|
||||
memcpy(newval, val, vlen);
|
||||
}
|
||||
|
||||
/* Free old value if existing variable */
|
||||
if (h->h_val)
|
||||
free(h->h_val);
|
||||
free(h->h_val);
|
||||
h->h_val = newval;
|
||||
h->h_vlen = vlen;
|
||||
|
||||
/* Add to list only if new variable */
|
||||
if (new)
|
||||
INSQ(h, hash[hash_bucket(key)]);
|
||||
INSQ(h, hash[hash_bucket(key)]);
|
||||
|
||||
return h;
|
||||
|
||||
catch:
|
||||
if (new) {
|
||||
if (new->h_key)
|
||||
free(new->h_key);
|
||||
free(new);
|
||||
if (new->h_key)
|
||||
free(new->h_key);
|
||||
free(new);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
|
@ -299,17 +299,17 @@ catch:
|
|||
*/
|
||||
int
|
||||
clicon_hash_del(clicon_hash_t *hash,
|
||||
const char *key)
|
||||
const char *key)
|
||||
{
|
||||
clicon_hash_t h;
|
||||
|
||||
if (hash == NULL){
|
||||
clicon_err(OE_UNIX, EINVAL, "hash is NULL");
|
||||
return -1;
|
||||
clicon_err(OE_UNIX, EINVAL, "hash is NULL");
|
||||
return -1;
|
||||
}
|
||||
h = clicon_hash_lookup(hash, key);
|
||||
if (h == NULL)
|
||||
return -1;
|
||||
return -1;
|
||||
|
||||
DELQ(h, hash[hash_bucket(key)], clicon_hash_t);
|
||||
|
||||
|
|
@ -322,17 +322,17 @@ clicon_hash_del(clicon_hash_t *hash,
|
|||
|
||||
/*! Return vector of keys in hash table
|
||||
*
|
||||
* @param[in] hash Hash table
|
||||
* @param[in] hash Hash table
|
||||
* @param[out] vector Vector of keys, NULL if not found
|
||||
* @param[out] nkeys Size of key vector
|
||||
* @param[out] nkeys Size of key vector
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
* @note: vector needs to be deallocated with free
|
||||
*/
|
||||
int
|
||||
clicon_hash_keys(clicon_hash_t *hash,
|
||||
char ***vector,
|
||||
size_t *nkeys)
|
||||
char ***vector,
|
||||
size_t *nkeys)
|
||||
{
|
||||
int retval = -1;
|
||||
int bkt;
|
||||
|
|
@ -341,47 +341,47 @@ clicon_hash_keys(clicon_hash_t *hash,
|
|||
char **keys = NULL;
|
||||
|
||||
if (hash == NULL){
|
||||
clicon_err(OE_UNIX, EINVAL, "hash is NULL");
|
||||
return -1;
|
||||
clicon_err(OE_UNIX, EINVAL, "hash is NULL");
|
||||
return -1;
|
||||
}
|
||||
*nkeys = 0;
|
||||
for (bkt = 0; bkt < HASH_SIZE; bkt++) {
|
||||
h = hash[bkt];
|
||||
do {
|
||||
if (h == NULL)
|
||||
break;
|
||||
tmp = realloc(keys, ((*nkeys)+1) * sizeof(char *));
|
||||
if (tmp == NULL){
|
||||
clicon_err(OE_UNIX, errno, "realloc");
|
||||
goto catch;
|
||||
}
|
||||
keys = tmp;
|
||||
keys[*nkeys] = h->h_key;
|
||||
(*nkeys)++;
|
||||
h = NEXTQ(clicon_hash_t, h);
|
||||
} while (h != hash[bkt]);
|
||||
h = hash[bkt];
|
||||
do {
|
||||
if (h == NULL)
|
||||
break;
|
||||
tmp = realloc(keys, ((*nkeys)+1) * sizeof(char *));
|
||||
if (tmp == NULL){
|
||||
clicon_err(OE_UNIX, errno, "realloc");
|
||||
goto catch;
|
||||
}
|
||||
keys = tmp;
|
||||
keys[*nkeys] = h->h_key;
|
||||
(*nkeys)++;
|
||||
h = NEXTQ(clicon_hash_t, h);
|
||||
} while (h != hash[bkt]);
|
||||
}
|
||||
if (vector){
|
||||
*vector = keys;
|
||||
keys = NULL;
|
||||
*vector = keys;
|
||||
keys = NULL;
|
||||
}
|
||||
retval = 0;
|
||||
catch:
|
||||
if (keys)
|
||||
free(keys);
|
||||
free(keys);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Dump contents of hash to FILE pointer.
|
||||
*
|
||||
* @param[in] hash Hash structure
|
||||
* @param[in] f FILE pointer for print output
|
||||
* @param[in] hash Hash structure
|
||||
* @param[in] f FILE pointer for print output
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*/
|
||||
int
|
||||
clicon_hash_dump(clicon_hash_t *hash,
|
||||
FILE *f)
|
||||
FILE *f)
|
||||
{
|
||||
int retval = -1;
|
||||
int i;
|
||||
|
|
@ -391,18 +391,18 @@ clicon_hash_dump(clicon_hash_t *hash,
|
|||
size_t vlen;
|
||||
|
||||
if (hash == NULL)
|
||||
goto ok;
|
||||
goto ok;
|
||||
if (clicon_hash_keys(hash, &keys, &klen) < 0)
|
||||
goto done;
|
||||
goto done;
|
||||
for(i = 0; i < klen; i++) {
|
||||
val = clicon_hash_value(hash, keys[i], &vlen);
|
||||
printf("%s =\t 0x%p , length %zu\n", keys[i], val, vlen);
|
||||
val = clicon_hash_value(hash, keys[i], &vlen);
|
||||
printf("%s =\t 0x%p , length %zu\n", keys[i], val, vlen);
|
||||
}
|
||||
|
||||
ok:
|
||||
retval = 0;
|
||||
done:
|
||||
if (keys)
|
||||
free(keys);
|
||||
free(keys);
|
||||
return retval;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue