Doxygen
Toggle main menu visibility
Loading...
Searching...
No Matches
resourcemgr.cpp
Go to the documentation of this file.
1
/******************************************************************************
2
*
3
* Copyright (C) 1997-2015 by Dimitri van Heesch.
4
*
5
* Permission to use, copy, modify, and distribute this software and its
6
* documentation under the terms of the GNU General Public License is hereby
7
* granted. No representations are made about the suitability of this software
8
* for any purpose. It is provided "as is" without express or implied warranty.
9
* See the GNU General Public License for more details.
10
*
11
* Documents produced by Doxygen are derivative works derived from the
12
* input used in their production; they are not affected by this license.
13
*
14
*/
15
16
// own header
17
#include "
resourcemgr.h
"
18
19
// standard includes
20
#include <cstdint>
21
#include <map>
22
#include <memory>
23
#include <string>
24
25
// other includes
26
#include "
message.h
"
27
#include "
portable.h
"
28
#include "
util.h
"
29
30
class
ResourceMgr::Private
31
{
32
public
:
33
std::map<std::string,Resource>
resources
;
34
};
35
36
ResourceMgr
&
ResourceMgr::instance
()
37
{
38
static
ResourceMgr
theInstance;
39
return
theInstance;
40
}
41
42
ResourceMgr::ResourceMgr
() :
p
(
std
::make_unique<
Private
>())
43
{
44
}
45
46
ResourceMgr::~ResourceMgr
()
47
{
48
}
49
50
void
ResourceMgr::registerResources
(std::initializer_list<Resource> resources)
51
{
52
for
(
auto
&res : resources)
53
{
54
p
->resources.emplace(res.name,res);
55
}
56
}
57
58
bool
ResourceMgr::writeCategory
(
const
DString
&categoryName,
const
DString
&targetDir)
const
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
}
81
82
bool
ResourceMgr::copyResourceAs
(
const
DString
&name,
const
DString
&targetDir,
const
DString
&targetName,
bool
append)
const
83
{
84
DString
pathName = targetDir+
"/"
+targetName;
85
const
Resource
*res =
get
(name);
86
if
(res)
87
{
88
switch
(res->
type
)
89
{
90
case
Resource::Verbatim
:
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
}
124
125
bool
ResourceMgr::copyResource
(
const
DString
&name,
const
DString
&targetDir)
const
126
{
127
return
copyResourceAs
(name,targetDir,name);
128
}
129
130
const
Resource
*
ResourceMgr::get
(
const
DString
&name)
const
131
{
132
auto
it =
p
->resources.find(name.
str
());
133
if
(it!=
p
->resources.end())
return
&it->second;
134
return
nullptr
;
135
}
136
137
DString
ResourceMgr::getAsString
(
const
DString
&name)
const
138
{
139
const
Resource
*res =
get
(name);
140
if
(res)
141
{
142
DString
result(res->
size
,
DString::ExplicitSize
);
143
memcpy(result.
rawData
(),res->
data
,res->
size
);
144
return
result;
145
}
146
else
147
{
148
return
DString
();
149
}
150
}
151
DString
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition
dstring.h:84
DString::rawData
char * rawData()
Returns a writable pointer to the data.
Definition
dstring.h:166
DString::ExplicitSize
@ ExplicitSize
Definition
dstring.h:131
DString::str
const std::string & str() const
Definition
dstring.h:645
ResourceMgr::Private
Definition
resourcemgr.cpp:31
ResourceMgr::Private::resources
std::map< std::string, Resource > resources
Definition
resourcemgr.cpp:33
ResourceMgr::instance
static ResourceMgr & instance()
Returns the one and only instance of this class.
Definition
resourcemgr.cpp:36
ResourceMgr::copyResource
bool copyResource(const DString &name, const DString &targetDir) const
Copies a registered resource to a given target directory.
Definition
resourcemgr.cpp:125
ResourceMgr::copyResourceAs
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.
Definition
resourcemgr.cpp:82
ResourceMgr::getAsString
DString getAsString(const DString &name) const
Gets the resource data as a C string.
Definition
resourcemgr.cpp:137
ResourceMgr::writeCategory
bool writeCategory(const DString &categoryName, const DString &targetDir) const
Writes all resource belonging to a given category to a given target directory.
Definition
resourcemgr.cpp:58
ResourceMgr::get
const Resource * get(const DString &name) const
Returns a pointer to the resource object with the given name.
Definition
resourcemgr.cpp:130
ResourceMgr::ResourceMgr
ResourceMgr()
Definition
resourcemgr.cpp:42
ResourceMgr::p
std::unique_ptr< Private > p
Definition
resourcemgr.h:66
ResourceMgr::registerResources
void registerResources(std::initializer_list< Resource > resources)
Registers an array of resources.
Definition
resourcemgr.cpp:50
ResourceMgr::~ResourceMgr
~ResourceMgr()
Definition
resourcemgr.cpp:46
message.h
err
#define err(fmt,...)
Definition
message.h:127
Portable::openOutputStream
std::ofstream openOutputStream(const DString &name, bool append=false)
Definition
portable.cpp:681
std
Definition
dstring.h:913
portable.h
Portable versions of functions that are platform dependent.
resourcemgr.h
Resource
Compiled resource.
Definition
resourcemgr.h:26
Resource::size
int size
Definition
resourcemgr.h:31
Resource::SVG
@ SVG
Definition
resourcemgr.h:27
Resource::Verbatim
@ Verbatim
Definition
resourcemgr.h:27
Resource::data
const unsigned char * data
Definition
resourcemgr.h:30
Resource::type
Type type
Definition
resourcemgr.h:32
replaceColorMarkers
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
util.h
A bunch of utility functions.
src
resourcemgr.cpp
Generated by
1.19.0