完成gfile文件内容操作方法改进

This commit is contained in:
john
2018-10-23 14:55:52 +08:00
parent a8028bc3ef
commit 18d2610a90
8 changed files with 71 additions and 21 deletions

View File

@ -200,7 +200,7 @@ func (client *Client) SyncSend(message *Message) error {
return nil
}
// Send data to kafka in asynchronized way.
// Send data to kafka in asynchronized way(concurrent safe).
func (client *Client) AsyncSend(message *Message) error {
if client.asyncProducer == nil {
if p, err := sarama.NewAsyncProducer(strings.Split(client.Config.Servers, ","), &client.Config.Config); err != nil {

View File

@ -15,7 +15,9 @@ import (
const (
// 方法中涉及到读取的时候的缓冲大小
gREAD_BUFFER = 1024
gREAD_BUFFER = 1024
// 方法中涉及到文件指针池的默认缓存时间(秒)
gFILE_POOL_EXPIRE = 60
)
// (文本)读取文件内容
@ -88,10 +90,10 @@ func GetNextCharOffset(file *os.File, char byte, start int64) int64 {
if n, err := file.ReadAt(buffer, offset); n > 0 {
for i := 0; i < n; i++ {
if buffer[i] == char {
return int64(i)
return int64(i) + offset
}
}
offset++
offset += int64(n)
} else if err != nil {
break
}
@ -99,10 +101,32 @@ func GetNextCharOffset(file *os.File, char byte, start int64) int64 {
return -1
}
// 获得文件内容直到下一个指定字节的位置(返回值不包含该位置字符内容)
// 获得文件内容下一个指定字节的位置
func GetNextCharOffsetByPath(path string, char byte, start int64) int64 {
if f, err := gfpool.Open(path, os.O_RDONLY, 0666, gFILE_POOL_EXPIRE*1000); err == nil {
defer f.Close()
return GetNextCharOffset(&f.File, char, start)
} else {
// panic(err)
}
return -1
}
// 获得文件内容直到下一个指定字节的位置(返回值包含该位置字符内容)
func GetBinContentsTilChar(file *os.File, char byte, start int64) ([]byte, int64) {
if offset := GetNextCharOffset(file, char, start); offset != -1 {
return GetBinContentsByTwoOffsets(file, start, offset), offset
return GetBinContentsByTwoOffsets(file, start, offset + 1), offset
}
return nil, -1
}
// 获得文件内容直到下一个指定字节的位置(返回值包含该位置字符内容)
func GetBinContentsTilCharByPath(path string, char byte, start int64) ([]byte, int64) {
if f, err := gfpool.Open(path, os.O_RDONLY, 0666, gFILE_POOL_EXPIRE*1000); err == nil {
defer f.Close()
return GetBinContentsTilChar(&f.File, char, start)
} else {
// panic(err)
}
return nil, -1
}
@ -114,4 +138,15 @@ func GetBinContentsByTwoOffsets(file *os.File, start int64, end int64) []byte {
return nil
}
return buffer
}
// 获得文件内容中两个offset之间的内容 [start, end)
func GetBinContentsByTwoOffsetsByPath(path string, start int64, end int64) []byte {
if f, err := gfpool.Open(path, os.O_RDONLY, 0666, gFILE_POOL_EXPIRE*1000); err == nil {
defer f.Close()
return GetBinContentsByTwoOffsets(&f.File, start, end)
} else {
// panic(err)
}
return nil
}

View File

@ -13,11 +13,11 @@ import (
// 文件大小(bytes)
func Size(path string) int64 {
f, e := os.Stat(path)
s, e := os.Stat(path)
if e != nil {
return 0
}
return f.Size()
return s.Size()
}
// 格式化文件大小

View File

@ -33,7 +33,7 @@ type File struct {
// 全局指针池expire < 0表示不过期expire = 0表示使用完立即回收expire > 0表示超时回收
var pools = gmap.NewStringInterfaceMap()
// 获得文件对象,并自动创建指针池
// 获得文件对象,并自动创建指针池(过期时间单位:毫秒)
func Open(path string, flag int, perm os.FileMode, expire...int) (*File, error) {
fpExpire := 0
if len(expire) > 0 {
@ -54,6 +54,7 @@ func OpenFile(path string, flag int, perm os.FileMode, expire...int) (*File, err
}
// 创建一个文件指针池expire = 0表示不过期expire < 0表示使用完立即回收expire > 0表示超时回收默认值为0不过期
// 过期时间单位:毫秒
func New(path string, flag int, perm os.FileMode, expire...int) *Pool {
fpExpire := 0
if len(expire) > 0 {

View File

@ -9,11 +9,10 @@ func main() {
path := "/tmp/temp"
content := `123
456
789`
789
`
gfile.PutContents(path, content)
f, err := gfile.Open(path)
if err != nil {
panic(err)
}
fmt.Println(gfile.GetBinContentsTilChar(f, '\n', 0))
fmt.Println(gfile.GetBinContentsTilCharByPath(path, '\n', 0))
fmt.Println(gfile.GetBinContentsTilCharByPath(path, '\n', 3))
fmt.Println(gfile.GetBinContentsTilCharByPath(path, '\n', 8))
}

View File

@ -7,6 +7,7 @@ import (
// 执行shell指令
func main () {
r, err := gproc.ShellExec("sleep 3s; echo 'hello';")
fmt.Println("result:", r, err)
r, err := gproc.ShellExec(`sleep 3s; echo "hello gf!";`)
fmt.Println("result:", r)
fmt.Println(err)
}

View File

@ -2,10 +2,13 @@ package main
import (
"fmt"
"gitee.com/johng/gf/g/os/gfile"
"gitee.com/johng/gf/g/os/gtime"
)
func main() {
fmt.Println(gtime.NewFromTimeStamp(gfile.MTime("/home/john/Documents/temp")).String())
func test() {
defer fmt.Println(1)
fmt.Println(2)
}
func main() {
test()
}

11
geg/util/gregex/gregex.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"fmt"
"gitee.com/johng/gf/g/util/gregex"
)
func main() {
match, _ := gregex.MatchString(`(\w+).+\-\-\s*(.+)`, `GF is best! -- John`)
fmt.Printf(`%s says "%s" is the one he loves!`, match[2], match[1])
}