• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 // Copyright (C) 2008-2012 IBM Corporation and Others. All Rights Reserved.
4 #include <stdio.h>
5 #include "xmlout.h"
6 #include <unistd.h>
7 #include <stdlib.h>
8 
XMLFile(FILE * f)9 XMLFile::XMLFile(FILE *f) {
10     file = f;
11     level = 0;
12 }
13 
~XMLFile()14 XMLFile::~XMLFile()
15 {
16 
17 }
18 
write(const char * s)19 void XMLFile::write(const char *s) {
20     fputs(s, file);
21 }
22 
writeln(const char * s)23 void XMLFile::writeln(const char *s) {
24     writeIndent();
25     write(s);
26     write("\n");
27 }
28 
indent(const char * s,bool single)29 int XMLFile::indent(const char *s, bool single) {
30     int oldLevel = level;
31     writeln(s);
32     level++;
33     if(single) {
34         level--;
35     }
36     return oldLevel;
37 }
outdent(const char * s)38 int XMLFile::outdent(const char *s) {
39     level--;
40     writeln(s);
41     return level;
42 }
43 
writeIndent()44 void XMLFile::writeIndent() {
45     for(int i=0;i<level;i++) {
46         write("\t");
47     }
48 }
49 
XMLElement(XMLFile & f,const char * name,const char * attribs,bool single)50 XMLElement::XMLElement(XMLFile &f, const char *name, const char *attribs, bool single) : file(f), name(name), single(single) {
51     char outs[200];
52     if(attribs!=NULL) {
53         sprintf(outs,"<%s %s", name, attribs);
54     } else {
55         sprintf(outs, "<%s", name);
56     }
57     if(single) {
58         strcat(outs, "/>");
59     } else {
60         strcat(outs, ">");
61     }
62     oldlevel = file.indent(outs, single);
63 }
64 
~XMLElement()65 XMLElement::~XMLElement() {
66     if(!single) {
67         char outs[200];
68         sprintf(outs,"</%s>", name);
69         int newlevel = file.outdent(outs);
70         if(newlevel != oldlevel) {
71             fprintf(stderr, "@@@ ERROR: elemet %s popped out to level %d but expected %d. Abort.\n", name, newlevel, oldlevel);
72             fflush(stderr);
73             abort();
74         }
75     }
76 }
77 
78 
79 
80