File: SUniMap.cpp

package info (click to toggle)
yudit 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 18,472 kB
  • sloc: cpp: 76,344; perl: 5,630; makefile: 989; ansic: 823; sh: 441
file content (930 lines) | stat: -rw-r--r-- 20,307 bytes parent folder | download
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 1997-2023  Gaspar Sinai <gaspar@yudit.org>  
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2,
 *  dated June 1991. See file COPYYING for details.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
#include "stoolkit/SUniMap.h"
#include "stoolkit/SBMap.h"
#include "stoolkit/SBinHashtable.h"
#include "stoolkit/SIO.h"
#include "stoolkit/SUtil.h"
#include "stoolkit/STypes.h" 
#include <stdio.h>

static SS_UCS4 getMaxBytes (unsigned int inp);

/**
 * A unicode Map. It maps SV_UCS2 to SV_UCS4 or SString<->SV_UCS4
 * It is generally useful when mapping UCS4 to Font encodings and
 * Keyboard trasliterations to unicode strings. 
 *
 * This is a tamed version of SBMap.
 * @author: Gaspar Sinai <gaspar@yudit.org>
 * @version: 2000-05-12
 */

class SUniMapDelegate
{
public:
  SUniMapDelegate(const SString& name, SBMap* _map);
  ~SUniMapDelegate();
  /* these routines supposed to speed it up */
  SString name;
  int    count;
  SBMap* map;
};


/**
 * We use the delegate to hide the ugly SBMap and
 * maintain one reference in a cache
 */
SUniMapDelegate::SUniMapDelegate(const SString& n, SBMap* _map)
{
  name = n;
  map = _map;
  count = 1;
}
SUniMapDelegate::~SUniMapDelegate()
{
  delete map;
}

typedef SBinHashtable<SUniMapDelegate*> SUniMapHashtable;
/*
 * This canche is consulted when a new map is requested.
 */
static SUniMapHashtable mapCache;

static SStringVector searchPath;
/*
("/,maps,../maps,/etc/maps,../mytool/uni,../mytool/kmap,../mytool/mys,mytool/uni,mytool/kmap,mytool/mys,uni,kmap,mys");
*/
static void _uniAppend (SString* s, SS_UCS4 u);

void
SUniMap::setPath (const SStringVector& l)
{
  searchPath = l;
}

/**
 * search files for property in order and set the path to the 
 * property. Always add YUDIT_DATA/data
 */
void
SUniMap::guessPath ()
{
  SStringVector unipath = getUserPath("yudit.datapath", "data");
//  fprintf (stderr, "uni is %*.*s\n", SSARGS(unipath.join(",")));
  setPath (unipath);
}

const SStringVector&
SUniMap::getPath ()
{
  return searchPath;
}


/**
 * Vector available maps
 */
SStringVector 
SUniMap::list()
{
  SStringVector ret;
  for (unsigned int i=0; i<searchPath.size(); i++)
  {
    SDir d(searchPath[i]);
    SStringVector l = d.list("*.my");
    for (unsigned int j=0; j<l.size(); j++)
    {
      SString s = l[j];
      s.truncate(s.size()-3);
      ret.append (s);
    }
  }
  return SStringVector (ret);
}

/**
 * This is a straight map.
 */
SUniMap::SUniMap (void)
{
  delegate = 0;
  dmodel = 0;
  emodel = 0;
  dmodel4 = 0;
  emodel4 = 0;
  eindex = -1;
  dindex = -1;
  bumap = 0;
  ok = true;
}

/**
 * Try to load the delegate with the name.
 * The suffix ".my" will be added to the name here in this routine.
 */
SUniMap::SUniMap (const SString& name)
{
  bumap = 0;
  load (name);
}

/**
 * Try to load the map. The name is the name without file extension .my
 */
