Skip to content

Commit

Permalink
Merge pull request #2560 from MaaXYZ/perf/read_file
Browse files Browse the repository at this point in the history
perf: ReadBinaryFromFile supports Chinese path
  • Loading branch information
Jiang-Jia-Jun authored Jan 10, 2025
2 parents 17d204b + e321ae5 commit 618826b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
61 changes: 51 additions & 10 deletions fastdeploy/utils/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
#include "fastdeploy/utils/utils.h"

#include <sstream>
#include <fstream>
#include <string_view>

#ifdef _WIN32
#include <Windows.h>
#endif

namespace fastdeploy {

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

bool ReadBinaryFromFile(const std::string& file, std::string* contents) {
std::ifstream fin(file, std::ios::in | std::ios::binary);
if (!fin.is_open()) {
FDERROR << "Failed to open file: " << file << " to read." << std::endl;
// using os_string = std::filesystem::path::string_type;
#ifdef _WIN32
using os_string = std::wstring;
#else
using os_string = std::string;
#endif

os_string to_osstring(std::string_view utf8_str)
{
#ifdef _WIN32
int len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.data(), (int)utf8_str.size(), nullptr, 0);
os_string result(len, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8_str.data(), (int)utf8_str.size(), result.data(), len);
return result;
#else
return std::string(utf8_str);
#endif
}

bool ReadBinaryFromFile(const std::string& path, std::string* contents)
{
if (!contents) {
return false;
}
auto& result = *contents;
result.clear();

std::ifstream file(to_osstring(path), std::ios::binary | std::ios::ate);
if (!file.is_open()) {
return false;
}
fin.seekg(0, std::ios::end);
contents->clear();
contents->resize(fin.tellg());
fin.seekg(0, std::ios::beg);
fin.read(&(contents->at(0)), contents->size());
fin.close();

auto fileSize = file.tellg();
if (fileSize != -1) {
result.resize(fileSize);
file.seekg(0, std::ios::beg);
file.read(const_cast<char*>(result.data()), fileSize);
}
else {
// no size available, read to EOF
constexpr auto chunksize = 4096;
std::string chunk(chunksize, 0);
while (!file.fail()) {
file.read(const_cast<char*>(chunk.data()), chunksize);
result.insert(result.end(), chunk.data(), chunk.data() + file.gcount());
}
}
return true;
}

Expand Down
6 changes: 5 additions & 1 deletion fastdeploy/vision/ocr/ppocr/rec_postprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@
#include "fastdeploy/vision/ocr/ppocr/rec_postprocessor.h"
#include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/ocr/ppocr/utils/ocr_utils.h"
#include "fastdeploy/utils/utils.h"

namespace fastdeploy {
namespace vision {
namespace ocr {

std::vector<std::string> ReadDict(const std::string& path) {
std::ifstream in(path);
std::string content;
ReadBinaryFromFile(path, &content);
std::stringstream in(std::move(content));
FDASSERT(in, "Cannot open file %s to read.", path.c_str());
std::string line;
std::vector<std::string> m_vec;
while (getline(in, line)) {
if (!line.empty() && *line.rbegin() == '\r') line.pop_back();
m_vec.push_back(line);
}
m_vec.insert(m_vec.begin(), "#"); // blank char for ctc
Expand Down

0 comments on commit 618826b

Please sign in to comment.