diff options
Diffstat (limited to 'src/stable/tafwriter.cpp')
-rw-r--r-- | src/stable/tafwriter.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/stable/tafwriter.cpp b/src/stable/tafwriter.cpp new file mode 100644 index 0000000..b24bd1e --- /dev/null +++ b/src/stable/tafwriter.cpp | |||
@@ -0,0 +1,114 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/taf.h" | ||
9 | #include "bu/stream.h" | ||
10 | |||
11 | Bu::TafWriter::TafWriter( Bu::Stream &sOut ) : | ||
12 | sOut( sOut ), | ||
13 | iDepth( 0 ) | ||
14 | { | ||
15 | } | ||
16 | |||
17 | Bu::TafWriter::~TafWriter() | ||
18 | { | ||
19 | } | ||
20 | |||
21 | void Bu::TafWriter::ident() | ||
22 | { | ||
23 | for( int j = 0; j < iDepth; j++ ) | ||
24 | sOut.write(" ", 4 ); | ||
25 | } | ||
26 | |||
27 | void Bu::TafWriter::writeGroup( const Bu::TafGroup *pRoot ) | ||
28 | { | ||
29 | ident(); | ||
30 | sOut.write("{", 1 ); | ||
31 | if( pRoot->getName().isSet() ) | ||
32 | writeString( pRoot->getName() ); | ||
33 | sOut.write(":\n", 2 ); | ||
34 | iDepth++; | ||
35 | const Bu::TafGroup::NodeList &nl = pRoot->getChildren(); | ||
36 | for( Bu::TafGroup::NodeList::const_iterator i = nl.begin(); i != nl.end(); i++ ) | ||
37 | { | ||
38 | switch( (*i)->getType() ) | ||
39 | { | ||
40 | case Bu::TafNode::typeGroup: | ||
41 | writeGroup( (Bu::TafGroup *)(*i) ); | ||
42 | break; | ||
43 | |||
44 | case Bu::TafNode::typeProperty: | ||
45 | writeProperty( (Bu::TafProperty *)(*i) ); | ||
46 | break; | ||
47 | |||
48 | case Bu::TafNode::typeComment: | ||
49 | writeComment( (Bu::TafComment *)(*i) ); | ||
50 | break; | ||
51 | } | ||
52 | } | ||
53 | iDepth--; | ||
54 | ident(); | ||
55 | sOut.write("}\n", 2 ); | ||
56 | } | ||
57 | |||
58 | void Bu::TafWriter::writeProperty( const Bu::TafProperty *pProp ) | ||
59 | { | ||
60 | ident(); | ||
61 | if( !pProp->getName().isEmpty() ) | ||
62 | { | ||
63 | writeString( pProp->getName() ); | ||
64 | sOut.write("=", 1 ); | ||
65 | writeString( pProp->getValue() ); | ||
66 | } | ||
67 | else | ||
68 | { | ||
69 | writeString( pProp->getValue() ); | ||
70 | } | ||
71 | sOut.write("\n", 1 ); | ||
72 | } | ||
73 | |||
74 | void Bu::TafWriter::writeComment( const Bu::TafComment *pComment ) | ||
75 | { | ||
76 | ident(); | ||
77 | if( pComment->isEOLStyle() ) | ||
78 | { | ||
79 | sOut.write("//", 2 ); | ||
80 | sOut.write( pComment->getText().getStr(), pComment->getText().getSize() ); | ||
81 | sOut.write("\n", 1 ); | ||
82 | } | ||
83 | else | ||
84 | { | ||
85 | sOut.write("/*", 2 ); | ||
86 | sOut.write( pComment->getText().getStr(), pComment->getText().getSize() ); | ||
87 | sOut.write("*/ ", 3 ); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | void Bu::TafWriter::writeString( const Bu::String &str ) | ||
92 | { | ||
93 | sOut.write("\"", 1 ); | ||
94 | for( Bu::String::const_iterator s = str.begin(); s != str.end(); s++ ) | ||
95 | { | ||
96 | if( *s == '\"' ) | ||
97 | sOut.write("\\\"", 2 ); | ||
98 | else if( *s == '\\' ) | ||
99 | sOut.write("\\\\", 2 ); | ||
100 | else if( *s < 32 || *s > 126 ) | ||
101 | { | ||
102 | char buf[5]; | ||
103 | sprintf( buf, "\\x%02X", (unsigned char)*s ); | ||
104 | sOut.write(buf, 4 ); | ||
105 | } | ||
106 | else | ||
107 | { | ||
108 | const char buf = *s; | ||
109 | sOut.write( &buf, 1 ); | ||
110 | } | ||
111 | } | ||
112 | sOut.write("\"", 1 ); | ||
113 | } | ||
114 | |||