Skip to content

Commit 618826b

Browse files
Merge pull request #2560 from MaaXYZ/perf/read_file
perf: ReadBinaryFromFile supports Chinese path
2 parents 17d204b + e321ae5 commit 618826b

2 files changed

Lines changed: 56 additions & 11 deletions

File tree

fastdeploy/utils/utils.cc

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
#include "fastdeploy/utils/utils.h"
1616

1717
#include <sstream>
18+
#include <fstream>
19+
#include <string_view>
20+
21+
#ifdef _WIN32
22+
#include <Windows.h>
23+
#endif
1824

1925
namespace fastdeploy {
2026

@@ -48,18 +54,53 @@ FDLogger& FDLogger::operator<<(std::ostream& (*os)(std::ostream&)) {
4854
return *this;
4955
}
5056

51-
bool ReadBinaryFromFile(const std::string& file, std::string* contents) {
52-
std::ifstream fin(file, std::ios::in | std::ios::binary);
53-
if (!fin.is_open()) {
54-
FDERROR << "Failed to open file: " << file << " to read." << std::endl;
57+
// using os_string = std::filesystem::path::string_type;
58+
#ifdef _WIN32
59+
using os_string = std::wstring;
60+
#else
61+
using os_string = std::string;
62+
#endif
63+
64+
os_string to_osstring(std::string_view utf8_str)
65+
{
66+
#ifdef _WIN32
67+
int len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.data(), (int)utf8_str.size(), nullptr, 0);
68+
os_string result(len, 0);
69+
MultiByteToWideChar(CP_UTF8, 0, utf8_str.data(), (int)utf8_str.size(), result.data(), len);
70+
return result;
71+
#else
72+
return std::string(utf8_str);
73+
#endif
74+
}
75+
76+
bool ReadBinaryFromFile(const std::string& path, std::string* contents)
77+
{
78+
if (!contents) {
79+
return false;
80+
}
81+
auto& result = *contents;
82+
result.clear();
83+
84+
std::ifstream file(to_osstring(path), std::ios::binary | std::ios::ate);
85+
if (!file.is_open()) {
5586
return false;
5687
}
57-
fin.seekg(0, std::ios::end);
58-
contents->clear();
59-
contents->resize(fin.tellg());
60-
fin.seekg(0, std::ios::beg);
61-
fin.read(&(contents->at(0)), contents->size());
62-
fin.close();
88+
89+
auto fileSize = file.tellg();
90+
if (fileSize != -1) {
91+
result.resize(fileSize);
92+
file.seekg(0, std::ios::beg);
93+
file.read(const_cast<char*>(result.data()), fileSize);
94+
}
95+
else {
96+
// no size available, read to EOF
97+
constexpr auto chunksize = 4096;
98+
std::string chunk(chunksize, 0);
99+
while (!file.fail()) {
100+
file.read(const_cast<char*>(chunk.data()), chunksize);
101+
result.insert(result.end(), chunk.data(), chunk.data() + file.gcount());
102+
}
103+
}
63104
return true;
64105
}
65106

fastdeploy/vision/ocr/ppocr/rec_postprocessor.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@
1515
#include "fastdeploy/vision/ocr/ppocr/rec_postprocessor.h"
1616
#include "fastdeploy/utils/perf.h"
1717
#include "fastdeploy/vision/ocr/ppocr/utils/ocr_utils.h"
18+
#include "fastdeploy/utils/utils.h"
1819

1920
namespace fastdeploy {
2021
namespace vision {
2122
namespace ocr {
2223

2324
std::vector<std::string> ReadDict(const std::string& path) {
24-
std::ifstream in(path);
25+
std::string content;
26+
ReadBinaryFromFile(path, &content);
27+
std::stringstream in(std::move(content));
2528
FDASSERT(in, "Cannot open file %s to read.", path.c_str());
2629
std::string line;
2730
std::vector<std::string> m_vec;
2831
while (getline(in, line)) {
32+
if (!line.empty() && *line.rbegin() == '\r') line.pop_back();
2933
m_vec.push_back(line);
3034
}
3135
m_vec.insert(m_vec.begin(), "#"); // blank char for ctc

0 commit comments

Comments
 (0)