xml_example.odin 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package xml_example
  2. import "core:encoding/xml"
  3. import "core:os"
  4. import "core:mem"
  5. import "core:fmt"
  6. import "core:time"
  7. import "core:strings"
  8. import "core:hash"
  9. example :: proc() {
  10. using fmt
  11. doc: ^xml.Document
  12. err: xml.Error
  13. DOC :: #load("../../../../tests/core/assets/XML/unicode.xml")
  14. parse_duration: time.Duration
  15. {
  16. time.SCOPED_TICK_DURATION(&parse_duration)
  17. doc, err = xml.parse(DOC, xml.Options{flags={.Ignore_Unsupported}})
  18. }
  19. defer xml.destroy(doc)
  20. ms := time.duration_milliseconds(parse_duration)
  21. speed := (f64(1000.0) / ms) * f64(len(DOC)) / 1_024.0 / 1_024.0
  22. fmt.printf("Parse time: %v bytes in %.2f ms (%.2f MiB/s).\n", len(DOC), ms, speed)
  23. if err != .None {
  24. printf("Load/Parse error: %v\n", err)
  25. if err == .File_Error {
  26. println("\"unicode.xml\" not found. Did you run \"tests\\download_assets.py\"?")
  27. }
  28. os.exit(1)
  29. }
  30. println("\"unicode.xml\" loaded and parsed.")
  31. charlist, charlist_ok := xml.find_child_by_ident(doc.root, "charlist")
  32. if !charlist_ok {
  33. eprintln("Could not locate top-level `<charlist>` tag.")
  34. os.exit(1)
  35. }
  36. printf("Found `<charlist>` with %v children.\n", len(charlist.children))
  37. crc32 := doc_hash(doc)
  38. printf("[%v] CRC32: 0x%08x\n", "🎉" if crc32 == 0xcaa042b9 else "🤬", crc32)
  39. }
  40. doc_hash :: proc(doc: ^xml.Document, print := false) -> (crc32: u32) {
  41. buf: strings.Builder
  42. defer strings.destroy_builder(&buf)
  43. w := strings.to_writer(&buf)
  44. xml.print(w, doc)
  45. tree := strings.to_string(buf)
  46. if print { fmt.println(tree) }
  47. return hash.crc32(transmute([]u8)tree)
  48. }
  49. main :: proc() {
  50. using fmt
  51. track: mem.Tracking_Allocator
  52. mem.tracking_allocator_init(&track, context.allocator)
  53. context.allocator = mem.tracking_allocator(&track)
  54. example()
  55. if len(track.allocation_map) > 0 {
  56. println()
  57. for _, v in track.allocation_map {
  58. printf("%v Leaked %v bytes.\n", v.location, v.size)
  59. }
  60. }
  61. println("Done and cleaned up!")
  62. }