gfile_contents.go 7.2 KB

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