new function rrd_fetch_r() (and make the
[rrdtool.git] / src / rrd_tool.h
1 /*****************************************************************************
2  * RRDtool 1.2.19  Copyright by Tobi Oetiker, 1997-2007
3  *****************************************************************************
4  * rrd_tool.h   Common Header File
5  *****************************************************************************/
6 #ifdef  __cplusplus
7 extern "C" {
8 #endif
9
10
11 #ifndef _RRD_TOOL_H
12 #define _RRD_TOOL_H
13
14 #ifdef HAVE_CONFIG_H
15 #include "../rrd_config.h"
16 #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
17 #include "../win32/config.h"
18 #endif
19
20 #ifdef MUST_DISABLE_SIGFPE
21 #include <signal.h>
22 #endif
23
24 #ifdef MUST_DISABLE_FPMASK
25 #include <floatingpoint.h>
26 #endif
27     
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <time.h>
33 #include <ctype.h>
34
35 #if HAVE_SYS_PARAM_H
36 #  include <sys/param.h>
37 #endif
38
39 #ifndef MAXPATH
40 #  define MAXPATH 1024
41 #endif
42
43 #if HAVE_MATH_H
44 # include <math.h>
45 #endif
46 /* Sorry: don't know autoconf as well how to check the exist of
47    dirent.h ans sys/stat.h
48 */
49
50 #if HAVE_DIRENT_H
51 # include <dirent.h>
52 # define NAMLEN(dirent) strlen((dirent)->d_name)
53 #else
54 # define dirent direct
55 # define NAMLEN(dirent) (dirent)->d_namlen
56 # if HAVE_SYS_NDIR_H
57 #  include <sys/ndir.h>
58 # endif
59 # if HAVE_SYS_DIR_H
60 #  include <sys/dir.h>
61 # endif
62 # if HAVE_NDIR_H
63 #  include <ndir.h>
64 # endif
65 #endif
66
67 #if HAVE_SYS_TYPES_H
68 # include <sys/types.h>
69 #endif
70
71 #if HAVE_SYS_STAT_H
72 # include <sys/stat.h>
73 #endif
74
75
76 #if HAVE_UNISTD_H
77 # include <unistd.h>
78 #endif
79 #if HAVE_SYS_TIME_H
80 # include <sys/time.h>
81 #endif
82 #if HAVE_SYS_TIMES_H
83 # include <sys/times.h>
84 #endif
85 #if HAVE_SYS_RESOURCE_H
86 # include <sys/resource.h>
87 #if (defined(__svr4__) && defined(__sun__))
88 /* Solaris headers (pre 2.6) don't have a getrusage prototype.
89    Use this instead. */
90 extern int getrusage(int, struct rusage *);
91 #endif /* __svr4__ && __sun__ */
92 #endif
93
94 #include "rrd.h"
95
96 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
97
98 /* Win32 only includes */
99
100 #include <float.h>        /* for _isnan  */
101 #include <io.h>           /* for chdir   */
102
103 struct tm* localtime_r(const time_t *timep, struct tm* result);
104 char* ctime_r(const time_t *timep, char* result);
105 struct tm* gmtime_r(const time_t *timep, struct tm* result);
106 char *strtok_r(char *str, const char *sep, char **last);
107
108 #else
109
110 /* unix-only includes */
111 #ifndef isnan
112 int isnan(double value);
113 #endif
114
115 #endif
116
117 /* local include files -- need to be after the system ones */
118 #include "rrd_getopt.h"
119 #include "rrd_format.h"
120
121 #ifndef max
122 #define max(a,b) ((a) > (b) ? (a) : (b))
123 #endif
124
125 #ifndef min
126 #define min(a,b) ((a) < (b) ? (a) : (b))
127 #endif                                                   
128
129 #define DIM(x) (sizeof(x)/sizeof(x[0]))
130
131 /* rrd info interface */
132 enum info_type   { RD_I_VAL=0,
133                RD_I_CNT,
134                RD_I_STR, 
135                    RD_I_INT };
136
137 typedef union infoval { 
138     unsigned long u_cnt; 
139     rrd_value_t   u_val;
140     char         *u_str;
141     int           u_int;
142 } infoval;
143
144 typedef struct info_t {
145     char            *key;
146     enum info_type  type;
147     union infoval   value;
148     struct info_t   *next;
149 } info_t;
150
151 info_t *rrd_info(int, char **);
152 int rrd_lastupdate(int argc, char **argv, time_t *last_update,
153                 unsigned long *ds_cnt, char ***ds_namv, char ***last_ds);
154 info_t *rrd_update_v(int, char **);
155 char * sprintf_alloc(char *, ...);
156 info_t *info_push(info_t *, char *, enum info_type, infoval);
157
158 /* HELPER FUNCTIONS */
159
160 int PngSize(FILE *, long *, long *);
161
162 int rrd_create_fn(const char *file_name, rrd_t *rrd);
163 int rrd_fetch_fn(const char *filename, enum cf_en cf_idx,
164                  time_t *start,time_t *end,
165                  unsigned long *step,
166                  unsigned long *ds_cnt,
167                  char        ***ds_namv,
168                  rrd_value_t **data);
169
170 void rrd_free(rrd_t *rrd);
171 void rrd_freemem(void *mem);
172 void rrd_init(rrd_t *rrd);
173
174 int rrd_open(const char *file_name, FILE **in_file, rrd_t *rrd, int rdwr);
175 int readfile(const char *file, char **buffer, int skipfirst);
176
177 #define RRD_READONLY    0
178 #define RRD_READWRITE   1
179
180 enum cf_en cf_conv(const char *string);
181 enum dst_en dst_conv(char *string);
182 long ds_match(rrd_t *rrd,char *ds_nam);
183 double rrd_diff(char *a, char *b);
184
185     /* rrd_strerror is thread safe, but still it uses a global buffer
186        (but one per thread), thus subsequent calls within a single
187        thread overwrite the same buffer */
188 const char *rrd_strerror(int err);
189
190 #endif
191
192 #ifdef  __cplusplus
193 }
194 #endif