ParseTORReader parses a Luciol OTDR .tor text file from a reader
(r io.Reader)
| 77 | |
| 78 | // ParseTORReader parses a Luciol OTDR .tor text file from a reader |
| 79 | func ParseTORReader(r io.Reader) (*TORFile, error) { |
| 80 | tor := &TORFile{} |
| 81 | scanner := bufio.NewScanner(r) |
| 82 | |
| 83 | // Skip header lines until we find the first section |
| 84 | for scanner.Scan() { |
| 85 | line := cleanLine(scanner.Text()) |
| 86 | if line == "[DateTime]" { |
| 87 | break |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | currentSection := "DateTime" |
| 92 | for scanner.Scan() { |
| 93 | line := cleanLine(scanner.Text()) |
| 94 | |
| 95 | if line == "[-]" { |
| 96 | currentSection = "" |
| 97 | continue |
| 98 | } |
| 99 | |
| 100 | if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { |
| 101 | currentSection = line[1 : len(line)-1] |
| 102 | continue |
| 103 | } |
| 104 | |
| 105 | if currentSection == "" && line == "" { |
| 106 | continue |
| 107 | } |
| 108 | |
| 109 | switch currentSection { |
| 110 | case "DateTime": |
| 111 | ts, err := strconv.ParseInt(line, 10, 64) |
| 112 | if err != nil { |
| 113 | return nil, fmt.Errorf("parsing DateTime %q: %w", line, err) |
| 114 | } |
| 115 | tor.DateTime = time.Unix(ts, 0) |
| 116 | case "InstrumentInfo": |
| 117 | if tor.InstrumentInfo == "" { |
| 118 | tor.InstrumentInfo = line |
| 119 | } else if sn, ok := strings.CutPrefix(line, "OTDR module S/N:"); ok { |
| 120 | tor.ModuleSerialNumber = strings.TrimSpace(sn) |
| 121 | } |
| 122 | case "CableID": |
| 123 | tor.CableID = line |
| 124 | case "FiberID": |
| 125 | tor.FiberID = line |
| 126 | case "FiberType": |
| 127 | v, err := strconv.Atoi(line) |
| 128 | if err != nil { |
| 129 | return nil, fmt.Errorf("parsing FiberType %q: %w", line, err) |
| 130 | } |
| 131 | tor.FiberType = v |
| 132 | case "Wavelength": |
| 133 | v, err := strconv.Atoi(line) |
| 134 | if err != nil { |
| 135 | return nil, fmt.Errorf("parsing Wavelength %q: %w", line, err) |
| 136 | } |
searching dependent graphs…