gfile_contents.go 7.2 KB

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