-
Notifications
You must be signed in to change notification settings - Fork 28
/
utils.go
78 lines (65 loc) · 1.36 KB
/
utils.go
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
package tox
import "errors"
import "fmt"
import "io/ioutil"
import "os"
import "bytes"
func toxerr(errno interface{}) error {
return errors.New(fmt.Sprintf("toxcore error: %v", errno))
}
func toxerrf(f string, args ...interface{}) error {
return errors.New(fmt.Sprintf(f, args...))
}
var toxdebug = false
func SetDebug(debug bool) {
toxdebug = debug
}
var loglevel = 0
func SetLogLevel(level int) {
loglevel = level
}
func FileExist(fname string) bool {
_, err := os.Stat(fname)
if err != nil {
return false
}
return true
}
func (this *Tox) WriteSavedata(fname string) error {
if !FileExist(fname) {
err := ioutil.WriteFile(fname, this.GetSavedata(), 0755)
if err != nil {
return err
}
} else {
data, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
liveData := this.GetSavedata()
if bytes.Compare(data, liveData) != 0 {
err := ioutil.WriteFile(fname, this.GetSavedata(), 0755)
if err != nil {
return err
}
}
}
return nil
}
func (this *Tox) LoadSavedata(fname string) ([]byte, error) {
return ioutil.ReadFile(fname)
}
func LoadSavedata(fname string) ([]byte, error) {
return ioutil.ReadFile(fname)
}
func ConnStatusString(status int) (s string) {
switch status {
case CONNECTION_NONE:
s = "CONNECTION_NONE"
case CONNECTION_TCP:
s = "CONNECTION_TCP"
case CONNECTION_UDP:
s = "CONNECTION_UDP"
}
return
}