summaryrefslogtreecommitdiffstats
path: root/kviewshell/plugins/djvu/libdjvu/DjVmDir.cpp
blob: 2d8d2467c2e9568bc581bbfc1673c41d16f36654 (plain)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
//C-  -*- C++ -*-
//C- -------------------------------------------------------------------
//C- DjVuLibre-3.5
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
//C- Copyright (c) 2001  AT&T
//C-
//C- This software is subject to, and may be distributed under, the
//C- GNU General Public License, Version 2. The license should have
//C- accompanied the software or you may obtain a copy of the license
//C- from the Free Software Foundation at http://www.fsf.org .
//C-
//C- This program is distributed in the hope that it will be useful,
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//C- GNU General Public License for more details.
//C- 
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
//C- Software authorized us to replace the original DjVu(r) Reference 
//C- Library notice by the following text (see doc/lizard2002.djvu):
//C-
//C-  ------------------------------------------------------------------
//C- | DjVu (r) Reference Library (v. 3.5)
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
//C- | 6,058,214 and patents pending.
//C- |
//C- | This software is subject to, and may be distributed under, the
//C- | GNU General Public License, Version 2. The license should have
//C- | accompanied the software or you may obtain a copy of the license
//C- | from the Free Software Foundation at http://www.fsf.org .
//C- |
//C- | The computer code originally released by LizardTech under this
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
//C- | General Public License.   This grant only confers the right to 
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
//C- | the extent such infringement is reasonably necessary to enable 
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
//C- | any greater extent that may be necessary to utilize further 
//C- | modifications or combinations.
//C- |
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//C- +------------------------------------------------------------------
// 
// $Id: DjVmDir.cpp,v 1.10 2004/05/05 15:12:42 leonb Exp $
// $Name: release_3_5_15 $

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if NEED_GNUG_PRAGMAS
# pragma implementation
#endif

#include "DjVmDir.h"
#include "BSByteStream.h"
#include "GURL.h"
#include "debug.h"

#include <ctype.h>


#ifdef HAVE_NAMESPACES
namespace DJVU {
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
}
#endif
#endif


GP<DjVmDir::File>
DjVmDir::File::create(const GUTF8String &load_name,
  const GUTF8String &save_name, const GUTF8String &title,
  const FILE_TYPE file_type)
{
  File *file_ptr=new File();
  GP<File> file=file_ptr;
  file_ptr->set_load_name(load_name);
  file_ptr->set_save_name(save_name);
  file_ptr->set_title(title);
  file_ptr->flags=(file_type & TYPE_MASK);
  return file;
}

const GUTF8String &
DjVmDir::File::check_save_name(const bool xis_bundled)
{
  if(!xis_bundled && !valid_name)
  {
    GUTF8String retval=name.length()?name:id;
    if(GUTF8String(GNativeString(retval)) != retval)
    {
      const_cast<bool &>(valid_name)=true;
      char *buf;
      GPBuffer<char> gbuf(buf,2*retval.length()+1);
      char *s=buf;
      int i=0;
      for(char c=retval[i++];c;)
      {
        static const char hex[]="0123456789ABCDEF";
        int len=retval.nextChar(i)-i;
        if(len>1 || ((len == 1)&&(c&0x80)))
        {
          do
          {
            s++[0]=hex[(c>>4)&0xf];
            s++[0]=hex[(c&0xf)];
            c=retval[i++];
          } while(c && ((--len) > 0));
        }else
        {
          s++[0]=c;
          c=retval[i++];
        }
      }
      s++[0]=0;
      oldname=retval;
      name=buf;
    }
    const_cast<bool &>(valid_name)=true;
  }
  return *(name.length()?&name:&id);
}

const GUTF8String &
DjVmDir::File::get_save_name(void) const
{
  return *(name.length()?&name:&id);
}

void
DjVmDir::File::set_load_name(const GUTF8String &xid)
{
  GURL url=GURL::UTF8(xid);
  if(!url.is_valid())
  {
    url=GURL::Filename::UTF8(xid);
  }
  id=url.fname();
}

