Skip to content

Commit

Permalink
perf: ReadBinaryFromFile supports Chinese path
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Nov 19, 2024
1 parent 3bb05ac commit f5ccf62
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
56 changes: 46 additions & 10 deletions fastdeploy/utils/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
#include "fastdeploy/utils/utils.h"

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

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

namespace fastdeploy {

Expand Down Expand Up @@ -48,18 +55,47 @@ 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;

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;
}
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& result = *contents;
result.clear();

auto fileSize = file.tellg();
if (fileSize != -1) {
result.resize(fileSize);
file.seekg(0, std::ios::beg);
file.read(reinterpret_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(reinterpret_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 f5ccf62

Please sign in to comment.