-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathzutil.go
254 lines (229 loc) · 4.45 KB
/
zutil.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package gimg
import (
"crypto/md5"
"encoding/hex"
"fmt"
"math"
"os"
_ "reflect"
"regexp"
"runtime"
"strconv"
"strings"
)
/*
#include <stdlib.h>
#include <stdio.h>
char **EMPTY = NULL;
*/
import "C" //此行和上面的注释之间不能有空行,否则会报错
import "unsafe"
/**
* @brief str_hash Hash algorithm of processing a md5 string.
*
* @param str The md5 string.
*
* @return The number less than 1024.
*/
func str_hash(str string) int {
b := []byte(str)
c := b[0:3]
cc := C.CString(string(c))
defer C.free(unsafe.Pointer(cc))
d := C.strtol(cc, C.EMPTY, 16)
d = d / 4
return int(d)
}
/**
* @brief is_md5 Check the string is a md5 style.
*
* @param s The string.
*
* @return 1 for yes and -1 for no.
*/
func is_md5(str string) bool {
regular := `^([0-9a-zA-Z]){32}$`
regx := regexp.MustCompile(regular)
return regx.MatchString(str)
}
/**
* @brief delete_file delete a file
*
* @param path the path of the file
*
* @return 1 for OK or -1 for fail
*/
func delete_file(path string) bool {
os.RemoveAll(path)
return true
}
/**
* @brief is_file Check a filename is a file.
*
* @param filename The filename input.
*
* @return 1 for yes and -1 for no.
*/
func is_file(str string) bool {
fmt.Println(str)
f, err := os.Open(str)
if err != nil {
return false
}
fi, err := f.Stat()
if err != nil {
return false
}
if fi.Mode().IsDir() {
return false
}
return true
}
/**
* @brief is_dir Check a path is a directory.
*
* @param path The path input.
*
* @return 1 for yes and -1 for no.
*/
func is_dir(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return false
}
if fi.IsDir() {
return true
}
return false
}
/**
* @brief is_img Check a file is a image we support(jpg, png, gif).
*
* @param filename The name of the file.
*
* @return 1 for success and 0 for fail.
*/
func is_img(file_name string) bool {
return true
}
/**
* @brief get_type It tell you the type of a file.
*
* @param filename The name of the file.
* @param type Save the type string.
*
* @return 1 for success and -1 for fail.
*/
func get_type(file_name string) (string, error) {
i := strings.LastIndex(file_name, ".")
if i == -1 {
return "", fmt.Errorf("FileName [%s] Has No '.' in It.", file_name)
}
//fmt.Printf("ext index : %d", i)
ext := file_name[(i + 1):len(file_name)]
return ext, nil
}
func is_exist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
/**
* @brief mk_dir It create a new directory with the path input.
*
* @param path The path you want to create.
*
* @return 1 for success and -1 for fail.
*/
func mk_dir(path string) bool {
if is_exist(path) {
fmt.Printf("Path[%s] is Existed!", path)
return false
}
if err := os.MkdirAll(path, 0755); err != nil {
if os.IsPermission(err) {
fmt.Printf("permission is error")
}
return false
}
return true
}
/**
* @brief is_special_dir check if the path is a special path
*
* @param path the path want to check
*
* @return 1 for yes and -1 for not
*/
func is_special_dir(path string) bool {
if path == "." || path == ".." {
return true
}
return false
}
/**
* @brief get_cpu_cores get the cpu cores of a server
*
* @return the cpu core number
*/
func get_cpu_cores() int {
return runtime.NumCPU()
}
/**
* @brief gettid get pid
*
* @return pid
*/
func gettid() int {
return os.Getpid()
}
/**
* @brief get_file_path get the file's path
*
* @param path the file's parent path
* @param file_name the file name
* @param file_path the full path of the file
*/
func get_file_path(path string, file_name string) string {
s := string(path[(len(path) - 1):len(path)])
if s != "/" {
return path + "/" + file_name
} else {
return path + file_name
}
}
/**
* @brief gen_key Generate storage key from md5 and other args.
*
* @param key The key string.
* @param md5 The md5 string.
* @param argc Count of args.
* @param ... Args.
*
* @return Generate result.
*/
func gen_key(md5 string, args ...interface{}) string {
s := []string{}
s = append(s, md5)
for _, argv := range args {
switch v := argv.(type) {
case string:
s = append(s, v)
case int:
s = append(s, strconv.Itoa(v))
}
}
return strings.Join(s, ":")
}
func gen_md5_str(data []byte) string {
fmt.Println("Begin to Caculate MD5...")
m := md5.New()
m.Write(data)
return hex.EncodeToString(m.Sum(nil))
}
func round(val float64) float64 {
if val > 0.0 {
return math.Floor(val + 0.5)
} else {
return math.Ceil(val - 0.5)
}
}