/*
本模块存储一些公共函数供其它所有模块调用
Package utils provides common utility functions used across all modules.
*/
package utils

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// ReadTxtFile reads a text file line by line and returns all lines as a string slice.
// This function handles file reading errors gracefully using recover.
func ReadTxtFile(filepath string) []string {
	var datalists []string
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("read file error!")
		}
	}()
	file, err := os.Open(filepath)
	if err == nil {
		buff := bufio.NewReader(file)
		for {
			line, _, err := buff.ReadLine()
			if err == io.EOF {
				break
			}
			datalists = append(datalists, string(line))
		}
	}
	file.Close()
	return datalists
}

// Read_txt_File is an alias for ReadTxtFile to maintain backward compatibility
// with legacy code that uses the old naming convention.
// Deprecated: Use ReadTxtFile instead.
func Read_txt_File(filepath string) []string {
	return ReadTxtFile(filepath)
}
