55#include " sherpa-onnx/csrc/file-utils.h"
66
77#include < fstream>
8- #include < memory>
98#include < sstream>
109#include < string>
1110#include < vector>
1413#include < windows.h>
1514#else
1615#include < sys/stat.h>
17- #include < unistd.h>
1816#include < limits.h>
1917#include < stdlib.h>
2018#endif
2119
2220#include " sherpa-onnx/csrc/macros.h"
21+ #include " sherpa-onnx/csrc/text-utils.h"
2322
2423namespace sherpa_onnx {
25- std::wstring ToWideString (const std::string &s);
26- std::string ToString (const std::wstring &s);
24+
25+ std::ifstream OpenInputFile (const std::string &filename,
26+ std::ios_base::openmode mode) {
27+ #ifdef _WIN32
28+ return std::ifstream (ToWideString (filename).c_str (), mode);
29+ #else
30+ return std::ifstream (filename, mode);
31+ #endif
32+ }
33+
34+ std::ofstream OpenOutputFile (const std::string &filename,
35+ std::ios_base::openmode mode) {
36+ #ifdef _WIN32
37+ return std::ofstream (ToWideString (filename).c_str (), mode);
38+ #else
39+ return std::ofstream (filename, mode);
40+ #endif
41+ }
2742
2843bool FileExists (const std::string &filename) {
2944#ifdef _WIN32
30- DWORD attributes = GetFileAttributesW (ToWideString (filename).c_str ());
31-
32- return attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY );
45+ DWORD attributes = GetFileAttributesW (ToWideString (filename).c_str ());
46+ return attributes != INVALID_FILE_ATTRIBUTES &&
47+ !(attributes & FILE_ATTRIBUTE_DIRECTORY );
3348#else
34- struct stat file_stat;
35- return stat (filename.c_str (), &file_stat) == 0 && S_ISREG (file_stat.st_mode );
49+ struct stat file_stat;
50+ return stat (filename.c_str (), &file_stat) == 0 && S_ISREG (file_stat.st_mode );
3651#endif
3752}
3853
@@ -47,67 +62,24 @@ std::vector<char> ReadFile(const std::string &filename) {
4762 if (filename.empty ()) {
4863 return {};
4964 }
50- try {
51- #ifdef _WIN32
52- HANDLE hFile = CreateFileW (
53- ToWideString (filename).c_str (),
54- GENERIC_READ ,
55- FILE_SHARE_READ ,
56- nullptr ,
57- OPEN_EXISTING ,
58- FILE_ATTRIBUTE_NORMAL ,
59- nullptr
60- );
61-
62- if (hFile == INVALID_HANDLE_VALUE ) {
63- return {};
64- }
65-
66- std::unique_ptr<void , decltype (&CloseHandle)> file_guard (
67- hFile, CloseHandle);
68-
69- LARGE_INTEGER file_size;
70- if (!GetFileSizeEx (hFile, &file_size) || file_size.QuadPart > SIZE_MAX ) {
71- return {};
72- }
73-
74- std::vector<char > buffer (static_cast <size_t >(file_size.QuadPart ));
7565
76- DWORD bytes_read = 0 ;
77- bool read_success = ::ReadFile (
78- hFile,
79- buffer.data (),
80- static_cast <DWORD >(buffer.size ()),
81- &bytes_read,
82- nullptr
83- );
84- if (!read_success || bytes_read != buffer.size ()) {
85- return {};
86- }
87-
88- return buffer;
89- #else
90- std::ifstream file (filename, std::ios::binary | std::ios::ate);
91- if (!file.is_open ()) {
92- return {};
93- }
94-
95- std::streamsize size = file.tellg ();
96- if (size < 0 ) {
97- return {};
98- }
99- file.seekg (0 , std::ios::beg);
66+ std::ifstream file = OpenInputFile (filename, std::ios::binary | std::ios::ate);
67+ if (!file.is_open ()) {
68+ return {};
69+ }
10070
101- std::vector<char > buffer (static_cast <size_t >(size));
102- if (!file.read (buffer.data (), size)) {
103- return {};
104- }
71+ std::streamsize size = file.tellg ();
72+ if (size < 0 ) {
73+ return {};
74+ }
75+ file.seekg (0 , std::ios::beg);
10576
106- return buffer;
107- #endif
108- } catch (const std::exception&) {
77+ std::vector<char > buffer (static_cast <size_t >(size));
78+ if (!file.read (buffer.data (), size)) {
10979 return {};
11080 }
81+
82+ return buffer;
11183}
11284
11385#if __ANDROID_API__ >= 9
0 commit comments