void
DjVmDir::File::set_save_name(const GUTF8String &xname)
{
  GURL url;
  valid_name=false;
  if(!xname.length())
  {
    GURL url=GURL::UTF8(id);
    if(!url.is_valid())
    {
      name=id;
    }else
    {
      name=url.fname();
    }
  }else
  {
    GURL url=GURL::UTF8(xname);
    if(!url.is_valid())
    {
      url=GURL::Filename::UTF8(xname);
    }
    name=url.fname();
  }
  oldname="";
}

/* DjVmDir::File */

DjVmDir::File::File(void) : offset(0), size(0), valid_name(false),
   flags(0), page_num(-1) { }

GUTF8String
DjVmDir::File::get_str_type(void) const
{
   GUTF8String type;
   switch(flags & TYPE_MASK)
   {
      case INCLUDE:
        type="INCLUDE";
        break;
      case PAGE:
        type="PAGE";
        break;
      case THUMBNAILS:
        type="THUMBNAILS";
        break;
      case SHARED_ANNO:
        type="SHARED_ANNO";
        break;
      default:
        //  Internal error: please modify DjVmDir::File::get_str_type()
        //  to contain all possible File types.
	      G_THROW( ERR_MSG("DjVmDir.get_str_type") );
   }
   return type;
}


const int DjVmDir::version=1;