void
SUniMap::load (const SString& name)
{
  delegate = 0;
  dmodel = 0;
  emodel = 0;
  dmodel4 = 0;
  emodel4 = 0;
  eindex = -1;
  dindex = -1;
  ok = false;

  if (mapCache.get (name))
  {
    SUniMapDelegate* d = mapCache[name];
    d->count++;
    delegate = d;
    ok = true;
    setModel(1, 2, 2, 1);
    return;
  }
  SString n (name);
  n.append (".my");
  SFile f(n, searchPath);
  if (f.size()  <= 0) return ;
  SFileImage i = f.getFileImage();
  if ( i.size() <= 0) return;
  SBMap* b = new SBMap();
  CHECK_NEW (b);
  if (!b->setFileImage(i))
  {
    delete b;
    return;
  }
  SUniMapDelegate* ud = new SUniMapDelegate(name, b);
  CHECK_NEW (ud);
  delegate = ud;
  mapCache.put (name, ud);
  setModel(1, 2, 2, 1);
  bumap = (ud->map->isUMap()) ? ud->map : 0;
  ok = true;
}

/**
 * Allocates new models
 * @param din is the decode input size 0=SS_WORD8 1=SS_WORD16 3=SS_WORD32
 * @param dout is the decode output size
 * @param ein is the encode input size
 * @param eout is the encode output size
 */
void
SUniMap::setModel(int din, int dout, int ein, int eout)
{
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  SBMap *map = d->map;

  eindex = indexOf(true);
  dindex = indexOf(false);

  emodel = 0; dmodel = 0;
  if (eindex>=0)
  {
      /* From 4 byte  to 2 byte */
      emodel = new SStateModel (ein, map->getInWordSize (eindex),
            map->getOutWordSize (eindex), eout);
      emodel4 = new SStateModel (2, map->getInWordSize (eindex),
            map->getOutWordSize (eindex), 2);
  }

  if (dindex>=0)
  {
      /* From 2byte  to 4 byte */
      dmodel = new SStateModel (din, map->getInWordSize (dindex),
            map->getOutWordSize (dindex), dout);
      dmodel4 = new SStateModel (2, map->getInWordSize (dindex),
            map->getOutWordSize (dindex), 2);
  } 
}

/**
 * Copy an existing map
 * @param m is an existing map
 */
SUniMap::SUniMap (const SUniMap &m)
{
  dmodel = 0;
  emodel = 0;
  dmodel4 = 0;
  emodel4 = 0;
  delegate = 0;
  dindex = -1;
  eindex = -1;
  bumap = m.bumap;
  if (!m.isOK())
  {
    ok = false;
  }
  else
  {
    SUniMapDelegate* d = (SUniMapDelegate*) m.delegate;
    if (d)
    {
      d->count++;
      delegate = d;
      dindex = m.dindex;
      eindex = m.eindex;
      setModel(1, 2, 2, 1);
    }
    ok = true;
  }
}

/**
 * Assign a map
 */
SUniMap&
SUniMap::operator = (const SUniMap &m)
{
  if (&m == this) return *this;
  derefer();
  dmodel = 0;
  emodel = 0;
  dmodel4 = 0;
  emodel4 = 0;
  delegate = 0;
  dindex = -1;
  eindex = -1;
  bumap = m.bumap;
  reset();
  if (!m.isOK())
  {
    ok = false;
  }
  else
  {
    SUniMapDelegate* d = (SUniMapDelegate*) m.delegate;
    if (d)
    {
      d->count++;
      delegate = d;
      dindex = m.dindex;
      eindex = m.eindex;
      setModel(1, 2, 2, 1);
    }
    ok = true;
  }
  return *this;
}

SUniMap::~SUniMap()
{
  if (delegate !=0) derefer();
}

/**
 * dereference the current map
 */
void
SUniMap::derefer()
{
  if (isOK())
  {
    SUniMapDelegate* d = (SUniMapDelegate*) delegate;
    if (d!=0)
    {
      d->count--;
      if (d->count==0)
      { 
        // FIXME: if I delete it it gets deleted twice !
        //mapCache.remove (d->name);
        //fprintf (stderr, "FIXME: STRANGE DELETE %*.*s\n", SSARGS(d->name));
        //delete d;
      }
      if (dmodel) delete ((SStateModel*) dmodel);
      if (emodel) delete ((SStateModel*) emodel);
      if (dmodel4) delete ((SStateModel*) dmodel4);
      if (emodel4) delete ((SStateModel*) emodel4);
    }
  }
}

