gfile_contents.go 6.9 KB

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