aboutsummaryrefslogtreecommitdiff
path: root/unix/gdev/sgidev/sgiUtil.c
blob: 25214450a056a1d9a5ce2a90376b9ef93eb6bcca (plain) (blame)
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
/**
 *  SGIUTIL.C -- Shared utility procedures for the SGI translators.
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define	import_spp
#define	import_error
#include <iraf.h>


/** 
 *  BSWAP2 -- Move bytes from array "a" to array "b", swapping successive
 *  pairs of bytes.  The two arrays may be the same but may not be offset
 *  and overlapping.
 */
void
bswap2 (
    unsigned char *a,			/* input array			*/
    unsigned char *b,			/* output array			*/
    int	  nbytes		/* number of bytes to swap	*/
)
{
    register unsigned char *ip, *op, *otop;
    register unsigned int temp;

    ip = a;
    op = b;
    otop = op + (nbytes & ~1);

    /* Swap successive pairs of bytes.
     */
    while (op < otop) {
        temp  = *ip++;
        *op++ = *ip++;
        *op++ = temp;
    }

    /* If there is an odd byte left, move it to the output array.
     */
    if (nbytes & 1)
        *op = *ip;
}


/* BSWAP4 - Move bytes from array "a" to array "b", swapping the four bytes
 * in each successive 4 byte group, i.e., 12345678 becomes 43218765.
 * The input and output arrays may be the same but may not partially overlap.
*/
void
bswap4 (
    unsigned char  *a,                     /* input array                  */
    unsigned char  *b,                     /* output array                 */
    int    nbytes                 /* number of bytes to swap      */
)
{
    register int    n;
    register unsigned char *ip, *op, *tp;
    static   unsigned char  temp[4];

    tp = temp;
    ip = (unsigned char *)a;
    op = (unsigned char *)b;

    /* Swap successive four byte groups.
     */
    for (n = nbytes >> 2;  --n >= 0;  ) {
        *tp++ = *ip++;
        *tp++ = *ip++;
        *tp++ = *ip++;
        *tp++ = *ip++;
        *op++ = *--tp;
        *op++ = *--tp;
        *op++ = *--tp;
        *op++ = *--tp;
    }

    /* If there are any odd bytes left, move them to the output array.
     * Do not bother to swap as it is unclear how to swap a partial
     * group, and really incorrect if the data is not modulus 4.
     */
    for (n = nbytes & 03;  --n >= 0;  )
        *op++ = *ip++;
}


/**
 *  ISSWAPPED -- Test whether we are running on a byte-swapped machine.
 */
int
isSwapped (void)
{
    union {
        short   tswap;
        char    b[2];
    } test;

    test.tswap = 1;
    return ((int) test.b[0]);
}	


/**
 *  GET_IARG -- Get an integer argument, whether appended directly to flag
 *  or separated by a whitespace character; if error, report and assign
 *  default.
 */
int
get_iarg (
    char   argp,
    char **argv,
    int    argno,
    int    def_val
)
{
    int     temp_val;

    if (argp == (char) 0) {
        if (argv[argno+1] == NULL) {
            fprintf (stderr, "missing arg to switch `%c';", argp);
            fprintf (stderr, " reset to %d\n", def_val);
            temp_val = def_val;
        } else
            temp_val = atoi (argv[++argno]);
    } else
        temp_val = atoi (argv[argno]+2);

    return (temp_val);
}