/**
 * A simplistic encoder. 
 * @return the UCS2 value of the first decoded value or zero
 */
SS_UCS2
SUniMap::encode (SS_UCS4 in)
{
  if (bumap)
  {
    if (in > 0xffff) return 0;
    return ((SBMap*) bumap)->encode (in);
  }
  if (!ok) return 0;
  if (in > 0xffff) return 0;
  if (delegate==0) return (SS_UCS2) in;
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  SBMap *map = d->map;

  SStateModel* enc = (SStateModel*) emodel;
  if (enc == 0) return 0;
  SS_UCS2 maxBytes = getMaxBytes (map->getInWordSize (eindex));
  if (in > maxBytes || maxBytes==0) return 0;

  int len = map->encode (eindex, &in, 1, enc, 0);
  if (len < 0)
  {
    enc->reset(); return 0;
  }
  if (enc->out.length != 1)
  {
    enc->reset(); return 0;
  }
  SS_UCS2 retVle = enc->out.u.u16[0];
  enc->reset();
  return retVle;
}

/**
 * A simplistic decoder. 
 * @return the UCS4 value of the first decoded value or zero
 */
SS_UCS4
SUniMap::decode (SS_UCS2 in)
{
  if (bumap)
  {
    return ((SBMap*) bumap)->decode (in);
  }
  if (!ok) return 0;
  if (delegate==0) return (SS_UCS4) in;
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  SBMap *map = d->map;

  SStateModel* dec = (SStateModel*) dmodel;
  if (dec == 0) return 0;

  SS_UCS4 maxBytes = getMaxBytes (map->getInWordSize (dindex));
  if (in > maxBytes && maxBytes == 0) return 0;
  int len = map->encode (dindex, &in, 1, dec, 0);
  if (len < 0)
  {
    dec->reset(); return 0;
  }
  if (dec->out.length != 1)
  {
    dec->reset(); return 0;
  }
  SS_UCS4 retVle = dec->out.u.u32[0];
  dec->reset();
  return retVle;
}

SObject*
SUniMap::clone() const
{
  SObject* n = new SUniMap(*this);
  CHECK_NEW (n);
  return n;
}

/**
 * @param encode is true if we are looking at the encoding map
 * return 0 if map was designed to accept SS_WORD8
 *        1 if map was designed to accept SS_WORD16
 *        2 if map was designed to accept SS_WORD32
 *        3 if map was designed to accept SS_WORD64
 *        -1 if map is not initialized
 */
int
SUniMap::getInWordSize (bool encode)
{
  if (!ok) return -1;
  if (delegate ==0) return 1;
  int index = indexOf(encode);
  if (index < 0) return -1;
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  return d->map->getInWordSize(index);
}

/**
 * @param encode is true if we are looking at the encoding map
 * return 0 if map was designed to produce SS_WORD8
 *        1 if map was designed to produce SS_WORD16
 *        2 if map was designed to produce SS_WORD32
 *        3 if map was designed to produce SS_WORD64
 *        -1 if map is not initialized
 */
int
SUniMap::getOutWordSize (bool encode)
{
  if (!ok) return -1;
  if (delegate ==0) return 1;
  int index = indexOf(encode);
  if (index < 0) return -1;
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  return d->map->getOutWordSize(index);
}

/**
 * Get the index in the map
 * @param encode is true if we are looking at the encoding map
 * @return the index in the SBMap or -1
 */
int
SUniMap::indexOf(bool encode) const
{
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  if (d==0) return -1;
  SBMap *map = d->map;
  for (int i=0; i<map->getSize(); i++)
  {
    if (map->getType(i)==(encode ? SBMap::SBMap_ENCODE : SBMap::SBMap_DECODE)) 
    {
      return i;
    }
  }
  return -1;
}

