main.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "bufio"
  4. "compress/gzip"
  5. "encoding/binary"
  6. "flag"
  7. "fmt"
  8. "io"
  9. "log"
  10. "os"
  11. "strings"
  12. "tickserver/server/market"
  13. )
  14. func main() {
  15. flag.Parse()
  16. n := flag.NArg()
  17. if n != 2 {
  18. log.Println("number of parameter must be 2", n)
  19. return
  20. }
  21. file0 := flag.Arg(0)
  22. f0, _ := os.Open(file0)
  23. file1 := flag.Arg(1)
  24. w, err := os.OpenFile(file1, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
  25. if err != nil {
  26. return
  27. }
  28. defer w.Close()
  29. gw := gzip.NewWriter(w)
  30. bw := bufio.NewWriter(gw)
  31. var candle market.Candle
  32. buf := bufio.NewReader(f0)
  33. for {
  34. line, err := buf.ReadString('\n')
  35. line = strings.TrimSpace(line)
  36. if err != nil {
  37. if err != io.EOF {
  38. log.Println(err)
  39. }
  40. break
  41. }
  42. var year, month, day, hour, minute, second int
  43. _, err = fmt.Sscanf(line, "%04d%02d%02d %02d:%02d:%02d {%d %f %f %f %f %f %f}", &year, &month, &day, &hour, &minute, &second, &candle.Timestamp, &candle.Open, &candle.High, &candle.Low, &candle.Close, &candle.RealVolums, &candle.TickVolums)
  44. if err != nil {
  45. log.Println(line, err)
  46. break
  47. }
  48. log.Println(candle)
  49. binary.Write(bw, binary.LittleEndian, candle)
  50. }
  51. bw.Flush()
  52. gw.Close()
  53. }