fix off by 1 error
[rrdtool.git] / src / rrd_format.c
1 /*****************************************************************************
2  * RRDtool 1.4.3  Copyright by Tobi Oetiker, 1997-2010
3  *****************************************************************************
4  * rrd_format.c  RRD Database Format helper functions
5  *****************************************************************************
6  * $Id$
7  * $Log$
8  * Revision 1.5  2004/05/18 18:53:03  oetiker
9  * big spell checking patch -- slif@bellsouth.net
10  *
11  * Revision 1.4  2003/02/13 07:05:27  oetiker
12  * Find attached the patch I promised to send to you. Please note that there
13  * are three new source files (src/rrd_is_thread_safe.h, src/rrd_thread_safe.c
14  * and src/rrd_not_thread_safe.c) and the introduction of librrd_th. This
15  * library is identical to librrd, but it contains support code for per-thread
16  * global variables currently used for error information only. This is similar
17  * to how errno per-thread variables are implemented.  librrd_th must be linked
18  * alongside of libpthred
19  *
20  * There is also a new file "THREADS", holding some documentation.
21  *
22  * -- Peter Stamfest <peter@stamfest.at>
23  *
24  * Revision 1.3  2002/02/01 20:34:49  oetiker
25  * fixed version number and date/time
26  *
27  * Revision 1.2  2001/03/10 23:54:39  oetiker
28  * Support for COMPUTE data sources (CDEF data sources). Removes the RPN
29  * parser and calculator from rrd_graph and puts then in a new file,
30  * rrd_rpncalc.c. Changes to core files rrd_create and rrd_update. Some
31  * clean-up of aberrant behavior stuff, including a bug fix.
32  * Documentation update (rrdcreate.pod, rrdupdate.pod). Change xml format.
33  * -- Jake Brutlag <jakeb@corp.webtv.net>
34  *
35  * Revision 1.1.1.1  2001/02/25 22:25:05  oetiker
36  * checkin
37  *
38  * Revision 1.3  1998/03/08 12:35:11  oetiker
39  * checkpointing things because the current setup seems to work
40  * according to the things said in the manpages
41  *
42  * Revision 1.2  1998/02/26 22:58:22  oetiker
43  * fixed define
44  *
45  * Revision 1.1  1998/02/21 16:14:41  oetiker
46  * Initial revision
47  *
48  *
49  *****************************************************************************/
50 #include "rrd_tool.h"
51 #ifdef WIN32
52 #include "stdlib.h"
53 #endif
54
55 #define converter(VV,VVV) \
56    if (strcmp(#VV, string) == 0) return VVV;
57
58 /* conversion functions to allow symbolic entry of enumerations */
59 enum dst_en dst_conv(
60     char *string)
61 {
62     converter(COUNTER, DST_COUNTER)
63         converter(ABSOLUTE, DST_ABSOLUTE)
64         converter(GAUGE, DST_GAUGE)
65         converter(DERIVE, DST_DERIVE)
66         converter(COMPUTE, DST_CDEF)
67         rrd_set_error("unknown data acquisition function '%s'", string);
68     return (enum dst_en)(-1);
69 }
70
71
72 enum cf_en cf_conv(
73     const char *string)
74 {
75
76     converter(AVERAGE, CF_AVERAGE)
77         converter(MIN, CF_MINIMUM)
78         converter(MAX, CF_MAXIMUM)
79         converter(LAST, CF_LAST)
80         converter(HWPREDICT, CF_HWPREDICT)
81         converter(MHWPREDICT, CF_MHWPREDICT)
82         converter(DEVPREDICT, CF_DEVPREDICT)
83         converter(SEASONAL, CF_SEASONAL)
84         converter(DEVSEASONAL, CF_DEVSEASONAL)
85         converter(FAILURES, CF_FAILURES)
86         rrd_set_error("unknown consolidation function '%s'", string);
87     return (enum cf_en)(-1);
88 }
89
90 const char *cf_to_string (enum cf_en cf)
91 {
92     switch (cf)
93     {
94         case CF_AVERAGE:     return "AVERAGE";
95         case CF_MINIMUM:     return "MIN";
96         case CF_MAXIMUM:     return "MAX";
97         case CF_LAST:        return "LAST";
98         case CF_HWPREDICT:   return "HWPREDICT";
99         case CF_SEASONAL:    return "SEASONAL";
100         case CF_DEVPREDICT:  return "DEVPREDICT";
101         case CF_DEVSEASONAL: return "DEVSEASONAL";
102         case CF_FAILURES:    return "FAILURES";
103         case CF_MHWPREDICT:  return "MHWPREDICT";
104
105         default:
106             return NULL;
107     }
108 } /* char *cf_to_string */
109
110 #undef converter
111
112 long ds_match(
113     rrd_t *rrd,
114     char *ds_nam)
115 {
116     unsigned long i;
117
118     for (i = 0; i < rrd->stat_head->ds_cnt; i++)
119         if ((strcmp(ds_nam, rrd->ds_def[i].ds_nam)) == 0)
120             return i;
121     rrd_set_error("unknown data source name '%s'", ds_nam);
122     return -1;
123 }
124
125 off_t rrd_get_header_size(
126     rrd_t *rrd)
127 {
128     return sizeof(stat_head_t) + \
129         sizeof(ds_def_t) * rrd->stat_head->ds_cnt + \
130         sizeof(rra_def_t) * rrd->stat_head->rra_cnt + \
131         ( atoi(rrd->stat_head->version) < 3 ? sizeof(time_t) : sizeof(live_head_t) ) + \
132         sizeof(pdp_prep_t) * rrd->stat_head->ds_cnt + \
133         sizeof(cdp_prep_t) * rrd->stat_head->ds_cnt * rrd->stat_head->rra_cnt + \
134         sizeof(rra_ptr_t) * rrd->stat_head->rra_cnt;
135 }