/**
 * return true if the map is 'usable'
 * a useless map is a map that could not be found.
 */
bool
SUniMap::isOK() const
{
  return ok;
}

void
SUniMap::reset ()
{
  reset (false);
  reset (true);
}

void
SUniMap::reset(bool en)
{
  if (en)
  {
    ucs4vIn.clear();
    sstringOut.clear();
  }
  else
  {
    sstringIn.clear();
    ucs4vOut.clear();
  }
}

void
SUniMap::undo (bool encode)
{
  if (encode)
  {
    if (ucs4vIn.size() > 0) ucs4vIn.truncate(ucs4vIn.size()-1);
  }
  else
  {
    if (sstringIn.size() > 0) sstringIn.truncate(sstringIn.size()-1);
  }
}

/**
 * Decode the input string
 * if in.size() is zero flush it.
 * It can return a zero sized array, in this case more input is needed.
 */
const SV_UCS4&
SUniMap::decode (const SString& in)
{
  ucs4vOut.clear ();
  sstringIn.append(in);
  unsigned int i;
  if (dmodel==0)
  {
    for (i=0; i<sstringIn.size(); i++)
    {
      ucs4vOut.append (sstringIn[i]);
    }
    sstringIn.clear();
    return ucs4vOut;
  }
  SV_UCS2 buffer;
  for (i=0; i<sstringIn.size(); i++)
  {
    buffer.append (SS_UCS2((unsigned char)sstringIn[i]));
  }
  SStateModel* model = (SStateModel*) dmodel;
  model->reset();
  SUniMapDelegate* delg = (SUniMapDelegate*) delegate;
  SBMap* map = (delg==0 || model==0) ? 0 : delg->map;

  // Now go in circles and append till matches.
  int st;
  unsigned int fullsize = buffer.size();
  i=0;
  while (i<fullsize)
  {
    if (map ==0)
    {
      st = SS_REJECT; 
    }
    else
    {
      model->reset ();
      int more = (in.size()>0)?1:0;
      /* imposing a limit of 10 because map->circle sucks. */
      /* FIXME we really need a better map and encoding  */
      unsigned int bsz = ((int)(buffer.size()-i) > 10) 
          ? 10 : buffer.size()-i; 
      st = map->circle (SBMap::SBMap_DECODE, &buffer.array()[i],
          (int)(bsz), model, more);
    }

    if (st == SS_ACCEPT)
    {
       model->reset ();
       int more = (in.size()>0)?1:0;
       st = map->circle (SBMap::SBMap_DECODE, &buffer.array()[i],
          (int)(buffer.size()-i), model, more);
       if (st == SS_ACCEPT)
       {
         break;
       }
    }

    // Append stuff as it is and crunch buffer.
    if (st == SS_REJECT)
    {
      ucs4vOut.append ((SS_UCS4)buffer[i]);
      i++;
    }
    // There is a match here. append the output and
    // Shift the input.
    else if (st>0)
    {
      ucs4vOut.append ((SS_UCS4*)model->out.u.u32, model->out.length);
      while (st--)
      {
        i++;
      }
    }
    // Provision for the bad maps.
    else 
    {
      //fprintf (stderr, "SUniMap:: decode bad map zero matched!\n");
      ucs4vOut.append ((SS_UCS4)buffer[i]);
      i++;
    }
  }
  SString vrest;
  while (i<fullsize)
  {
    vrest.append (sstringIn[i]);
    i++;
  }
  sstringIn = vrest;
  return ucs4vOut;
}

const SString&
SUniMap::remainder() const
{
  return sstringIn;
}

