File: unsigned.c

package info (click to toggle)
xplot-xplot.org 0.90.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,884 kB
  • sloc: ansic: 4,311; perl: 1,087; sh: 152; makefile: 112
file content (237 lines) | stat: -rw-r--r-- 4,551 bytes parent folder | download | duplicates (4)
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
/* 
This software is being provided to you, the LICENSEE, by the
Massachusetts Institute of Technology (M.I.T.) under the following
license.  By obtaining, using and/or copying this software, you agree
that you have read, understood, and will comply with these terms and
conditions:

Permission to use, copy, modify and distribute, including the right to
grant others the right to distribute at any tier, this software and
its documentation for any purpose and without fee or royalty is hereby
granted, provided that you agree to comply with the following
copyright notice and statements, including the disclaimer, and that
the same appear on ALL copies of the software and documentation,
including modifications that you make for internal use or for
distribution:

Copyright 1992,1993 by the Massachusetts Institute of Technology.
                    All rights reserved.

THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
OR WARRANTIES, EXPRESS OR IMPLIED.  By way of example, but not
limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE
OF THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD
PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.

The name of the Massachusetts Institute of Technology or M.I.T. may
NOT be used in advertising or publicity pertaining to distribution of
the software.  Title to copyright in this software and any associated
documentation shall at all times remain with M.I.T., and USER agrees
to preserve same.
*/
#include "xplot.h"
#include <stdio.h>

#ifdef ultrix
#define LIBC_ATOI_IS_BROKEN
#endif

#ifdef linux
#define LIBC_ATOI_IS_BROKEN
#endif

#ifdef __NetBSD__
#define LIBC_ATOI_IS_BROKEN
#endif

#ifdef LIBC_ATOI_IS_BROKEN
#include <ctype.h>
#endif


char *unsigned_unparse(coord c)
{

  extern void panic();

  char *r;
  char buf[50];
  (void) sprintf(buf,"%u",c.u);
  r = malloc((unsigned) strlen(buf)+1);
  if (r == 0)
    fatalerror("malloc returned 0");
  (void) strcpy(r, buf);
  return r;
}
	 
coord unsigned_parse(char *s)
{
  coord r;
#ifndef LIBC_ATOI_IS_BROKEN
  extern int atoi();

  r.u = atoi(s);
#else
  char *p;
  r.u = 0;
  p = s;

  while (isdigit(*p)) {
    r.u *= 10;
    r.u += (*p - '0');
    p++;
  }
  if (*p != '\0')
    fprintf(stderr,"warning: unsigned_parse format error in string: %s\n", s);
#endif  

  return r;
}

coord unsigned_zero(void)
{
  coord r;
  r.u = 0;
  return r;
}

int unsigned_cmp(coord c1, coord c2)
{
#ifdef SDO
  /* SDO's way, consider wrap around at edges of picture */
  int diff;
  
  diff = c1.u - c2.u;
  if (diff < 0)
    return(-1);
  else if (diff > 0)
    return(1);
  else
    return(0);
#else
  if (c1.u > c2.u) return 1;
  else if (c1.u < c2.u) return -1;
  else return 0;
#endif
}

coord unsigned_add(coord c1, coord c2)
{
  coord r;
  r.u = c1.u + c2.u;
  /****** need to check for overflow ? */
  return r;
}

coord unsigned_subtract(coord c1, coord c2)
{
  coord r;
  r.u = c1.u - c2.u;
  /****** need to check for overflow ? */
  return r;
}

coord unsigned_round_down(coord c1, coord c2)
{
  coord r;
  r.u = c1.u - (c1.u % c2.u);
  /****** need to check for overflow ? */
  return r;

}

coord unsigned_round_up(coord c1, coord c2)
{
  coord r;

  if (c1.u % c2.u == 0)
    r.u = c1.u;
  else
    r.u = c1.u + (c2.u - (c1.u % c2.u));

  return r;
}

coord unsigned_tick(int level)
{
  coord r;

  r.u = 1;
  while (level >= 3) {
    r.u *= 10;
    level -= 3;
  }

  switch (level) {
  case 2:
    r.u *= 5;
    level -= 2;
    break;
  case 1:
    r.u *= 2;
    level -= 1;
    break;
  case 0:
    break;
  }

  return r;
}

int unsigned_subtick(int level)
{
  int r = level;

  if (level < 2) r = 0;
  else
    switch (level%3) {
    case 2:
      r = level - 2;
      break;
    case 1:
      r = level - 1;
      break;
    case 0:
      r = level - 1;
      break;
    }

  return r;
}



double unsigned_map(coord c1,coord c2, int n, coord c)
{
  double r;

  r = (((double) ((double) c.u - (double) c1.u)) *
	(((double) n) / ((double) (c2.u - c1.u))));

  return r;
}

coord unsigned_unmap(coord c1, coord c2, int n, double x)
{
  coord r;

  r.u = rint(((double) c1.u) + (((double) x) *
				(((double) (c2.u - c1.u)) / (double) n)));
  return r;
}

struct coord_impl unsigned_impl  = {
  unsigned_unparse,
  unsigned_parse,
  unsigned_zero,
  unsigned_cmp,
  unsigned_add,
  unsigned_subtract,
  unsigned_round_up,
  unsigned_round_down,
  unsigned_tick,
  unsigned_subtick,
  unsigned_map,
  unsigned_unmap
  };