1 /*
2 * SysDB - src/core/timeseries.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/timeseries.h"
35 #include <stdlib.h>
36 #include <string.h>
38 /*
39 * public API
40 */
42 sdb_timeseries_t *
43 sdb_timeseries_create(size_t data_names_len, const char * const *data_names,
44 size_t data_len)
45 {
46 sdb_timeseries_t *ts;
47 size_t i;
49 ts = calloc(1, sizeof(*ts));
50 if (! ts)
51 return NULL;
53 ts->data = calloc(data_names_len, sizeof(*ts->data));
54 if (! ts->data) {
55 sdb_timeseries_destroy(ts);
56 return NULL;
57 }
58 ts->data_names_len = data_names_len;
59 for (i = 0; i < data_names_len; ++i) {
60 ts->data[i] = calloc(data_len, sizeof(**ts->data));
61 if (! ts->data[i]) {
62 sdb_timeseries_destroy(ts);
63 return NULL;
64 }
65 }
66 ts->data_len = data_len;
68 ts->data_names = calloc(data_names_len, sizeof(*ts->data_names));
69 if (! ts->data_names) {
70 sdb_timeseries_destroy(ts);
71 return NULL;
72 }
73 for (i = 0; i < data_names_len; ++i) {
74 ts->data_names[i] = strdup(data_names[i]);
75 if (! ts->data_names[i]) {
76 sdb_timeseries_destroy(ts);
77 return NULL;
78 }
79 }
80 return ts;
81 } /* sdb_timeseries_create */
83 void
84 sdb_timeseries_destroy(sdb_timeseries_t *ts)
85 {
86 size_t i;
88 if (! ts)
89 return;
91 if (ts->data) {
92 for (i = 0; i < ts->data_names_len; ++i) {
93 if (ts->data[i])
94 free(ts->data[i]);
95 ts->data[i] = NULL;
96 }
97 free(ts->data);
98 }
99 ts->data = NULL;
100 ts->data_len = 0;
102 if (ts->data_names) {
103 for (i = 0; i < ts->data_names_len; ++i) {
104 if (ts->data_names[i])
105 free(ts->data_names[i]);
106 ts->data_names[i] = NULL;
107 }
108 free(ts->data_names);
109 }
110 ts->data_names = NULL;
111 ts->data_names_len = 0;
112 free(ts);
113 } /* sdb_timeseries_destroy */
115 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */