]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/generic_utils.h
added 'named' configs.
[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
57 // ---------------------------------------------------------------------------------
58 // Error handling interface.
59 // ---------------------------------------------------------------------------------
60
61 void fatal_handler( char* message, ...);
62 void warning_handler( char* message, ... );
63 void info_handler( char* message, ... );
64 void debug_handler( char* message, ... );
65
66 /** If we return 0 either the log level is less than LOG_ERROR  
67   * or we could not open the log file
68   */
69 int log_init( int log_level, char* log_file );
70
71 // ---------------------------------------------------------------------------------
72 // Config file module
73 // ---------------------------------------------------------------------------------
74 struct config_reader_struct {
75         xmlDocPtr config_doc;
76         xmlXPathContextPtr xpathCx;
77         char* name;
78         struct config_reader_struct* next;
79 };
80 typedef struct config_reader_struct config_reader;
81 config_reader* conf_reader;
82
83 //void config_reader_init( char* config_file );
84 void config_reader_init( char* name, char* config_file );
85
86 void config_reader_free();
87
88 // allocastes a char*. FREE me.
89 //char* config_value( const char* xpath_query, ... );
90 char* config_value( const char* config_name, const char* xp_query, ... );
91 //char* config_value( config_reader* reader, const char* xp_query, ... );
92
93 #endif