github.com/zhongdalu/gf@v1.0.0/g/os/gfile/gfile_contents.go (about)

     1  // Copyright 2017-2018 gf Author(https://github.com/zhongdalu/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/zhongdalu/gf.
     6  
     7  package gfile
     8  
     9  import (
    10  	"io"
    11  	"io/ioutil"
    12  	"os"
    13  )
    14  
    15  const (
    16  	// Buffer size for reading file content.
    17  	gREAD_BUFFER = 1024
    18  )
    19  
    20  // GetContents returns the file content of <path> as string.
    21  // It returns en empty string if it fails reading.
    22  func GetContents(path string) string {
    23  	return string(GetBinContents(path))
    24  }
    25  
    26  // GetBinContents returns the file content of <path> as []byte.
    27  // It returns nil if it fails reading.
    28  func GetBinContents(path string) []byte {
    29  	data, err := ioutil.ReadFile(path)
    30  	if err != nil {
    31  		return nil
    32  	}
    33  	return data
    34  }
    35  
    36  // putContents puts binary content to file of <path>.
    37  func putContents(path string, data []byte, flag int, perm int) error {
    38  	// It supports creating file of <path> recursively.
    39  	dir := Dir(path)
    40  	if !Exists(dir) {
    41  		if err := Mkdir(dir); err != nil {
    42  			return err
    43  		}
    44  	}
    45  	// Opening file with given <flag> and <perm>.
    46  	f, err := OpenWithFlagPerm(path, flag, perm)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	defer f.Close()
    51  	if n, err := f.Write(data); err != nil {
    52  		return err
    53  	} else if n < len(data) {
    54  		return io.ErrShortWrite
    55  	}
    56  	return nil
    57  }
    58  
    59  // Truncate truncates file of <path> to given size by <size>.
    60  func Truncate(path string, size int) error {
    61  	return os.Truncate(path, int64(size))
    62  }
    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, gDEFAULT_PERM)
    68  }
    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, gDEFAULT_PERM)
    74  }
    75  
    76  // PutBinContents puts binary <content> to file of <path>.
    77  // It creates file of <path> recursively if it does not exist.
    78  func PutBinContents(path string, content []byte) error {
    79  	return putContents(path, content, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, gDEFAULT_PERM)
    80  }
    81  
    82  // PutBinContentsAppend appends binary <content> to file of <path>.
    83  // It creates file of <path> recursively if it does not exist.
    84  func PutBinContentsAppend(path string, content []byte) error {
    85  	return putContents(path, content, os.O_WRONLY|os.O_CREATE|os.O_APPEND, gDEFAULT_PERM)
    86  }
    87  
    88  // GetNextCharOffset returns the file offset for given <char> starting from <start>.
    89  func GetNextCharOffset(reader io.ReaderAt, char byte, start int64) int64 {
    90  	buffer := make([]byte, gREAD_BUFFER)
    91  	offset := start
    92  	for {
    93  		if n, err := reader.ReadAt(buffer, offset); n > 0 {
    94  			for i := 0; i < n; i++ {
    95  				if buffer[i] == char {
    96  					return int64(i) + offset
    97  				}
    98  			}
    99  			offset += int64(n)
   100  		} else if err != nil {
   101  			break
   102  		}
   103  	}
   104  	return -1
   105  }
   106  
   107  // GetNextCharOffsetByPath returns the file offset for given <char> starting from <start>.
   108  // It opens file of <path> for reading with os.O_RDONLY flag and default perm.
   109  func GetNextCharOffsetByPath(path string, char byte, start int64) int64 {
   110  	if f, err := OpenWithFlagPerm(path, os.O_RDONLY, gDEFAULT_PERM); err == nil {
   111  		defer f.Close()
   112  		return GetNextCharOffset(f, char, start)
   113  	}
   114  	return -1
   115  }
   116  
   117  // GetBinContentsTilChar returns the contents of the file as []byte
   118  // until the next specified byte <char> position.
   119  //
   120  // Note: Returned value contains the character of the last position.
   121  func GetBinContentsTilChar(reader io.ReaderAt, char byte, start int64) ([]byte, int64) {
   122  	if offset := GetNextCharOffset(reader, char, start); offset != -1 {
   123  		return GetBinContentsByTwoOffsets(reader, start, offset+1), offset
   124  	}
   125  	return nil, -1
   126  }
   127  
   128  // GetBinContentsTilCharByPath returns the contents of the file given by <path> as []byte
   129  // until the next specified byte <char> position.
   130  // It opens file of <path> for reading with os.O_RDONLY flag and default perm.
   131  //
   132  // Note: Returned value contains the character of the last position.
   133  func GetBinContentsTilCharByPath(path string, char byte, start int64) ([]byte, int64) {
   134  	if f, err := OpenWithFlagPerm(path, os.O_RDONLY, gDEFAULT_PERM); err == nil {
   135  		defer f.Close()
   136  		return GetBinContentsTilChar(f, char, start)
   137  	}
   138  	return nil, -1
   139  }
   140  
   141  // GetBinContentsByTwoOffsets returns the binary content as []byte from <start> to <end>.
   142  // Note: Returned value does not contain the character of the last position, which means
   143  // it returns content range as [start, end).
   144  func GetBinContentsByTwoOffsets(reader io.ReaderAt, start int64, end int64) []byte {
   145  	buffer := make([]byte, end-start)
   146  	if _, err := reader.ReadAt(buffer, start); err != nil {
   147  		return nil
   148  	}
   149  	return buffer
   150  }
   151  
   152  // GetBinContentsByTwoOffsetsByPath returns the binary content as []byte from <start> to <end>.
   153  // Note: Returned value does not contain the character of the last position, which means
   154  // it returns content range as [start, end).
   155  // It opens file of <path> for reading with os.O_RDONLY flag and default perm.
   156  func GetBinContentsByTwoOffsetsByPath(path string, start int64, end int64) []byte {
   157  	if f, err := OpenWithFlagPerm(path, os.O_RDONLY, gDEFAULT_PERM); err == nil {
   158  		defer f.Close()
   159  		return GetBinContentsByTwoOffsets(f, start, end)
   160  	}
   161  	return nil
   162  }