]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/generic_utils.h
srfsh now:
[OpenSRF.git] / include / opensrf / generic_utils.h
1 #include <string.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <time.h>
5
6 /* libxml stuff for the config reader */
7 #include <libxml/xmlmemory.h>
8 #include <libxml/parser.h>
9 #include <libxml/xpath.h>
10 #include <libxml/xpathInternals.h>
11 #include <libxml/tree.h>
12
13 #ifndef GENERIC_UTILS_H
14 #define GENERIC_UTILS_H
15
16 #define LOG_ERROR 1
17 #define LOG_WARNING 2
18 #define LOG_INFO 3
19 #define LOG_DEBUG 4
20
21
22 #define equals(a,b) !strcmp(a,b) 
23
24 /** Malloc's, checks for NULL, clears all memory bits and 
25   * returns the pointer
26   * 
27   * @param size How many bytes of memory to allocate
28   */
29 inline void* safe_malloc( int size );
30
31 /* 10M limit on buffers for overflow protection */
32 #define BUFFER_MAX_SIZE 10485760 
33
34 // ---------------------------------------------------------------------------------
35 // Generic growing buffer. Add data all you want
36 // ---------------------------------------------------------------------------------
37 struct growing_buffer_struct {
38         char *buf;
39         int n_used;
40         int size;
41 };
42 typedef struct growing_buffer_struct growing_buffer;
43
44 growing_buffer* buffer_init( int initial_num_bytes);
45 int buffer_addchar(growing_buffer* gb, char c);
46 int buffer_add(growing_buffer* gb, char* c);
47 int buffer_reset( growing_buffer* gb);
48 char* buffer_data( growing_buffer* gb);
49 int buffer_free( growing_buffer* gb );
50
51
52 void log_free(); 
53
54 // Utility method
55 void get_timestamp( char buf_36chars[]);
56 double get_timestamp_millis();
57
58 // ---------------------------------------------------------------------------------
59 // Error handling interface.
60 // ---------------------------------------------------------------------------------
61
62 void fatal_handler( char* message, ...);
63 void warning_handler( char* message, ... );
64 void info_handler( char* message, ... );
65 void debug_handler( char* message, ... );
66
67 /** If we return 0 either the log level is less than LOG_ERROR  
68   * or we could not open the log file
69   */
70 int log_init( int log_level, char* log_file );
71
72 // ---------------------------------------------------------------------------------
73 // Config file module
74 // ---------------------------------------------------------------------------------
75 struct config_reader_struct {
76         xmlDocPtr config_doc;
77         xmlXPathContextPtr xpathCx;
78         char* name;
79         struct config_reader_struct* next;
80 };
81 typedef struct config_reader_struct config_reader;
82 config_reader* conf_reader;
83
84 //void config_reader_init( char* config_file );
85 void config_reader_init( char* name, char* config_file );
86
87 void config_reader_free();
88
89 // allocastes a char*. FREE me.
90 //char* config_value( const char* xpath_query, ... );
91 char* config_value( const char* config_name, const char* xp_query, ... );
92 //char* config_value( config_reader* reader, const char* xp_query, ... );
93
94 #endif