1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
%% options
copyright owner = Dirk Krause
copyright year = 2013-xxxx
SPDX-License-Identifier: BSD-3-Clause
%% header
#ifdef __cplusplus
extern "C" {
#endif
/** Add one file name to the list of files to copy.
@param job Job structure.
@param fn File name.
@return 1 on success, 0 on error.
*/
int
hbtool_add_file(hb_job_t *job, dkChar const *fn);
/** Add one file (probably with CGI arguments) to list of files to copy.
The URL is only added if it references a local file name
in the current working directory without any protocol
specification, (src="filename.png"). For all other URLs we skip
adding and return 1.
@param job Job structure.
@param url URL as specified.
@return 1 on success, 0 on error.
*/
int
hbtool_add_url(hb_job_t *job, dkChar const *url);
/** Check file name. The file must be specified without a path.
The file name must be encodable as URL
so it must be in range 0x00000000 to 0x000000FF.
For Windows systems the file name must be encodable as ANSI,
so the following codes are excluded from the range above:
0x80 to 0x8C, 0x8E, 0x91 to 0x9C, 0x9E to 0x9F.
@param job Job structure.
@param fn File name.
@return 1 on success, 0 on error.
*/
int
hbtool_check_filename(hb_job_t *job, dkChar const *fn);
/** Check file name encoding.
The file name must be encodable as URL
so it must be in range 0x00000000 to 0x000000FF.
For Windows systems the file name must be encodable as ANSI,
so the following codes are excluded from the range above:
0x80 to 0x8C, 0x8E, 0x91 to 0x9C, 0x9E to 0x9F.
@param job Job structure.
@param fn File name.
@return 1 on success, 0 on error.
*/
int
hbtool_check_filename_encoding(hb_job_t *job, dkChar const *fn);
/** Retrieve start character from text.
@param text Text to obtain character from.
@param enc Text encoding.
@return Start character on success, 0UL on error.
*/
dk3_c32_t
hbtool_get_start_char(dkChar const *text, int enc);
/** Enumerate section and subsections.
@param job Job structure.
*/
void
hbtool_enumerate(hb_job_t *job);
/** Write hiearchical header number.
@param job Job structure.
@param nptr Node to write header for.
@return 1 if number was written, 0 otherwise.
*/
int
hbtool_write_header_number(hb_job_t *job, hb_node_t *nptr);
/** Write all titles, separated by arrows.
For the primary node we write the full header, for parent
nodes the short header.
@param job Job structure.
@param nptr Current node to write title for.
@param isprim Flag: Is primary node.
*/
int
hbtool_write_all_titles(hb_job_t *job, hb_node_t *nptr, int isprim);
/** Correct backslashes to slash on Windows systems.
@param str String to correct.
*/
void
hbtool_correct_backslash_to_slash(dkChar *str);
/** Write text (file name) UTF-8 encoded to jobs output file.
@param job Job structure.
@param src String to write.
@return 1 on success, 0 on error.
*/
int
hbtool_write_text_utf(hb_job_t *job, dkChar const *src);
#ifdef __cplusplus
}
#endif
%% module
#include "dk3all.h"
#include "dk3se.h"
#include "htmlbook.h"
#include "dk3unused.h"
$!trace-include
int
hbtool_add_file(hb_job_t *job, dkChar const *fn)
{
dkChar bu[DK3_MAX_PATH]; /* File name buffer */
dkChar *p1; /* Start of file name */
int back = 0;
$? "+ hbtool_add_file \"%!ds\"", TR_STR(fn)
if(fn) {
if(dk3str_len(fn) < DK3_SIZEOF(bu,dkChar)) {
p1 = dk3str_start(fn, NULL);
if(p1) {
dk3str_cpy(bu, p1);
dk3str_chomp(bu, NULL);
if(dk3sto_it_find_like(job->i_files, bu, 0)) {
back = 1;
} else {
p1 = dk3str_dup_app(bu, job->app);
if(p1) {
if(dk3sto_add(job->s_files, p1)) {
back = 1;
} else {
dk3_delete(p1);
}
}
}
}
} else {
dk3app_log_i3(job->app, DK3_LL_ERROR, 65, 66, fn);
}
} $? "- hbtool_add_file %d", back
return back;
}
int
hbtool_check_filename_encoding(hb_job_t *job, dkChar const *fn)
{
int back;
int se; /* Standard encoding to check */
$? "+ hbtool_check_filename_encoding \"%!ds\"", TR_STR(fn)
#if DK3_ON_WINDOWS
se = (DK3SE_ANSI | DK3SE_URL);
#else
if((job->options) & HB_JOB_OPT_CHM) {
se = (DK3SE_ANSI | DK3SE_URL);
} else {
se = DK3SE_URL;
}
#endif
back = dk3se_check(fn,dk3app_get_encoding(job->app),se,NULL,NULL,job->app);
$? "- hbtool_check_filename_encoding %d", back
return back;
}
int
hbtool_check_filename(hb_job_t *job, dkChar const *fn)
{
int back;
$? "+ hbtool_check_filename \"%!ds\"", TR_STR(fn)
back = hbtool_check_filename_encoding(job, fn);
if(dk3str_chr(fn, DK3_CHAR_SEP)) {
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 12);
back = 0;
} $? "- hbtool_check_filename %d", back
return back;
}
dk3_c32_t
hbtool_get_start_char(
dkChar const *text,
#if DK3_CHAR_SIZE > 1
int DK3_ARG_UNUSED(enc)
#else
int enc
#endif
)
{
dk3_c32_t back = 0UL;
dk3_c32_t c32 = 0UL; /* Start character */
size_t sl; /* String length */
size_t used; /* Number of bytes used */
#if DK3_CHAR_SIZE <= 1
char c; /* Result character */
#endif
#if DK3_CHAR_SIZE > 1
DK3_UNUSED_ARG(enc)
#endif
$? "+ hbtool_get_start_char"
if(text) {
sl = dk3str_len(text);
#if DK3_CHAR_SIZE > 1
#if DK3_CHAR_SIZE > 2
back = *text;
#else
used = 0;
if(dk3enc_utf162uc(&c32, text, sl, &used)) {
back = c32;
}
#endif
#else
if(enc == DK3_ENCODING_UTF8) {
used = 0;
if(dk3enc_utf82uc(&c32, (unsigned char const *)text, sl, &used)) {
back = c32;
}
} else {
c = *text;
back = (dk3_c32_t)c;
back &= ((dk3_c32_t)0x000000FFUL);
}
#endif
} $? "- hbtool_get_start_char 0x%lx %lu", dk3enc_uc_to_ul(back), dk3enc_uc_to_ul(back)
return back;
}
void
hbtool_enumerate(hb_job_t *job)
{
hb_node_t *nptr; /* Next node */
hb_node_t *cptr; /* Current node */
dk3sto_it_reset(job->i_nodes);
while(NULL != (nptr = (hb_node_t *)dk3sto_it_next(job->i_nodes))) {
nptr->headno = 0UL; nptr->nextsubheadno = 0UL;
nptr->tocstate = HB_TOC_STATE_INIT;
}
cptr = job->rootnode;
while(cptr) {
switch(cptr->tocstate) {
case HB_TOC_STATE_INIT: {
dk3sto_it_reset(cptr->i_subnodes);
nptr = (hb_node_t *)dk3sto_it_next(cptr->i_subnodes);
if(nptr) {
cptr->tocstate = HB_TOC_STATE_IN_PROGRESS;
nptr->headno = cptr->nextsubheadno;
cptr->nextsubheadno += 1UL;
cptr = nptr;
} else {
cptr->tocstate = HB_TOC_STATE_FINISHED;
cptr = cptr->parent;
}
} break;
case HB_TOC_STATE_IN_PROGRESS: {
nptr = (hb_node_t *)dk3sto_it_next(cptr->i_subnodes);
if(nptr) {
nptr->headno = cptr->nextsubheadno;
cptr->nextsubheadno += 1UL;
cptr = nptr;
} else {
cptr->tocstate = HB_TOC_STATE_FINISHED;
cptr = cptr->parent;
}
} break;
case HB_TOC_STATE_FINISHED: {
cptr = cptr->parent;
} break;
}
}
dk3sto_it_reset(job->i_nodes);
while(NULL != (nptr = (hb_node_t *)dk3sto_it_next(job->i_nodes))) {
nptr->tocstate = HB_TOC_STATE_INIT;
}
}
int
hbtool_write_header_number(hb_job_t *job, hb_node_t *nptr)
{
dkChar bu[32];
int back = 0;
$? "+ hbtool_write_header_number"
if((job) && (nptr)) {
if(nptr->parent) { $? ". have parent"
if(nptr->depth <= job->headlevels) { $? ". within limit"
back = 1;
if(job->impressumnode) {
if((job->impressumnode)->objno == nptr->objno) {
back = 0; $? ". this is impressum"
}
}
if(job->privacynode) {
if((job->privacynode)->objno == nptr->objno) {
back = 0; $? ". this is privacy"
}
}
if(back) { $? ". ok to write number"
hbtool_write_header_number(job, nptr->parent);
if(1 < nptr->depth) { $? ". write dot"
hbhtml_output_for_text(job, dkT("."));
}
#if VERSION_BEFORE_20140716
dk3sf_sprintf3(bu, dkT("%lu"), (1UL + nptr->headno));
hbhtml_output_for_text(job, bu); $? ". number written"
#else
if (dk3ma_um_to_string(bu, DK3_SIZEOF(bu,dkChar), (dk3_um_t)(1UL + nptr->headno))) {
hbhtml_output_for_text(job, bu);
}
#endif
} else { $? ". do not write number"
}
} else { $? ". not in limit"
}
} else { $? ". no parent"
}
} $? "- hbtool_write_header_number %d", back
return back;
}
int
hbtool_add_url(hb_job_t *job, dkChar const *url)
{
dkChar bu[DK3_MAX_PATH]; /* Copy of URL */
dkChar *p1; /* Start of text in url */
int back = 1;
if(!hbhtml_check_link_for_external(url)) {
if(dk3str_len(url) < DK3_SIZEOF(bu,dkChar)) {
dk3str_cpy(bu, url);
p1 = dk3str_chr(bu, dkT('?'));
if(p1) {
*p1 = dkT('\0');
} else {
p1 = dk3str_chr(bu, dkT('#'));
if(p1) {
*p1 = dkT('\0');
}
}
if(NULL == dk3str_chr(bu, dkT('/'))) {
if(0 < dk3str_len(bu)) {
back = hbtool_add_file(job, bu);
}
}
} else {
/* ERROR: File name too long! */
dk3app_log_i3(job->app, DK3_LL_ERROR, 65, 66, url);
back = 0;
}
}
return back;
}
void
hbtool_correct_backslash_to_slash(dkChar * DK3_ARG_UNUSED(str) )
{
#if DK3_ON_WINDOWS
dkChar *ptr;
ptr = str;
while(*ptr) {
if(dkT('\\') == *ptr) { *ptr = dkT('/'); }
ptr++;
}
#else
DK3_UNUSED_ARG(str)
#endif
}
int
hbtool_write_all_titles(hb_job_t *job, hb_node_t *nptr, int isprim)
{
int back = 0;
/* hbhtml_output_for_text(job, dkT(" ")); */
if ((job) && (nptr)) {
if (nptr->parent) {
back = 1;
if (job->impressumnode) {
if ((job->impressumnode)->objno == nptr->objno) {
back = 0;
}
}
if (job->privacynode) {
if ((job->privacynode)->objno == nptr->objno) {
back = 0;
}
}
if (back) {
if (hbtool_write_all_titles(job, nptr->parent, 0)) {
hbhtml_output_for_text(job, dkT(" → "));
}
if (isprim) {
if (nptr->title) {
hbhtml_output_for_text(job, nptr->title);
}
} else {
if (nptr->shorttitle) {
hbhtml_output_for_text(job, nptr->shorttitle);
} else {
if (nptr->title) {
hbhtml_output_for_text(job, nptr->title);
}
}
}
}
}
}
return back;
}
int
hbtool_write_text_utf(hb_job_t *job, dkChar const *src)
{
char bu[(1024 < DK3_MAX_PATH) ? DK3_MAX_PATH : 1024];
int back = 0;
int ie;
if ((job) && (src)) {
ie = dk3app_get_encoding(job->app);
if (dk3str_to_c8u_app(bu, sizeof(bu), src, ie, job->app)) {
if (EOF != fputs(bu, job->of)) {
back = 1;
}
}
}
return back;
}
|