Doxygen
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
31{
32 public:
33 std::map<std::string,Resource> resources;
34};
35
37{
38 static ResourceMgr theInstance;
39 return theInstance;
40}
41
43{
44}
45
49
50void ResourceMgr::registerResources(std::initializer_list<Resource> resources)
51{
52 for (auto &res : resources)
53 {
54 p->resources.emplace(res.name,res);
55 }
56}
57
58bool 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
82bool 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 {
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 {
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
125bool ResourceMgr::copyResource(const DString &name,const DString &targetDir) const
126{
127 return copyResourceAs(name,targetDir,name);
128}
129
130const 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
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
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
char * rawData()
Returns a writable pointer to the data.
Definition dstring.h:166
@ ExplicitSize
Definition dstring.h:131
const std::string & str() const
Definition dstring.h:645
std::map< std::string, Resource > resources
static ResourceMgr & instance()
Returns the one and only instance of this class.
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.
bool writeCategory(const DString &categoryName, const DString &targetDir) const
Writes all resource belonging to a given category to a given target directory.
const Resource * get(const DString &name) const
Returns a pointer to the resource object with the given name.
std::unique_ptr< Private > p
Definition resourcemgr.h:66
void registerResources(std::initializer_list< Resource > resources)
Registers an array of resources.
#define err(fmt,...)
Definition message.h:127
std::ofstream openOutputStream(const DString &name, bool append=false)
Definition portable.cpp:681
Definition dstring.h:913
Portable versions of functions that are platform dependent.
Compiled resource.
Definition resourcemgr.h:26
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
A bunch of utility functions.