Facebook
From Aqua Terrapin, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 124
  1. ・・・・
  2.  
  3. type iScanner interface {
  4.         Err() error
  5.         Scan() bool
  6.         Text() string
  7. }
  8.  
  9. func ifuncNewScanner(r io.Reader) iScanner {
  10.         return bufio.NewScanner(r)
  11. }
  12.  
  13. type hookBufio struct {
  14.         NewScanner func(r io.Reader) iScanner
  15. }
  16.  
  17. type iFile interface {
  18.         Close() error
  19.         WriteString(s string) (n int, err error)
  20.         Read(p []byte) (n int, err error)
  21. }
  22.  
  23. func ifuncOpen(name string) (iFile, error) {
  24.         return os.Open(name)
  25. }
  26. func ifuncCreate(name string) (iFile, error) {
  27.         return os.Create(name)
  28. }
  29.  
  30. type hookOs struct {
  31.         Open   func(name string) (iFile, error)
  32.         Create func(name string) (iFile, error)
  33. }
  34. type hook struct {
  35.         bufio *hookBufio
  36.         os    *hookOs
  37. }
  38.  
  39. // textLines はテキストを格納します。
  40. // [TODO] 文字エンコーディング関係機能
  41. type textLines struct {
  42.         // 文字列スライス
  43.         lines []string
  44.         // フック
  45.         hook *hook
  46. }
  47.  
  48.  
  49. ・・・・
  50.  
  51.  
  52. func (t *textLines) LoadFrom(filename string) error {
  53.         f, err := t.hook.os.Open(filename)
  54.         if err != nil {
  55.                 return fmt.Errorf("os.Open(%s) return %v", filename, err)
  56.         }
  57.         defer f.Close()
  58.  
  59.         s := t.hook.bufio.NewScanner(f)
  60.         for s.Scan() {
  61.                 t.lines = append(t.lines, s.Text())
  62.         }
  63.         err = s.Err()
  64.         if err != nil {
  65.                 return fmt.Errorf("scanner.Scan(%s) return %v", filename, err)
  66.         }
  67.         return err
  68. }