1 //C- -*- C++ -*-
2 //C- -------------------------------------------------------------------
3 //C- DjVuLibre-3.5
4 //C- Copyright (c) 2002 Leon Bottou and Yann Le Cun.
5 //C- Copyright (c) 2001 AT&T
6 //C-
7 //C- This software is subject to, and may be distributed under, the
8 //C- GNU General Public License, either Version 2 of the license,
9 //C- or (at your option) any later version. The license should have
10 //C- accompanied the software or you may obtain a copy of the license
11 //C- from the Free Software Foundation at http://www.fsf.org .
12 //C-
13 //C- This program is distributed in the hope that it will be useful,
14 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 //C- GNU General Public License for more details.
17 //C-
18 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19 //C- Lizardtech Software. Lizardtech Software has authorized us to
20 //C- replace the original DjVu(r) Reference Library notice by the following
21 //C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22 //C-
23 //C- ------------------------------------------------------------------
24 //C- | DjVu (r) Reference Library (v. 3.5)
25 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26 //C- | The DjVu Reference Library is protected by U.S. Pat. No.
27 //C- | 6,058,214 and patents pending.
28 //C- |
29 //C- | This software is subject to, and may be distributed under, the
30 //C- | GNU General Public License, either Version 2 of the license,
31 //C- | or (at your option) any later version. The license should have
32 //C- | accompanied the software or you may obtain a copy of the license
33 //C- | from the Free Software Foundation at http://www.fsf.org .
34 //C- |
35 //C- | The computer code originally released by LizardTech under this
36 //C- | license and unmodified by other parties is deemed "the LIZARDTECH
37 //C- | ORIGINAL CODE." Subject to any third party intellectual property
38 //C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39 //C- | non-exclusive license to make, use, sell, or otherwise dispose of
40 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42 //C- | General Public License. This grant only confers the right to
43 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44 //C- | the extent such infringement is reasonably necessary to enable
45 //C- | recipient to make, have made, practice, sell, or otherwise dispose
46 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47 //C- | any greater extent that may be necessary to utilize further
48 //C- | modifications or combinations.
49 //C- |
50 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54 //C- +------------------------------------------------------------------
55
56 #ifdef HAVE_CONFIG_H
57 # include "config.h"
58 #endif
59 #if NEED_GNUG_PRAGMAS
60 # pragma implementation
61 #endif
62
63 #include "DjVmDir.h"
64 #include "BSByteStream.h"
65 #include "GURL.h"
66 #include "debug.h"
67
68 #include <ctype.h>
69
70
71 #ifdef HAVE_NAMESPACES
72 namespace DJVU {
73 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
74 }
75 #endif
76 #endif
77
78
79 GP<DjVmDir::File>
create(const GUTF8String & load_name,const GUTF8String & save_name,const GUTF8String & title,const FILE_TYPE file_type)80 DjVmDir::File::create(const GUTF8String &load_name,
81 const GUTF8String &save_name, const GUTF8String &title,
82 const FILE_TYPE file_type)
83 {
84 File *file_ptr=new File();
85 GP<File> file=file_ptr;
86 file_ptr->set_load_name(load_name);
87 file_ptr->set_save_name(save_name);
88 file_ptr->set_title(title);
89 file_ptr->flags=(file_type & TYPE_MASK);
90 return file;
91 }
92
93 const GUTF8String &
check_save_name(const bool xis_bundled)94 DjVmDir::File::check_save_name(const bool xis_bundled)
95 {
96 if(!xis_bundled && !valid_name)
97 {
98 GUTF8String retval=name.length()?name:id;
99 if(GUTF8String(GNativeString(retval)) != retval)
100 {
101 const_cast<bool &>(valid_name)=true;
102 char *buf;
103 GPBuffer<char> gbuf(buf,2*retval.length()+1);
104 char *s=buf;
105 int i=0;
106 for(char c=retval[i++];c;)
107 {
108 static const char hex[]="0123456789ABCDEF";
109 int len=retval.nextChar(i)-i;
110 if(len>1 || ((len == 1)&&(c&0x80)))
111 {
112 do
113 {
114 s++[0]=hex[(c>>4)&0xf];
115 s++[0]=hex[(c&0xf)];
116 c=retval[i++];
117 } while(c && ((--len) > 0));
118 }else
119 {
120 s++[0]=c;
121 c=retval[i++];
122 }
123 }
124 s++[0]=0;
125 oldname=retval;
126 name=buf;
127 }
128 const_cast<bool &>(valid_name)=true;
129 }
130 return *(name.length()?&name:&id);
131 }
132
133 const GUTF8String &
get_save_name(void) const134 DjVmDir::File::get_save_name(void) const
135 {
136 return *(name.length()?&name:&id);
137 }
138
139 void
set_load_name(const GUTF8String & xid)140 DjVmDir::File::set_load_name(const GUTF8String &xid)
141 {
142 GURL url=GURL::UTF8(xid);
143 if(!url.is_valid())
144 {
145 url=GURL::Filename::UTF8(xid);
146 }
147 id=url.fname();
148 }
149
150 void
set_save_name(const GUTF8String & xname)151 DjVmDir::File::set_save_name(const GUTF8String &xname)
152 {
153 GURL url;
154 valid_name=false;
155 if(!xname.length())
156 {
157 GURL url=GURL::UTF8(id);
158 if(!url.is_valid())
159 {
160 name=id;
161 }else
162 {
163 name=url.fname();
164 }
165 }else
166 {
167 GURL url=GURL::UTF8(xname);
168 if(!url.is_valid())
169 {
170 url=GURL::Filename::UTF8(xname);
171 }
172 name=url.fname();
173 }
174 oldname="";
175 }
176
177 /* DjVmDir::File */
178
File(void)179 DjVmDir::File::File(void) : offset(0), size(0), valid_name(false),
180 flags(0), page_num(-1) { }
181
182 GUTF8String
get_str_type(void) const183 DjVmDir::File::get_str_type(void) const
184 {
185 GUTF8String type;
186 switch(flags & TYPE_MASK)
187 {
188 case INCLUDE:
189 type="INCLUDE";
190 break;
191 case PAGE:
192 type="PAGE";
193 break;
194 case THUMBNAILS:
195 type="THUMBNAILS";
196 break;
197 case SHARED_ANNO:
198 type="SHARED_ANNO";
199 break;
200 default:
201 // Internal error: please modify DjVmDir::File::get_str_type()
202 // to contain all possible File types.
203 G_THROW( ERR_MSG("DjVmDir.get_str_type") );
204 }
205 return type;
206 }
207
208
209 const int DjVmDir::version=1;
210
211 void
decode(const GP<ByteStream> & gstr)212 DjVmDir::decode(const GP<ByteStream> &gstr)
213 {
214 ByteStream &str=*gstr;
215 DEBUG_MSG("DjVmDir::decode(): decoding contents of 'DIRM' chunk...\n");
216 DEBUG_MAKE_INDENT(3);
217
218 GCriticalSectionLock lock(&class_lock);
219
220 GPosition pos;
221
222 files_list.empty();
223 page2file.resize(-1);
224 name2file.empty();
225 id2file.empty();
226
227 int ver=str.read8();
228 bool bundled=(ver & 0x80)!=0;
229 ver&=0x7f;
230
231 DEBUG_MSG("DIRM version=" << ver << ", our version=" << version << "\n");
232 if (ver>version)
233 G_THROW( ERR_MSG("DjVmDir.version_error") "\t"
234 + GUTF8String(version) + "\t" + GUTF8String(ver));
235 // Unable to read DJVM directories of versions higher than xxx
236 // Data version number is yyy.
237 DEBUG_MSG("bundled directory=" << bundled << "\n");
238 DEBUG_MSG("reading the directory records...\n");
239 int files=str.read16();
240 DEBUG_MSG("number of files=" << files << "\n");
241
242 if (files)
243 {
244 DEBUG_MSG("reading offsets (and sizes for ver==0)\n");
245 for(int nfile=0;nfile<files;nfile++)
246 {
247 GP<File> file=new File();
248 files_list.append(file);
249 if (bundled)
250 {
251 file->offset=str.read32();
252 if (ver==0)
253 file->size=str.read24();
254 if (file->offset==0)
255 G_THROW( ERR_MSG("DjVmDir.no_indirect") );
256 } else
257 {
258 file->offset=file->size=0;
259 }
260 }
261
262 GP<ByteStream> gbs_str=BSByteStream::create(gstr);
263 ByteStream &bs_str=*gbs_str;
264 if (ver>0)
265 {
266 DEBUG_MSG("reading and decompressing sizes...\n");
267 for(GPosition pos=files_list;pos;++pos)
268 files_list[pos]->size=bs_str.read24();
269 }
270
271 DEBUG_MSG("reading and decompressing flags...\n");
272 for(pos=files_list;pos;++pos)
273 files_list[pos]->flags=bs_str.read8();
274
275 if (!ver)
276 {
277 DEBUG_MSG("converting flags from version 0...\n");
278 for(pos=files_list;pos;++pos)
279 {
280 unsigned char flags_0=files_list[pos]->flags;
281 unsigned char flags_1;
282 flags_1=(flags_0 & File::IS_PAGE_0)?(File::PAGE):(File::INCLUDE);
283 if (flags_0 & File::HAS_NAME_0)
284 flags_1|=File::HAS_NAME;
285 if (flags_0 & File::HAS_TITLE_0)
286 flags_1|=File::HAS_TITLE;
287 files_list[pos]->flags=flags_1;
288 }
289 }
290
291 DEBUG_MSG("reading and decompressing names...\n");
292 GTArray<char> strings;
293 char buffer[1024];
294 int length;
295 while((length=bs_str.read(buffer, 1024)))
296 {
297 int strings_size=strings.size();
298 strings.resize(strings_size+length-1);
299 memcpy((char*) strings+strings_size, buffer, length);
300 }
301 DEBUG_MSG("size of decompressed names block=" << strings.size() << "\n");
302 int strings_size=strings.size();
303 strings.resize(strings_size+3);
304 memset((char*) strings+strings_size, 0, 4);
305
306 // Copy names into the files
307 const char * ptr=strings;
308 for(pos=files_list;pos;++pos)
309 {
310 GP<File> file=files_list[pos];
311 if (ptr >= (const char*)strings + strings_size)
312 G_THROW( ByteStream::EndOfFile );
313 file->id=ptr;
314 ptr+=file->id.length()+1;
315 if (file->flags & File::HAS_NAME)
316 {
317 file->name=ptr;
318 ptr+=file->name.length()+1;
319 }
320 else
321 {
322 file->name=file->id;
323 }
324 if (file->flags & File::HAS_TITLE)
325 {
326 file->title=ptr;
327 ptr+=file->title.length()+1;
328 }
329 else
330 {
331 file->title=file->id;
332 }
333 /* msr debug: multipage file, file->title is null.
334 DEBUG_MSG(file->name << ", " << file->id << ", " << file->title << ", " <<
335 file->offset << ", " << file->size << ", " <<
336 file->is_page() << "\n"); */
337 }
338
339 // Check that there is only one file with SHARED_ANNO flag on
340 int shared_anno_cnt=0;
341 for(pos=files_list;pos;++pos)
342 {
343 if (files_list[pos]->is_shared_anno())
344 {
345 shared_anno_cnt++;
346 }
347 }
348 if (shared_anno_cnt>1)
349 G_THROW( ERR_MSG("DjVmDir.corrupt") );
350
351 // Now generate page=>file array for direct access
352 int pages=0;
353 for(pos=files_list;pos;++pos)
354 pages+=files_list[pos]->is_page() ? 1 : 0;
355 DEBUG_MSG("got " << pages << " pages\n");
356 page2file.resize(pages-1);
357 int page_num=0;
358 for(pos=files_list;pos;++pos)
359 {
360 GP<File> file=files_list[pos];
361 if (file->is_page())
362 {
363 page2file[page_num]=file;
364 file->page_num=page_num++;
365 }
366 }
367
368 // Generate name2file map
369 for(pos=files_list;pos;++pos)
370 {
371 GP<File> file=files_list[pos];
372 if (name2file.contains(file->name))
373 G_THROW( ERR_MSG("DjVmDir.dupl_name") "\t" + file->name );
374 name2file[file->name]=file;
375 }
376
377 // Generate id2file map
378 for(pos=files_list;pos;++pos)
379 {
380 GP<File> file=files_list[pos];
381 if (id2file.contains(file->id))
382 G_THROW( ERR_MSG("DjVmDir.dupl_id") "\t" + file->id);
383 id2file[file->id]=file;
384 }
385 }
386 }
387
388
389 void
encode(const GP<ByteStream> & gstr,const bool do_rename) const390 DjVmDir::encode(const GP<ByteStream> &gstr, const bool do_rename) const
391 {
392 bool bundled = true;
393 GPosition pos = files_list;
394 if (files_list.size() && !files_list[pos]->offset)
395 bundled = false;
396 for (pos=files_list; pos; ++pos)
397 if ( !bundled != !files_list[pos]->offset)
398 // There directory contains both indirect and bundled records.
399 G_THROW( ERR_MSG("DjVmDir.bad_dir") );
400 // Do the real work
401 encode(gstr, bundled, do_rename);
402 }
403
404 void
encode(const GP<ByteStream> & gstr,const bool bundled,const bool do_rename) const405 DjVmDir::encode(const GP<ByteStream> &gstr, const bool bundled, const bool do_rename) const
406 {
407 ByteStream &str=*gstr;
408 DEBUG_MSG("DjVmDir::encode(): encoding contents of the 'DIRM' chunk do_rename=" << do_rename << "\n");
409 DEBUG_MAKE_INDENT(3);
410
411 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
412 GPosition pos;
413
414 DEBUG_MSG("encoding version number=" << version << ", bundled=" << bundled << "\n");
415 str.write8(version | ((int) bundled<< 7));
416
417 DEBUG_MSG("storing the number of records=" << files_list.size() << "\n");
418 str.write16(files_list.size());
419
420 if (files_list.size())
421 {
422 // Check that there is only one file with shared annotations
423 int shared_anno_cnt=0;
424 for (pos=files_list; pos; ++pos)
425 if (files_list[pos]->is_shared_anno())
426 shared_anno_cnt++;
427 if (shared_anno_cnt>1)
428 G_THROW( ERR_MSG("DjVmDir.multi_save") );
429
430 if (bundled)
431 {
432 // We need to store offsets uncompressed. That's because when
433 // we save a DjVmDoc, we first compress the DjVmDir with dummy
434 // offsets and after computing the real offsets we rewrite the
435 // DjVmDir, which should not change its size during this operation
436 DEBUG_MSG("storing offsets for every record\n");
437 for (pos=files_list; pos; ++pos)
438 {
439 GP<File> file=files_list[pos];
440 if (!file->offset)
441 // The directory contains record without offset
442 G_THROW( ERR_MSG("DjVmDir.bad_dir") );
443 str.write32(file->offset);
444 }
445 }
446
447 GP<ByteStream> gbs_str=BSByteStream::create(gstr, 50);
448 ByteStream &bs_str=*gbs_str;
449 DEBUG_MSG("storing and compressing sizes for every record\n");
450 for (pos=files_list; pos; ++pos)
451 {
452 const GP<File> file(files_list[pos]);
453 bs_str.write24(file->size);
454 }
455 DEBUG_MSG("storing and compressing flags for every record\n");
456 const bool xdo_rename=(do_rename||!bundled);
457 for (pos=files_list;pos;++pos)
458 {
459 const GP<File> file(files_list[pos]);
460 if (xdo_rename)
461 {
462 const GUTF8String new_id = file->name;
463 if (!new_id)
464 {
465 if (!file->oldname.length() || file->oldname == new_id)
466 file->flags &= ~File::HAS_NAME;
467 else
468 file->flags |= File::HAS_NAME;
469 }
470 }
471 else
472 {
473 if (!file->name.length() || file->name == file->id)
474 file->flags &= ~File::HAS_NAME;
475 else
476 file->flags |= File::HAS_NAME;
477 }
478 if (file->title.length() && (file->title!=file->id))
479 file->flags |= File::HAS_TITLE;
480 else
481 file->flags &= ~File::HAS_TITLE;
482
483 bs_str.write8(file->flags);
484 }
485
486 DEBUG_MSG("storing and compressing names...\n");
487 for(pos=files_list;pos;++pos)
488 {
489 GP<File> file=files_list[pos];
490 GUTF8String id;
491 GUTF8String name;
492 GUTF8String title;
493 if (xdo_rename)
494 {
495 id = file->name;
496 if (! id)
497 id = file->id;
498 if ((file->flags) & File::HAS_NAME)
499 name = file->oldname;
500 }
501 else
502 {
503 id=file->id;
504 if ((file->flags) & File::HAS_NAME)
505 name = file->name;
506 }
507 if ((file->flags) & File::HAS_TITLE)
508 title = file->title;
509 DEBUG_MSG("rename=" <<xdo_rename<<" id='" << id << "' name='" << name << "' title='" << title << "'\n");
510 bs_str.writestring(id);
511 bs_str.write8(0);
512 if (name.length())
513 {
514 bs_str.writestring(name);
515 bs_str.write8(0);
516 }
517 if (title.length())
518 {
519 bs_str.writestring(title);
520 bs_str.write8(0);
521 }
522 }
523 }
524 DEBUG_MSG("done\n");
525 }
526
527 GP<DjVmDir::File>
page_to_file(int page_num) const528 DjVmDir::page_to_file(int page_num) const
529 {
530 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
531
532 return (page_num<page2file.size())?page2file[page_num]:(GP<DjVmDir::File>(0));
533 }
534
535 GP<DjVmDir::File>
name_to_file(const GUTF8String & name) const536 DjVmDir::name_to_file(const GUTF8String & name) const
537 {
538 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
539
540 GPosition pos;
541 return (name2file.contains(name, pos))?name2file[pos]:(GP<DjVmDir::File>(0));
542 }
543
544 GP<DjVmDir::File>
id_to_file(const GUTF8String & id) const545 DjVmDir::id_to_file(const GUTF8String &id) const
546 {
547 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
548
549 GPosition pos;
550 return (id2file.contains(id, pos))?id2file[pos]:(GP<DjVmDir::File>(0));
551 }
552
553 GP<DjVmDir::File>
title_to_file(const GUTF8String & title,GPosition spos) const554 DjVmDir::title_to_file(const GUTF8String &title, GPosition spos) const
555 {
556 if (! title)
557 return 0;
558 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
559 if (! spos)
560 for (GPosition pos = spos; pos; ++pos)
561 if (files_list[pos]->is_page() && files_list[pos]->title == title)
562 return files_list[pos];
563 for (GPosition pos = files_list; pos; ++pos)
564 if (files_list[pos]->is_page() && files_list[pos]->title == title)
565 return files_list[pos];
566 return 0;
567 }
568
569 GP<DjVmDir::File>
pos_to_file(int fileno,int * ppageno) const570 DjVmDir::pos_to_file(int fileno, int *ppageno) const
571 {
572 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
573 GPosition pos = files_list;
574 int pageno = 0;
575 while (pos && --fileno >= 0) {
576 if (files_list[pos]->is_page())
577 ++pageno;
578 ++pos;
579 }
580 if (!pos)
581 return 0;
582 if (ppageno)
583 *ppageno = pageno;
584 return files_list[pos];
585 }
586
587 GPList<DjVmDir::File>
get_files_list(void) const588 DjVmDir::get_files_list(void) const
589 {
590 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
591 return files_list;
592 }
593
594 int
get_files_num(void) const595 DjVmDir::get_files_num(void) const
596 {
597 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
598 return files_list.size();
599 }
600
601 int
get_pages_num(void) const602 DjVmDir::get_pages_num(void) const
603 {
604 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
605 return page2file.size();
606 }
607
608 int
get_file_pos(const File * f) const609 DjVmDir::get_file_pos(const File * f) const
610 {
611 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
612 int cnt;
613 GPosition pos;
614 for(pos=files_list, cnt=0;pos&&(files_list[pos]!=f);++pos, cnt++)
615 continue;
616 return (pos)?cnt:(-1);
617 }
618
619 int
get_page_pos(int page_num) const620 DjVmDir::get_page_pos(int page_num) const
621 {
622 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
623
624 GP<File> file=page_to_file(page_num);
625 return (file)?get_file_pos(file):(-1);
626 }
627
628 GP<DjVmDir::File>
get_shared_anno_file(void) const629 DjVmDir::get_shared_anno_file(void) const
630 {
631 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
632
633 GP<File> file;
634 for(GPosition pos=files_list;pos;++pos)
635 {
636 GP<File> frec=files_list[pos];
637 if (frec->is_shared_anno())
638 {
639 file=frec;
640 break;
641 }
642 }
643 return file;
644 }
645
646 int
insert_file(const GP<File> & file,int pos_num)647 DjVmDir::insert_file(const GP<File> & file, int pos_num)
648 {
649 DEBUG_MSG("DjVmDir::insert_file(): name='"
650 << file->name << "', pos=" << pos_num << "\n");
651 DEBUG_MAKE_INDENT(3);
652
653 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
654
655 if (pos_num<0)
656 pos_num=files_list.size();
657
658 //// Modify maps
659 // if (! File::is_legal_id(file->id))
660 // G_THROW( ERR_MSG("DjVmDir.bad_file") "\t" + file->id);
661 if (id2file.contains(file->id))
662 G_THROW( ERR_MSG("DjVmDir.dupl_id2") "\t" + file->id);
663 if (name2file.contains(file->name))
664 G_THROW( ERR_MSG("DjVmDir.dupl_name2") "\t" + file->name);
665 name2file[file->name]=file;
666 id2file[file->id]=file;
667
668 // Make sure that there is no more than one file with shared annotations
669 if (file->is_shared_anno())
670 {
671 for(GPosition pos=files_list;pos;++pos)
672 if (files_list[pos]->is_shared_anno())
673 G_THROW( ERR_MSG("DjVmDir.multi_save2") );
674 }
675
676 // Add the file to the list
677 int cnt;
678 GPosition pos;
679 for(pos=files_list, cnt=0;pos&&(cnt!=pos_num);++pos, cnt++)
680 continue;
681 if (pos)
682 files_list.insert_before(pos, file);
683 else
684 files_list.append(file);
685
686 if (file->is_page())
687 {
688 // This file is also a page
689 // Count its number
690 int page_num=0;
691 for(pos=files_list;pos;++pos)
692 {
693 GP<File> &f=files_list[pos];
694 if (f==file)
695 break;
696 if (f->is_page())
697 page_num++;
698 }
699
700 int i;
701 page2file.resize(page2file.size());
702 for(i=page2file.size()-1;i>page_num;i--)
703 page2file[i]=page2file[i-1];
704 page2file[page_num]=file;
705 for(i=page_num;i<page2file.size();i++)
706 page2file[i]->page_num=i;
707 }
708 return pos_num;
709 }
710
711 void
delete_file(const GUTF8String & id)712 DjVmDir::delete_file(const GUTF8String &id)
713 {
714 DEBUG_MSG("Deleting file with id='" << (const char *)id << "'\n");
715 DEBUG_MAKE_INDENT(3);
716
717 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
718
719 for(GPosition pos=files_list;pos;++pos)
720 {
721 GP<File> & f=files_list[pos];
722 if (id == f->id)
723 {
724 name2file.del(f->name);
725 id2file.del(f->id);
726 if (f->is_page())
727 {
728 for(int page=0;page<page2file.size();page++)
729 {
730 if (page2file[page]==f)
731 {
732 int i;
733 for(i=page;i<page2file.size()-1;i++)
734 page2file[i]=page2file[i+1];
735 page2file.resize(page2file.size()-2);
736 for(i=page;i<page2file.size();i++)
737 page2file[i]->page_num=i;
738 break;
739 }
740 }
741 }
742 files_list.del(pos);
743 break;
744 }
745 }
746 }
747
748 void
set_file_name(const GUTF8String & id,const GUTF8String & name)749 DjVmDir::set_file_name(const GUTF8String &id, const GUTF8String &name)
750 {
751 DEBUG_MSG("DjVmDir::set_file_name(): id='" << id << "', name='" << name << "'\n");
752 DEBUG_MAKE_INDENT(3);
753
754 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
755
756 GPosition pos;
757
758 // First see, if the name is unique
759 for(pos=files_list;pos;++pos)
760 {
761 GP<File> file=files_list[pos];
762 if (file->id!=id && file->name==name)
763 G_THROW( ERR_MSG("DjVmDir.name_in_use") "\t" + GUTF8String(name));
764 }
765
766 // Check if ID is valid
767 if (!id2file.contains(id, pos))
768 G_THROW( ERR_MSG("DjVmDir.no_info") "\t" + GUTF8String(id));
769 GP<File> file=id2file[pos];
770 name2file.del(file->name);
771 file->name=name;
772 name2file[name]=file;
773 }
774
775 void
set_file_title(const GUTF8String & id,const GUTF8String & title)776 DjVmDir::set_file_title(const GUTF8String &id, const GUTF8String &title)
777 {
778 DEBUG_MSG("DjVmDir::set_file_title(): id='" << id << "', title='" << title << "'\n");
779 DEBUG_MAKE_INDENT(3);
780 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
781 GPosition pos;
782 // Check if ID is valid
783 if (!id2file.contains(id, pos))
784 G_THROW( ERR_MSG("DjVmDir.no_info") "\t" + GUTF8String(id));
785 GP<File> file=id2file[pos];
786 file->title=title;
787 }
788
789 GPList<DjVmDir::File>
resolve_duplicates(const bool save_as_bundled)790 DjVmDir::resolve_duplicates(const bool save_as_bundled)
791 {
792 GCriticalSectionLock lock((GCriticalSection *) &class_lock);
793 // Make sure all the filenames are unique.
794 GPosition pos;
795 GMap<GUTF8String,void *> save_map;
796 GMap<GUTF8String,GPList<DjVmDir::File> > conflicts;
797 for(pos=files_list;pos;++pos)
798 {
799 const GUTF8String save_name=files_list[pos]->check_save_name(save_as_bundled).downcase();
800 if(save_map.contains(save_name))
801 {
802 conflicts[save_name].append(files_list[pos]);
803 }else
804 {
805 save_map[save_name]=0;
806 }
807 }
808 for(pos=conflicts;pos;++pos)
809 {
810 const GUTF8String &save_name=conflicts.key(pos);
811 const int dot=save_name.rsearch('.',0);
812 GPList<DjVmDir::File> &cfiles=conflicts[pos];
813 int count=1;
814 for(GPosition qpos=cfiles;qpos;++qpos)
815 {
816 GUTF8String new_name=cfiles[qpos]->get_load_name();
817 if((new_name != GUTF8String(GNativeString(new_name)))
818 ||conflicts.contains(new_name))
819 {
820 do
821 {
822 new_name=(dot<0)
823 ?(save_name+"-"+GUTF8String(count++))
824 :(save_name.substr(0,dot)+"-"+GUTF8String(count++)+
825 save_name.substr(dot,(unsigned int)(-1)));
826 } while(save_map.contains(new_name.downcase()));
827 }
828 cfiles[qpos]->set_save_name(new_name);
829 save_map[new_name]=0;
830 }
831 }
832 return files_list;
833 }
834
835
836 #ifdef HAVE_NAMESPACES
837 }
838 # ifndef NOT_USING_DJVU_NAMESPACE
839 using namespace DJVU;
840 # endif
841 #endif
842