void 
DjVmDir::decode(const GP<ByteStream> &gstr)
{
   ByteStream &str=*gstr;
   DEBUG_MSG("DjVmDir::decode(): decoding contents of 'DIRM' chunk...\n");
   DEBUG_MAKE_INDENT(3);
   
   GCriticalSectionLock lock(&class_lock);

   GPosition pos;

   files_list.empty();
   page2file.resize(-1);
   name2file.empty();
   id2file.empty();
   title2file.empty();

   int ver=str.read8();
   bool bundled=(ver & 0x80)!=0;
   ver&=0x7f;

   DEBUG_MSG("DIRM version=" << ver << ", our version=" << version << "\n");
   if (ver>version)
      G_THROW( ERR_MSG("DjVmDir.version_error") "\t" 
               + GUTF8String(version) + "\t" + GUTF8String(ver));
   // Unable to read DJVM directories of versions higher than xxx
   // Data version number is yyy.
   DEBUG_MSG("bundled directory=" << bundled << "\n");
   DEBUG_MSG("reading the directory records...\n");
   int files=str.read16();
   DEBUG_MSG("number of files=" << files << "\n");

   if (files)
   {
      DEBUG_MSG("reading offsets (and sizes for ver==0)\n");
      for(int nfile=0;nfile<files;nfile++)
      {
         GP<File> file=new File();
         files_list.append(file);
         if (bundled)
         {
            file->offset=str.read32();
            if (ver==0)
              file->size=str.read24();
            if (file->offset==0)
               G_THROW( ERR_MSG("DjVmDir.no_indirect") );
         } else
         {
           file->offset=file->size=0;
         }
      }

      GP<ByteStream> gbs_str=BSByteStream::create(gstr);
      ByteStream &bs_str=*gbs_str;
      if (ver>0)
      {
         DEBUG_MSG("reading and decompressing sizes...\n");
         for(GPosition pos=files_list;pos;++pos)
            files_list[pos]->size=bs_str.read24();
      }
         
      DEBUG_MSG("reading and decompressing flags...\n");
      for(pos=files_list;pos;++pos)
         files_list[pos]->flags=bs_str.read8();

      if (!ver)
      {
         DEBUG_MSG("converting flags from version 0...\n");
         for(pos=files_list;pos;++pos)
         {
            unsigned char flags_0=files_list[pos]->flags;
            unsigned char flags_1;
            flags_1=(flags_0 & File::IS_PAGE_0)?(File::PAGE):(File::INCLUDE);
            if (flags_0 & File::HAS_NAME_0)
              flags_1|=File::HAS_NAME;
            if (flags_0 & File::HAS_TITLE_0)
              flags_1|=File::HAS_TITLE;
            files_list[pos]->flags=flags_1;
         }
      }
   
      DEBUG_MSG("reading and decompressing names...\n");
      GTArray<char> strings;
      char buffer[1024];
      int length;
      while((length=bs_str.read(buffer, 1024)))
      {
         int strings_size=strings.size();
         strings.resize(strings_size+length-1);
         memcpy((char*) strings+strings_size, buffer, length);
      }
      DEBUG_MSG("size of decompressed names block=" << strings.size() << "\n");
   
         // Copy names into the files
      const char * ptr=strings;
      for(pos=files_list;pos;++pos)
      {
         GP<File> file=files_list[pos];

         file->id=ptr;
         ptr+=file->id.length()+1;
         if (file->flags & File::HAS_NAME)
         {
            file->name=ptr;
            ptr+=file->name.length()+1;
         } else
         {
            file->name=file->id;
         }
         if (file->flags & File::HAS_TITLE)
         {
            file->title=ptr;
       ptr+=file->title.length()+1;
         } else
       file->title=file->id;
   /* msr debug:  multipage file, file->title is null.  
         DEBUG_MSG(file->name << ", " << file->id << ", " << file->title << ", " <<
                   file->offset << ", " << file->size << ", " <<
                   file->is_page() << "\n"); */
      }

         // Check that there is only one file with SHARED_ANNO flag on
      int shared_anno_cnt=0;
      for(pos=files_list;pos;++pos)
      {
         if (files_list[pos]->is_shared_anno())
         {
            shared_anno_cnt++;
         }
      }
      if (shared_anno_cnt>1)
        G_THROW( ERR_MSG("DjVmDir.corrupt") );

         // Now generate page=>file array for direct access
      int pages=0;
      for(pos=files_list;pos;++pos)
              pages+=files_list[pos]->is_page() ? 1 : 0;
      DEBUG_MSG("got " << pages << " pages\n");
      page2file.resize(pages-1);
      int page_num=0;
      for(pos=files_list;pos;++pos)
      {
               GP<File> file=files_list[pos];
               if (file->is_page())
               {
                  page2file[page_num]=file;
                  file->page_num=page_num++;
               }
      }

         // Generate name2file map
      for(pos=files_list;pos;++pos)
      {
	       GP<File> file=files_list[pos];
	       if (name2file.tqcontains(file->name))
	          G_THROW( ERR_MSG("DjVmDir.dupl_name") "\t" + file->name );
	       name2file[file->name]=file;
      }

         // Generate id2file map
      for(pos=files_list;pos;++pos)
      {
	       GP<File> file=files_list[pos];
	       if (id2file.tqcontains(file->id))
	          G_THROW( ERR_MSG("DjVmDir.dupl_id") "\t" + file->id);
	       id2file[file->id]=file;
      }

         // Generate title2file map
      for(pos=files_list;pos;++pos)
      {
	       GP<File> file=files_list[pos];
	       if (file->title.length())
	       {
	          if (title2file.tqcontains(file->title))
	             G_THROW( ERR_MSG("DjVmDir.dupl_title") "\t" + file->title);
	          title2file[file->title]=file;
	       }
      }
   }
}


void
DjVmDir::encode(const GP<ByteStream> &gstr, const bool do_rename) const
{
  bool bundled = true;
  GPosition pos = files_list;
  if (files_list.size() && !files_list[pos]->offset)
    bundled = false;
  for (pos=files_list; pos; ++pos)
    if ( !bundled !=  !files_list[pos]->offset)
      //  There directory contains both indirect and bundled records.
      G_THROW( ERR_MSG("DjVmDir.bad_dir") );
  // Do the real work
  encode(gstr, bundled, do_rename);
}

