File: misc.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (371 lines) | stat: -rw-r--r-- 7,232 bytes parent folder | download | duplicates (2)
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
/****************************************************************
 * July 5, 1991
 * Copyright 1991 Lance Norskog And Sundry Contributors
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained. 
 * Lance Norskog And Sundry Contributors are not responsible for 
 * the consequences of using this software.
 ****************************************************************/

/**************************************************************
 * Sound Tools miscellaneous stuff.
 ****************************************************************/

#include "st.h"
#include "../machine.h"

extern void sciprint __PARAMS((char *fmt,...));

EXPORT char *sizes[] = {
			 "NONSENSE!",
			 "bytes",
			 "shorts",
			 "NONSENSE",
			 "longs",
			 "32-bit floats",
			 "64-bit floats",
			 "IEEE floats"
		       };

EXPORT char *styles[] = {
			  "NONSENSE!",
			  "unsigned",
			  "signed (2's complement)",
			  "u-law",
			  "a-law"
			};

char readerr[] = "Premature EOF while reading sample file.";
char writerr[] = "Error writing sample file.  You are probably out of disk space.";

/* Utilities */
/* Read short, little-endian: little end first. VAX/386 style. */

unsigned short rlshort(ft_t ft)
{
  unsigned char uc, uc2;
  uc  = getc(ft->fp);
  uc2 = getc(ft->fp);
  return (uc2 << 8) | uc;
}

/* Read short, bigendian: big first. 68000/SPARC style. */

unsigned short rbshort(ft_t ft)
{
  unsigned char uc, uc2;
  uc2 = getc(ft->fp);
  uc  = getc(ft->fp);
  return (uc2 << 8) | uc;
}

/* Write short, little-endian: little end first. VAX/386 style. */

void wlshort(ft_t ft, short unsigned int us)
{
  putc(us, ft->fp);
  putc(us >> 8, ft->fp);
  if (ferror(ft->fp))
    {
      sciprint("%s\r\n",writerr);
      ft->ierr=1;
    }
}

/* Write short, big-endian: big end first. 68000/SPARC style. */

void wbshort(ft_t ft, short unsigned int us)
{
  putc(us >> 8, ft->fp);
  putc(us, ft->fp);
  if (ferror(ft->fp))
    {
      sciprint("%s\r\n",writerr);
      ft->ierr=1;
    }
}

/* Read long, little-endian: little end first. VAX/386 style. */

unsigned long
rllong(ft_t ft)
{
  unsigned char uc, uc2, uc3, uc4;
  uc  = getc(ft->fp);
  uc2 = getc(ft->fp);
  uc3 = getc(ft->fp);
  uc4 = getc(ft->fp);
  return ((long)uc4 << 24) | ((long)uc3 << 16) | ((long)uc2 << 8) | (long)uc;
}


/* Read long, bigendian: big first. 68000/SPARC style. */
unsigned long
rblong(ft_t ft)
{
  unsigned char uc, uc2, uc3, uc4;
  uc  = getc(ft->fp);
  uc2 = getc(ft->fp);
  uc3 = getc(ft->fp);
  uc4 = getc(ft->fp);
  return ((long)uc << 24) | ((long)uc2 << 16) | ((long)uc3 << 8) | (long)uc4;
}

/* Write long, little-endian: little end first. VAX/386 style. */
void wllong(ft_t ft, long unsigned int ul)
{
  int datum;
  datum = (ul) & 0xff;
  putc(datum, ft->fp);
  datum = (ul >> 8) & 0xff;
  putc(datum, ft->fp);
  datum = (ul >> 16) & 0xff;
  putc(datum, ft->fp);
  datum = (ul >> 24) & 0xff;
  putc(datum, ft->fp);
  if (ferror(ft->fp))
    {
      sciprint("%s\r\n",writerr);
      ft->ierr=1;
    }
}

/* Write long, big-endian: big end first. 68000/SPARC style. */
void wblong(ft_t ft, long unsigned int ul)
{
  int datum;

  datum = (ul >> 24) & 0xff;
  putc(datum, ft->fp);
  datum = (ul >> 16) & 0xff;
  putc(datum, ft->fp);
  datum = (ul >> 8) & 0xff;
  putc(datum, ft->fp);
  datum = (ul) & 0xff;
  putc(datum, ft->fp);
  if (ferror(ft->fp))
    {
      sciprint("%s\r\n",writerr);
      ft->ierr=1;
    }
}

/* Read and write words and longs in "machine format".  Swap if indicated. */

/* Read short. */
unsigned short
rshort(ft_t ft)
{
  unsigned short us;
  fread(&us, 2, 1, ft->fp);
  if (ft->swap)
    us = swapw(us);
  return us;
}

