gambuzzi

reader.go

May 11th, 2015
51
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package rpgkata
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "os"
  7.     "strings"
  8. )
  9.  
  10. type StringOrArray struct {
  11.     Type      int //1 = string, 2 = array
  12.     Thestring string
  13.     Thearray  []*StringOrArray
  14. }
  15.  
  16. func ReadLine(line string) *StringOrArray {
  17.     thisline := new(StringOrArray)
  18.     thisline.Type = 2
  19.  
  20.     s := ""
  21.     level := 0
  22.     var prev_level *StringOrArray
  23.     curent_level := thisline
  24.     skipnextcomma := false
  25.  
  26.     for i := 0; i < len(line); i++ {
  27.         if string(line[i]) == "," {
  28.             if skipnextcomma {
  29.                 skipnextcomma = false
  30.             } else {
  31.                 sa := new(StringOrArray)
  32.                 sa.Thestring = strings.Trim(s, " ")
  33.                 sa.Type = 1
  34.                 curent_level.Thearray = append(curent_level.Thearray, sa)
  35.                 curent_level.Type = 2
  36.                 s = ""
  37.             }
  38.         } else if string(line[i]) == "(" {
  39.             level++
  40.             new_level := new(StringOrArray)
  41.             curent_level.Thearray = append(curent_level.Thearray, new_level)
  42.             prev_level = curent_level
  43.             curent_level = new_level
  44.         } else if string(line[i]) == ")" {
  45.             level--
  46.             sa := new(StringOrArray)
  47.             sa.Thestring = strings.Trim(s, " ")
  48.             sa.Type = 1
  49.             curent_level.Thearray = append(curent_level.Thearray, sa)
  50.             curent_level = prev_level
  51.             s = ""
  52.             skipnextcomma = true
  53.         } else {
  54.             s += string(line[i])
  55.         }
  56.     }
  57.  
  58.     sa := new(StringOrArray)
  59.     sa.Thestring = strings.Trim(s, " ")
  60.     sa.Type = 1
  61.     curent_level.Thearray = append(curent_level.Thearray, sa)
  62.  
  63.     return thisline
  64. }
  65.  
  66. func ReadFile(filename string) *StringOrArray {
  67.     infile, err := os.Open(filename)
  68.  
  69.     if err != nil {
  70.         fmt.Println(err)
  71.         os.Exit(1)
  72.     }
  73.  
  74.     defer infile.Close()
  75.  
  76.     reader := bufio.NewScanner(infile)
  77.  
  78.     ret := new(StringOrArray)
  79.     ret.Type = 2 // array
  80.  
  81.     for reader.Scan() {
  82.         line := reader.Text()
  83.  
  84.         thisline := ReadLine(line)
  85.  
  86.         ret.Thearray = append(ret.Thearray, thisline)
  87.     }
  88.     return ret
  89. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×