Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//
//INCLUDE files for :FSCANBIN.C
//**************************************
//
/* +++Date last modified: 05-Jul-1997 */
/*
** SNIPPETS header file for FSCANBIN.C
*/
#ifndef FSCANBIN__H
#define FSCANBIN__H
#include
#include "sniptype.h"
#define WORDswap(n) (*n = (*n << 8) | (*n >> 8))
#define DWORDswap(n) (\
WORDswap(&((WORD *)n)[0]),\
WORDswap(&((WORD *)n)[1]),\
*n = (*n << 16) | (*n >> 16)\
)
#define maxk 32767
int fscanbin (FILE *fp, char *format, ...);
#endif /* FSCANBIN__H */
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
//
/* +++Date last modified: 05-Jul-1997 */
/* fscanbin.c -- scan binary fields via format string
**
** public domain by Ray GardnerEnglewood, Colorado11/29/89
**
** Usage: fscanbin(FILE *fp, char *format, ...)
**
** where format string contains specifiers:
**-ddd means skip ddd bytes
**i means read a 16-bit int
**l means read a 32-bit int
**sddd means read a character string of up to ddd bytes
**reads up to a nul byte if ddd is zero or missing
**cnnn means read a character field of nnn bytes (not nul-terminated)
**reads one byte if nnn is zero or missing
*/
#include
#include
#include
#include "fscanbin.h"
#define SWAP16 0
#define SWAP32 0
int fscanbin (FILE *fp, char *format, ...)
{
va_list argp;
unsigned char *p;
unsigned k;
int c;
char *charp;
WORD *WORDp;
DWORD *DWORDp;
int bytes_read;
bytes_read = 0;
va_start(argp, format);
for ( p = (unsigned char *)format; *p; )
{
switch( *p & 0xFF )
{
case '-':
for ( k = 0, c = *++p; isdigit(c); c = *++p )
k = 10 * k + c - '0';
if ( k == 0 )
k = 1;
if ( fseek(fp, (long)k, SEEK_CUR) )
return -2; /* i/o error */
bytes_read += k;
break;
case 'i':
WORDp = va_arg(argp, WORD *);
if ( fread((void *)WORDp, sizeof(WORD), 1, fp) != 1 )
return -2; /* i/o error */
#if SWAP16
WORDswap(WORDp);
#endif
p++;
bytes_read += sizeof(WORD);
break;
case 'l':
DWORDp = va_arg(argp, DWORD *);
if ( fread((void *)DWORDp, sizeof(DWORD), 1, fp) != 1 )
return -2; /* i/o error */
#if SWAP32
DWORDswap(DWORDp);
#endif
p++;
bytes_read += sizeof(DWORD);
break;
case 's':
charp = va_arg(argp, char *);
for ( k = 0, c = *++p; isdigit(c); c = *++p )
k = 10 * k + c - '0';
do
{
c = getc(fp);
if ( c == EOF )
return -2;
*charp++ = (char)c;
bytes_read++;
} while ( c && (k == 0 || --k) );
break;
case 'c':
charp = va_arg(argp, char *);
for ( k = 0, c = *++p; isdigit(c); c = *++p )
k = 10 * k + c - '0';
if ( k == 0 )
k = 1;
if ( fread((void *)charp, sizeof(char), k, fp) != k )
return -2; /* i/o error */
bytes_read += k;
break;
default:
return -1; /* bad format */
}
}
va_end(argp);
return bytes_read;
}