Skip to content

Commit

Permalink
ChmFile::SmartToUtf8() => SmartToUtf8Temp()
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed May 23, 2024
1 parent bc712d2 commit bbdb02c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
30 changes: 14 additions & 16 deletions src/ChmFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,19 @@ ByteSlice ChmFile::GetData(const char* fileName) const {
return {d, len};
}

char* ChmFile::SmartToUtf8(const char* s, uint overrideCP) const {
TempStr ChmFile::SmartToUtf8Temp(const char* s, uint overrideCP) const {
if (str::StartsWith(s, UTF8_BOM)) {
return str::Dup(s + 3);
return str::DupTemp(s + 3);
}
if (overrideCP) {
return strconv::ToMultiByte(s, overrideCP, CP_UTF8);
char* s2 = strconv::ToMultiByte(s, overrideCP, CP_UTF8);
return str::DupTemp(s2);
}
if (CP_UTF8 == codepage) {
return str::Dup(s);
return str::DupTemp(s);
}
return strconv::ToMultiByte(s, codepage, CP_UTF8);
char* s2 = strconv::ToMultiByte(s, codepage, CP_UTF8);
return s2;
}

WCHAR* ChmFile::SmartToWStr(const char* text) const {
Expand Down Expand Up @@ -254,25 +256,23 @@ void ChmFile::FixPathCodepage(AutoFreeStr& path, uint& fileCP) {
return;
}

char* utf8Path = SmartToUtf8(path.Get());
TempStr utf8Path = SmartToUtf8Temp(path.Get());
if (HasData(utf8Path)) {
path.Set(utf8Path);
path.SetCopy(utf8Path);
fileCP = codepage;
return;
}
str::Free(utf8Path);

if (fileCP == codepage) {
return;
}

utf8Path = SmartToUtf8(path.Get(), fileCP);
utf8Path = SmartToUtf8Temp(path.Get(), fileCP);
if (HasData(utf8Path)) {
path.Set(utf8Path);
path.SetCopy(utf8Path);
codepage = fileCP;
return;
}
str::Free(utf8Path);
}

bool ChmFile::Load(const char* path) {
Expand Down Expand Up @@ -325,19 +325,17 @@ bool ChmFile::Load(const char* path) {
TempStr ChmFile::GetPropertyTemp(const char* name) const {
char* result = nullptr;
if (str::Eq(kPropTitle, name) && title.CStr()) {
result = SmartToUtf8(title.CStr());
result = SmartToUtf8Temp(title.CStr());
} else if (str::Eq(kPropCreatorApp, name) && creator.CStr()) {
result = SmartToUtf8(creator.CStr());
result = SmartToUtf8Temp(creator.CStr());
}
// TODO: shouldn't it be up to the front-end to normalize whitespace?
if (!result) {
return nullptr;
}
// TODO: original code called str::RemoveCharsInPlace(result, "\n\r\t")
str::NormalizeWSInPlace(result);
TempStr temp = str::DupTemp(result);
str::Free(result);
return temp;
return result;
}

const char* ChmFile::GetHomePath() const {
Expand Down
2 changes: 1 addition & 1 deletion src/ChmFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct ChmFile {
ByteSlice GetData(const char* fileName) const;
char* ResolveTopicID(unsigned int id) const;

char* SmartToUtf8(const char* text, uint overrideCP = 0) const;
TempStr SmartToUtf8Temp(const char* text, uint overrideCP = 0) const;
WCHAR* SmartToWStr(const char* text) const;

TempStr GetPropertyTemp(const char* name) const;
Expand Down
3 changes: 1 addition & 2 deletions src/EngineEbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1511,9 +1511,8 @@ class ChmHtmlCollector : public EbookTocVisitor {
}
html.AppendFmt("<pagebreak page_path=\"%s\" page_marker />", plainUrl);
uint charset = ExtractHttpCharset((const char*)pageHtml.Get(), pageHtml.size());
char* s = doc->SmartToUtf8((const char*)pageHtml.Get(), charset);
TempStr s = doc->SmartToUtf8Temp((const char*)pageHtml.Get(), charset);
html.Append(s);
str::Free(s);
added.Append(plainUrl);
pageHtml.Free();
}
Expand Down

0 comments on commit bbdb02c

Please sign in to comment.