#include "Report.h" using namespace std; #define REPORT_SPACE 500024U //500kb char *Report::m_xml = 0; Filter Report::fAmps(1, 1, "&", "&"); const char *Report::spiderStatus[] = { "connecting", "written", "reading", "red", "finished", "reconnecting", "waitReconnect", "stopped" }; const char *Report::dcMode[] = { "waitRead", "waitWrite", "waitNowt" }; Report::Report(const SpiderManager *_sm): m_sm(_sm), m_tagNeedsClose(false) { if (!m_xml) m_xml = (char*)mallocCheck(REPORT_SPACE); //500 Kb static report area m_pos = m_xml; *m_pos = 0; //in case no work is done } Report::~Report() {} void Report::write(const char *xml) { while (*m_pos = *xml++) m_pos++; if (m_pos - m_xml > REPORT_SPACE - 1024U) { DEBUGERROR0("Out of memory for the Report"); exit(9); } } void Report::opentag(const char *tagname) { if (m_tagNeedsClose) {*m_pos++='>';m_tagNeedsClose=false;} *m_pos++='<'; write(tagname); m_tagNeedsClose=true; } void Report::closetag(const char *tagname=0) { if (m_tagNeedsClose) { *m_pos++='/';*m_pos++='>'; m_tagNeedsClose=false; } else { *m_pos++='<';*m_pos++='/'; write(tagname); *m_pos++='>'; } *m_pos=0; //zero terminate string } void Report::addAttribute(const char *name, const long int value) { addAttribute(name, (const int)value); } void Report::addAttribute(const char *name, const bool value) { addAttribute(name, (value?"true":"false")); } void Report::addAttribute(const char *name, const size_t value) { addAttribute(name, (int)value); } void Report::addAttribute(const char *name, const int value) { char n[32]; _SNPRINTF(n, 32, "%i", value); addAttribute(name, n); } void Report::addAttribute(const char *name, const double value, const unsigned short accuracy) { char n[32]; _SNPRINTF(n, 32, "%.2f", value); addAttribute(name, n); } void Report::addAttribute(const char *name, in_addr addr) { /* ------------------------ WINDOWS: typedef struct in_addr { union { struct { UCHAR s_b1,s_b2,s_b3,s_b4; } S_un_b; struct { USHORT s_w1,s_w2; } S_un_w; ULONG S_addr; } S_un; #define s_addr S_un.S_addr // can be used for most tcp & ip code #define s_host S_un.S_un_b.s_b2 // host on imp #define s_net S_un.S_un_b.s_b1 // network #define s_imp S_un.S_un_w.s_w2 // imp #define s_impno S_un.S_un_b.s_b4 // imp # #define s_lh S_un.S_un_b.s_b3 // logical host } IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR; ------------------------ UNIX: struct in_addr { u_int32_t s_addr; // IPv4 address in network byte order }; */ //n IS STATICALLY ALLOCATED! char *n = inet_ntoa(addr); addAttribute(name, n); } void Report::addAttribute(const char *name, const char *value, const bool escapeAmpersands) { //caller frees its own inputs if necessary const char *valueEscaped=0; if (!m_tagNeedsClose) 000; //throw exception if (escapeAmpersands) fAmps.replace(value, 0, &valueEscaped); else valueEscaped=value; *m_pos++=' '; write(name); *m_pos++='=';*m_pos++='"'; write(valueEscaped); *m_pos++='"'; if (valueEscaped!=value) free((void*)valueEscaped); } void Report::CDATA(const char *value) { if (m_tagNeedsClose) {*m_pos++='>';m_tagNeedsClose=false;} write(""); }