Skip to content

Instantly share code, notes, and snippets.

@radare
Last active May 8, 2020 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radare/bb2c8fa4d0c22586c68de660afa5b388 to your computer and use it in GitHub Desktop.
Save radare/bb2c8fa4d0c22586c68de660afa5b388 to your computer and use it in GitHub Desktop.
vxml
struct Toxml {
mut:
str string
stack []string
}
fn toxml_new() &Toxml {
x := &Toxml{}
x.str = ''
x.stack = []
return x
}
fn attributes(kvs map[string]string) string {
mut a := ''
for k,v in kvs {
a += ' $k="$v"'
}
return a
}
fn (x &Toxml)indent() string {
return ' '.repeat(x.stack.len)
}
fn xmlstr(s string) string {
return ''
}
fn (x &Toxml)body(msg string) {
x.str += '$msg\n'
}
fn (x &Toxml)openclose(tag string, kvs map[string]string) {
x.llopen(tag, kvs, '/')
}
fn (x &Toxml)prolog(tag string, kvs map[string]string) {
x.llopen('?' + tag, kvs, '?')
}
fn (x &Toxml)comment(tag string) {
nokv := map[string]string{}
x.llopen('!--', nokv, '--')
}
fn (x &Toxml)open(tag string, kvs map[string]string) {
x.llopen(tag, kvs, '')
x.stack.push(tag)
}
fn (x &Toxml)llopen(tag string, kvs map[string]string, ch string) {
attrs := attributes(kvs)
instr := x.indent()
x.str += '$instr<$tag$attrs$ch>\n'
}
fn (x &Toxml)pop() string {
// return *&string(x.stack.pop())
tag := x.stack.last()
x.stack.delete(x.stack.len - 1)
return tag
}
fn (x &Toxml)close() {
tag := x.pop()
instr := x.indent()
x.str += '$instr</$tag>\n'
}
fn (x &Toxml)finish() {
for x.stack.len > 0 {
x.close()
}
}
fn main() {
x := toxml_new()
x.prolog('xml', {
'version': '1.0'
'encoding': 'UTF-8'
})
x.open('testsuites', {
'id': '20150612_170402'
'name': 'new-config'
'tests': '1234'
'failures': '1234'
'time': '1234'
})
x.open('testsuite', {
'id': '20150612_170402'
'name': 'new-config'
'time': '1234'
})
x.open('testcase', {
'id': '20150612_170402'
'name': 'new-config'
'time': '1234'
})
x.open('failure', {
'message': 'Warning message here'
'type': 'WARNING'
})
x.body('Warning')
x.close()
x.finish()
println(x.str)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment