File: helpers.go

package info (click to toggle)
golang-github-la5nta-wl2k-go 0.11.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,856 kB
  • sloc: ansic: 14; makefile: 2
file content (88 lines) | stat: -rw-r--r-- 1,677 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2015 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
// Use of this source code is governed by the MIT-license that can be
// found in the LICENSE file.

package fbb

import (
	"bytes"
	"fmt"
	"io"
	"strings"
)

type ByDate []*Message

func (d ByDate) Len() int           { return len(d) }
func (d ByDate) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }
func (d ByDate) Less(i, j int) bool { return d[i].Date().Before(d[j].Date()) }

func ReadLine(rd io.Reader) (string, error) {
	var lineBuffer bytes.Buffer

	for {
		buf := make([]byte, 1)
		n, err := rd.Read(buf)
		if err != nil {
			return ``, err
		} else if n < 1 {
			continue
		}

		if buf[0] == '\n' || buf[0] == '\r' {
			if lineBuffer.Len() > 0 {
				return cleanString(string(lineBuffer.Bytes())), nil
			}
			continue
		} else {
			lineBuffer.WriteByte(buf[0])
		}
	}
}

func (s *Session) nextLineRemoteErr(parseErr bool) (string, error) {
	line, err := s.rd.ReadString('\r')
	if err != nil {
		return line, err
	}

	line = cleanString(line)
	s.pLog.Println(line)

	if err := errLine(line); parseErr && err != nil {
		return "", err
	} else {
		return line, nil
	}
}

func (s *Session) nextLine() (string, error) {
	return s.nextLineRemoteErr(true)
}

func errLine(str string) error {
	if len(str) == 0 || str[0] != '*' {
		return nil
	}

	idx := strings.LastIndex(str, "*")
	if idx+1 >= len(str) {
		return nil
	}

	return fmt.Errorf(strings.TrimSpace(str[idx+1:]))
}

func cleanString(str string) string {
	str = strings.TrimSpace(str)
	if len(str) < 1 {
		return str
	}
	if str[0] == byte(0) {
		str = str[1:]
	}
	if str[len(str)-1] == byte(0) {
		str = str[0 : len(str)-2]
	}
	return str
}