From 020d8b1580c016b87fd0c88d7ba164bcd10779f7 Mon Sep 17 00:00:00 2001 From: Joel Matthew Rees Date: Mon, 29 Apr 2019 13:53:13 +0900 Subject: [PATCH] extracts okay, formats okay, no insertion into images, still must use dd to build images --- bif-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bif-img.c b/bif-img.c index dfdbbc1..202f0a3 100644 --- a/bif-img.c +++ b/bif-img.c @@ -1 +1 @@ -/* Tool for working with BIF-6809 images. // Written by Joel Matthew Rees, Amagasaki, Japan, April 2019, // Parts adapted from the author's 32col.c, written 1999. // Copyright 1999, 2019, Joel Matthew Rees. // Permission granted in advance for all uses // with the condition that this copyright and permission notice are retained. // // BIF-6809 project page: https://osdn.net/projects/bif-6809/ */ #include #include /* for EXIT_SUCCESS */ #include #include #define ScreenWidth 32 #define ScreenHeight 32 #define BufferPlay 3 /* room for CR/LF and NUL */ #define BufferWidth ( ScreenWidth + BufferPlay ) #define TO_SCREEN 1 const char kTo_ScreenStr[] = "--to-screens"; #define TO_EOLN_TEXT 2 const char kTo_EOLN_textStr[] = "--to-eoln-text"; void toEOLNtext( FILE * input, FILE * output /*, int linecountflag */ ) { char buffer[ ScreenHeight ][ BufferWidth ]; while ( !feof( input ) ) { int lineCount; for ( lineCount = 0; lineCount < ScreenHeight && !feof( input ); ++lineCount ) { int length = fread( buffer[ lineCount ], sizeof (char), ScreenWidth, input ); while ( --length >= 0 && ( isspace( buffer[ lineCount ][ length ] ) || !isprint( buffer[ lineCount ][ length ] ) ) ) /* "empty" loop */; buffer[ lineCount ][ ++length ] = '\0'; } if ( lineCount > 1 || ( lineCount == 1 && buffer[ 0 ][ 0 ] != '\0' ) ) { int line = 0; while ( --lineCount > 0 && buffer[ lineCount ][ 0 ] == '\0' ) /* "empty" loop */; for ( line = 0; line <= lineCount; ++line ) /* check end conditions again. */ { fputs( buffer[ line ], output ); fputc( '\n', output ); } fputc( '\f', output ); } } } #define FILE_START 0x200 /* beyond char range. */ #define LINE_START 0x400 /* beyond char range. */ void toScreens( FILE * input, FILE * output /*, int linecountflag */ ) { char buffer[ ScreenHeight ][ BufferWidth ]; int eolFlag = FILE_START; while ( !feof( input ) ) { int lineCount; for ( lineCount = 0; lineCount < ScreenHeight; ++lineCount ) { int length = 0; char * line = buffer[ lineCount ]; int ch = LINE_START; while ( ( length < ScreenWidth ) && !feof( input ) ) { ch = fgetc( input ); if ( ( length == 0 ) && ( ( ( ch == '\r' ) && ( eolFlag == '\n' ) ) || ( ( ch == '\n' ) && ( eolFlag == '\r' ) ) ) ) { ch = fgetc( input ); } eolFlag = ch; if ( ( ch == '\n' ) || ( ch == '\r' ) || feof( input ) ) { break; /* The habit is to set a NUL, but not for SCREENs. */ } line[ length++ ] = ch; /* dbg * / putchar( ch ); */ } /* dbg * / printf( "||end:%d:", length ); */ while ( length < ScreenWidth ) { line[ length++ ] = ' '; /* dbg * / putchar( '*' );*/ } /* dbg * / printf( "||:%d:%d\n", length, lineCount ); */ } /* dbg * / printf( "<>\n", lineCount ); */ if ( lineCount > 0 ) { int line = 0; for ( line = 0; line < lineCount; ++line ) { fwrite( buffer[ line ], sizeof (char), ScreenWidth, output ); } } } } int main(int argc, char * argv[] ) { FILE * input = stdin; FILE * output = stdout; int direction = 0; int blocksize = 1024; int offset = 0; int count = 0; if ( argc > 3 ) { if ( strcmp( argv[ 1 ], kTo_ScreenStr ) == 0 ) { direction = TO_SCREEN; } else if ( strcmp( argv[ 1 ], kTo_EOLN_textStr ) == 0 ) { direction = TO_EOLN_TEXT; } if ( direction != 0 ) { if ( strcmp( argv[ 2 ], "--" ) != 0 ) { input = fopen( argv[ 2 ], "rb" ); } if ( input == NULL ) { printf( "Error opening file <%s> for input.\n", argv[ 2 ] ); direction = -1; } if ( strcmp( argv[ 3 ], "--" ) != 0 ) { output = fopen( argv[ 3 ], "wb" ); } if ( output == NULL ) { printf( "Error opening file <%s> for output.\n", argv[ 3 ] ); fclose( input ); direction = -1; } } } if ( direction < 0 ) { printf( "*** %s quitting. ***\n", argv[ 0 ] ); return EXIT_FAILURE; } else if ( direction == 0 ) { puts( "usage:" ); printf( "\t%s %s \n", argv[ 0 ], kTo_ScreenStr ); printf( "\t%s %s [ [ -size= ] -off= [ -count= ] ]\n", argv[ 0 ], kTo_EOLN_textStr ); printf( "** Default block size is 1024, compatible with Forth SCREENs.\n" ); printf( "** Default count is length of input file.\n" ); printf( "** 0xhexadecimal and 0octal permitted for size, etc.\n" ); printf( "** Replace with -- for stdfiles in pipes\n" ); /* printf( "\t%s --to-image \n", argv[ 0 ] ); */ return EXIT_SUCCESS; } switch ( direction ) { case TO_SCREEN: toScreens( input, output ); break; case TO_EOLN_TEXT: toEOLNtext( input, output ); break; } return EXIT_SUCCESS; } \ No newline at end of file +/* Tool for working with BIF-6809 images. // Written by Joel Matthew Rees, Amagasaki, Japan, April 2019, // Parts adapted from the author's 32col.c, written 1999. // Copyright 1999, 2019, Joel Matthew Rees. // Permission granted in advance for all uses // with the condition that this copyright and permission notice are retained. // // BIF-6809 project page: https://osdn.net/projects/bif-6809/ */ #include #include #include /* for EXIT_SUCCESS */ #include #include #define ScreenWidth 32 #define ScreenHeight 32 #define BufferPlay 3 /* room for CR/LF and NUL */ #define BufferWidth ( ScreenWidth + BufferPlay ) #define TO_SCREEN 1 const char kTo_ScreenStr[] = "--to-screens"; #define TO_EOLN_TEXT 2 const char kTo_EOLN_textStr[] = "--to-eoln-text"; const char kBlockSize[] = "-size"; const char kBlockOffset[] = "-off"; const char kBlockCount[] = "-count"; void toEOLNtext( FILE * input, FILE * output, unsigned blocksize, unsigned offset, unsigned count /*, int linecountflag */ ) { char buffer[ ScreenHeight ][ BufferWidth ]; unsigned long start = blocksize * offset; unsigned long bytecount = blocksize * count; unsigned long totalBytes = 0; /* dbg */ printf( "size: %u; off: %u; count: %u\n", blocksize, offset, count ); if ( start > 0 ) { fseek( input, start, SEEK_SET ); } while ( !feof( input ) && ( totalBytes < bytecount ) ) { int lineCount; for ( lineCount = 0; lineCount < ScreenHeight && !feof( input ); ++lineCount ) { int length = fread( buffer[ lineCount ], sizeof (char), ScreenWidth, input ); totalBytes += length; while ( --length >= 0 && ( isspace( buffer[ lineCount ][ length ] ) || !isprint( buffer[ lineCount ][ length ] ) ) ) /* "empty" loop */; buffer[ lineCount ][ ++length ] = '\0'; } if ( lineCount > 1 || ( lineCount == 1 && buffer[ 0 ][ 0 ] != '\0' ) ) { int line = 0; while ( --lineCount > 0 && buffer[ lineCount ][ 0 ] == '\0' ) /* This actually is probably a bug. */ /* "empty" loop */; for ( line = 0; line <= lineCount; ++line ) /* check end conditions again. */ { fputs( buffer[ line ], output ); fputc( '\n', output ); } /* fputc( '\f', output ); This is not useful. */ } } } #define FILE_START 0x200 /* beyond char range. */ #define LINE_START 0x400 /* beyond char range. */ void toScreens( FILE * input, FILE * output /*, int linecountflag */ ) { char buffer[ ScreenHeight ][ BufferWidth ]; int eolFlag = FILE_START; while ( !feof( input ) ) { int lineCount; for ( lineCount = 0; lineCount < ScreenHeight; ++lineCount ) { int length = 0; char * line = buffer[ lineCount ]; int ch = LINE_START; while ( ( length < ScreenWidth ) && !feof( input ) ) { ch = fgetc( input ); if ( ( length == 0 ) && ( ( ( ch == '\r' ) && ( eolFlag == '\n' ) ) || ( ( ch == '\n' ) && ( eolFlag == '\r' ) ) ) ) { ch = fgetc( input ); } eolFlag = ch; if ( ( ch == '\n' ) || ( ch == '\r' ) || feof( input ) ) { break; /* The habit is to set a NUL, but not for SCREENs. */ } line[ length++ ] = ch; /* dbg * / putchar( ch ); */ } /* dbg * / printf( "||end:%d:", length ); */ while ( length < ScreenWidth ) { line[ length++ ] = ' '; /* dbg * / putchar( '*' );*/ } /* dbg * / printf( "||:%d:%d\n", length, lineCount ); */ } /* dbg * / printf( "<>\n", lineCount ); */ if ( lineCount > 0 ) { int line = 0; for ( line = 0; line < lineCount; ++line ) { fwrite( buffer[ line ], sizeof (char), ScreenWidth, output ); } } } } int getNumericParameter( const char parameter[], char * argstr, unsigned long * rval, long low, unsigned long limit ) { char * scanpt = argstr; unsigned long result = 0; size_t eqpt = strlen( parameter ); if ( strncmp( parameter, argstr, eqpt ) == 0 ) { if ( argstr[ eqpt ] != '=' ) { printf( "\t%s needs '=' in '%s'\n,", parameter, argstr ); return INT_MIN | 16; } ++eqpt; scanpt += eqpt; result = strtoul( scanpt, &scanpt, 0 ); if ( scanpt <= argstr + eqpt ) { printf( "\tBad %s value specified in '%s'\n,", parameter, argstr ); return INT_MIN | 32; } if ( ( result < low ) || ( result >= limit ) ) { printf( "\t%s value %lu out of range in '%s', try %lu\n,", parameter, result, argstr, * rval ); return INT_MIN | 64; } * rval = result; return 1; } return 0; } int main(int argc, char * argv[] ) { FILE * input = stdin; FILE * output = stdout; int direction = 0; int errval = 0; unsigned long blocksize = 1024; unsigned long offset = 0; unsigned long count = UINT_MAX; int i; for ( i = 4; i < argc; ++i ) { int berr = 0; int oerr = 0; int cerr = 0; if ( ( ( berr |= getNumericParameter( kBlockSize, argv[ i ], &blocksize, 1, 0x8000UL ) ) <= 0 ) && ( ( oerr |= getNumericParameter( kBlockOffset, argv[ i ], &offset, 1, USHRT_MAX ) ) <= 0 ) && ( ( cerr |= getNumericParameter( kBlockCount, argv[ i ], &count, 1, USHRT_MAX ) ) <= 0 ) ) { printf( "Unrecognized %s\n", argv[ i ] ); errval |= berr | oerr | cerr; } } if ( ( errval >= 0 ) && ( argc > 3 ) ) { if ( strcmp( argv[ 1 ], kTo_ScreenStr ) == 0 ) { direction = TO_SCREEN; } else if ( strcmp( argv[ 1 ], kTo_EOLN_textStr ) == 0 ) { direction = TO_EOLN_TEXT; } if ( direction != 0 ) { if ( strcmp( argv[ 2 ], "--" ) != 0 ) { input = fopen( argv[ 2 ], "rb" ); } if ( input == NULL ) { printf( "Error opening file <%s> for input.\n", argv[ 2 ] ); direction |= INT_MIN | 4; } if ( strcmp( argv[ 3 ], "--" ) != 0 ) { output = fopen( argv[ 3 ], "wb" ); } if ( output == NULL ) { printf( "Error opening file <%s> for output.\n", argv[ 3 ] ); fclose( input ); direction |= INT_MIN | 8; } } } if ( direction < -1 ) { printf( "*** %s quitting. ***\n", argv[ 0 ] ); return EXIT_FAILURE; } else if ( direction == 0 ) { puts( "usage:" ); printf( "\t%s %s \n", argv[ 0 ], kTo_ScreenStr ); printf( "\t%s %s [ %s= ] [ %s= ] [ %s= ]\n", argv[ 0 ], kTo_EOLN_textStr, kBlockSize, kBlockOffset, kBlockCount ); printf( "** Default block size is 1024, compatible with Forth SCREENs.\n" ); printf( "** Default count is length of input file.\n" ); printf( "** 0xhexadecimal and 0octal permitted for size, etc.\n" ); printf( "** Replace with -- for stdfiles in pipes\n" ); /* printf( "\t%s --to-image \n", argv[ 0 ] ); */ return EXIT_SUCCESS; } switch ( direction ) { case TO_SCREEN: toScreens( input, output ); break; case TO_EOLN_TEXT: toEOLNtext( input, output, blocksize, offset, count ); break; } return EXIT_SUCCESS; } \ No newline at end of file -- 2.11.0