/
file.go
170 lines (137 loc) · 3.38 KB
/
file.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package vfp
import "os"
import "os/exec"
import (
"fmt"
"io/ioutil"
"path/filepath"
)
//Places information about files into an array and then returns the number of files.
func Adir(zdir string) ([]os.FileInfo, error) {
zpath := Justpath(zdir)
zfile := Strtran(Justfname(zdir), "*", "", -1)
zfile = Lower(Strtran(zfile, "?", "", -1))
list, err := ioutil.ReadDir(zpath)
zl := make([]os.FileInfo, 0)
for _, zf := range list {
if At(zfile, Lower(zf.Name())) > 0 {
zl = append(zl, zf)
}
}
return zl, err
}
func Strtofile(zstrArg interface{}, zfile string) error {
//file, err := os.Create(zfile)
//if err != nil {
// return err
//}
//defer file.Close()
//file.WriteString(zstr)
//return nil
var zstr []byte
switch zstrArg.(type) {
case string:
zstr = []byte(zstrArg.(string))
case []byte:
zstr = zstrArg.([]byte)
default:
zstr = []byte(fmt.Sprintf("%v", zstrArg))
}
return ioutil.WriteFile(zfile, zstr, os.ModeAppend)
}
//Returns the contents of a file as a []byte.
func Filetostr(zfile string) (zstr []byte, err error) {
return ioutil.ReadFile(zfile)
//file, err := os.Open(zfile)
//if err != nil {
// return
//}
//defer file.Close()
//zbr := bufio.NewReader(file)
//zstr, err = ioutil.ReadAll(zbr)
//return
}
//Returns the characters of a file extension from a complete path.
func Justext(zpath string) string {
return filepath.Ext(zpath)
}
//Returns the path portion of a complete path and file name.
//Example:
// vfp.Justpath("c:/sek/ww.dat")
func Justpath(zpath string) string {
return filepath.Dir(zpath)
}
//Returns the file name portion of a complete path and file name.
/*Example:
print ww.dat
fmt.Println("justfname:", vfp.Justfname("c:\\sek\\ww.dat"))
*/
func Justfname(zpath string) string {
_, zv := filepath.Split(zpath)
return zv
}
//Returns the stem name (the file name before the extension) from a complete path and file name.
/*Example:
print xx
vfp.Juststem("c:\\ccc\\xx.dat")
*/
func Juststem(zpath string) string {
zv := Justfname(zpath)
for zi := 0; zi < len(zv); zi++ {
if zv[zi] == '.' {
return zv[:zi]
}
}
return zv
}
//Adds a backslash (if needed) to a path expression.
/*Example:
Both print C:\Windows\
vfp.Addbs( "C:\\Windows" )
vfp.Addbs( "C:\\Windows\\" )
*/
func Addbs(zpath string) (zret string) {
if !os.IsPathSeparator(zpath[len(zpath)-1]) {
return zpath + string(os.PathSeparator)
}
return zpath
}
func sourceExist(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}
//Locates the specified file.
func File(zfile string) bool {
return sourceExist(zfile)
}
//Locates the specified directory.
func Directory(zdir string) bool {
return sourceExist(zdir)
}
//Returns the current directory.
func Curdir() string {
zpwd, _ := os.Getwd()
return zpwd
}
//Returns the path to a specified file or the path relative to another file.
func Fullpath(zfile string) string {
zp, zerr := exec.LookPath(zfile)
if zerr != nil {
return ""
}
//os.Args[0] 当前执行程序的文件路径
return zp
}
//Save objects to file
func Saveto(zfile string, zobjs ...interface{}) bool {
return true
}
//Retrieves variables and variable arrays saved in a variable file or a memo field and places them into memory.
func Restorefrom(zfile string) bool {
return true
}
//Returns the size in bytes of a specified field or file.
func Fsize(zfile string) int64 {
f, _ := os.Stat(zfile)
return f.Size()
}