void
DjVmDir::encode(const GP<ByteStream> &gstr, const bool bundled, const bool do_rename) const
{
  ByteStream &str=*gstr;
  DEBUG_MSG("DjVmDir::encode(): encoding contents of the 'DIRM' chunk do_rename=" << do_rename << "\n");
  DEBUG_MAKE_INDENT(3);
   
  GCriticalSectionLock lock((GCriticalSection *) &class_lock);
  GPosition pos;

  DEBUG_MSG("encoding version number=" << version << ", bundled=" << bundled << "\n");
  str.write8(version | ((int) bundled<< 7));
   
  DEBUG_MSG("storing the number of records=" << files_list.size() << "\n");
  str.write16(files_list.size());

  if (files_list.size())
    {
      // Check that there is only one file with shared annotations
      int shared_anno_cnt=0;
      for (pos=files_list; pos; ++pos)
        if (files_list[pos]->is_shared_anno())
          shared_anno_cnt++;
      if (shared_anno_cnt>1)
        G_THROW( ERR_MSG("DjVmDir.multi_save") );
      
      if (bundled)
        {
          // We need to store offsets uncompressed. That's because when
          // we save a DjVmDoc, we first compress the DjVmDir with dummy
          // offsets and after computing the real offsets we rewrite the
          // DjVmDir, which should not change its size during this operation
          DEBUG_MSG("storing offsets for every record\n");
          for (pos=files_list; pos; ++pos)
            {
              GP<File> file=files_list[pos];
              if (!file->offset)
                // The directory contains record without offset
                G_THROW( ERR_MSG("DjVmDir.bad_dir") );
              str.write32(file->offset);
            }
        }
      
      GP<ByteStream> gbs_str=BSByteStream::create(gstr, 50);
      ByteStream &bs_str=*gbs_str;
      DEBUG_MSG("storing and compressing sizes for every record\n");
      for (pos=files_list; pos; ++pos)
        {
          const GP<File> file(files_list[pos]);
          bs_str.write24(file->size);
        }
      DEBUG_MSG("storing and compressing flags for every record\n");
      const bool xdo_rename=(do_rename||!bundled);
      for(pos=files_list;pos;++pos)
        {
          const GP<File> file(files_list[pos]);
          if(xdo_rename)
            {
              const GUTF8String new_id = file->name;
              if (! new_id)
                if(!file->oldname.length() || file->oldname == new_id)
                  file->flags &= ~File::HAS_NAME;
                else
                  file->flags |= File::HAS_NAME;
            }
          else 
            {
              if (!file->name.length() || file->name == file->id)
                file->flags &= ~File::HAS_NAME;
              else
                file->flags |= File::HAS_NAME;
            }
          if (file->title.length() && (file->title!=file->id))
            file->flags |= File::HAS_TITLE;
          else
            file->flags &= ~File::HAS_TITLE;

       bs_str.write8(file->flags);
     }

     DEBUG_MSG("storing and compressing names...\n");
     for(pos=files_list;pos;++pos)
     {
         GP<File> file=files_list[pos];
         GUTF8String id;
         GUTF8String name;
         GUTF8String title;
         if (xdo_rename)
           {
             id = file->name;
             if (! id)
               id = file->id;
             if ((file->flags) & File::HAS_NAME)
               name = file->oldname;
           }
         else
           {
             id=file->id;
             if ((file->flags) & File::HAS_NAME)
               name = file->name;
           }
         if ((file->flags) & File::HAS_TITLE)
           title = file->title;
         DEBUG_MSG("rename=" <<xdo_rename<<" id='" << id << "' name='" << name << "' title='" << title << "'\n");
         bs_str.writestring(id);
         bs_str.write8(0);
         if (name.length())
           {
             bs_str.writestring(name);
             bs_str.write8(0);
           }
         if (title.length())
           {
             bs_str.writestring(title);
             bs_str.write8(0);
           }
     }
    }
  DEBUG_MSG("done\n");
}

GP<DjVmDir::File>
DjVmDir::page_to_file(int page_num) const
{
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);

   return (page_num<page2file.size())?page2file[page_num]:(GP<DjVmDir::File>(0));
}

GP<DjVmDir::File>
DjVmDir::name_to_file(const GUTF8String & name) const
{
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);

   GPosition pos;
   return (name2file.tqcontains(name, pos))?name2file[pos]:(GP<DjVmDir::File>(0));
}

GP<DjVmDir::File>
DjVmDir::id_to_file(const GUTF8String &id) const
{
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);

   GPosition pos;
   return (id2file.tqcontains(id, pos))?id2file[pos]:(GP<DjVmDir::File>(0));
}

