example.odin 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //+ignore
  2. package gzip
  3. import "core:compress/gzip"
  4. import "core:bytes"
  5. import "core:os"
  6. // Small GZIP file with fextra, fname and fcomment present.
  7. @private
  8. TEST: []u8 = {
  9. 0x1f, 0x8b, 0x08, 0x1c, 0xcb, 0x3b, 0x3a, 0x5a,
  10. 0x02, 0x03, 0x07, 0x00, 0x61, 0x62, 0x03, 0x00,
  11. 0x63, 0x64, 0x65, 0x66, 0x69, 0x6c, 0x65, 0x6e,
  12. 0x61, 0x6d, 0x65, 0x00, 0x54, 0x68, 0x69, 0x73,
  13. 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x63, 0x6f,
  14. 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x2b, 0x48,
  15. 0xac, 0xcc, 0xc9, 0x4f, 0x4c, 0x01, 0x00, 0x15,
  16. 0x6a, 0x2c, 0x42, 0x07, 0x00, 0x00, 0x00,
  17. };
  18. main :: proc() {
  19. // Set up output buffer.
  20. buf: bytes.Buffer;
  21. defer bytes.buffer_destroy(&buf);
  22. stdout :: proc(s: string) {
  23. os.write_string(os.stdout, s);
  24. }
  25. stderr :: proc(s: string) {
  26. os.write_string(os.stderr, s);
  27. }
  28. args := os.args;
  29. if len(args) < 2 {
  30. stderr("No input file specified.\n");
  31. err := gzip.load(TEST, &buf);
  32. if gzip.is_kind(err, gzip.E_General.OK) {
  33. stdout("Displaying test vector: ");
  34. stdout(bytes.buffer_to_string(&buf));
  35. stdout("\n");
  36. }
  37. }
  38. // The rest are all files.
  39. args = args[1:];
  40. err: gzip.Error;
  41. for file in args {
  42. if file == "-" {
  43. // Read from stdin
  44. s := os.stream_from_handle(os.stdin);
  45. err = gzip.load(s, &buf);
  46. } else {
  47. err = gzip.load(file, &buf);
  48. }
  49. if !gzip.is_kind(err, gzip.E_General.OK) {
  50. if gzip.is_kind(err, gzip.E_General.File_Not_Found) {
  51. stderr("File not found: ");
  52. stderr(file);
  53. stderr("\n");
  54. os.exit(1);
  55. }
  56. stderr("GZIP returned an error.\n");
  57. os.exit(2);
  58. }
  59. stdout(bytes.buffer_to_string(&buf));
  60. }
  61. os.exit(0);
  62. }