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 #include <qdict.h>
16 #include <qfile.h>
17 #include <qcstring.h>
18 #include <qglobal.h>
19 #include <string.h>
20 
21 #include "resourcemgr.h"
22 #include "util.h"
23 #include "version.h"
24 #include "ftextstream.h"
25 #include "message.h"
26 #include "config.h"
27 
29 {
30  public:
31  Private() : resources(257) {}
32  QDict<Resource> resources;
33 };
34 
36 {
37  static ResourceMgr theInstance;
38  return theInstance;
39 }
40 
42 {
43  p = new Private;
44 }
45 
47 {
48  delete p;
49 }
50 
51 void ResourceMgr::registerResources(const Resource resources[],int numResources)
52 {
53  for (int i=0;i<numResources;i++)
54  {
55  p->resources.insert(resources[i].name,&resources[i]);
56  }
57 }
58 
59 bool ResourceMgr::copyCategory(const char *categoryName,const char *targetDir) const
60 {
61  QDictIterator<Resource> it(p->resources);
62  const Resource *res;
63  for (it.toFirst();(res=it.current());++it)
64  {
65  if (qstrcmp(res->category,categoryName)==0)
66  {
67  if (!copyResource(res->name,targetDir))
68  {
69  return FALSE;
70  }
71  }
72  }
73  return TRUE;
74 }
75 
76 bool ResourceMgr::copyResourceAs(const char *name,const char *targetDir,const char *targetName) const
77 {
78  QCString pathName = QCString(targetDir)+"/"+targetName;
79  const Resource *res = get(name);
80  if (res)
81  {
82  switch (res->type)
83  {
84  case Resource::Verbatim:
85  {
86  QFile f(pathName);
87  if (f.open(IO_WriteOnly) && f.writeBlock((const char *)res->data,res->size)==res->size)
88  {
89  return TRUE;
90  }
91  }
92  break;
94  {
95  QCString n = name;
96  n = n.left(n.length()-4)+".png"; // replace .lum by .png
97  uchar *p = (uchar*)res->data;
98  int width = (p[0]<<8)+p[1];
99  int height = (p[2]<<8)+p[3];
100  ColoredImgDataItem images[2];
101  images[0].name = n;
102  images[0].width = width;
103  images[0].height = height;
104  images[0].content = &p[4];
105  images[0].alpha = 0;
106  images[1].name = 0; // terminator
107  writeColoredImgData(targetDir,images);
108  return TRUE;
109  }
110  break;
111  case Resource::LumAlpha:
112  {
113  QCString n = name;
114  n = n.left(n.length()-5)+".png"; // replace .luma by .png
115  uchar *p = (uchar*)res->data;
116  int width = (p[0]<<8)+p[1];
117  int height = (p[2]<<8)+p[3];
118  ColoredImgDataItem images[2];
119  images[0].name = n;
120  images[0].width = width;
121  images[0].height = height;
122  images[0].content = &p[4];
123  images[0].alpha = &p[4+width*height];
124  images[1].name = 0; // terminator
125  writeColoredImgData(targetDir,images);
126  return TRUE;
127  }
128  break;
129  case Resource::CSS:
130  {
131  QFile f(pathName);
132  if (f.open(IO_WriteOnly))
133  {
134  QCString buf(res->size+1);
135  memcpy(buf.rawData(),res->data,res->size);
136  FTextStream t(&f);
137  buf = replaceColorMarkers(buf);
138  if (qstrcmp(name,"navtree.css")==0)
139  {
140  t << substitute(buf,"$width",QCString().setNum(Config_getInt("TREEVIEW_WIDTH"))+"px");
141  }
142  else
143  {
144  t << substitute(buf,"$doxygenversion",versionString);
145  }
146  return TRUE;
147  }
148  }
149  break;
150  }
151  }
152  else
153  {
154  err("requested resource '%s' not compiled in!\n",name);
155  }
156  return FALSE;
157 }
158 
159 bool ResourceMgr::copyResource(const char *name,const char *targetDir) const
160 {
161  return copyResourceAs(name,targetDir,name);
162 }
163 
164 const Resource *ResourceMgr::get(const char *name) const
165 {
166  return p->resources.find(name);
167 }
168 
170 {
171  const Resource *res = get(name);
172  if (res)
173  {
174  QCString result(res->size+1);
175  memcpy(result.rawData(),res->data,res->size);
176  return result;
177  }
178  else
179  {
180  return QCString();
181  }
182 }
183 
static QCString name
Definition: declinfo.cpp:673
char * rawData() const
Definition: qcstring.h:216
unsigned short width
Definition: util.h:437
QCString getAsString(const char *name) const
static QCString result
uint length() const
Definition: qcstring.h:195
unsigned char * alpha
Definition: util.h:440
#define IO_WriteOnly
Definition: qiodevice.h:62
unsigned char * content
Definition: util.h:439
const bool FALSE
Definition: qglobal.h:370
bool copyResource(const char *name, const char *targetDir) const
static ResourceMgr & instance()
Definition: resourcemgr.cpp:35
QCString left(uint len) const
Definition: qcstring.cpp:213
void writeColoredImgData(const char *dir, ColoredImgDataItem data[])
Definition: util.cpp:7889
Simplified and optimized version of QTextStream.
Definition: ftextstream.h:11
const Resource * get(const char *name) const
unsigned char uchar
Definition: nybbler.cc:11
Compiled resource.
Definition: resourcemgr.h:21
#define Config_getInt(val)
Definition: config.cpp:661
const char * name
Definition: util.h:436
int writeBlock(const char *data, uint len)
Definition: qfile_unix.cpp:537
QDict< Resource > resources
Definition: resourcemgr.cpp:32
const unsigned char * data
Definition: resourcemgr.h:26
Singleton for managing resources compiled into an executable.
Definition: resourcemgr.h:32
std::void_t< T > n
bool open(int)
Definition: qfile_unix.cpp:134
char versionString[]
Definition: version.cpp:1
unsigned short height
Definition: util.h:438
A bunch of utility functions.
int size
Definition: resourcemgr.h:27
const char * name
Definition: resourcemgr.h:25
void err(const char *fmt,...)
Definition: message.cpp:226
The QFile class is an I/O device that operates on files.
Definition: qfile.h:50
Private * p
Definition: resourcemgr.h:59
bool copyCategory(const char *categoryName, const char *targetDir) const
Definition: resourcemgr.cpp:59
bool copyResourceAs(const char *name, const char *targetDir, const char *targetName) const
Definition: resourcemgr.cpp:76
Type type
Definition: resourcemgr.h:28
QCString replaceColorMarkers(const char *str)
Definition: util.cpp:7919
void registerResources(const Resource resources[], int numResources)
Definition: resourcemgr.cpp:51
Q_EXPORT int qstrcmp(const char *str1, const char *str2)
Definition: qcstring.h:95
const bool TRUE
Definition: qglobal.h:371
QCString substitute(const QCString &s, const QCString &src, const QCString &dst)
substitute all occurrences of src in s by dst
Definition: util.cpp:5088
const char * category
Definition: resourcemgr.h:24