"Fossies" - the Fresh Open Source Software Archive 
Member "tcpflow-1.6.1/src/be13_api/dfxml/src/dfxml_writer.h" (19 Feb 2021, 7977 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "dfxml_writer.h" see the
Fossies "Dox" file reference documentation.
1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 * Simson's XML output class.
4 * Include this AFTER your config file with the HAVE statements.
5 * Optimized for DFXML generation.
6 */
7
8 #ifndef _DFXML_WRITER_H_
9 #define _DFXML_WRITER_H_
10
11 #ifndef __STDC_FORMAT_MACROS
12 #define __STDC_FORMAT_MACROS
13 #endif
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <time.h>
18 #include <sys/types.h>
19 #include <sys/time.h>
20 #include <inttypes.h>
21
22 /* c++ */
23 #include <fstream>
24 #include <map>
25 #include <set>
26 #include <sstream>
27 #include <stack>
28 #include <string>
29
30 #ifdef HAVE_PTHREAD
31 #include <pthread.h>
32 #endif
33
34 #ifdef HAVE_SYS_CDEFS_H
35 #include <sys/cdefs.h>
36 #endif
37
38 #ifdef HAVE_SYS_RESOURCE_H
39 #include <sys/resource.h>
40 #endif
41
42 #ifdef HAVE_PWD_H
43 #include <pwd.h>
44 #endif
45
46 #ifdef HAVE_SYS_UTSNAME_H
47 #include <sys/utsname.h>
48 #endif
49
50 #ifndef __BEGIN_DECLS
51 #if defined(__cplusplus)
52 #define __BEGIN_DECLS extern "C" {
53 #define __END_DECLS }
54 #else
55 #define __BEGIN_DECLS
56 #define __END_DECLS
57 #endif
58 #endif
59
60 #ifdef HAVE_LIBTSK3
61 #include <tsk3/libtsk.h>
62 #endif
63
64 #ifdef __cplusplus
65 #include "cppmutex.h"
66 class dfxml_writer {
67 private:
68 /*** neither copying nor assignment is implemented ***
69 *** We do this by making them private constructors that throw exceptions. ***/
70 dfxml_writer(const dfxml_writer &);
71 dfxml_writer &operator=(const dfxml_writer &);
72 /****************************************************************/
73
74 public:
75 typedef std::map<std::string,std::string> strstrmap_t;
76 typedef std::set<std::string> stringset;
77 typedef std::set<std::string> tagid_set_t;
78 private:
79
80 #ifdef HAVE_PTHREAD
81 pthread_mutex_t M; // mutext protecting out
82 #else
83 int M; // placeholder
84 #endif
85 std::fstream outf;
86 std::ostream *out; // where it is being written; defaulst to stdout
87 stringset tags; // XML tags
88 std::stack<std::string>tag_stack;
89 std::string tempfilename;
90 std::string tempfile_template;
91 struct timeval t0;
92 struct timeval t_last_timestamp; // for creating delta timestamps
93 bool make_dtd;
94 std::string outfilename;
95 void write_doctype(std::fstream &out);
96 void write_dtd();
97 void verify_tag(std::string tag);
98 void spaces(); // print spaces corresponding to tag stack
99 //static std::string xml_PRId32; // for compiler bug
100 //static std::string xml_PRIu32; // for compiler bug
101 //static std::string xml_PRId64; // for compiler bug
102 //static std::string xml_PRIu64; // for compiler bug
103 bool oneline;
104 public:
105 static std::string make_command_line(int argc,char * const *argv){
106 std::string command_line;
107 for(int i=0;i<argc;i++){
108 // append space separator between arguments
109 if(i>0) command_line.push_back(' ');
110 if (strchr(argv[i],' ') != NULL) {
111 // the argument has a space, so quote the argument
112 command_line.append("\"");
113 command_line.append(argv[i]);
114 command_line.append("\"");
115 } else {
116 // the argument has no space, so append as is
117 command_line.append(argv[i]);
118 }
119 }
120 return command_line;
121 }
122
123
124 dfxml_writer(); // defaults to stdout
125 dfxml_writer(const std::string &outfilename,bool makeDTD); // write to a file, optionally making a DTD
126 virtual ~dfxml_writer(){};
127 void set_tempfile_template(const std::string &temp);
128
129 static std::string xmlescape(const std::string &xml);
130 static std::string xmlstrip(const std::string &xml);
131
132 /** xmlmap turns a map into an XML block */
133 static std::string xmlmap(const strstrmap_t &m,const std::string &outer,const std::string &attrs);
134
135 void close(); // writes the output to the file
136
137 void flush(){outf.flush();}
138 void tagout( const std::string &tag,const std::string &attribute);
139 void push(const std::string &tag,const std::string &attribute);
140 void push(const std::string &tag) {push(tag,"");}
141
142 // writes a std::string as parsed data
143 void puts(const std::string &pdata);
144
145 // writes a std::string as parsed data
146 void printf(const char *fmt,...) __attribute__((format(printf, 2, 3))); // "2" because this is "1"
147 void pop(); // close the tag
148
149 void add_timestamp(const std::string &name);
150 void add_DFXML_build_environment();
151 static void cpuid(uint32_t op, unsigned long *eax, unsigned long *ebx,unsigned long *ecx, unsigned long *edx);
152 void add_cpuid();
153 void add_DFXML_execution_environment(const std::string &command_line);
154 void add_DFXML_creator(const std::string &program,const std::string &version,
155 const std::string &svn_r,
156 const std::string &command_line){
157 push("creator","version='1.0'");
158 xmlout("program",program);
159 xmlout("version",version);
160 if(svn_r.size()>0) xmlout("svn_version",svn_r);
161 add_DFXML_build_environment();
162 add_DFXML_execution_environment(command_line);
163 pop(); // creator
164 }
165 void add_rusage();
166 void set_oneline(bool v);
167 const std::string &get_outfilename() const {return outfilename; } ;
168
169 /********************************
170 *** THESE ARE ALL THREADSAFE ***
171 ********************************/
172 void comment(const std::string &comment);
173 void xmlprintf(const std::string &tag,const std::string &attribute,const char *fmt,...)
174 __attribute__((format(printf, 4, 5))); // "4" because this is "1";
175 void xmlout( const std::string &tag,const std::string &value, const std::string &attribute, const bool escape_value);
176
177 /* These all call xmlout or xmlprintf which already has locking, so these are all threadsafe! */
178 void xmlout( const std::string &tag,const std::string &value){ xmlout(tag,value,"",true); }
179 // void xmlout( const std::string &tag,const int value){ xmlprintf(tag,"","%d",value); }
180 void xmloutl(const std::string &tag,const long value){ xmlprintf(tag,"","%ld",value); }
181 #ifdef WIN32
182 void xmlout( const std::string &tag,const int32_t value){ xmlprintf(tag,"","%I32d",value); }
183 void xmlout( const std::string &tag,const uint32_t value){ xmlprintf(tag,"","%I32u",value); }
184 void xmlout( const std::string &tag,const int64_t value){ xmlprintf(tag,"","%I64d",value); }
185 void xmlout( const std::string &tag,const uint64_t value){ xmlprintf(tag,"","%I64u",value); }
186 #else
187 void xmlout( const std::string &tag,const int32_t value){ xmlprintf(tag,"","%" PRId32,value); }
188 void xmlout( const std::string &tag,const uint32_t value){ xmlprintf(tag,"","%" PRIu32,value); }
189 void xmlout( const std::string &tag,const int64_t value){ xmlprintf(tag,"","%" PRId64,value); }
190 void xmlout( const std::string &tag,const uint64_t value){ xmlprintf(tag,"","%" PRIu64,value); }
191 #ifdef __APPLE__
192 void xmlout( const std::string &tag,const size_t value){ xmlprintf(tag,"","%" PRIu64,(unsigned long long)value); }
193 #endif
194 #endif
195 void xmlout( const std::string &tag,const double value){ xmlprintf(tag,"","%f",value); }
196 void xmlout( const std::string &tag,const struct timeval &ts) {
197 xmlprintf(tag,"","%d.%06d",(int)ts.tv_sec, (int)ts.tv_usec);
198 }
199 static std::string to8601(const struct timeval &ts) {
200 struct tm tm;
201 char buf[64];
202 #ifdef HAVE_GMTIME_R
203 gmtime_r(&ts.tv_sec,&tm);
204 #else
205 time_t t = ts.tv_sec;
206 struct tm *tmp;
207 tmp = gmtime(&t);
208 if(!tmp) return std::string("INVALID");
209 tm = *tmp;
210 #endif
211 strftime(buf,sizeof(buf),"%Y-%m-%dT%H:%M:%S",&tm);
212 if(ts.tv_usec>0){
213 int len = strlen(buf);
214 snprintf(buf+len,sizeof(buf)-len,".%06d",(int)ts.tv_usec);
215 }
216 strcat(buf,"Z");
217 return std::string(buf);
218 }
219 };
220 #endif
221
222 #endif