home *** CD-ROM | disk | FTP | other *** search
- /*
- * munge.c
- */
-
- #include <string.h>
-
- extern int * sprintf ( char * , const char * , ... ) ;
- unsigned char * C2P ( char * ) ;
-
-
- /* 0 == whitespace
- 1 == line feed chars
- 2 == digit
- 3 == colon
- 4 == letter
- 5 == bracket
- 6 == bracket
- */
-
- char ch_type [ ] = {
- /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F */
- /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
- /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* 2 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 0, 0, 0, 0, 0,
- /* 4 */ 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- /* 5 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 4,
- /* 6 */ 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- /* 7 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 0, 6, 0, 0,
- /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* A */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* B */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* C */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* D */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* E */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* F */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- } ;
-
-
- extern void Idle ( void ) ;
- extern short vRefNum ;
- void MungeFolder ( long parID ) ;
- extern void Message ( unsigned char * ) ;
- extern Boolean DoLine ( unsigned char * line ) ;
- extern void LookupParent ( char * child , char * parent ) ;
-
-
- CInfoPBRec rec ;
- short err ;
- Str63 name ;
- Str255 str ;
- short lineNo ;
-
-
- static void
- CopyP ( unsigned char * from , unsigned char * to ) {
- BlockMove ( from , to , 1 + * from ) ;
- }
-
-
- static void
- ConcP ( unsigned char * to , unsigned char * tail ) {
-
- short len = * tail ;
- if ( len + * to > 255 ) {
- len = 255 - * to ;
- }
- BlockMove ( tail + 1 , to + * to + 1 , len ) ;
- * to += len ;
- }
-
-
- unsigned char *
- C2P ( char * c ) {
- int len = strlen ( c ) ;
- if ( len ) {
- BlockMove ( c , c + 1 , len ) ;
- }
- * c = len ;
- return ( unsigned char * ) c ;
- }
-
-
- static void
- ErrMsg ( unsigned char * why , unsigned char * file , short code ) {
-
- Str15 nn ;
- long tick ;
- static long lastBeep = 0 ;
-
- NumToString ( code , nn ) ;
- CopyP ( "\p ERROR " , str ) ;
- ConcP ( str , why ) ;
- ConcP ( str , "\p " ) ;
- ConcP ( str , nn ) ;
- ConcP ( str , "\p\r" ) ;
- Message ( str ) ;
- tick = TickCount ( ) + 5 ;
- if ( lastBeep < tick - 600 ) { /* Only beep every 10 seconds if lots of errors */
- SysBeep ( 20 ) ;
- lastBeep = tick ;
- }
- }
-
-
- static long
- token ( Handle text , long pos , long end , char * tok , long * before ) {
-
- char type ;
-
- while ( pos < end ) {
- while ( ! ch_type [ ( * text ) [ pos ] ] && pos < end ) { // Skip junk
- pos ++ ;
- }
- * before = pos ;
- * tok = ( * text ) [ pos ] ; // First token char
- pos ++ ;
- type = ch_type [ * tok ] ;
- tok ++ ;
- if ( type == 1 ) { // line break
- * tok = 0 ;
- return pos ;
- }
- while ( pos < end ) {
- char xTyp = ch_type [ ( * text ) [ pos ] ] ;
- if ( ( xTyp != type ) &&
- ! ( type == 4 && xTyp == 2 ) ) { // Identifier number
- * tok = 0 ;
- return pos ;
- }
- * ( tok ++ ) = ( * text ) [ pos ++ ] ;
- }
- }
- * tok = 0 ;
- return end ;
- }
-
-
- static int
- Replace ( Handle text , long pos , long len , char * with ) {
- Munger ( text , pos , NULL , len , with , strlen ( with ) ) ;
- return strlen ( with ) - len ;
- }
-
-
- /* This function contains the total text of a TEXT file. */
- /* The contents of the handle will be re-written to disk when done if true returned. */
- /* Call Idle ( ) now and then for lengthy tasks */
- static Boolean
- MungeHandle ( Handle text ) {
-
- long end = GetHandleSize ( text ) ;
- long pos = 0 ;
- char tok [ 256 ] ;
- char className [ 256 ] ;
- char other [ 256 ] ;
- long before = 0 ;
- long level = 0 ;
- long oldPos ;
- long oldBefore ;
- int line = 1 ;
- Boolean chng = 0 ;
-
- className [ 0 ] = 0 ;
- tok [ 0 ] = 0 ;
- while ( pos < end ) {
- oldPos = pos ;
- oldBefore = before ;
- tok [ 0 ] = 0 ;
- pos = token ( text , pos , end , tok , & before ) ;
- if ( ! tok [ 0 ] ) {
- break ;
- }
- if ( ch_type [ tok [ 0 ] ] == 1 ) {
- line ++ ;
- if ( ! ( line % 10 ) ) {
- Idle ( ) ;
- }
- }
- if ( level ) {
- if ( ! strcmp ( tok , "inherited" ) ) {
- int delta = Replace ( text , before , 9 , className ) ;
- pos += delta ;
- end += delta ;
- chng = true ;
- } else if ( ! strcmp ( tok , "{" ) ) {
- level ++ ;
- } else if ( ! strcmp ( tok , "}" ) ) {
- level -- ;
- }
- } else { // no level - set context ?
- if ( ! strcmp ( tok , "::" ) ) {
- strncpy ( other , ( * text ) + oldBefore , oldPos - oldBefore ) ;
- other [ oldPos - oldBefore ] = 0 ;
- LookupParent ( other , className ) ;
- if ( ! className [ 0 ] ) {
- sprintf ( className , "\rLine %d: Unknown superclass" , line ) ;
- Message ( C2P ( className ) ) ;
- strcpy ( className , "/*UNKNOWN_SUPERCLASS*/" ) ;
- }
- } else if ( ! strcmp ( tok , "{" ) ) {
- level ++ ;
- } else if ( ! strcmp ( tok , "}" ) ) {
- sprintf ( other , "\r• Error Line %d: level < 0 " , line ) ;
- Message ( C2P ( other ) ) ;
- }
- }
- }
- return true ;
- }
-
-
- static void
- MungeFile ( short vRef , long parID , unsigned char * fn ) {
-
- Handle text ;
- short ref = 0 ;
- long size ;
- Boolean doWrite ;
-
- Message ( fn ) ;
-
- err = HOpenDF ( vRef , parID , fn , fsRdWrPerm , & ref ) ;
- if ( err ) {
- ErrMsg ( "\pOpening" , fn , err ) ;
- return ;
- }
- err = GetEOF ( ref , & size ) ;
- if ( ! err ) {
- err = SetFPos ( ref , fsFromStart , 0L ) ;
- }
- if ( err ) {
- ErrMsg ( "\pStarting" , fn , err ) ;
- FSClose ( ref ) ;
- return ;
- }
- text = NewHandle ( size ) ;
- if ( ! text ) {
- ErrMsg ( "\pAllocating" , fn , MemError ( ) ) ;
- FSClose ( ref ) ;
- return ;
- }
- HLock ( text ) ;
- err = FSRead ( ref , & size , * text ) ;
- if ( err ) {
- ErrMsg ( "\pReading" , fn , err ) ;
- FSClose ( ref ) ;
- DisposeHandle ( text ) ;
- }
- HUnlock ( text ) ;
- doWrite = MungeHandle ( text ) ;
- if ( doWrite ) {
- size = GetHandleSize ( text ) ;
- err = SetEOF ( ref , size ) ;
- if ( ! err ) {
- err = SetFPos ( ref , fsFromStart , 0L ) ;
- }
- if ( err ) {
- ErrMsg ( "\pFinishing" , fn , err ) ;
- FSClose ( ref ) ;
- DisposeHandle ( text ) ;
- return ;
- }
- HLock ( text ) ;
- err = FSWrite ( ref , & size , * text ) ;
- if ( err ) {
- ErrMsg ( "\pWriting" , fn , err ) ;
- FSClose ( ref ) ;
- DisposeHandle ( text ) ;
- return ;
- }
- }
- err = FSClose ( ref ) ;
- if ( doWrite && ! err ) {
- err = FlushVol ( NULL , vRef ) ;
- }
- DisposeHandle ( text ) ;
- if ( err ) {
- ErrMsg ( "\pClosing" , fn , err ) ;
- }
- Message ( "\p DONE\r" ) ;
- }
-
-
- void
- MungeFolder ( long parID ) {
-
- short ix ;
-
- for ( ix = 1 ; ix > 0 ; ix ++ ) {
- rec . hFileInfo . ioCompletion = 0L ;
- rec . hFileInfo . ioFVersNum = 0 ;
- rec . hFileInfo . ioNamePtr = name ;
- rec . hFileInfo . ioVRefNum = vRefNum ;
- rec . hFileInfo . ioDirID = parID ;
- rec . hFileInfo . ioFDirIndex = ix ;
- err = PBGetCatInfoAsync ( & rec ) ;
- do {
- Idle ( ) ;
- } while ( rec . hFileInfo . ioResult == 1 ) ;
- err = rec . hFileInfo . ioResult ;
- if ( err ) {
- break ;
- }
- if ( rec . hFileInfo . ioFlAttrib & 0x10 ) { /* Folder */
- CopyP ( "\p\r•Entering Folder " , str ) ;
- ConcP ( str , name ) ;
- ConcP ( str , "\p\r" ) ;
- Message ( str ) ;
- MungeFolder ( rec . hFileInfo . ioDirID ) ;
- Message ( "\p---- Leaving folder ----\r\r" ) ;
- } else if ( rec . hFileInfo . ioFlFndrInfo . fdType == 'TEXT' ) {
- MungeFile ( vRefNum , parID , name ) ;
- }
- }
- }
-