This is utility.c in view mode; [Download] [Up]
#import "utility.h" #import <stdio.h> #define UTIL_DEBUG // // getbits() // // Return n bits of x that begin at p. // // i.e. getbits(x, 4, 3) returns a byte with bits 4, 3 and 2 right-adjusted. // long getbits(long x, long p, long n) { if((p > HIGHBIT) || (n > (p + 1)) || (p < 0) || (n < 0)) return 0L; return (x >> (p+1-n)) & ~(~0 << n); } // // getnibble() // // Get a nibble (right-adjusted). // long getnibble(long b, long n) { if((n > HIGHNIBBLE) || (n < 0)) return 0L; return getbits(b, (((n+1)*4)-1), 4); } // // getbit() // // Get a bit from a byte. // long getbit(long b, long n) { if((n > HIGHBIT) || (n < 0)) return 0L; return getbits(b, n, 1); } // // getbyte() // // Get a byte from a word. // long getbyte(long w, long n) { if((n > HIGHBYTE) || (n < 0)) return 0L; return getbits(w, (((n+1)*8)-1), 8); } // // add() // long add(long a, long b) { long carry[5]; long tempsum[4]; long sum[4]; long value; long niba, nibb; long loop; carry[0] = 0; for(loop = 0; loop < 4; loop++) { niba = getnibble(a, loop); nibb = getnibble(b, loop); tempsum[loop] = niba + nibb + carry[loop]; carry[loop+1] = tempsum[loop] / 16; tempsum[loop] = tempsum[loop] % 16; sum[loop] = tempsum[loop] + carry[loop]; } value = sum[0] + (16 * sum[1]) + (256 * sum[2]) + (4096 * sum[3]); #ifdef UTIL_DEBUG printf("Util: Sum of %04X and %04X is %04X.\n", a, b, value); #endif return value; } // // readline() // // return of // 0 : No problems. // -1 : End of file. // int readline(FILE *file, char *buffer) { long count = 0; char c = '\0'; while(1) { c = fgetc(file); switch(c) { case '\n': buffer[count] = '\0'; return 0; break; case EOF: buffer[count] = '\0'; return -1; break; default: buffer[count] = c; count++; break; } } return 1; } // // End of file. //
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.