GP<DjVmDir::File>
DjVmDir::title_to_file(const GUTF8String &title) const
{
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);

   GPosition pos;
   return (title2file.tqcontains(title, pos))?title2file[pos]:(GP<DjVmDir::File>(0));
}

GPList<DjVmDir::File>
DjVmDir::get_files_list(void) const
{
  GCriticalSectionLock lock((GCriticalSection *) &class_lock);
  return files_list;
}

int
DjVmDir::get_files_num(void) const
{
  GCriticalSectionLock lock((GCriticalSection *) &class_lock);
  return files_list.size();
}

int
DjVmDir::get_pages_num(void) const
{
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);
   return page2file.size();
}

int
DjVmDir::get_file_pos(const File * f) const
{
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);
   int cnt;
   GPosition pos;
   for(pos=files_list, cnt=0;pos&&(files_list[pos]!=f);++pos, cnt++)
                   continue;
   return (pos)?cnt:(-1);
}

int
DjVmDir::get_page_pos(int page_num) const
{
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);
   
   GP<File> file=page_to_file(page_num);
   return (file)?get_file_pos(file):(-1);
}

GP<DjVmDir::File>
DjVmDir::get_shared_anno_file(void) const
{
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);

   GP<File> file;
   for(GPosition pos=files_list;pos;++pos)
   {
      GP<File> frec=files_list[pos];
      if (frec->is_shared_anno())
      {
         file=frec;
         break;
      }
   }
   return file;
}

int
DjVmDir::insert_file(const GP<File> & file, int pos_num)
{
   DEBUG_MSG("DjVmDir::insert_file(): name='" << file->name << "', pos=" << pos_num << "\n");
   DEBUG_MAKE_INDENT(3);
   
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);
   
   if (pos_num<0)
     pos_num=files_list.size();

      // Modify maps
//   if (! File::is_legal_id(file->id))
//     G_THROW( ERR_MSG("DjVmDir.bad_file") "\t" + file->id);
   if (id2file.tqcontains(file->id))
     G_THROW( ERR_MSG("DjVmDir.dupl_id2") "\t" + file->id);
   if (name2file.tqcontains(file->name))
     G_THROW( ERR_MSG("DjVmDir.dupl_name2") "\t" + file->name);
   name2file[file->name]=file;
   id2file[file->id]=file;
   if (file->title.length())
     {
       if (title2file.tqcontains(file->title))  // duplicate titles may become ok some day
         G_THROW( ERR_MSG("DjVmDir.dupl_title2") "\t" + file->title);
       title2file[file->title]=file;
     }

      // Make sure that there is no more than one file with shared annotations
   if (file->is_shared_anno())
   {
      for(GPosition pos=files_list;pos;++pos)
         if (files_list[pos]->is_shared_anno())
            G_THROW( ERR_MSG("DjVmDir.multi_save2") );
   }
   
      // Add the file to the list
   int cnt;
   GPosition pos;
   for(pos=files_list, cnt=0;pos&&(cnt!=pos_num);++pos, cnt++)
                   continue;
   if (pos)
     files_list.insert_before(pos, file);
   else
     files_list.append(file);

   if (file->is_page())
   {
         // This file is also a page
         // Count its number
      int page_num=0;
      for(pos=files_list;pos;++pos)
      {
         GP<File> &f=files_list[pos];
         if (f==file)
           break;
         if (f->is_page())
           page_num++;
      }

      int i;
      page2file.resize(page2file.size());
      for(i=page2file.size()-1;i>page_num;i--)
         page2file[i]=page2file[i-1];
      page2file[page_num]=file;
      for(i=page_num;i<page2file.size();i++)
         page2file[i]->page_num=i;
   }
   return pos_num;
}

void
DjVmDir::delete_file(const GUTF8String &id)
{
   DEBUG_MSG("Deleting file with id='" << (const char *)id << "'\n");
   DEBUG_MAKE_INDENT(3);

   GCriticalSectionLock lock((GCriticalSection *) &class_lock);
   
   for(GPosition pos=files_list;pos;++pos)
   {
      GP<File> & f=files_list[pos];
      if (id == f->id)
      {
         name2file.del(f->name);
         id2file.del(f->id);
         title2file.del(f->title);
         if (f->is_page())
         {
            for(int page=0;page<page2file.size();page++)
            {
               if (page2file[page]==f)
               {
                  int i;
                  for(i=page;i<page2file.size()-1;i++)
                     page2file[i]=page2file[i+1];
                  page2file.resize(page2file.size()-2);
                  for(i=page;i<page2file.size();i++)
                     page2file[i]->page_num=i;
                  break;
               }
            }
         }
         files_list.del(pos);
         break;
      }
   }
}

