1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use data::{NBT, NBTFile};
use errors::Result;
use byteorder::WriteBytesExt;
use std::io::Write;
/// Given an NBT file, write it to the writer in the pretty text format
pub fn write_file<W: Write>(w: &mut W, file: &NBTFile) -> Result<()> {
write!(w, "{}", file.compression.to_str())?;
write_tag(w, &file.root, 0, true)?;
Ok(())
}
fn write_tag<W: Write>(w: &mut W,
tag: &NBT,
indent: u64,
compound: bool)
-> Result<()> {
match tag {
&NBT::End => (),
&NBT::Byte(x) => {
if compound {
write!(w, " ")?;
}
writeln!(w, "{}", x)?;
},
&NBT::Short(x) => {
if compound {
write!(w, " ")?;
}
writeln!(w, "{}", x)?;
},
&NBT::Int(x) => {
if compound {
write!(w, " ")?;
}
writeln!(w, "{}", x)?;
},
&NBT::Long(x) => {
if compound {
write!(w, " ")?;
}
writeln!(w, "{}", x)?;
},
&NBT::Float(x) => {
if compound {
write!(w, " ")?;
}
writeln!(w, "{}", x)?;
},
&NBT::Double(x) => {
if compound {
write!(w, " ")?;
}
writeln!(w, "{}", x)?;
},
&NBT::ByteArray(ref x) => {
writeln!(w, " {}", x.len())?;
for val in x {
write_indent(w, indent)?;
writeln!(w, "{}", val)?;
}
},
&NBT::String(ref x) => {
if compound {
write!(w, " ")?;
}
writeln!(w,
r#""{}""#,
/* Order is important here */
x.replace(r"\", r"\\").replace(r#"""#, r#"\""#))?
},
&NBT::List(ref x) => {
/* If the list has length 0, then it just defaults to type "End". */
let tag_type = if x.len() > 0 {
x[0].type_string()
} else {
"End"
};
writeln!(w, " {} {}", tag_type, x.len())?;
for val in x {
match val {
&NBT::Compound(..) => (),
_ => write_indent(w, indent)?,
}
write_tag(w, val, indent + 1, false)?;
}
},
&NBT::Compound(ref x) => {
if compound {
writeln!(w, "")?;
}
for &(ref key, ref val) in x {
write_indent(w, indent)?;
w.write_all(val.type_string().as_bytes())?;
write!(w,
r#" "{}""#,
/* Order is important here */
key.replace(r"\", r"\\").replace(r#"""#, r#"\""#))?;
write_tag(w, val, indent + 1, true)?;
}
write_indent(w, indent)?;
writeln!(w, "End")?;
},
&NBT::IntArray(ref x) => {
writeln!(w, " {}", x.len())?;
for val in x {
write_indent(w, indent)?;
writeln!(w, "{}", val)?;
}
},
}
Ok(())
}
fn write_indent<W: Write>(w: &mut W, indent: u64) -> Result<()> {
for _ in 0..indent {
/* 9 = tab character */
w.write_u8(9)?;
}
Ok(())
}