Skip to content

Commit

Permalink
refactor DocumentProperty => kProp*
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed May 23, 2024
1 parent 5380333 commit cc94a78
Show file tree
Hide file tree
Showing 31 changed files with 395 additions and 314 deletions.
17 changes: 9 additions & 8 deletions src/ChmFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,19 @@ bool ChmFile::Load(const char* path) {
return true;
}

TempStr ChmFile::GetPropertyTemp(DocumentProperty prop) const {
TempStr ChmFile::GetPropertyTemp(const char* name) const {
char* result = nullptr;
if (DocumentProperty::Title == prop && title) {
result = SmartToUtf8(title);
} else if (DocumentProperty::CreatorApp == prop && creator) {
result = SmartToUtf8(creator);
if (str::Eq(kPropTitle, name) && title.CStr()) {
result = SmartToUtf8(title.CStr());
} else if (str::Eq(kPropCreatorApp, name) && creator.CStr()) {
result = SmartToUtf8(creator.CStr());
}
// TODO: shouldn't it be up to the front-end to normalize whitespace?
if (result) {
// TODO: original code called str::RemoveCharsInPlace(result, "\n\r\t")
str::NormalizeWSInPlace(result);
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;
Expand Down
2 changes: 1 addition & 1 deletion src/ChmFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct ChmFile {
char* SmartToUtf8(const char* text, uint overrideCP = 0) const;
WCHAR* SmartToWstr(const char* text) const;

TempStr GetPropertyTemp(DocumentProperty prop) const;
TempStr GetPropertyTemp(const char* name) const;
const char* GetHomePath() const;
void GetAllPaths(StrVec*) const;

Expand Down
4 changes: 2 additions & 2 deletions src/ChmModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ int ChmModel::PageCount() const {
return pages.Size();
}

TempStr ChmModel::GetPropertyTemp(DocumentProperty prop) {
return doc->GetPropertyTemp(prop);
TempStr ChmModel::GetPropertyTemp(const char* name) {
return doc->GetPropertyTemp(name);
}

int ChmModel::CurrentPageNo() const {
Expand Down
2 changes: 1 addition & 1 deletion src/ChmModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct ChmModel : DocController {
const char* GetFilePath() const override;
const char* GetDefaultFileExt() const override;
int PageCount() const override;
TempStr GetPropertyTemp(DocumentProperty prop) override;
TempStr GetPropertyTemp(const char* name) override;

// page navigation (stateful)
int CurrentPageNo() const override;
Expand Down
4 changes: 2 additions & 2 deletions src/DisplayModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ int DisplayModel::PageCount() const {
return engine->PageCount();
}

TempStr DisplayModel::GetPropertyTemp(DocumentProperty prop) {
return engine->GetPropertyTemp(prop);
TempStr DisplayModel::GetPropertyTemp(const char* name) {
return engine->GetPropertyTemp(name);
}

void DisplayModel::GoToPage(int pageNo, bool addNavPoint) {
Expand Down
2 changes: 1 addition & 1 deletion src/DisplayModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct DisplayModel : DocController {
const char* GetFilePath() const override;
const char* GetDefaultFileExt() const override;
int PageCount() const override;
TempStr GetPropertyTemp(DocumentProperty prop) override;
TempStr GetPropertyTemp(const char* name) override;

// page navigation (stateful)
int CurrentPageNo() const override;
Expand Down
39 changes: 22 additions & 17 deletions src/DocController.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,28 @@ struct MainWindow;
struct FileState;
enum class DisplayMode;

using onBitmapRenderedCb = std::function<void(RenderedBitmap*)>;
// TODO: those are implementd in EngineBase.cpp
extern const char* kPropTitle;
extern const char* kPropAuthor;
extern const char* kPropCopyright;
extern const char* kPropSubject;
extern const char* kPropCreationDate;
extern const char* kPropModificationDate;
extern const char* kPropCreatorApp;
extern const char* kPropUnsupportedFeatures;
extern const char* kPropFontList;
extern const char* kPropPdfVersion;
extern const char* kPropPdfProducer;
extern const char* kPropPdfFileStructure;

// Props are stored in StrVec as key, value sequentially
using Props = StrVec;
int PropsCount(const Props& props);
int FindPropIdx(const Props& props, const char* key);
char* FindProp(const Props& props, const char* key);
void AddProp(Props& props, const char* key, const char* val, bool replaceIfExists = false);

// TODO: "format", "encryption", "info::Keywords" as in fz_lookup_metadata
enum class DocumentProperty {
Title,
Author,
Copyright,
Subject,
CreationDate,
ModificationDate,
CreatorApp,
UnsupportedFeatures,
FontList,
PdfVersion,
PdfProducer,
PdfFileStructure,
};
using onBitmapRenderedCb = std::function<void(RenderedBitmap*)>;

struct ILinkHandler {
virtual ~ILinkHandler(){};
Expand Down Expand Up @@ -77,7 +82,7 @@ struct DocController {
virtual const char* GetFilePath() const = 0;
virtual const char* GetDefaultFileExt() const = 0;
virtual int PageCount() const = 0;
virtual TempStr GetPropertyTemp(DocumentProperty prop) = 0;
virtual TempStr GetPropertyTemp(const char* name) = 0;

// page navigation (stateful)
virtual int CurrentPageNo() const = 0;
Expand Down
97 changes: 35 additions & 62 deletions src/EbookDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,42 +237,6 @@ static ByteSlice DecodeDataURI(const char* url) {
return {(u8*)str::Dup(data), str::Len(data)};
}

PropertyMap::~PropertyMap() {
int n = dimofi(values);
for (int i = 0; i < n; i++) {
str::Free(values[i]);
}
}

int PropertyMap::Find(DocumentProperty prop) const {
int n = (int)(prop);
if ((n >= 0) && (n < dimofi(values))) {
return n;
}
return -1;
}

// takes ownership of val
void PropertyMap::SetVal(DocumentProperty prop, char* val, bool setIfExists) {
int idx = Find(prop);
CrashIf(-1 == idx);
char* oldVal = values[idx];
if (idx < 0 || (oldVal && !setIfExists)) {
str::Free(val);
return;
}
values[idx] = val;
str::Free(oldVal);
}

TempStr PropertyMap::GetTemp(DocumentProperty prop) const {
int idx = Find(prop);
if (idx >= 0 && values[idx]) {
return values[idx];
}
return nullptr;
}

/* ********** EPUB ********** */

const char* EPUB_CONTAINER_NS = "urn:oasis:names:tc:opendocument:xmlns:container";
Expand Down Expand Up @@ -491,13 +455,13 @@ bool EpubDoc::Load() {
}

void EpubDoc::ParseMetadata(const char* content) {
struct {
DocumentProperty prop;
static struct {
const char* prop;
const char* name;
} metadataMap[] = {
{DocumentProperty::Title, "dc:title"}, {DocumentProperty::Author, "dc:creator"},
{DocumentProperty::CreationDate, "dc:date"}, {DocumentProperty::ModificationDate, "dcterms:modified"},
{DocumentProperty::Subject, "dc:description"}, {DocumentProperty::Copyright, "dc:rights"},
{kPropTitle, "dc:title"}, {kPropAuthor, "dc:creator"},
{kPropCreationDate, "dc:date"}, {kPropModificationDate, "dcterms:modified"},
{kPropSubject, "dc:description"}, {kPropCopyright, "dc:rights"},
};

HtmlPullParser pullParser(content, str::Len(content));
Expand Down Expand Up @@ -525,7 +489,8 @@ void EpubDoc::ParseMetadata(const char* content) {
if (tok && tok->IsText()) {
auto prop = metadataMap[i].prop;
char* val = ResolveHtmlEntities(tok->s, tok->sLen);
props.SetVal(prop, val);
AddProp(props, prop, val);
str::Free(val);
}
break;
}
Expand Down Expand Up @@ -605,8 +570,8 @@ ByteSlice EpubDoc::GetFileData(const char* relPath, const char* pagePath) {
return zip->GetFileDataByName(url);
}

TempStr EpubDoc::GetPropertyTemp(DocumentProperty prop) const {
return props.GetTemp(prop);
TempStr EpubDoc::GetPropertyTemp(const char* name) const {
return FindProp(props, name);
}

const char* EpubDoc::GetFileName() const {
Expand Down Expand Up @@ -905,7 +870,8 @@ bool Fb2Doc::Load() {
}
if (tok->IsText()) {
char* val = ResolveHtmlEntities(tok->s, tok->sLen);
props.SetVal(DocumentProperty::Title, val);
AddProp(props, kPropTitle, val);
str::Free(val);
}
} else if ((inTitleInfo || inDocInfo) && tok->IsStartTag() && tok->NameIsNS("author", FB2_MAIN_NS)) {
AutoFreeStr docAuthor;
Expand All @@ -922,31 +888,34 @@ bool Fb2Doc::Load() {
}
if (docAuthor) {
str::NormalizeWSInPlace(docAuthor);
if (!str::IsEmpty(docAuthor.Get())) {
char* val = docAuthor.Release();
bool setIfExists = inTitleInfo != 0;
props.SetVal(DocumentProperty::Author, val, setIfExists);
if (!str::IsEmpty(docAuthor.CStr())) {
char* val = docAuthor.CStr();
bool replaceIfExists = inTitleInfo != 0;
AddProp(props, kPropAuthor, val, replaceIfExists);
}
}
} else if (inTitleInfo && tok->IsStartTag() && tok->NameIsNS("date", FB2_MAIN_NS)) {
AttrInfo* attr = tok->GetAttrByNameNS("value", FB2_MAIN_NS);
if (attr) {
char* val = ResolveHtmlEntities(attr->val, attr->valLen);
props.SetVal(DocumentProperty::CreationDate, val);
AddProp(props, kPropCreationDate, val);
str::Free(val);
}
} else if (inDocInfo && tok->IsStartTag() && tok->NameIsNS("date", FB2_MAIN_NS)) {
AttrInfo* attr = tok->GetAttrByNameNS("value", FB2_MAIN_NS);
if (attr) {
char* val = ResolveHtmlEntities(attr->val, attr->valLen);
props.SetVal(DocumentProperty::ModificationDate, val);
AddProp(props, kPropModificationDate, val);
str::Free(val);
}
} else if (inDocInfo && tok->IsStartTag() && tok->NameIsNS("program-used", FB2_MAIN_NS)) {
if ((tok = parser.Next()) == nullptr || tok->IsError()) {
break;
}
if (tok->IsText()) {
char* val = ResolveHtmlEntities(tok->s, tok->sLen);
props.SetVal(DocumentProperty::CreatorApp, val);
AddProp(props, kPropCreatorApp, val);
str::Free(val);
}
} else if (inTitleInfo && tok->IsStartTag() && tok->NameIsNS("coverpage", FB2_MAIN_NS)) {
tok = parser.Next();
Expand Down Expand Up @@ -1016,8 +985,8 @@ ByteSlice* Fb2Doc::GetCoverImage() const {
return GetImageData(coverImage);
}

TempStr Fb2Doc::GetPropertyTemp(DocumentProperty prop) const {
return props.GetTemp(prop);
TempStr Fb2Doc::GetPropertyTemp(const char* name) const {
return FindProp(props, name);
}

const char* Fb2Doc::GetFileName() const {
Expand Down Expand Up @@ -1233,7 +1202,7 @@ ByteSlice PalmDoc::GetHtmlData() const {
return htmlData.AsByteSlice();
}

TempStr PalmDoc::GetPropertyTemp(DocumentProperty) const {
TempStr PalmDoc::GetPropertyTemp(const char*) const {
return nullptr;
}

Expand Down Expand Up @@ -1305,7 +1274,8 @@ bool HtmlDoc::Load() {
tok = parser.Next();
if (tok && tok->IsText()) {
char* val = ResolveHtmlEntities(tok->s, tok->sLen);
props.SetVal(DocumentProperty::Title, val);
AddProp(props, kPropTitle, val);
str::Free(val);
}
} else if ((tok->IsStartTag() || tok->IsEmptyElementEndTag()) && Tag_Meta == tok->tag) {
AttrInfo* attrName = tok->GetAttrByName("name");
Expand All @@ -1314,13 +1284,16 @@ bool HtmlDoc::Load() {
/* ignore this tag */;
} else if (attrName->ValIs("author")) {
char* val = ResolveHtmlEntities(attrValue->val, attrValue->valLen);
props.SetVal(DocumentProperty::Author, val);
AddProp(props, kPropAuthor, val);
str::Free(val);
} else if (attrName->ValIs("date")) {
char* val = ResolveHtmlEntities(attrValue->val, attrValue->valLen);
props.SetVal(DocumentProperty::CreationDate, val);
AddProp(props, kPropCreationDate, val);
str::Free(val);
} else if (attrName->ValIs("copyright")) {
char* val = ResolveHtmlEntities(attrValue->val, attrValue->valLen);
props.SetVal(DocumentProperty::Copyright, val);
AddProp(props, kPropCopyright, val);
str::Free(val);
}
}
}
Expand Down Expand Up @@ -1370,8 +1343,8 @@ ByteSlice HtmlDoc::LoadURL(const char* url) {
return file::ReadFile(path);
}

TempStr HtmlDoc::GetPropertyTemp(DocumentProperty prop) const {
return props.GetTemp(prop);
TempStr HtmlDoc::GetPropertyTemp(const char* name) const {
return FindProp(props, name);
}

const char* HtmlDoc::GetFileName() const {
Expand Down Expand Up @@ -1629,7 +1602,7 @@ ByteSlice TxtDoc::GetHtmlData() const {
return htmlData.AsByteSlice();
}

TempStr TxtDoc::GetPropertyTemp(DocumentProperty) const {
TempStr TxtDoc::GetPropertyTemp(const char*) const {
return nullptr;
}

Expand Down

0 comments on commit cc94a78

Please sign in to comment.