gfile_contents.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gfile
  7. import (
  8. "bufio"
  9. "io"
  10. "io/ioutil"
  11. "os"
  12. "github.com/gogf/gf/v2/errors/gerror"
  13. )
  14. var (
  15. // DefaultReadBuffer is the buffer size for reading file content.
  16. DefaultReadBuffer = 1024
  17. )
  18. // GetContents returns the file content of `path` as string.
  19. // It returns en empty string if it fails reading.
  20. func GetContents(path string) string {
  21. return string(GetBytes(path))
  22. }
  23. // GetBytes returns the file content of `path` as []byte.
  24. // It returns nil if it fails reading.
  25. func GetBytes(path string) []byte {
  26. data, err := ioutil.ReadFile(path)
  27. if err != nil {
  28. return nil
  29. }
  30. return data
  31. }
  32. // putContents puts binary content to file of `path`.
  33. func putContents(path string, data []byte, flag int, perm os.FileMode) error {
  34. // It supports creating file of `path` recursively.
  35. dir := Dir(path)
  36. if !Exists(dir) {
  37. if err := Mkdir(dir); err != nil {
  38. return err
  39. }
  40. }
  41. // Opening file with given `flag` and `perm`.
  42. f, err := OpenWithFlagPerm(path, flag, perm)
  43. if err != nil {
  44. return err
  45. }
  46. defer f.Close()
  47. // Write data.
  48. var n int
  49. if n, err = f.Write(data); err != nil {
  50. err = gerror.Wrapf(err, `Write data to file "%s" failed`, path)
  51. return err
  52. } else if n < len(data) {
  53. return io.ErrShortWrite
  54. }
  55. return nil
  56. }
  57. // Truncate truncates file of `path` to given size by `size`.
  58. func Truncate(path string, size int) (err error) {
  59. err = os.Truncate(path, int64(size))
  60. if err != nil {
  61. err = gerror.Wrapf(err, `os.Truncate failed for file "%s", size "%d"`, path, size)
  62. }
  63. return
  64. }
  65. // PutContents puts string `content` to file of `path`.
  66. // It creates file of `path` recursively if it does not exist.
  67. func PutContents(path string, content string) error {
  68. return putContents(path, []byte(content), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, DefaultPermOpen)
  69. }
  70. // PutContentsAppend appends string `content` to file of `path`.
  71. // It creates file of `path` recursively if it does not exist.
  72. func PutContentsAppend(path string, content string) error {
  73. return putContents(path, []byte(content), os.O_WRONLY|os.O_CREATE|os.O_APPEND, DefaultPermOpen)
  74. }
  75. // PutBytes puts binary `content` to file of `path`.
  76. // It creates file of `path` recursively if it does not exist.
  77. func PutBytes(path string, content []byte) error {
  78. return putContents(path, content, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, DefaultPermOpen)
  79. }
  80. // PutBytesAppend appends binary `content` to file of `path`.
  81. // It creates file of `path` recursively if it does not exist.
  82. func PutBytesAppend(path string, content []byte) error {
  83. return putContents(path, content, os.O_WRONLY|os.O_CREATE|os.O_APPEND, DefaultPermOpen)
  84. }
  85. // GetNextCharOffset returns the file offset for given `char` starting from `start`.
  86. func GetNextCharOffset(reader io.ReaderAt, char byte, start int64) int64 {
  87. buffer := make([]byte, DefaultReadBuffer)
  88. offset := start
  89. for {
  90. if n, err := reader.ReadAt(buffer, offset); n > 0 {
  91. for i := 0; i < n; i++ {
  92. if buffer[i] == char {
  93. return int64(i) + offset
  94. }
  95. }
  96. offset += int64(n)
  97. } else if err != nil {
  98. break
  99. }
  100. }
  101. return -1
  102. }
  103. // GetNextCharOffsetByPath returns the file offset for given `char` starting from `start`.
  104. // It opens file of `path` for reading with os.O_RDONLY flag and default perm.
  105. func GetNextCharOffsetByPath(path string, char byte, start int64) int64 {
  106. if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPermOpen); err == nil {
  107. defer f.Close()
  108. return GetNextCharOffset(f, char, start)
  109. }
  110. return -1
  111. }
  112. // GetBytesTilChar returns the contents of the file as []byte
  113. // until the next specified byte `char` position.
  114. //
  115. // Note: Returned value contains the character of the last position.
  116. func GetBytesTilChar(reader io.ReaderAt, char byte, start int64) ([]byte, int64) {
  117. if offset := GetNextCharOffset(reader, char, start); offset != -1 {
  118. return GetBytesByTwoOffsets(reader, start, offset+1), offset
  119. }
  120. return nil, -1
  121. }
  122. // GetBytesTilCharByPath returns the contents of the file given by `path` as []byte
  123. // until the next specified byte `char` position.
  124. // It opens file of `path` for reading with os.O_RDONLY flag and default perm.
  125. //
  126. // Note: Returned value contains the character of the last position.
  127. func GetBytesTilCharByPath(path string, char byte, start int64) ([]byte, int64) {
  128. if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPermOpen); err == nil {
  129. defer f.Close()
  130. return GetBytesTilChar(f, char, start)
  131. }
  132. return nil, -1
  133. }
  134. // GetBytesByTwoOffsets returns the binary content as []byte from `start` to `end`.
  135. // Note: Returned value does not contain the character of the last position, which means
  136. // it returns content range as [start, end).
  137. func GetBytesByTwoOffsets(reader io.ReaderAt, start int64, end int64) []byte {
  138. buffer := make([]byte, end-start)
  139. if _, err := reader.ReadAt(buffer, start); err != nil {
  140. return nil
  141. }
  142. return buffer
  143. }
  144. // GetBytesByTwoOffsetsByPath returns the binary content as []byte from `start` to `end`.
  145. // Note: Returned value does not contain the character of the last position, which means
  146. // it returns content range as [start, end).
  147. // It opens file of `path` for reading with os.O_RDONLY flag and default perm.
  148. func GetBytesByTwoOffsetsByPath(path string, start int64, end int64) []byte {
  149. if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPermOpen); err == nil {
  150. defer f.Close()
  151. return GetBytesByTwoOffsets(f, start, end)
  152. }
  153. return nil
  154. }
  155. // ReadLines reads file content line by line, which is passed to the callback function `callback` as string.
  156. // It matches each line of text, separated by chars '\r' or '\n', stripped any trailing end-of-line marker.
  157. //
  158. // Note that the parameter passed to callback function might be an empty value, and the last non-empty line
  159. // will be passed to callback function `callback` even if it has no newline marker.
  160. func ReadLines(file string, callback func(text string) error) error {
  161. f, err := Open(file)
  162. if err != nil {
  163. return err
  164. }
  165. defer f.Close()
  166. scanner := bufio.NewScanner(f)
  167. for scanner.Scan() {
  168. if err = callback(scanner.Text()); err != nil {
  169. return err
  170. }
  171. }
  172. return nil
  173. }
  174. // ReadLinesBytes reads file content line by line, which is passed to the callback function `callback` as []byte.
  175. // It matches each line of text, separated by chars '\r' or '\n', stripped any trailing end-of-line marker.
  176. //
  177. // Note that the parameter passed to callback function might be an empty value, and the last non-empty line
  178. // will be passed to callback function `callback` even if it has no newline marker.
  179. func ReadLinesBytes(file string, callback func(bytes []byte) error) error {
  180. f, err := Open(file)
  181. if err != nil {
  182. return err
  183. }
  184. defer f.Close()
  185. scanner := bufio.NewScanner(f)
  186. for scanner.Scan() {
  187. if err = callback(scanner.Bytes()); err != nil {
  188. return err
  189. }
  190. }
  191. return nil
  192. }