const SString&
SUniMap::encode (const SV_UCS4& in)
{
  sstringOut.clear ();
  ucs4vIn.append(in);
  unsigned int i;
  if (emodel==0)
  {
    for (i=0; i<ucs4vIn.size(); i++)
    {
      _uniAppend (&sstringOut, ucs4vIn[i]);
    }
    ucs4vIn.clear();
    return sstringOut;
  }
  SStateModel* model = (SStateModel*) emodel;
  model->reset();
  SUniMapDelegate* delg = (SUniMapDelegate*) delegate;
  SBMap* map = (delg==0 || model==0) ? 0 : delg->map;

  // Now go in circles and append till matches.
  int st;
  unsigned int fullsize = ucs4vIn.size();
  i=0;
  while (i<fullsize)
  {
    if (map ==0)
    {
      st = SS_REJECT; 
    }
    else
    {
      model->reset ();
      int more = (in.size()>0)?1:0;
      /* check if map can possible handle our input */
      SS_UCS4 maxBytes = getMaxBytes (map->getInWordSize (eindex));
      unsigned int k;
      for (k=i; k<ucs4vIn.size(); k++)
      {
        if (ucs4vIn[k] > maxBytes) break;
        if ((k-i) >= 10) break; /* FIXME self-imposed limit - map->circle sucks */
      }
      if (k==i)
      {
        st = SS_REJECT;
      }
      else
      {
        st = map->circle (SBMap::SBMap_ENCODE, 
              &ucs4vIn.array()[i], (int)(k-i), model, more);
      }
    }

    if (st == SS_ACCEPT)
    {
       /* remove self-imposed limit */
       model->reset ();
       int more = (in.size()>0)?1:0;
       /* check if map can possible handle our input */
       SS_UCS4 maxBytes = getMaxBytes (map->getInWordSize (eindex));
       unsigned int k;
       for (k=i; k<ucs4vIn.size(); k++)
       {
          if (ucs4vIn[k] > maxBytes) break;
       }
       /* k==i was checked before */
       st = map->circle (SBMap::SBMap_ENCODE, 
            &ucs4vIn.array()[i], (int)(k-i), model, more);
       if (st == SS_ACCEPT)
       {
           break;
       }
    }

    // Append stuff as it is and crunch buffer.
    if (st == SS_REJECT)
    {
      _uniAppend (&sstringOut, ucs4vIn[i]);
      i++;
    }
    // There is a match here. append the output and
    // Shift the input.
    else if (st>0)
    {
      // Model is not char !
      for (int j = 0; j<model->out.length; j++)
      {
        _uniAppend (&sstringOut, (SS_UCS4) model->out.u.u16[j]);
      }
      while (st--)
      {
        i++;
      }
    }
    // Provision for the bad maps.
    else 
    {
      fprintf (stderr, "SUniMap:: decode bad map zero matched!\n");
      _uniAppend (&sstringOut, ucs4vIn[i]);
      i++;
    }
  }
  SV_UCS4 vrest;
  while (i<fullsize)
  {
    vrest.append (ucs4vIn[i]);
    i++;
  }
  ucs4vIn = vrest;
  return sstringOut;
}

const SString&
SUniMap::encodeBuffer()
{
  return sstringIn;
}

const SV_UCS4&
SUniMap::decodeBuffer()
{
  return ucs4vIn;
}

static void
_uniAppend (SString* s, SS_UCS4 u)
{
  if (u < 0x100)
  {
    char u8= (char) u;
    s->append (&u8, 1);
  }
  else if (u < 0x10000)
  {
    char u16[32];
    snprintf (u16, 32, "\\u%04x", (unsigned int) u);
    s->append (u16);
  }
  else 
  {
    char u32[32];
    snprintf (u32, 32, "\\U%04x", (unsigned int) u);
    s->append (u32);
  }
}

/**
 * Lift off whetever can be decoded/encoded
 * This version does not work with circular maps.
 * @param in is the input
 * @param inindex is the starting point to process input.
 * @param out is the output. It can be null.
 * @praram decode is true if we are decoding.
 * @return the new index after liftoff.
 */
