comparison pads2gpcb/gpcbout.c @ 45:3bdb1b5ff3d0

pads2gpcb: gpcb dimension output implemented
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Sat, 30 Jan 2016 07:15:31 +0000
parents
children
comparison
equal deleted inserted replaced
44:bf1be6c97c28 45:3bdb1b5ff3d0
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5
6 static char *
7 generate_decimal(dim, nfract, cp)
8 long dim;
9 int nfract;
10 char *cp;
11 {
12 int i;
13
14 *cp = '\0';
15 for (i = 0; i < nfract; i++) {
16 *--cp = '0' + dim % 10;
17 dim /= 10;
18 }
19 *--cp = '.';
20 do {
21 *--cp = '0' + dim % 10;
22 dim /= 10;
23 } while (dim);
24 return cp;
25 }
26
27 static void
28 trim_decimal(str)
29 char *str;
30 {
31 char *point, *cp;
32
33 point = index(str, '.');
34 if (!point) {
35 fprintf(stderr,
36 "BUG: gpcbout.c:trim_decimal(): no '.' found\n");
37 exit(1);
38 }
39 cp = index(str, '\0');
40 while (cp > point + 1) {
41 if (cp[-1] != '0')
42 break;
43 cp--;
44 }
45 if (cp == point + 1)
46 cp--;
47 *cp = '\0';
48 }
49
50 char *
51 output_gpcb_dimension(dim, buf)
52 long dim;
53 char *buf;
54 {
55 int negative = 0;
56 int nfract;
57 char *unit_suffix, *outptr;
58
59 if (dim == 0)
60 return "0";
61 if (dim < 0) {
62 negative = 1;
63 dim = -dim;
64 }
65 if (dim % 254) {
66 nfract = 6;
67 unit_suffix = "mm";
68 } else {
69 dim /= 254;
70 nfract = 2;
71 unit_suffix = "mil";
72 }
73 outptr = generate_decimal(dim, nfract, buf + 12);
74 trim_decimal(outptr);
75 strcat(outptr, unit_suffix);
76 if (negative)
77 *--outptr = '-';
78 return outptr;
79 }