/* Write short. */
void wshort(ft_t ft, short unsigned int us)
{
  if (ft->swap)
    us = swapw(us);
  if (fwrite(&us, 2, 1, ft->fp) != 1)
    {
      sciprint("%s\r\n",writerr);
      ft->ierr=1;
    }
}

/* Read a long. : note that long size is machine dependant  */
unsigned long
rlong(ft_t ft)
{
  unsigned long ul;
  fread(&ul, sizeof(long), 1, ft->fp);
  if (ft->swap)
    ul = swapl(ul);
  return ul;
}

/* Write long. */
void wlong(ft_t ft, long unsigned int ul)
{
  if (ft->swap)
    ul = swapl(ul);
  if (fwrite(&ul,sizeof(long), 1, ft->fp) != 1)
    {
      sciprint("%s\r\n",writerr);
      ft->ierr=1;
    }
}

/* Read float. */
float
rfloat(ft_t ft)
{
  float f;
  fread(&f, sizeof(float), 1, ft->fp);
  if (ft->swap)
    f = swapf(f);
  return f;
}

void wfloat(ft_t ft, float f)
{
  float t = f;
  if (ft->swap)
    t = swapf(t);
  if (fwrite(&t, sizeof(float), 1, ft->fp) != 1)
    {
      sciprint("%s\r\n",writerr);
      ft->ierr=1;
    }
}

/* Read double. */
double
rdouble(ft_t ft)
{
  double d;
  fread(&d, sizeof(double), 1, ft->fp);
  if (ft->swap)
    d = swapd(d);
  return d;
}

/* Write double. */
void
wdouble(ft_t ft, double d)
{
  if (ft->swap)
    d = swapd(d);
  if (fwrite(&d, sizeof(double), 1, ft->fp) != 1)
    {
      sciprint("%s\r\n",writerr);
      ft->ierr=1;
    }
}

/* generic swap routine */
static void
swapb(char *l, char *f, int n)
{    
  int i;
  for (i= 0; i< n; i++) f[i]= l[n-i-1];
}


/* Byte swappers */

unsigned short
#if	defined(__STDC__) || defined(ARM)
swapw(unsigned short us)
#else
     swapw(us)
     unsigned short us;
#endif
{
  return ((us >> 8) | (us << 8)) & 0xffff;
}

/** swapl : swap a long : note that a long size is machine dependant **/

unsigned long
swapl(long unsigned int ul)
{
  unsigned long  sdf;
  swapb((char *) &ul,(char *) &sdf, sizeof(unsigned long));
  return (sdf);
}

/** swap an int assumed to be on 4 bytes **/

unsigned int 
swapi(unsigned int ul)
{
  return (ul >> 24) | ((ul >> 8) & 0xff00) | ((ul << 8) & 0xff0000) | (ul << 24);
}

/* return swapped 32-bit float */
float
#if	defined(__STDC__) || defined(ARM)
swapf(float uf)
#else
     swapf(uf)
     float uf;
#endif
{
  if (sizeof(long) == sizeof(float)){
    union {
      unsigned long l;  /** we assume here long is 4 bytes **/
      float f;
    } u;
    u.f= (float)uf;
    u.l= (u.l>>24) | ((u.l>>8)&0xff00) | ((u.l<<8)&0xff0000) | (u.l<<24);
    return u.f;
  }
  else {
    union {
      unsigned int l;  /** we assume here int is 4 bytes **/
      float f;
    } u;
    u.f= (float) uf;
    u.l= (u.l>>24) | ((u.l>>8)&0xff00) | ((u.l<<8)&0xff0000) | (u.l<<24);
    return u.f;
  }
}

double
swapd(double df)
{
  double sdf;
  swapb((char *) &df,(char *) &sdf, sizeof(double));
  return (sdf);
}


/* dummy routine for do-nothing functions */
/* int nothing() {;} */
     
/* dummy drain routine for effects */


/* here for linear interp.  might be useful for other things */
long gcd(long int a, long int b)
{
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

long lcm(long int a, long int b)
{
  return (a * b) / gcd(a, b);
}

/** configure should make this for mingw32 */

#ifdef __MINGW32__
#define HAVE_STRERROR
#endif 


#ifndef HAVE_STRERROR

/* strerror function */

char *strerror(int errcode)
{
  static char  nomesg[30];
  extern int sys_nerr;
  extern char *sys_errlist[];
  if (errcode < sys_nerr)
    return (sys_errlist[errcode]);
  else
    {
      sprintf (nomesg, "Undocumented error %d", errcode);
      return (nomesg);
    }
}
#endif