Doxygen
Loading...
Searching...
No Matches
ResourceMgr Class Reference

Singleton for managing resources compiled into an executable. More...

#include <src/resourcemgr.h>

Classes

class  Private

Public Member Functions

void registerResources (std::initializer_list< Resource > resources)
 Registers an array of resources.
bool writeCategory (const DString &categoryName, const DString &targetDir) const
 Writes all resource belonging to a given category to a given target directory.
bool copyResource (const DString &name, const DString &targetDir) const
 Copies a registered resource to a given target directory.
bool copyResourceAs (const DString &name, const DString &targetDir, const DString &targetName, bool append=false) const
 Copies a registered resource to a given target directory under a given target name.
DString getAsString (const DString &name) const
 Gets the resource data as a C string.

Static Public Member Functions

static ResourceMgrinstance ()
 Returns the one and only instance of this class.

Private Member Functions

const Resourceget (const DString &name) const
 Returns a pointer to the resource object with the given name.
 ResourceMgr ()
 ~ResourceMgr ()

Private Attributes

std::unique_ptr< Privatep

Detailed Description

Singleton for managing resources compiled into an executable.

Definition at line 36 of file resourcemgr.h.

Constructor & Destructor Documentation

◆ ResourceMgr()

ResourceMgr::ResourceMgr ( )
private

Definition at line 42 of file resourcemgr.cpp.

42 : p(std::make_unique<Private>())
43{
44}
std::unique_ptr< Private > p
Definition resourcemgr.h:66

References p.

Referenced by instance().

◆ ~ResourceMgr()

ResourceMgr::~ResourceMgr ( )
private

Definition at line 46 of file resourcemgr.cpp.

47{
48}

Member Function Documentation

◆ copyResource()

bool ResourceMgr::copyResource ( const DString & name,
const DString & targetDir ) const

Copies a registered resource to a given target directory.

Definition at line 125 of file resourcemgr.cpp.

126{
127 return copyResourceAs(name,targetDir,name);
128}
bool copyResourceAs(const DString &name, const DString &targetDir, const DString &targetName, bool append=false) const
Copies a registered resource to a given target directory under a given target name.

References copyResourceAs().

Referenced by CitationManager::generatePage(), generateXML(), HtmlGenerator::init(), HtmlGenerator::writeSearchPage(), and HtmlGenerator::writeTabData().

◆ copyResourceAs()

bool ResourceMgr::copyResourceAs ( const DString & name,
const DString & targetDir,
const DString & targetName,
bool append = false ) const

Copies a registered resource to a given target directory under a given target name.

Definition at line 82 of file resourcemgr.cpp.

83{
84 DString pathName = targetDir+"/"+targetName;
85 const Resource *res = get(name);
86 if (res)
87 {
88 switch (res->type)
89 {
91 {
92 std::ofstream f = Portable::openOutputStream(pathName,append);
93 bool ok=false;
94 if (f.is_open())
95 {
96 f.write(reinterpret_cast<const char *>(res->data),res->size);
97 ok = !f.fail();
98 }
99 if (ok)
100 {
101 return true;
102 }
103 }
104 break;
105 case Resource::SVG:
106 {
107 std::ofstream t = Portable::openOutputStream(pathName,append);
108 if (t.is_open())
109 {
110 DString buf(res->size, DString::ExplicitSize);
111 memcpy(buf.rawData(),res->data,res->size);
112 t << replaceColorMarkers(buf);
113 return true;
114 }
115 }
116 }
117 }
118 else
119 {
120 err("requested resource '{}' not compiled in!\n",name);
121 }
122 return false;
123}
@ ExplicitSize
Definition dstring.h:131
const Resource * get(const DString &name) const
Returns a pointer to the resource object with the given name.
#define err(fmt,...)
Definition message.h:127
std::ofstream openOutputStream(const DString &name, bool append=false)
Definition portable.cpp:681
const unsigned char * data
Definition resourcemgr.h:30
Type type
Definition resourcemgr.h:32
DString replaceColorMarkers(const DString &str)
Replaces any markers of the form ##AA in input string str by new markers of the form #AABBCC,...
Definition util.cpp:4568

References Resource::data, err, DString::ExplicitSize, get(), Portable::openOutputStream(), DString::rawData(), replaceColorMarkers(), Resource::size, Resource::SVG, Resource::type, and Resource::Verbatim.

Referenced by copyResource().

◆ get()

const Resource * ResourceMgr::get ( const DString & name) const
private

Returns a pointer to the resource object with the given name.

Definition at line 130 of file resourcemgr.cpp.

131{
132 auto it = p->resources.find(name.str());
133 if (it!=p->resources.end()) return &it->second;
134 return nullptr;
135}
const std::string & str() const
Definition dstring.h:645

References p, and DString::str().

Referenced by copyResourceAs(), and getAsString().

◆ getAsString()

◆ instance()

◆ registerResources()

void ResourceMgr::registerResources ( std::initializer_list< Resource > resources)

Registers an array of resources.

Definition at line 50 of file resourcemgr.cpp.

51{
52 for (auto &res : resources)
53 {
54 p->resources.emplace(res.name,res);
55 }
56}

References p.

◆ writeCategory()

bool ResourceMgr::writeCategory ( const DString & categoryName,
const DString & targetDir ) const

Writes all resource belonging to a given category to a given target directory.

Definition at line 58 of file resourcemgr.cpp.

59{
60 for (auto &[name,res] : p->resources)
61 {
62 if (res.category==categoryName)
63 {
64 DString pathName = targetDir+"/"+res.name;
65 std::ofstream f = Portable::openOutputStream(pathName);
66 bool ok=false;
67 if (f.is_open())
68 {
69 f.write(reinterpret_cast<const char *>(res.data),res.size);
70 ok = !f.fail();
71 }
72 if (!ok)
73 {
74 err("Failed to write resource '{}' to directory '{}'\n",res.name,targetDir);
75 return false;
76 }
77 }
78 }
79 return true;
80}

References err, Portable::openOutputStream(), and p.

Member Data Documentation

◆ p

std::unique_ptr<Private> ResourceMgr::p
private

Definition at line 66 of file resourcemgr.h.

Referenced by get(), registerResources(), ResourceMgr(), and writeCategory().


The documentation for this class was generated from the following files: