Debian Codegraph @

src/af/xap/xp/xap_ResourceManager.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Program Utilities
4 *
5 * Copyright (C) 2002 Francis James Franklin <[email protected]>
6 * Copyright (C) 2002 AbiSource, Inc.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA.
22 */
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "ut_assert.h"
28 #include "ut_debugmsg.h"
29 #include "ut_string_class.h"
30
31 #include "xap_Resource.h"
32 #include "xap_ResourceManager.h"
33
34 XAP_ResourceManager::XAP_ResourceManager () :
35 m_current(0),
36 m_resource(0),
37 m_resource_count(0),
38 m_resource_max(0),
39 m_id_number(0)
40 {
41 //
42 }
43
44 XAP_ResourceManager::~XAP_ResourceManager ()
45 {
46 for (UT_uint32 i = 0; i < m_resource_count; i++) delete m_resource[i];
47 if (m_resource) g_free (m_resource);
48 }
49
50 const UT_UTF8String XAP_ResourceManager::new_id (bool bInternal)
51 {
52 static const char utf8_hex[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
53
54 char buf[11];
55
56 /* external references (hyperlinks) are remapped from, say, http://this.com/that
57 * to, say, /re_abc123
58 */
59 if (!bInternal)
60 {
61 buf[0] = '/';
62 buf[1] = 'r';
63 buf[2] = 'e';
64 }
65 else
66 {
67 buf[0] = '#';
68 buf[1] = 'r';
69 buf[2] = 'i';
70 }
71 buf[3] = '_';
72
73 if ((m_id_number & 0xffffff) != m_id_number)
74 {
75 UT_DEBUGMSG(("argh!! too many resources! who do you think I am?!\n"));
76 UT_ASSERT((m_id_number & 0xffffff) == m_id_number);
77
78 buf[4] = 0;
79 }
80 else
81 {
82 UT_uint32 number = m_id_number++;
83
84 buf[9] = utf8_hex[number & 0x0f]; number >>= 4;
85 buf[8] = utf8_hex[number & 0x0f]; number >>= 4;
86 buf[7] = utf8_hex[number & 0x0f]; number >>= 4;
87 buf[6] = utf8_hex[number & 0x0f]; number >>= 4;
88 buf[5] = utf8_hex[number & 0x0f]; number >>= 4;
89 buf[4] = utf8_hex[number & 0x0f];
90
91 buf[10] = 0;
92 }
93 return UT_UTF8String(buf);
94 }
95
96 /* returns resource corresponding to href
97 * returns 0 if none is found
98 */
99 XAP_Resource * XAP_ResourceManager::resource (const char * href, bool bInternal, UT_uint32 * index)
100 {
101 m_current = 0;
102
103 if ( href == 0) return 0;
104 if (*href == 0) return 0;
105
106 if (bInternal)
107 {
108 if (*href == '/') return 0;
109 if (*href == '#') href++;
110 }
111 else
112 {
113 if (*href == '#') return 0;
114 if (*href == '/') href++;
115 }
116 if (*href != 'r') return 0;
117
118 for (UT_uint32 i = 0; i < m_resource_count; i++)
119 if (m_resource[i]->bInternal == bInternal)
120 if (strcmp (href, m_resource[i]->name().utf8_str()) == 0)
121 {
122 m_current = m_resource[i];
123 if (index) *index = i;
124 break;
125 }
126
127 return m_current;
128 }
129
130 /* resource objects are created/destroyed via ref/unref
131 * ref() returns false if href is not, and cannot be, created
132 */
133 bool XAP_ResourceManager::ref (const char * href)
134 {
135 if ( href == 0) return false;
136 if (*href == 0) return false;
137
138 bool bInternal = false;
139 if (*href == '#') bInternal = true;
140 else if (*href != '/') return false;
141
142 XAP_Resource * match = resource (href, bInternal);
143 if (match)
144 {
145 match->ref ();
146 return true;
147 }
148 if (!grow ()) return false;
149
150 XAP_Resource * r = 0;
151 try
152 {
153 if (bInternal)
154 r = new XAP_InternalResource (href);
155 else
156 r = new XAP_ExternalResource (href);
157 }
158 catch (...)
159 {
160 r = 0;
161 }
162 if (r == 0) return false;
163
164 m_resource[m_resource_count++] = r;
165 return true;
166 }
167
168 /* resource objects are created/destroyed via ref/unref
169 * ref() returns false if href is not and cannot be created
170 */
171 void XAP_ResourceManager::unref (const char * href)
172 {
173 if ( href == 0) return;
174 if (*href == 0) return;
175
176 bool bInternal = false;
177 if (*href == '#') bInternal = true;
178 else if (*href != '/') return;
179
180 UT_uint32 index;
181 XAP_Resource * match = resource (href, bInternal, &index);
182 if (match == 0) return;
183
184 if (match->unref () > 0) return;
185
186 delete m_resource[index];
187 m_resource_count--;
188 if (index < m_resource_count) m_resource[index] = m_resource[m_resource_count];
189 }
190
191 /* call's writer's write_xml() & write_base64() callbacks
192 */
193 UT_Error XAP_ResourceManager::write_xml (void * context, Writer & writer)
194 {
195 UT_Error err = UT_OK;
196
197 static const char * psz_id = "id";
198 static const char * psz_content_type = "type";
199 static const char * psz_description = "desc";
200
201 const char * atts[8];
202
203 atts[4] = NULL;
204 atts[5] = NULL;
205
206 for (UT_uint32 i = 0; i < m_resource_count; i++)
207 if (m_resource[i]->bInternal)
208 {
209 XAP_InternalResource * ri = dynamic_cast<XAP_InternalResource *>(m_resource[i]);
210
211 UT_uint32 n = 0;
212
213 atts[n++] = psz_id;
214 atts[n++] = ri->name().utf8_str ();
215
216 if (!(ri->type().empty ()))
217 {
218 atts[n++] = psz_content_type;
219 atts[n++] = ri->type().utf8_str ();
220 }
221 if (!(ri->Description.empty ()))
222 {
223 atts[n++] = psz_description;
224 atts[n++] = ri->Description.utf8_str ();
225 }
226 atts[n++] = NULL;
227 atts[n++] = NULL;
228
229 err = writer.write_xml (context, "resource", atts);
230 if (err != UT_OK) break;
231
232 err = ri->write_base64 (context, writer);
233 if (err != UT_OK) break;
234
235 err = writer.write_xml (context, "resource");
236 if (err != UT_OK) break;
237 }
238 return err;
239 }
240
241 bool XAP_ResourceManager::grow ()
242 {
243 if (m_resource_count < m_resource_max) return true;
244
245 if (m_resource == 0)
246 {
247 m_resource = static_cast<XAP_Resource **>(g_try_malloc (8 * sizeof (XAP_Resource *)));
248 if (m_resource == 0) return false;
249 m_resource_max = 8;
250 return true;
251 }
252 XAP_Resource ** more = static_cast<XAP_Resource **>(g_try_realloc (m_resource, (m_resource_max + 8) * sizeof (XAP_Resource *)));
253 if (more == 0) return false;
254 m_resource = more;
255 m_resource_max += 8;
256 return true;
257 }