summaryrefslogtreecommitdiffstats
path: root/contrib/groff/src/utils/hpftodit/hpftodit.cc
blob: f81f5ad80952e41ee27f42b88138fcf755e05ac0 (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
// -*- C++ -*-
/* Copyright (C) 1994, 2000, 2001 Free Software Foundation, Inc.
     Written by James Clark (jjc@jclark.com)

This file is part of groff.

groff is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

groff 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 groff; see the file COPYING.  If not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/*
TODO
put human readable font name in device file
devise new names for useful characters
use --- for unnamed characters
option to specify symbol sets to look in
make it work with TrueType fonts
put filename in error messages (or fix lib)
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <errno.h>
#include "assert.h"
#include "lib.h"
#include "posix.h"
#include "errarg.h"
#include "error.h"
#include "cset.h"
#include "nonposix.h"

#define SIZEOF(v) (sizeof(v)/sizeof(v[0]))

const int MULTIPLIER = 3;

inline
int scale(int n)
{
  return n * MULTIPLIER;
}

// tags in TFM file

enum tag_type {
  min_tag = 400,
  type_tag = 400,
  symbol_set_tag = 404,
  msl_tag = 403,
  inches_per_point_tag = 406,
  design_units_per_em_tag = 408,
  posture_tag = 409,
  stroke_weight_tag = 411,
  spacing_tag = 412,
  slant_tag = 413,
  appearance_width_tag = 414,
  word_spacing_tag = 421,
  x_height_tag = 424,
  lower_ascent_tag = 427,
  lower_descent_tag = 428,
  width_tag = 433,
  left_extent_tag = 435,
  right_extent_tag = 436,
  ascent_tag = 437,
  descent_tag = 438,
  pair_kern_tag = 439,
  typeface_tag = 442,
  max_tag = 443
  };

// types in TFM file

enum {
  ENUM_TYPE = 1,
  BYTE_TYPE = 2,
  USHORT_TYPE = 3,
  FLOAT_TYPE = 5,
  SIGNED_SHORT_TYPE = 17
  };


typedef unsigned char byte;
typedef unsigned short uint16;
typedef short int16;
typedef unsigned int uint32;

class File {
public:
  File(const char *);
  void skip(int n);
  byte get_byte();
  uint16 get_uint16();
  uint32 get_uint32();
  void seek(uint32 n);
private:
  unsigned char *buf_;
  const unsigned char *ptr_;
  const unsigned char *end_;
};

struct entry {
  char present;
  uint16 type;
  uint32 count;
  uint32 value;
  entry() : present(0) { }
};

struct char_info {
  uint16 msl;
  uint16 width;
  uint16 ascent;
  int16 descent;
  int16 left_extent;
  uint16 right_extent;
  uint16 symbol_set;
  unsigned char code;
};

const uint16 NO_SYMBOL_SET = 0;

struct name_list {
  char *name;
  name_list *next;
  name_list(const char *s, name_list *p) : name(strsave(s)), next(p) { }
  ~name_list() { a_delete name; }
};

struct symbol_set {
  uint16 select;
  uint16 index[256];
};

#define SYMBOL_SET(n, c) ((n) * 32 + ((c) - 64))

uint16 text_symbol_sets[] = {
  SYMBOL_SET(0, 'N'),		// Latin 1
  SYMBOL_SET(6, 'J'),		// Microsoft Publishing
  SYMBOL_SET(2, 'N'),		// Latin 2
  0
  };

uint16 special_symbol_sets[] = {
  SYMBOL_SET(8, 'M'),
  SYMBOL_SET(5, 'M'),
  SYMBOL_SET(15, 'U'),
  0
  };

entry tags[max_tag + 1 - min_tag];

char_info *char_table;
uint32 nchars;

int msl_name_table_size = 0;
name_list **msl_name_table = 0;

int n_symbol_sets;
symbol_set *symbol_set_table;

static int special_flag = 0;
static int italic_flag = 0;
static int italic_sep;

static void usage(FILE *stream);
static void usage();
static const char *xbasename(const char *);
static void read_tags(File &);
static void check_type();
static void check_units(File &);
static int read_map(const char *);
static void require_tag(tag_type);
static void dump_tags(File &f);
static void output_spacewidth();
static void output_pclweight();
static void output_pclproportional();
static void read_and_output_pcltypeface(File &);
static void output_pclstyle();
static void output_slant();
static void output_ligatures();
static void read_symbol_sets(File &);
static void read_and_output_kernpairs(File &);
static void output_charset();
static void read_char_table(File &f);

inline
entry &tag_info(tag_type t)
{
  return tags[t - min_tag];
}

int main(int argc, char **argv)
{
  program_name = argv[0];

  int opt;
  int debug_flag = 0;

  static const struct option long_options[] = {
    { "help", no_argument, 0, CHAR_MAX + 1 },
    { "version", no_argument, 0, 'v' },
    { NULL, 0, 0, 0 }
  };
  while ((opt = getopt_long(argc, argv, "dsvi:", long_options, NULL)) != EOF) {
    switch (opt) {
    case 'd':
      debug_flag = 1;
      break;
    case 's':
      special_flag = 1;
      break;
    case 'i':
      italic_flag = 1;
      italic_sep = atoi(optarg);
      break;
    case 'v':
      {
	extern const char *Version_string;
	printf("GNU hpftodit (groff) version %s\n", Version_string);
	exit(0);
      }
      break;
    case CHAR_MAX + 1: // --help
      usage(stdout);
      exit(0);
      break;
    case '?':
      usage();
      break;
    default:
      assert(0);
    }
  }
  if (argc - optind != 3)
    usage();
  File f(argv[optind]);
  if (!read_map(argv[optind + 1]))
    exit(1);
  current_filename = 0;
  current_lineno = -1;		// no line numbers
  if (freopen(argv[optind + 2], "w", stdout) == 0)
    fatal("cannot open `%1': %2", argv[optind + 2], strerror(errno));
  current_filename = argv[optind];
  printf("name %s\n", xbasename(argv[optind + 2]));
  if (special_flag)
    printf("special\n");
  read_tags(f);
  check_type();
  check_units(f);
  if (debug_flag)
    dump_tags(f);
  read_char_table(f);
  output_spacewidth();
  output_slant();
  read_and_output_pcltypeface(f);
  output_pclproportional();
  output_pclweight();
  output_pclstyle();
  read_symbol_sets(f);
  output_ligatures();
  read_and_output_kernpairs(f);
  output_charset();
  return 0;
}

static
void usage(FILE *stream)
{
  fprintf(stream, "usage: %s [-s] [-i n] tfm_file map_file output_font\n",
	  program_name);
}
static
void usage()
{
  usage(stderr);
  exit(1);
}

File::File(const char *s)
{
  // We need to read the file in binary mode because hpftodit relies
  // on byte counts.
  int fd = open(s, O_RDONLY | O_BINARY);
  if (fd < 0)
    fatal("cannot open `%1': %2", s, strerror(errno));
  current_filename = s;
  struct stat sb;
  if (fstat(fd, &sb) < 0)
    fatal("cannot stat: %1", strerror(errno));
  if (!S_ISREG(sb.st_mode))
    fatal("not a regular file");
  buf_ = new unsigned char[sb.st_size];
  long nread = read(fd, buf_, sb.st_size);
  if (nread < 0)
    fatal("read error: %1", strerror(errno));
  if (nread != sb.st_size)
    fatal("read unexpected number of bytes");
  ptr_ = buf_;
  end_ = buf_ + sb.st_size;
  // These are actually text files, so we must get rid of the `\r'
  // characters.  This is also enabled for Posix systems, in case the
  // input came from Windows...
  unsigned char *p = buf_, *q = buf_;
  while (q < end_)
    {
      if (*q == '\r')
	{
	  if (*++q != '\n')
	    *p++ = '\r';
	}
#if defined(__MSDOS__) || defined(_MSC_VER)
      if (*q == '\032')	// ^Z means ``software EOF''
	break;
#endif
      *p++ = *q++;
    }
  end_ = p;
}

void File::skip(int n)
{
  if (end_ - ptr_ < n)
    fatal("unexpected end of file");
  ptr_ += n;
}

void File::seek(uint32 n)
{
  if (end_ - buf_ < n)
    fatal("unexpected end of file");
  ptr_ = buf_ + n;
}

byte File::get_byte()
{
  if (ptr_ >= end_)
    fatal("unexpected end of file");
  return *ptr_++;
}

uint16 File::get_uint16()
{
  if (end_ - ptr_ < 2)
    fatal("unexpected end of file");
  uint16 n = *ptr_++;
  return n + (*ptr_++ << 8);
}

uint32 File::get_uint32()
{
  if (end_ - ptr_ < 4)
    fatal("unexpected end of file");
  uint32 n = *ptr_++;
  for (int i = 0; i < 3; i++)
    n += *ptr_++ << (i + 1)*8;
  return n;
}

static
void read_tags(File &f)
{
  if (f.get_byte() != 'I' || f.get_byte() != 'I')
    fatal("not an Intel format TFM file");
  f.skip(6);
  uint16 ntags = f.get_uint16();
  entry dummy;
  for (uint16 i = 0; i < ntags; i++) {
    uint16 tag = f.get_uint16();
    entry *p;
    if (min_tag <= tag && tag <= max_tag)
      p = tags + (tag - min_tag);
    else
      p = &dummy;
    p->present = 1;
    p->type = f.get_uint16();
    p->count = f.get_uint32();
    p->value = f.get_uint32();
  }
}

static
void check_type()
{
  require_tag(type_tag);
  if (tag_info(type_tag).value != 0) {
    if (tag_info(type_tag).value == 2)
      fatal("cannot handle TrueType tfm files");
    fatal("unknown type tag %1", int(tag_info(type_tag).value));
  }
}

static
void check_units(File &f)
{
  require_tag(design_units_per_em_tag);
  f.seek(tag_info(design_units_per_em_tag).value);
  uint32 num = f.get_uint32();
  uint32 den = f.get_uint32();
  if (num != 8782 || den != 1)
    fatal("design units per em != 8782/1");
  require_tag(inches_per_point_tag);
  f.seek(tag_info(inches_per_point_tag).value);
  num = f.get_uint32();
  den = f.get_uint32();
  if (num != 100 || den != 7231)
    fatal("inches per point not 100/7231");
}

static
void require_tag(tag_type t)
{
  if (!tag_info(t).present)
    fatal("tag %1 missing", int(t));
}

static
void output_spacewidth()
{
  require_tag(word_spacing_tag);
  printf("spacewidth %d\n", scale(tag_info(word_spacing_tag).value));
}

static
void read_symbol_sets(File &f)
{
  uint32 symbol_set_dir_length = tag_info(symbol_set_tag).count;
  n_symbol_sets = symbol_set_dir_length/14;
  symbol_set_table = new symbol_set[n_symbol_sets];
  int i;
  for (i = 0; i < n_symbol_sets; i++) {
    f.seek(tag_info(symbol_set_tag).value + i*14);
    (void)f.get_uint32();
    uint32 off1 = f.get_uint32();
    uint32 off2 = f.get_uint32();
    (void)f.get_uint16();		// what's this for?
    f.seek(off1);
    int j;
    uint16 kind = 0;
    for (j = 0; j < off2 - off1; j++) {
      unsigned char c = f.get_byte();
      if ('0' <= c && c <= '9')
	kind = kind*10 + (c - '0');
      else if ('A' <= c && c <= 'Z')
	kind = kind*32 + (c - 64);
    }
    symbol_set_table[i].select = kind;
    for (j = 0; j < 256; j++)
      symbol_set_table[i].index[j] = f.get_uint16();
  }
  for (i = 0; i < nchars; i++)
    char_table[i].symbol_set = NO_SYMBOL_SET;

  uint16 *symbol_set_selectors = (special_flag
				  ? special_symbol_sets
				  : text_symbol_sets);
  for (i = 0; symbol_set_selectors[i] != 0; i++) {
    int j;
    for (j = 0; j < n_symbol_sets; j++)
      if (symbol_set_table[j].select == symbol_set_selectors[i])
	break;
    if (j < n_symbol_sets) {
      for (int k = 0; k < 256; k++) {
	uint16 index = symbol_set_table[j].index[k];
	if (index != 0xffff
	    && char_table[index].symbol_set == NO_SYMBOL_SET) {
	  char_table[index].symbol_set = symbol_set_table[j].select;
	  char_table[index].code = k;
	}
      }
    }
  }
}

static
void read_char_table(File &f)
{
  require_tag(msl_tag);
  nchars = tag_info(msl_tag).count;
  char_table = new char_info[nchars];

  f.seek(tag_info(msl_tag).value);
  uint32 i;
  for (i = 0; i < nchars; i++)
    char_table[i].msl = f.get_uint16();
  
  require_tag(width_tag);
  f.seek(tag_info(width_tag).value);
  for (i = 0; i < nchars; i++)
    char_table[i].width = f.get_uint16();

  require_tag(ascent_tag);
  f.seek(tag_info(ascent_tag).value);
  for (i = 0; i < nchars; i++) {
    char_table[i].ascent = f.get_uint16();
  }

  require_tag(descent_tag);
  f.seek(tag_info(descent_tag).value);
  for (i = 0; i < nchars; i++) {
    char_table[i].descent = f.get_uint16();
    if (char_table[i].descent > 0)
      char_table[i].descent = 0;
  }

  require_tag(left_extent_tag);
  f.seek(tag_info(left_extent_tag).value);
  for (i = 0; i < nchars; i++)
    char_table[i].left_extent = int16(f.get_uint16());

  require_tag(right_extent_tag);
  f.seek(tag_info(right_extent_tag).value);
  for (i = 0; i < nchars; i++)
    char_table[i].right_extent = f.get_uint16();
}

static
void output_pclweight()
{
  require_tag(stroke_weight_tag);
  int stroke_weight = tag_info(stroke_weight_tag).value;
  int pcl_stroke_weight;
  if (stroke_weight < 128)
    pcl_stroke_weight = -3;
  else if (stroke_weight == 128)
    pcl_stroke_weight = 0;
  else if (stroke_weight <= 145)
    pcl_stroke_weight = 1;
  else if (stroke_weight <= 179)
    pcl_stroke_weight = 3;
  else
    pcl_stroke_weight = 4;
  printf("pclweight %d\n", pcl_stroke_weight);
}

static
void output_pclproportional()
{
  require_tag(spacing_tag);
  printf("pclproportional %d\n", tag_info(spacing_tag).value == 0);
}

static
void read_and_output_pcltypeface(File &f)
{
  printf("pcltypeface ");
  require_tag(typeface_tag);
  f.seek(tag_info(typeface_tag).value);
  for (uint32 i = 0; i < tag_info(typeface_tag).count; i++) {
    unsigned char c = f.get_byte();
    if (c == '\0')
      break;
    putchar(c);
  }
  printf("\n");
}

static
void output_pclstyle()
{
  unsigned pcl_style = 0;
  // older tfms don't have the posture tag
  if (tag_info(posture_tag).present) {
    if (tag_info(posture_tag).value)
      pcl_style |= 1;
  }
  else {
    require_tag(slant_tag);
    if (tag_info(slant_tag).value != 0)
      pcl_style |= 1;
  }
  require_tag(appearance_width_tag);
  if (tag_info(appearance_width_tag).value < 100) // guess
    pcl_style |= 4;
  printf("pclstyle %d\n", pcl_style);
}

static
void output_slant()
{
  require_tag(slant_tag);
  int slant = int16(tag_info(slant_tag).value);
  if (slant != 0)
    printf("slant %f\n", slant/100.0);
}

static
void output_ligatures()
{
  // don't use ligatures for fixed space font
  require_tag(spacing_tag);
  if (tag_info(spacing_tag).value != 0)
    return;
  static const char *ligature_names[] = {
    "fi", "fl", "ff", "ffi", "ffl"
    };

  static const char *ligature_chars[] = {
    "fi", "fl", "ff", "Fi", "Fl"
    };
  
  unsigned ligature_mask = 0;
  int i;
  for (i = 0; i < nchars; i++) {
    uint16 msl = char_table[i].msl;
    if (msl < msl_name_table_size
	&& char_table[i].symbol_set != NO_SYMBOL_SET) {
      for (name_list *p = msl_name_table[msl]; p; p = p->next)
	for (int j = 0; j < SIZEOF(ligature_chars); j++)
	  if (strcmp(p->name, ligature_chars[j]) == 0) {
	    ligature_mask |= 1 << j;
	    break;
	  }
      }
    }
  if (ligature_mask) {
    printf("ligatures");
    for (i = 0; i < SIZEOF(ligature_names); i++)
      if (ligature_mask & (1 << i))
	printf(" %s", ligature_names[i]);
    printf(" 0\n");
  }
}

static
void read_and_output_kernpairs(File &f)
{
  if (tag_info(pair_kern_tag).present) {
    printf("kernpairs\n");
    f.seek(tag_info(pair_kern_tag).value);
    uint16 n_pairs = f.get_uint16();
    for (int i = 0; i < n_pairs; i++) {
      uint16 i1 = f.get_uint16();
      uint16 i2 = f.get_uint16();
      int16 val = int16(f.get_uint16());
      if (char_table[i1].symbol_set != NO_SYMBOL_SET
	  && char_table[i2].symbol_set != NO_SYMBOL_SET
	  && char_table[i1].msl < msl_name_table_size
	  && char_table[i2].msl < msl_name_table_size) {
	for (name_list *p = msl_name_table[char_table[i1].msl];
	     p;
	     p = p->next)
	  for (name_list *q = msl_name_table[char_table[i2].msl];
	       q;
	       q = q->next)
	    printf("%s %s %d\n", p->name, q->name, scale(val));
      }
    }
  }
}

static 
void output_charset()
{
  require_tag(slant_tag);
  double slant_angle = int16(tag_info(slant_tag).value)*PI/18000.0;
  double slant = sin(slant_angle)/cos(slant_angle);

  require_tag(x_height_tag);
  require_tag(lower_ascent_tag);
  require_tag(lower_descent_tag);

  printf("charset\n");
  int i;
  for (i = 0; i < nchars; i++) {
    uint16 msl = char_table[i].msl;
    if (msl < msl_name_table_size
	&& msl_name_table[msl]) {
      if (char_table[i].symbol_set != NO_SYMBOL_SET) {
	printf("%s\t%d,%d",
	       msl_name_table[msl]->name,
	       scale(char_table[i].width),
	       scale(char_table[i].ascent));
	int depth = scale(- char_table[i].descent);
	if (depth < 0)
	  depth = 0;
	int italic_correction = 0;
	int left_italic_correction = 0;
	int subscript_correction = 0;
	if (italic_flag) {
	  italic_correction = scale(char_table[i].right_extent 
				    - char_table[i].width
				    + italic_sep);
	  if (italic_correction < 0)
	    italic_correction = 0;
	  subscript_correction = int((tag_info(x_height_tag).value
				      * slant * .8) + .5);
	  if (subscript_correction > italic_correction)
	    subscript_correction = italic_correction;
	  left_italic_correction = scale(italic_sep
					 - char_table[i].left_extent);
	}
	if (subscript_correction != 0)
	  printf(",%d,%d,%d,%d",
		 depth, italic_correction, left_italic_correction,
		 subscript_correction);
	else if (left_italic_correction != 0)
	  printf(",%d,%d,%d", depth, italic_correction, left_italic_correction);
	else if (italic_correction != 0)
	  printf(",%d,%d", depth, italic_correction);
	else if (depth != 0)
	  printf(",%d", depth);
	// This is fairly arbitrary.  Fortunately it doesn't much matter.
	unsigned type = 0;
	if (char_table[i].ascent > (tag_info(lower_ascent_tag).value*9)/10)
	  type |= 2;
	if (char_table[i].descent < (int16(tag_info(lower_descent_tag).value)*9)/10)
	  type |= 1;
	printf("\t%d\t%d\n",
	       type,
	       char_table[i].symbol_set*256 + char_table[i].code);
	for (name_list *p = msl_name_table[msl]->next; p; p = p->next)
	  printf("%s\t\"\n", p->name);
      }
      else
	warning("MSL %1 not in any of the searched symbol sets", msl);
    }
  }
}

static
void dump_tags(File &f)
{
  int i;
  for (i = min_tag; i <= max_tag; i++) {
    enum tag_type t = tag_type(i);
    if (tag_info(t).present) {
      fprintf(stderr,
	      "%d %d %d %d\n", i, tag_info(t).type, tag_info(t).count,
	      tag_info(t).value);
      if (tag_info(t).type == FLOAT_TYPE
	  && tag_info(t).count == 1) {
	f.seek(tag_info(t).value);
	uint32 num = f.get_uint32();
	uint32 den = f.get_uint32();
	fprintf(stderr, "(%u/%u = %g)\n", num, den, (double)num/den);
      }
    }
  }
}

static
int read_map(const char *file)
{
  errno = 0;
  FILE *fp = fopen(file, "r");
  if (!fp) {
    error("can't open `%1': %2", file, strerror(errno));
    return 0;
  }
  current_filename = file;
  char buf[512];
  current_lineno = 0;
  while (fgets(buf, int(sizeof(buf)), fp)) {
    current_lineno++;
    char *ptr = buf;
    while (csspace(*ptr))
      ptr++;
    if (*ptr == '\0' || *ptr == '#')
      continue;
    ptr = strtok(ptr, " \n\t");
    if (!ptr)
      continue;
    int n;
    if (sscanf(ptr, "%d", &n) != 1) {
      error("bad map file");
      fclose(fp);
      return 0;
    }
    if (n < 0) {
      error("negative code");
      fclose(fp);
      return 0;
    }
    if (n >= msl_name_table_size) {
      size_t old_size = msl_name_table_size;
      name_list **old_table = msl_name_table;
      msl_name_table_size = n + 256;
      msl_name_table = new name_list *[msl_name_table_size];
      if (old_table) {
	memcpy(msl_name_table, old_table, old_size*sizeof(name_list *));
	a_delete old_table;
      }
      for (size_t i = old_size; i < msl_name_table_size; i++)
	msl_name_table[i] = 0;
    }
    ptr = strtok(0, " \n\t");
    if (!ptr) {
      error("missing names");
      fclose(fp);
      return 0;
    }
    for (; ptr; ptr = strtok(0, " \n\t"))
      msl_name_table[n] = new name_list(ptr, msl_name_table[n]);
  }
  fclose(fp);
  return 1;
}

static
const char *xbasename(const char *s)
{
  // DIR_SEPS[] are possible directory separator characters, see
  // nonposix.h.  We want the rightmost separator of all possible
  // ones.  Example: d:/foo\\bar.
  const char *b = strrchr(s, DIR_SEPS[0]), *b1;
  const char *sep = &DIR_SEPS[1];

  while (*sep)
    {
      b1 = strrchr(s, *sep);
      if (b1 && (!b || b1 > b))
	b = b1;
      sep++;
    }
  return b ? b + 1 : s;
}
OpenPOWER on IntegriCloud