unsigned int 
SUniMap::lift (const SV_UCS4& in, unsigned int inindex, 
    bool isdecode, SV_UCS4* out)
{
  if (!ok) return inindex;
  if (delegate==0) return inindex;

  /* make sure we don't have too big values */
  SBMap *map = ((SUniMapDelegate*) delegate)->map;

  int mindex = isdecode ? dindex : eindex;
  SS_UCS4 maxBytes = getMaxBytes (map->getInWordSize (mindex));
  if (in[inindex] > maxBytes || maxBytes == 0) return inindex;

  if (mindex < 0) return inindex;

  SStateModel* model = isdecode ? 
       (SStateModel*) dmodel4 : (SStateModel*) emodel4;

  model->reset();
  
  unsigned int realend = in.size();
  int more = 1;
  int status = SS_ACCEPT;
  unsigned int i;
  for (i=inindex; status == SS_ACCEPT && i < realend; i++)
  {
    /* more == 0 would return - correctly, the whole string */
    if (i+1>=realend || in[i+1] > maxBytes)
    {
      more = 0;
    }
    status = map->encode (mindex, &in.array()[i], 1, model, more);
  }

  if (status <= 0) return inindex;
  if (out == 0) return inindex + (unsigned int) status;

  for (i=0; (int)i< model->out.length; i++)
  {
    out->append ((SS_UCS4)(model->out.u.u32[i]));
  }
  return inindex + (unsigned int) status;
}

/**
 * return key value map to see what decodes to what
 * @param key will contain the keys
 * @param value will contain the values
 * @param _size is the maximum size of returned arrays
 * @return the real size of the arrays.
 */
unsigned int
SUniMap::getDecoderMap (SStringVector* key, SStringVector* value,
        unsigned int _size)
{
  if (delegate==0) return 0;
  /* make sure we don't have too big values */
  SBMap *map = ((SUniMapDelegate*) delegate)->map;
  return map->getDecoderMap (key, value, _size);
}

/**
 * return true if this is an UMap.
 * umaps are generally faster.
 */
bool
SUniMap::isUMap() const
{
  if (delegate ==0) return true;
  return ((((SUniMapDelegate*) delegate)->map)->isUMap() !=0);
}

/**
 * return true if this is a type 4 bumap
 */
bool
SUniMap::isClustered() const
{
  if (delegate ==0) return true;
  return ((((SUniMapDelegate*) delegate)->map)->mapType==4);
}

  /* For maps with holes */
unsigned int
SUniMap::getDecodePosition (SS_UCS4 key)
{
  if (dindex < 0) return 0;
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  SBMap *map = d->map;
  return map->getLinearPosition((unsigned int)dindex, key);
}
SS_UCS4
SUniMap::getDecodeKey (unsigned int position)
{
  if (dindex < 0) return 0;
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  SBMap *map = d->map;
  return map->getLinearKey((unsigned int)dindex, position);
}
SS_UCS4
SUniMap::getDecodeValue (unsigned int position)
{
  if (dindex < 0) return 0;
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  SBMap *map = d->map;
  return map->getLinearValue((unsigned int)dindex, position);
}

unsigned int
SUniMap::getEncodePosition (SS_UCS4 key)
{
  if (eindex < 0) return 0;
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  SBMap *map = d->map;
  return map->getLinearPosition((unsigned int)eindex, key);
}
SS_UCS4
SUniMap::getEncodeKey (unsigned int position)
{
  if (eindex < 0) return 0;
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  SBMap *map = d->map;
  return map->getLinearKey((unsigned int)eindex, position);
}
SS_UCS4
SUniMap::getEncodeValue (unsigned int position)
{
  if (eindex < 0) return 0;
  SUniMapDelegate* d = (SUniMapDelegate*) delegate;
  SBMap *map = d->map;
  return map->getLinearValue((unsigned int)eindex, position);
}

/**
 * Convert mys length to max value.
 * return 0 on fail.
 */
static SS_UCS4
getMaxBytes (unsigned int inp)
{
  /* FIXME: maxBytes algorithm is fixed here. please fix it
   * in decode and encode - look for maxBytes.
   */
  switch (inp)
  {
  case 0: return (0xff);
  case 1: return (0xffff);
  case 2: return (0xffffffff);
  }
  /* don't support 64 bit */
  return 0;
}