initial load of upstream version 1.06.32
[xmlrpc-c] / src / trace.c
1 #include <ctype.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <assert.h>
5
6 #include "xmlrpc-c/base_int.h"
7 #include "xmlrpc-c/string_int.h"
8
9
10 static size_t
11 nextLineSize(const char * const string,
12              size_t       const startPos,
13              size_t       const stringSize) {
14 /*----------------------------------------------------------------------------
15    Return the length of the line that starts at offset 'startPos' in the
16    string 'string', which is 'stringSize' characters long.
17
18    'string' in not NUL-terminated.
19    
20    A line begins at beginning of string or after a newline character and
21    runs through the next newline character or end of string.  The line
22    includes the newline character at the end, if any.
23 -----------------------------------------------------------------------------*/
24     size_t i;
25
26     for (i = startPos; i < stringSize && string[i] != '\n'; ++i);
27
28     if (i < stringSize)
29         ++i;  /* Include the newline */
30
31     return i - startPos;
32 }
33
34
35
36 void
37 xmlrpc_traceXml(const char * const label, 
38                 char         const xml[],
39                 unsigned int const xmlLength) {
40
41     if (getenv("XMLRPC_TRACE_XML")) {
42         size_t cursor;  /* Index into xml[] */
43
44         fprintf(stderr, "%s:\n\n", label);
45
46         for (cursor = 0; cursor < xmlLength; ) {
47             /* Print one line of XML */
48
49             size_t const lineSize = nextLineSize(xml, cursor, xmlLength);
50             const char * const xmlPrintableLine =
51                 xmlrpc_makePrintable_lp(&xml[cursor], lineSize);
52
53             fprintf(stderr, "%s\n", xmlPrintableLine);
54
55             cursor += lineSize;
56
57             xmlrpc_strfree(xmlPrintableLine);
58         }
59         fprintf(stderr, "\n");
60     }
61 }
62