My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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::writeCategory(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  QCString pathName = QCString(targetDir)+"/"+res->name;
68  QFile f(pathName);
69  if (!f.open(IO_WriteOnly) || f.writeBlock((const char *)res->data,res->size)!=res->size)
70  {
71  err("Failed to write resource '%s' to directory '%s'\n",res->name,targetDir);
72  return FALSE;
73  }
74  }
75  }
76  return TRUE;
77 }
78 
79 bool ResourceMgr::copyResourceAs(const char *name,const char *targetDir,const char *targetName) const
80 {
81  QCString pathName = QCString(targetDir)+"/"+targetName;
82  const Resource *res = get(name);
83  if (res)
84  {
85  switch (res->type)
86  {
87  case Resource::Verbatim:
88  {
89  QFile f(pathName);
90  if (f.open(IO_WriteOnly) && f.writeBlock((const char *)res->data,res->size)==res->size)
91  {
92  return TRUE;
93  }
94  }
95  break;
97  {
98  QCString n = name;
99  n = n.left(n.length()-4)+".png"; // replace .lum by .png
100  uchar *p = (uchar*)res->data;
101  int width = (p[0]<<8)+p[1];
102  int height = (p[2]<<8)+p[3];
103  ColoredImgDataItem images[2];
104  images[0].name = n;
105  images[0].width = width;
106  images[0].height = height;
107  images[0].content = &p[4];
108  images[0].alpha = 0;
109  images[1].name = 0; // terminator
110  writeColoredImgData(targetDir,images);
111  return TRUE;
112  }
113  break;
114  case Resource::LumAlpha:
115  {
116  QCString n = name;
117  n = n.left(n.length()-5)+".png"; // replace .luma by .png
118  uchar *p = (uchar*)res->data;
119  int width = (p[0]<<8)+p[1];
120  int height = (p[2]<<8)+p[3];
121  ColoredImgDataItem images[2];
122  images[0].name = n;
123  images[0].width = width;
124  images[0].height = height;
125  images[0].content = &p[4];
126  images[0].alpha = &p[4+width*height];
127  images[1].name = 0; // terminator
128  writeColoredImgData(targetDir,images);
129  return TRUE;
130  }
131  break;
132  case Resource::CSS:
133  {
134  QFile f(pathName);
135  if (f.open(IO_WriteOnly))
136  {
137  QCString buf(res->size+1);
138  memcpy(buf.rawData(),res->data,res->size);
139  FTextStream t(&f);
140  buf = replaceColorMarkers(buf);
141  if (qstrcmp(name,"navtree.css")==0)
142  {
143  t << substitute(buf,"$width",QCString().setNum(Config_getInt(TREEVIEW_WIDTH))+"px");
144  }
145  else
146  {
147  t << substitute(buf,"$doxygenversion",versionString);
148  }
149  return TRUE;
150  }
151  }
152  break;
153  }
154  }
155  else
156  {
157  err("requested resource '%s' not compiled in!\n",name);
158  }
159  return FALSE;
160 }
161 
162 bool ResourceMgr::copyResource(const char *name,const char *targetDir) const
163 {
164  return copyResourceAs(name,targetDir,name);
165 }
166 
167 const Resource *ResourceMgr::get(const char *name) const
168 {
169  return p->resources.find(name);
170 }
171 
172 QCString ResourceMgr::getAsString(const char *name) const
173 {
174  const Resource *res = get(name);
175  if (res)
176  {
177  QCString result(res->size+1);
178  memcpy(result.rawData(),res->data,res->size);
179  return result;
180  }
181  else
182  {
183  return QCString();
184  }
185 }
186