cgf.go 753 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package cgf
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "os"
  7. "git.wecise.com/wecise/cgimport/cgf/reader"
  8. )
  9. func Import(filepath string) (blockcount int, err error) {
  10. f, e := os.Open(filepath)
  11. if e != nil {
  12. return blockcount, e
  13. }
  14. defer f.Close()
  15. return importReader(f)
  16. }
  17. func importReader(buf io.Reader) (blockcount int, err error) {
  18. br := reader.NewBlockReader(buf)
  19. for {
  20. block, e := br.ReadBlock()
  21. if e != nil {
  22. return blockcount, e
  23. }
  24. if block == nil {
  25. return
  26. }
  27. e = importBlock(block)
  28. if e != nil {
  29. return blockcount, e
  30. }
  31. blockcount++
  32. }
  33. }
  34. func importBlock(block map[string]any) (err error) {
  35. bs, e := json.MarshalIndent(block, "", " ")
  36. if e != nil {
  37. return e
  38. }
  39. fmt.Println("import:", string(bs))
  40. return
  41. }