00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00069 #ifndef AEGIS_COMMON_H
00070 #define AEGIS_COMMON_H
00071
00072 #include <sys/types.h>
00073 #include <unistd.h>
00074 #include <stdlib.h>
00075 #include <syslog.h>
00076
00077 #if 0
00078
00085 #define AEGIS_DEBUG_ENABLED
00086 #endif
00087
00096 #define RAWDATA_PTR void*
00097
00108 #define RAWDATA_RPTR void**
00109
00110 #ifdef __cplusplus
00111 #include <expat.h>
00112 #include <string>
00113 #include <vector>
00114 #include <map>
00115
00116 extern "C" {
00117
00125 bool absolute_pathname(const char* name, std::string& to_this);
00126
00133 int process_name_of_pid(pid_t of_pid, std::string& to_this);
00134
00140 bool process_name(std::string& to_this);
00141
00148 void append_hex(std::string& to_this, unsigned char* dta, unsigned len);
00149
00150 #else // Not C++
00151
00155 #define bool int
00156 #endif
00157
00162 #define PATH_SEP "/"
00163
00169 bool file_exists(const char* name);
00170
00176 bool directory_exists(const char* name);
00177
00187 int create_directory(const char* path, int mode);
00188
00199 typedef int aegis_callback(int nbr, void* item, void* context);
00200
00211 int iterate_files(const char* in_directory,
00212 const char* matching_names,
00213 aegis_callback* cb_func,
00214 void* ctx);
00215
00221 #define DLOG_TO_CONSOLE 17
00222
00235 void init_dlogger(int enabled, int use_signal, int other_signal);
00236
00240 void dlog_message(const char* format, ...);
00241
00254 const char* dynhex(const void* d, size_t len);
00255
00263 char* base64_encode(const RAWDATA_PTR data, size_t len);
00264
00272 size_t base64_decode(const char* string, RAWDATA_RPTR to_buf);
00273
00274 #ifdef __cplusplus
00275 }
00276
00277
00282 namespace aegis {
00283
00291 class c_xmlnode
00292 {
00293 public:
00299 c_xmlnode (c_xmlnode* of_parent, const char* of_name);
00300
00304 ~c_xmlnode ();
00305
00311 c_xmlnode* parent();
00312
00317 const char* name ();
00318
00326 std::string as_string (bool add_linebreaks,
00327 int indent_width,
00328 int indent_level);
00329
00335 void append_content (const char* data, int len);
00336
00341 void append_content (const char* data);
00342
00348 void append_file (const char* file_name);
00349
00358 const char* content ();
00359
00369 void append_attribute (const char* name, const char* value);
00370
00379 void append_attribute (const char* name, long value);
00380
00385 int nbrof_attributes ();
00386
00392 const char* attribute_name (unsigned of_pos);
00393
00399 const char* attribute_value (unsigned of_pos);
00400
00409 const char* attribute(const char* name,
00410 bool required,
00411 const char* defval);
00412
00419 void remove_attribute (const char* name);
00420
00428 c_xmlnode* navigate(const char* to_xpath, bool required);
00429
00437 c_xmlnode* navigate(const char* to_xpath, const char* default_content);
00438
00448 c_xmlnode* append_child (const char* element_name);
00449
00454 int nbrof_children ();
00455
00462 c_xmlnode* child (unsigned of_pos);
00463
00474 c_xmlnode* child (const char* of_name, bool required);
00475
00480 void remove_child (unsigned of_pos);
00481
00490 void set_cdata(bool to_this);
00491
00495 void trim_whitespace();
00496
00502 std::string xpath();
00503
00507 void reset();
00508
00509 private:
00510 c_xmlnode* navigate(const char* to_xpath, bool required, const char* default_content);
00511 void xml_escaped(const char *from, std::string& to);
00512
00513 std::string m_tagname;
00514 std::string m_content;
00515 c_xmlnode* m_parent;
00516 std::map<std::string,std::string> m_attributes;
00517 std::vector<c_xmlnode*> m_children;
00518 bool is_cdata;
00519 bool cdata_ended;
00520 };
00521
00529 class c_xmldoc
00530 {
00531 public:
00535 c_xmldoc ();
00536
00540 ~c_xmldoc ();
00541
00546 void parse_file (const char* file_name);
00547
00554 void parse_string (const char* xml_as_string, int length);
00555
00564 void release_parser();
00565
00575 void release_string_buffer ();
00576
00583 c_xmlnode* create (const char* root_node_name);
00584
00590 c_xmlnode* root();
00591
00599 std::string as_string (bool pretty_printing);
00600
00604 void release_content(void);
00605
00612 bool save(const char* to_file);
00613
00614
00615
00616
00617
00618
00619
00628 void xml_element_start(const char* element_name, const char** attributes);
00629
00636 void xml_element_end(const char* element_name);
00637
00645 void xml_character_data(const char* data, const int len);
00646
00652 void xml_cdata_start();
00653
00659 void xml_cdata_end();
00660
00668 bool trim_whitespace;
00669
00670 private:
00671 void init_parser();
00672 void xml_parsing_error();
00673
00674 XML_Parser expat_parser;
00675 c_xmlnode* root_node;
00676 c_xmlnode* cur_node;
00677 char* xml_str_buf;
00678 std::string m_filename;
00679 };
00680
00687 #define DIGESTLEN 20
00688
00695 typedef struct aegis_digest_t {
00696 unsigned char d[DIGESTLEN];
00697 } AEGIS_DIGEST_T;
00698
00699 }
00700
00701 #endif
00702
00703 #ifndef _STRING_H
00704 #include <string.h>
00705 #endif
00706
00712 #define bare_file_name(s) strrchr(s,'/')?strrchr(s,'/')+1:s
00713
00729 #ifdef AEGIS_SHOW_ERRORS
00730 #define AEGIS_ERROR(format,args...) \
00731 do { \
00732 fprintf(stderr, "%s(%d): ERROR: " format "\n", \
00733 bare_file_name(__FILE__), __LINE__ \
00734 ,##args); \
00735 fflush(stderr); \
00736 } while (0)
00737 #else
00738 #ifdef AEGIS_DEBUG_ENABLED
00739 #define AEGIS_ERROR(format,args...) \
00740 do { \
00741 dlog_message("<0>%s(%d)[%d]: ERROR " format, \
00742 bare_file_name(__FILE__), __LINE__, \
00743 getpid() ,##args); \
00744 syslog(LOG_ERR, "%s(%d): ERROR " format, \
00745 bare_file_name(__FILE__), __LINE__ \
00746 ,##args); \
00747 } while (0)
00748 #else
00749 #define AEGIS_ERROR(format,args...) \
00750 do { \
00751 syslog(LOG_ERR, "%s(%d): ERROR " format, \
00752 bare_file_name(__FILE__), __LINE__ \
00753 ,##args); \
00754 } while (0)
00755 #endif
00756 #endif
00757
00767 #ifdef AEGIS_DEBUG_ENABLED
00768 #define AEGIS_DEBUG(level,format,args...) \
00769 do { \
00770 dlog_message("<%d>%s(%d)[%d]: " format, level, bare_file_name(__FILE__), __LINE__, \
00771 getpid() ,##args); \
00772 } while (0)
00773
00779 #define AEGIS_ENTER \
00780 do { \
00781 dlog_message("<2>%s(%d)[%d]: => %s", bare_file_name(__FILE__), __LINE__, getpid(), \
00782 __PRETTY_FUNCTION__); \
00783 } while (0)
00784
00790 #define AEGIS_EXIT \
00791 do { \
00792 dlog_message("<2>%s(%d)[%d]: <= %s", bare_file_name(__FILE__), __LINE__, getpid(), \
00793 __PRETTY_FUNCTION__); \
00794 } while (0)
00795 #else
00796 #define AEGIS_DEBUG(level,format,args...)
00797 #define AEGIS_ENTER
00798 #define AEGIS_EXIT
00799 #endif
00800
00807 #define GETENV(name,deflt) ({char* c = getenv(name); c?c:deflt;})
00808
00809 #endif