void
DjVmDir::set_file_name(const GUTF8String &id, const GUTF8String &name)
{
   DEBUG_MSG("DjVmDir::set_file_name(): id='" << id << "', name='" << name << "'\n");
   DEBUG_MAKE_INDENT(3);
   
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);

   GPosition pos;
   
      // First see, if the name is unique
   for(pos=files_list;pos;++pos)
   {
      GP<File> file=files_list[pos];
      if (file->id!=id && file->name==name)
        G_THROW( ERR_MSG("DjVmDir.name_in_use") "\t" + GUTF8String(name));
   }

      // Check if ID is valid
   if (!id2file.tqcontains(id, pos))
      G_THROW( ERR_MSG("DjVmDir.no_info") "\t" + GUTF8String(id));
   GP<File> file=id2file[pos];
   name2file.del(file->name);
   file->name=name;
   name2file[name]=file;
}

void
DjVmDir::set_file_title(const GUTF8String &id, const GUTF8String &title)
{
   DEBUG_MSG("DjVmDir::set_file_title(): id='" << id << "', title='" << title << "'\n");
   DEBUG_MAKE_INDENT(3);
   
   GCriticalSectionLock lock((GCriticalSection *) &class_lock);

   GPosition pos;
   
      // First see, if the title is unique
   for(pos=files_list;pos;++pos)
   {
      GP<File> file=files_list[pos];
      if (file->id!=id && file->title==title)
        G_THROW( ERR_MSG("DjVmDir.title_in_use") "\t" + GUTF8String(title));
   }

      // Check if ID is valid
   if (!id2file.tqcontains(id, pos))
      G_THROW( ERR_MSG("DjVmDir.no_info") "\t" + GUTF8String(id));
   GP<File> file=id2file[pos];
   title2file.del(file->title);
   file->title=title;
   title2file[title]=file;
}

GPList<DjVmDir::File>
DjVmDir::resolve_duplicates(const bool save_as_bundled)
{
  GCriticalSectionLock lock((GCriticalSection *) &class_lock);
  // Make sure all the filenames are unique.
  GPosition pos;
  GMap<GUTF8String,void *> save_map;
  GMap<GUTF8String,GPList<DjVmDir::File> > conflicts;
  for(pos=files_list;pos;++pos)
  {
    const GUTF8String save_name=files_list[pos]->check_save_name(save_as_bundled).downcase();
    if(save_map.tqcontains(save_name))
    {
      conflicts[save_name].append(files_list[pos]);
    }else
    {
      save_map[save_name]=0;
    }
  }
  for(pos=conflicts;pos;++pos)
  {
    const GUTF8String &save_name=conflicts.key(pos);
    const int dot=save_name.rsearch('.',0);
    GPList<DjVmDir::File> &cfiles=conflicts[pos];
    int count=1;
    for(GPosition qpos=cfiles;qpos;++qpos)
    {
      GUTF8String new_name=cfiles[qpos]->get_load_name();
      if((new_name != GUTF8String(GNativeString(new_name)))
        ||conflicts.tqcontains(new_name))
      {
        do
        {
          new_name=(dot<0)
            ?(save_name+"-"+GUTF8String(count++))
            :(save_name.substr(0,dot)+"-"+GUTF8String(count++)+
              save_name.substr(dot,(unsigned int)(-1)));
        } while(save_map.tqcontains(new_name.downcase()));
      }
      cfiles[qpos]->set_save_name(new_name);
      save_map[new_name]=0;
    }
  }
  return files_list;
}


#ifdef HAVE_NAMESPACES
}
# ifndef NOT_USING_DJVU_NAMESPACE
using namespace DJVU;
# endif
#endif