| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
#include "error.h" |
|---|
| 21 |
#include "general.h" |
|---|
| 22 |
#include <errno.h> |
|---|
| 23 |
#include <stdio.h> |
|---|
| 24 |
#include <string.h> |
|---|
| 25 |
#include <stdlib.h> |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
#define A_BUNCH 16 |
|---|
| 29 |
|
|---|
| 30 |
void sensors_malloc_array(void *list, int *num_el, int *max_el, int el_size) |
|---|
| 31 |
{ |
|---|
| 32 |
void **my_list = (void **)list; |
|---|
| 33 |
|
|---|
| 34 |
*my_list = malloc(el_size*A_BUNCH); |
|---|
| 35 |
if (! *my_list) |
|---|
| 36 |
sensors_fatal_error("sensors_malloc_array","Allocating new elements"); |
|---|
| 37 |
*max_el = A_BUNCH; |
|---|
| 38 |
*num_el = 0; |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
void sensors_free_array(void *list, int *num_el, int *max_el) |
|---|
| 42 |
{ |
|---|
| 43 |
void **my_list = (void **)list; |
|---|
| 44 |
|
|---|
| 45 |
free(*my_list); |
|---|
| 46 |
*my_list = NULL; |
|---|
| 47 |
*num_el = 0; |
|---|
| 48 |
*max_el = 0; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
void sensors_add_array_el(const void *el, void *list, int *num_el, |
|---|
| 52 |
int *max_el, int el_size) |
|---|
| 53 |
{ |
|---|
| 54 |
int new_max_el; |
|---|
| 55 |
void **my_list = (void *)list; |
|---|
| 56 |
if (*num_el + 1 > *max_el) { |
|---|
| 57 |
new_max_el = *max_el + A_BUNCH; |
|---|
| 58 |
*my_list = realloc(*my_list,new_max_el * el_size); |
|---|
| 59 |
if (! *my_list) |
|---|
| 60 |
sensors_fatal_error("sensors_add_array_el","Allocating new elements"); |
|---|
| 61 |
*max_el = new_max_el; |
|---|
| 62 |
} |
|---|
| 63 |
memcpy(((char *) *my_list) + *num_el * el_size, el, el_size); |
|---|
| 64 |
(*num_el) ++; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
void sensors_add_array_els(const void *els, int nr_els, void *list, |
|---|
| 68 |
int *num_el, int *max_el, int el_size) |
|---|
| 69 |
{ |
|---|
| 70 |
int new_max_el; |
|---|
| 71 |
void **my_list = (void *)list; |
|---|
| 72 |
if (*num_el + nr_els > *max_el) { |
|---|
| 73 |
new_max_el = (*max_el + nr_els + A_BUNCH); |
|---|
| 74 |
new_max_el -= new_max_el % A_BUNCH; |
|---|
| 75 |
*my_list = realloc(*my_list,new_max_el * el_size); |
|---|
| 76 |
if (! *my_list) |
|---|
| 77 |
sensors_fatal_error("sensors_add_array_els","Allocating new elements"); |
|---|
| 78 |
*max_el = new_max_el; |
|---|
| 79 |
} |
|---|
| 80 |
memcpy(((char *)*my_list) + *num_el * el_size, els, el_size * nr_els); |
|---|
| 81 |
*num_el += nr_els; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
void sensors_strip_of_spaces(char *name) |
|---|
| 86 |
{ |
|---|
| 87 |
int i; |
|---|
| 88 |
for (i = strlen(name)-1; (i>=0) && (name[i] == ' '); i--); |
|---|
| 89 |
name[i+1] = '\0'; |
|---|
| 90 |
} |
|---|