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
#include <map>
17
#include <string.h>
18
#include <cstdint>
19
20
#include "
resourcemgr.h
"
21
#include "
util.h
"
22
#include "version.h"
23
#include "
message.h
"
24
#include "
config.h
"
25
#include "
portable.h
"
26
27
class
ResourceMgr::Private
28
{
29
public
:
30
std::map<std::string,Resource>
resources
;
31
};
32
33
ResourceMgr
&
ResourceMgr::instance
()
34
{
35
static
ResourceMgr
theInstance;
36
return
theInstance;
37
}
38
39
ResourceMgr::ResourceMgr
() :
p
(std::make_unique<
Private
>())
40
{
41
}
42
43
ResourceMgr::~ResourceMgr
()
44
{
45
}
46
47
void
ResourceMgr::registerResources
(std::initializer_list<Resource> resources)
48
{
49
for
(
auto
&res : resources)
50
{
51
p
->resources.emplace(res.name,res);
52
}
53
}
54
55
bool
ResourceMgr::writeCategory
(
const
QCString
&categoryName,
const
QCString
&targetDir)
const
56
{
57
for
(
auto
&[name,res] :
p
->resources)
58
{
59
if
(res.category==categoryName)
60
{
61
QCString
pathName = targetDir+
"/"
+res.name;
62
std::ofstream f =
Portable::openOutputStream
(pathName);
63
bool
ok=
false
;
64
if
(f.is_open())
65
{
66
f.write(
reinterpret_cast<
const
char
*
>
(res.data),res.size);
67
ok = !f.fail();
68
}
69
if
(!ok)
70
{
71
err
(
"Failed to write resource '{}' to directory '{}'\n"
,res.name,targetDir);
72
return
FALSE
;
73
}
74
}
75
}
76
return
TRUE
;
77
}
78
79
bool
ResourceMgr::copyResourceAs
(
const
QCString
&name,
const
QCString
&targetDir,
const
QCString
&targetName,
bool
append)
const
80
{
81
QCString
pathName = targetDir+
"/"
+targetName;
82
const
Resource
*res =
get
(name);
83
if
(res)
84
{
85
switch
(res->
type
)
86
{
87
case
Resource::Verbatim
:
88
{
89
std::ofstream f =
Portable::openOutputStream
(pathName,append);
90
bool
ok=
false
;
91
if
(f.is_open())
92
{
93
f.write(
reinterpret_cast<
const
char
*
>
(res->
data
),res->
size
);
94
ok = !f.fail();
95
}
96
if
(ok)
97
{
98
return
TRUE
;
99
}
100
}
101
break
;
102
case
Resource::SVG
:
103
{
104
std::ofstream t =
Portable::openOutputStream
(pathName,append);
105
if
(t.is_open())
106
{
107
QCString
buf(res->
size
,
QCString::ExplicitSize
);
108
memcpy(buf.
rawData
(),res->
data
,res->
size
);
109
t <<
replaceColorMarkers
(buf);
110
return
TRUE
;
111
}
112
}
113
}
114
}
115
else
116
{
117
err
(
"requested resource '{}' not compiled in!\n"
,name);
118
}
119
return
FALSE
;
120
}
121
122
bool
ResourceMgr::copyResource
(
const
QCString
&name,
const
QCString
&targetDir)
const
123
{
124
return
copyResourceAs
(name,targetDir,name);
125
}
126
127
const
Resource
*
ResourceMgr::get
(
const
QCString
&name)
const
128
{
129
auto
it =
p
->resources.find(name.
str
());
130
if
(it!=
p
->resources.end())
return
&it->second;
131
return
nullptr
;
132
}
133
134
QCString
ResourceMgr::getAsString
(
const
QCString
&name)
const
135
{
136
const
Resource
*res =
get
(name);
137
if
(res)
138
{
139
QCString
result(res->
size
,
QCString::ExplicitSize
);
140
memcpy(result.
rawData
(),res->
data
,res->
size
);
141
return
result;
142
}
143
else
144
{
145
return
QCString
();
146
}
147
}
148
QCString
This is an alternative implementation of QCString.
Definition
qcstring.h:101
QCString::rawData
char * rawData()
Returns a writable pointer to the data.
Definition
qcstring.h:178
QCString::str
const std::string & str() const
Definition
qcstring.h:552
QCString::ExplicitSize
@ ExplicitSize
Definition
qcstring.h:146
ResourceMgr::Private
Definition
resourcemgr.cpp:28
ResourceMgr::Private::resources
std::map< std::string, Resource > resources
Definition
resourcemgr.cpp:30
ResourceMgr::instance
static ResourceMgr & instance()
Returns the one and only instance of this class.
Definition
resourcemgr.cpp:33
ResourceMgr::get
const Resource * get(const QCString &name) const
Returns a pointer to the resource object with the given name.
Definition
resourcemgr.cpp:127
ResourceMgr::copyResource
bool copyResource(const QCString &name, const QCString &targetDir) const
Copies a registered resource to a given target directory.
Definition
resourcemgr.cpp:122
ResourceMgr::writeCategory
bool writeCategory(const QCString &categoryName, const QCString &targetDir) const
Writes all resource belonging to a given category to a given target directory.
Definition
resourcemgr.cpp:55
ResourceMgr::getAsString
QCString getAsString(const QCString &name) const
Gets the resource data as a C string.
Definition
resourcemgr.cpp:134
ResourceMgr::copyResourceAs
bool copyResourceAs(const QCString &name, const QCString &targetDir, const QCString &targetName, bool append=false) const
Copies a registered resource to a given target directory under a given target name.
Definition
resourcemgr.cpp:79
ResourceMgr::ResourceMgr
ResourceMgr()
Definition
resourcemgr.cpp:39
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:47
ResourceMgr::~ResourceMgr
~ResourceMgr()
Definition
resourcemgr.cpp:43
config.h
message.h
err
#define err(fmt,...)
Definition
message.h:127
Portable::openOutputStream
std::ofstream openOutputStream(const QCString &name, bool append=false)
Definition
portable.cpp:649
portable.h
Portable versions of functions that are platform dependent.
TRUE
#define TRUE
Definition
qcstring.h:37
FALSE
#define FALSE
Definition
qcstring.h:34
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
QCString replaceColorMarkers(const QCString &str)
Replaces any markers of the form ##AA in input string str by new markers of the form #AABBCC,...
Definition
util.cpp:5806
util.h
A bunch of utility functions.
src
resourcemgr.cpp
Generated by
1.17.0