fix-labels.go
1 package util 2 3 import ( 4 "bufio" 5 "fmt" 6 "io" 7 "os" 8 "regexp" 9 "strings" 10 11 "github.com/thunderbrewhq/binana/go/symbols" 12 ) 13 14 type FixLabelsParams struct { 15 // the input symbols file 16 Input string 17 // the list of fixed symbol names 18 FixList string 19 // regex to capture a word that will be matched against the fixlist 20 Capture string 21 // pattern to insert the match into so it may be checked against the fixlist 22 FixPattern string 23 } 24 25 func read_fixlist(path string) ([]string, error) { 26 file, err := os.Open(path) 27 if err != nil { 28 return nil, err 29 } 30 defer file.Close() 31 32 var lines []string 33 scanner := bufio.NewScanner(file) 34 for scanner.Scan() { 35 lines = append(lines, scanner.Text()) 36 } 37 return lines, scanner.Err() 38 } 39 40 func FixLabels(params *FixLabelsParams) { 41 capture := regexp.MustCompile(params.Capture) 42 var ( 43 input io.Reader 44 input_file *os.File 45 fixlist []string 46 table symbols.Table 47 err error 48 ) 49 50 if params.Input != "" { 51 input_file, err = os.Open(params.Input) 52 if err != nil { 53 return 54 } 55 input = input_file 56 } else { 57 input = os.Stdin 58 } 59 60 if params.FixList == "" { 61 panic("must have fix list") 62 } 63 64 fixlist, err = read_fixlist(params.FixList) 65 if err != nil { 66 panic(err) 67 } 68 69 table.Init() 70 if err = table.LoadFile(params.Input, input); err != nil { 71 return 72 } 73 if input_file != nil { 74 input_file.Close() 75 } 76 77 for entry := range table.Entries() { 78 symbol := entry.Symbol 79 matches := capture.FindAllStringSubmatch(symbol.Name, 1) 80 if len(matches) > 0 { 81 //fmt.Fprintln(os.Stderr, "matches: ", matches) 82 match := matches[0][1] 83 fixed_match := strings.Replace(params.FixPattern, "$", match, 1) 84 for _, fix := range fixlist { 85 if strings.ToUpper(fixed_match) == strings.ToUpper(fix) { 86 symbol.Name = fix 87 break 88 } 89 } 90 } 91 fmt.Println(symbol.String()) 92 } 93 }