|
Packit Service |
50ad14 |
#include "config.h"
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
#include <stdio.h>
|
|
Packit Service |
50ad14 |
#include <stdlib.h>
|
|
Packit Service |
50ad14 |
#include <string.h>
|
|
Packit Service |
50ad14 |
#include <errno.h>
|
|
Packit Service |
50ad14 |
#include <sys/types.h>
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
#include <keymap/array.h>
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_array_init(struct lk_array *a, size_t memb, size_t size)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!a)
|
|
Packit Service |
50ad14 |
return -EINVAL;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
memset(a, 0, sizeof(struct lk_array));
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
a->array = calloc(size, memb);
|
|
Packit Service |
50ad14 |
a->memb = memb;
|
|
Packit Service |
50ad14 |
a->total = size;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (size && !a->array)
|
|
Packit Service |
50ad14 |
return -ENOMEM;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_array_free(struct lk_array *a)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!a)
|
|
Packit Service |
50ad14 |
return -EINVAL;
|
|
Packit Service |
50ad14 |
free(a->array);
|
|
Packit Service |
50ad14 |
memset(a, 0, sizeof(struct lk_array));
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_array_empty(struct lk_array *a)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!a)
|
|
Packit Service |
50ad14 |
return -EINVAL;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
memset(a->array, 0, (a->memb * a->total));
|
|
Packit Service |
50ad14 |
a->count = 0;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_array_exists(struct lk_array *a, unsigned int i)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
char *s;
|
|
Packit Service |
50ad14 |
size_t k;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (!a || i >= a->total) {
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
s = (char *)(a->array + (a->memb * i));
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
for (k = 0; k < a->memb; k++) {
|
|
Packit Service |
50ad14 |
if (s[k] != 0)
|
|
Packit Service |
50ad14 |
return 1;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
void *
|
|
Packit Service |
50ad14 |
lk_array_get(struct lk_array *a, unsigned int i)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!a || i >= a->total) {
|
|
Packit Service |
50ad14 |
return NULL;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
return a->array + (a->memb * i);
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
void *
|
|
Packit Service |
50ad14 |
lk_array_get_ptr(struct lk_array *a, unsigned int i)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
void **ptr;
|
|
Packit Service |
50ad14 |
if (!a || i >= a->total) {
|
|
Packit Service |
50ad14 |
return NULL;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
ptr = a->array;
|
|
Packit Service |
50ad14 |
return *(ptr + i);
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
static int
|
|
Packit Service |
50ad14 |
array_resize(struct lk_array *a, unsigned int i)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!a)
|
|
Packit Service |
50ad14 |
return -EINVAL;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (i >= a->total) {
|
|
Packit Service |
50ad14 |
void *tmp = realloc(a->array, a->memb * (i + 1));
|
|
Packit Service |
50ad14 |
if (!tmp)
|
|
Packit Service |
50ad14 |
return -ENOMEM;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
memset(tmp + (a->memb * a->total), 0, a->memb * (i + 1 - a->total));
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
a->array = tmp;
|
|
Packit Service |
50ad14 |
a->total = i + 1;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_array_set(struct lk_array *a, unsigned int i, const void *e)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
int ret = array_resize(a, i);
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (ret < 0)
|
|
Packit Service |
50ad14 |
return ret;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
memcpy(a->array + (a->memb * i), e, a->memb);
|
|
Packit Service |
50ad14 |
a->count++;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_array_unset(struct lk_array *a, unsigned int i)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (!a || i >= a->total)
|
|
Packit Service |
50ad14 |
return -EINVAL;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (lk_array_exists(a, i)) {
|
|
Packit Service |
50ad14 |
memset(a->array + (a->memb * i), 0, a->memb);
|
|
Packit Service |
50ad14 |
a->count--;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
int lk_array_append(struct lk_array *a, const void *e)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
int ret = array_resize(a, a->count);
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
if (ret < 0)
|
|
Packit Service |
50ad14 |
return ret;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
memcpy(a->array + (a->memb * a->count), e, a->memb);
|
|
Packit Service |
50ad14 |
a->count++;
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
return 0;
|
|
Packit Service |
50ad14 |
}
|