1 /******************************************************************************
3 * Copyright (C) 1997-2015 by Dimitri van Heesch.
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.
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
20 #include "resourcemgr.h"
26 class ResourceMgr::Private
29 std::map<std::string,Resource> resources;
32 ResourceMgr &ResourceMgr::instance()
34 static ResourceMgr theInstance;
38 ResourceMgr::ResourceMgr() : p(std::make_unique<Private>())
42 ResourceMgr::~ResourceMgr()
46 void ResourceMgr::registerResources(std::initializer_list<Resource> resources)
48 for (auto &res : resources)
50 p->resources.insert({res.name,res});
54 bool ResourceMgr::writeCategory(const QCString &categoryName,const QCString &targetDir) const
56 for (auto &kv : p->resources)
58 Resource &res = kv.second;
59 if (res.category==categoryName)
61 std::string pathName = targetDir.str()+"/"+res.name;
62 std::ofstream f(pathName,std::ofstream::out | std::ofstream::binary);
66 f.write(reinterpret_cast<const char *>(res.data),res.size);
71 err("Failed to write resource '%s' to directory '%s'\n",res.name,qPrint(targetDir));
79 bool ResourceMgr::copyResourceAs(const QCString &name,const QCString &targetDir,const QCString &targetName,bool append) const
81 std::string pathName = targetDir.str()+"/"+targetName.str();
82 std::ios_base::openmode mode = std::ofstream::out | std::ofstream::binary;
83 if (append) mode |= std::ofstream::app;
84 const Resource *res = get(name);
89 case Resource::Verbatim:
91 std::ofstream f(pathName,mode);
95 f.write(reinterpret_cast<const char *>(res->data),res->size);
104 case Resource::Luminance:
107 n = n.left(n.length()-4)+".png"; // replace .lum by .png
108 const uchar *data = res->data;
109 ushort width = (data[0]<<8)+data[1];
110 ushort height = (data[2]<<8)+data[3];
111 ColoredImgDataItem images[2];
112 images[0].name = n.data();
113 images[0].width = width;
114 images[0].height = height;
115 images[0].content = &data[4];
117 images[1].name = 0; // terminator
118 writeColoredImgData(targetDir,images);
122 case Resource::LumAlpha:
125 n = n.left(n.length()-5)+".png"; // replace .luma by .png
126 const uchar *data = res->data;
127 ushort width = (data[0]<<8)+data[1];
128 ushort height = (data[2]<<8)+data[3];
129 ColoredImgDataItem images[2];
130 images[0].name = n.data();
131 images[0].width = width;
132 images[0].height = height;
133 images[0].content = &data[4];
134 images[0].alpha = &data[4+width*height];
135 images[1].name = 0; // terminator
136 writeColoredImgData(targetDir,images);
142 std::ofstream t(pathName,mode);
145 QCString buf(res->size+1);
146 memcpy(buf.rawData(),res->data,res->size);
147 buf = replaceColorMarkers(buf);
148 if (name=="navtree.css")
150 t << substitute(buf,"$width",QCString().setNum(Config_getInt(TREEVIEW_WIDTH))+"px");
154 t << substitute(buf,"$doxygenversion",getDoxygenVersion());
162 std::ofstream t(pathName,mode);
165 QCString buf(res->size+1);
166 memcpy(buf.rawData(),res->data,res->size);
167 t << replaceColorMarkers(buf);
175 err("requested resource '%s' not compiled in!\n",qPrint(name));
180 bool ResourceMgr::copyResource(const QCString &name,const QCString &targetDir) const
182 return copyResourceAs(name,targetDir,name);
185 const Resource *ResourceMgr::get(const QCString &name) const
187 auto it = p->resources.find(name.str());
188 if (it!=p->resources.end()) return &it->second;
192 QCString ResourceMgr::getAsString(const QCString &name) const
194 const Resource *res = get(name);
197 QCString result(res->size+1);
198 memcpy(result.rawData(),res->data,res->size);