help this non-programmer

Discussion in 'Mac Programming' started by iTwitch, Dec 23, 2006.

  1. macrumors 6502a

    iTwitch

    Joined:
    Mar 30, 2006
    Location:
    East of the Mississippi
    #1
    I have some source code I'm trying to compile on my iBook but I think I'm having a problem because the code contains a 'fcloseall()' and I've read that gcc under OSX doesn't support it. I figure substituting fclose() but I'm not sure on the arguements to use. Would someone take a look and tell me what to use to replace fcloseall() with? fclose(??)
    Code:
    #include <stdarg.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    #include "bmp2palm.h"
    
    #undef WINDOWS
    
    #ifdef __GNUCC__
    #define STRICMP strcasecmp
    #else
    #define STRICMP strcasecmp
    #endif
    
    #define resourceAttribute  0x0001
    
    FILE *in = NULL;
    FILE *out = NULL;
    int   interactive = true;
    UInt8* inBitmap;
    UInt8* outBitmap;
    
    void SafeFree( void** p )
    {
        if ( *p != NULL ) {
            free( *p );
            *p = NULL;
        }
    }
    
    void CleanUp( void )
    {
        SafeFree( ( void** )&inBitmap );
        SafeFree( ( void** )&outBitmap );
        fcloseall();
    }
    
    void msg(char* title, char *s, ...)
    {
        char out[ 2048 ];
    
        va_list argptr;
    
        if ( ! interactive )
            return;
    
        va_start(argptr,s);
        vsprintf(out,s,argptr);
        va_end(argptr);
    #ifdef WINDOWS
        MessageBox( NULL, out, title, MB_OK );
    #else
        fprintf( stderr, "%s\n", out );
    #endif
    
        CleanUp();
    
        exit(1);
    }
    
    void err(char *s, ...)
    {
        char out[ 2048 ];
    
        va_list argptr;
        va_start(argptr,s);
        vsprintf(out,s,argptr);
        va_end(argptr);
    
    #ifdef WINDOWS
        MessageBox( NULL, out, "Error", MB_OK );
    #else
        fprintf( stderr, "Fatal error: %s.\n", out );
    #endif
    
        CleanUp();
    
        exit(1);
    }
    
    FILE *myfopen(char *s,char *m)
    {
        FILE *f=fopen(s,m);
        if(f==NULL) err("Cannot open %s.",s);
        return f;
    }
    
    int myfgetc(FILE *f)
    {
        int c=fgetc(f);
        if(c==-1) err("Error reading font!");
        return c;
    }
    
    void* mymalloc( int n )
    {
        void* p = malloc( n );
    
        if ( p == NULL ) err("Error allocating %ld bytes.", n );
    
        return p;
    }
    
    int myfread( void* p, int n, int m, FILE* f )
    {
        if ( m != fread( p, n, m, f ) )
            err("Error reading %d x %d bytes.", n, m );
        return m;
    }
    
    void myseek(FILE *f, unsigned long x)
    {
        if(fseek(f,x,SEEK_SET)<0 ||
            ftell(f)!=(long)x) err("Error seeking!");
        return;
    }
    
    UInt32 UInt32LE( UInt32 in )
    {
        UInt8*  p;
        UInt32  n;
        int    i;
    
        p = ( UInt8* )&n;
    
        for ( i = 0 ; i < 4 ; i++ ) {
            p[ i ] = in % 256;
            in /= 256;
        }
    
        return n;
    }
    
    UInt32 UInt32BE( UInt32 in )
    {
        UInt8*  p;
        UInt32  n;
        int    i;
    
        p = ( UInt8* )&n;
    
        for ( i = 0 ; i < 4 ; i++ ) {
            p[ 3 - i ] = in % 256;
            in /= 256;
        }
    
        return n;
    }
    
    UInt16 UInt16LE( UInt16 in )
    {
        UInt8* p;
        int    i;
    
        p = ( UInt8* )&in;
    
        return p[ 0 ] + 256 * p[ 1 ];
    }
    
    UInt16 UInt16BE( UInt16 in )
    {
        UInt8* p;
        int    i;
    
        p = ( UInt8* )&in;
    
        return 256 * p[ 0 ] + p[ 1 ];
    }
    
    UInt32 UInt32LEP( UInt16* inP )
    {
        return UInt16LE( inP[ 0 ] ) + 65536 * UInt16LE( inP[ 1 ] );
    }
    
    
    #ifdef WINDOWS
    int getname(char *s)
    {
    int r;
    OPENFILENAME ofn;
    *s=0;
    ZeroMemory(&ofn, sizeof(OPENFILENAME));
    
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = NULL;
    ofn.lpstrFile = s;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrFilter = "Palm databases\0*.prc;*.pdb\0All files\0*.*\0";
    ofn.lpstrTitle = "Select database to compress";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    return GetOpenFileName(&ofn);
    }
    #endif
    
    
    static void WrongFormat( void )
    {
        err("Unsupported input format");
    }
    
    
    static UInt16 Convert888To565( UInt8* c )
    {
        return ( ( c[ 2 ] >> 3 ) << 11 ) | ( ( c[ 1 ] >> 2 ) << 5 ) | ( c[ 0 ] >> 3 );
    }
    
    
    static void ConvertRow( UInt8* dest, UInt8* src, UInt16 width )
    {
        int x;
        UInt16* dest16;
    
        dest16 = ( void* )dest;
    
        for ( x = 0 ; x < width ; x++ ) {
             dest16[ x ] = UInt16LE( Convert888To565( src + x * 3 ) );
        }
    }
    
    
    
    #define SEQUENCE_LIMIT 126
    
    Int16 GetRunLength( UInt16* data, UInt16 cols )
    {
        UInt16 i;
    
        for ( i = 1 ; i < cols && data[ i ] == data[ 0 ] && i < SEQUENCE_LIMIT ; i++ ) ;
    
        return i;
    }
    
    
    
    UInt8* PackRow( UInt8* out, UInt16* in, UInt16 cols )
    {
        UInt16  x;
        UInt16* inEnd;
    
        while ( cols ) {
            Int16 count;
    
            count = GetRunLength( in, cols );
    
            if ( 2 <= count ) {
                *out++ = ( UInt8 )( 1 - count );
                *out++ = *( UInt8* )in;
                *out++ = *( 1 + ( UInt8* )in );
    
                in   += count;
                cols -= count;
            }
            else {
                UInt16 i;
    
                for ( i = 1 ; i < cols && i < SEQUENCE_LIMIT && ( i + 1 == cols || in[ i ] != in[ i + 1 ] ) ; i++ );
                *out++ = ( UInt8 )( i - 1 );
                while ( i-- ) {
                    *out++ = *( UInt8* )in;
                    *out++ = *( 1 + ( UInt8* )in );
                    in++;
                    cols--;
                }
            }
        }
        return out;
    }
    
    
    
    void* PackData( UInt16* inData, UInt16 cols, UInt16 rows, UInt32* newSizeP )
    {
        UInt8* outData;
        UInt8* p;
        UInt16  row;
        /* Implement packbits compression */
    //return NULL;
    
        outData = mymalloc( rows * cols * ( sizeof( UInt16 ) + 1 ) + 4 );
    
        if ( outData == NULL )
            return NULL;
    
        p = outData + 4;
    
        for ( row = 0 ; row < rows ; row++ ) {
            p = PackRow( p, inData + row * cols, cols );
        }
    
        if ( rows * cols * sizeof( UInt16 ) <= p - outData ) {
            free( outData );
            return NULL;
        }
    
        *newSizeP = ( p - outData );
    
        *( UInt32* )outData = UInt32BE( *newSizeP - 4 );
    
        return outData;
    }
    
    
    UInt8* ScanLineCompressRow( Boolean firstLine, UInt8* out, UInt8* in, UInt16 rowBytes )
    {
        UInt8* startOut;
        UInt8* endIn;
        static UInt8* prevLine;
        UInt8* startIn;
    
        startIn  = in;
        startOut = out;
        endIn    = in + rowBytes;
    
        while ( in < endIn ) {
            UInt8* octupleMarker;
            UInt8 mask;
            octupleMarker = out;
            *octupleMarker = 0;
            out++;
            for ( mask = 0x80 ; mask != 0 && in < endIn ; mask >>= 1 ) {
                if ( firstLine || *in != *prevLine ) {
                    *octupleMarker |= mask;
                    *out++ = *in;
                }
                in++;
                prevLine++;
            }
        }
        prevLine = startIn;
        return out;
    }
    
    
    void* ScanLineData( UInt8* inData, UInt16 byteWidth, UInt16 rows, UInt32* newSizeP )
    {
        UInt8* outData;
        UInt8* p;
        UInt16  row;
        /* Implement scanline compression */
    
        outData = malloc( rows * byteWidth * 2 + 4 );
    
        if ( outData == NULL )
            return NULL;
    
        p = outData + 4;
    
        for ( row = 0 ; row < rows ; row++ ) {
            p = ScanLineCompressRow( row == 0, p, inData + row * byteWidth, byteWidth );
        }
    
        if ( rows * byteWidth <= p - outData + 4 ) {
            free( outData );
            return NULL;
        }
    
        *newSizeP = ( p - outData );
    
        *( UInt32* )outData = UInt32BE( *newSizeP - 4 );
    
        return outData;
    }
    
    
    void* CompressData( void* bitmap, UInt16 cols, UInt16 rows, UInt32* newSizeP, UInt8* type )
    {
        void* scanLine;
        UInt32 scanLineSize;
        void* pack;
        UInt32 packSize;
    
        pack     = PackData( bitmap, cols, rows, &packSize );
        scanLine = ScanLineData( bitmap, cols * sizeof( UInt16 ), rows, &scanLineSize );
    
        if ( pack == NULL ) {
            if ( scanLine != NULL ) {
                *type = palmBitmapCompressionTypeScanLine;
                *newSizeP = scanLineSize;
            }
    
            return scanLine;
        }
    
        if ( scanLine == NULL ) {
            if ( pack != NULL ) {
                *type = palmBitmapCompressionTypePackBits;
                *newSizeP = packSize;
            }
    
            return pack;
        }
    
        if ( packSize < scanLineSize ) {
            free( scanLine );
            *type = palmBitmapCompressionTypePackBits;
            *newSizeP = packSize;
            return pack;
        }
        else {
            free( pack );
            *type = palmBitmapCompressionTypeScanLine;
            *newSizeP = scanLineSize;
            return scanLine;
        }
    }
    
    
    int main(int argc, char **argv)
    {
        int i;
        int x;
        int y;
    
        UInt32 pos,pos2,pos3;
        UInt32 inLength;
        UInt32 outLength;
        UInt16 width;
        UInt16 height;
        UInt16 outHeight;
        UInt32 inRowLen;
        UInt16 outRowLen;
        void* compressedBitmap;
        UInt32 compressedSize;
    
        int yStart;
        int yEnd;
    
        BMPHeaderType    inH;
        PalmBitmapTypeV3 outH;
    
        interactive = 1;
        inBitmap    = NULL;
        outBitmap   = NULL;
    
        if ( argc != 3 && argc != 5 ) {
            msg("usage", "bmp2abmp inFileName outFileName [y0 height].\n");
            return 1;
        }
    
        in = myfopen( argv[ 1 ], "rb" );
    
        myfread( &inH, sizeof( inH ), 1, in );
    
        if ( inH.magic != ( UInt8 )'B' + 256 * ( UInt8 )'M' ||
             UInt16LE( inH.bitsPerPixel ) != 24 ||
             UInt32LEP( inH.compressionType ) != 0 ||
             UInt16LE( inH.planes ) != 1 ) {
            WrongFormat();
        }
    
        width  = UInt32LEP( inH.width );
        height = UInt32LEP( inH.height );
    
        if ( argc == 3 ) {
            yStart = 0;
            yEnd   = height - 1;
        }
        else {
            yStart = atoi( argv[ 3 ] );
            yEnd   = yStart + atoi( argv[ 4 ] ) - 1;
        }
    
        outHeight  = yEnd - yStart + 1;
    
        outH.width  = UInt16BE( width );
        outH.height = UInt16BE( outHeight );
    
        outRowLen = width * 2;
        outH.rowBytes = UInt16BE( outRowLen );
        outH.flags    = 0; // directColorLE;
        outH.reserved0 = 0;
        outH.pixelSize = 16;
        outH.version   = 3;
        outH.size      = sizeof( PalmBitmapTypeV3 );
        outH.pixelFormat = palmPixelFormat565LE;
        outH.unused      = 0;
        outH.compressionType = 0;
        outH.density         = UInt16BE( 144 );
        outH.transparentValue = 0;
        outH.nextBitmapOffset = 0;
    
        /* Ensure inRowLen is 0 mod 4. */
        inRowLen = width * 3 + width % 4;
    
        inBitmap  = mymalloc( inRowLen * height );
        outBitmap = mymalloc( outRowLen * outHeight );
    
        myseek( in, UInt32LEP( inH.offset ) );
    
        myfread( inBitmap, 1, inRowLen * height, in );
    
        out = myfopen( argv[ 2 ], "wb" );
    
        for ( y = yStart ; y <= yEnd ; y++ ) {
            int x;
    
            ConvertRow( outBitmap + outRowLen * ( y - yStart ),
                     inBitmap + inRowLen * ( height - 1 - y ), width );
        }
    
        compressedBitmap = CompressData( ( void* )outBitmap, width, outHeight, &compressedSize, 
                               &( outH.compressionType ) );
    
        if ( compressedBitmap != NULL ) {
            outH.flags |= compressedBE;
        }
    
    /*
        { int i; for ( i = 0 ; i < 8 ; i++ ) fputc( 0, out );
        fputc( 0xFF, out );
        fputc( 0x81, out );
        for ( i = 0 ; i < 6 ; i++ ) fputc( 0, out ); }
    */
        fwrite( &outH, 1, sizeof( outH ), out );
    
        if ( compressedBitmap != NULL ) {
            fwrite( compressedBitmap, 1, compressedSize, out );
            free( compressedBitmap );
        }
        else {
            fwrite( outBitmap, 1, outRowLen * outHeight, out );
        }
    
        CleanUp();
    
        return 0;
    }
    
    Thanks.
     
  2. macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    According to this man page entry, you could probably just replace each occurrence of fcloseall() with the following: (or stick it in your own function, like closeall() or something...)

    Code:
    fflush(in);
    fflush(out);
    fclose(in);
    fclose(out);
     
  3. thread starter macrumors 6502a

    iTwitch

    Joined:
    Mar 30, 2006
    Location:
    East of the Mississippi
    #3
    Thanks, I hadn't used fflush when I tried before. Even with fflush it still gives a bunch of warnings and compiles. As before the executable doesn't work. It was a fairly easy task of compiling on my linux box using gcc but no joy under OSX.
     
  4. macrumors 6502

    Joined:
    Jul 25, 2006
    #4
    Could you be a little more specific on what warnings/errors you're getting and for which lines?
     
  5. thread starter macrumors 6502a

    iTwitch

    Joined:
    Mar 30, 2006
    Location:
    East of the Mississippi
    #5
    Here's the warnings, which seem to be coming from a second batch of code? (I'll post in next message)
    -----------------------------------------------
    Woodys-Nest:~/Public/PL006beta Folder joe$ make -f convert-make
    gcc -O toprc.c -o toprc
    toprc.c:74:1: warning: "MAX_RESOURCE_SIZE" redefined
    In file included from toprc.c:33:
    palmtypes.h:174:1: warning: this is the location of the previous definition
    toprc.c:75:1: warning: "MAX_SAFE_RESOURCE_SIZE" redefined
    palmtypes.h:175:1: warning: this is the location of the previous definition
    toprc.c: In function 'PRCInit':
    toprc.c:227: warning: pointer targets in passing argument 1 of 'strncpy' differ in signedness
    toprc.c:234: warning: pointer targets in passing argument 1 of 'strncpy' differ in signedness
    toprc.c:235: warning: pointer targets in passing argument 1 of 'strncpy' differ in signedness
    toprc.c: In function 'main':
    toprc.c:427: warning: pointer targets in passing argument 1 of 'PRCAdd' differ in signedness
    toprc.c:432: warning: pointer targets in passing argument 1 of 'PRCAdd' differ in signedness
    toprc.c:446: warning: pointer targets in passing argument 1 of 'PRCAdd' differ in signedness
    gcc -O bmp2palm.c -o bmp2palm
    --------------------------------------
    Thanks for looking.
     
  6. thread starter macrumors 6502a

    iTwitch

    Joined:
    Mar 30, 2006
    Location:
    East of the Mississippi
    #6
    Here's 'toprc.c', my first post contained 'bmp2palm.c' they are supposed to work together once compiled into apps.
    ---------------------------------------------------------------
    /*
    * $Id: toprc.c,v 1.15 2006/08/16 17:58:48 arpruss Exp $
    *
    * The true home of this program is palmfontconv.sourceforge.net. If you
    * find it elsewhere and improve it, please send a patch to
    * Alexander R. Pruss <ap85@georgetown.edu>.
    *
    * fontconv - Copyright (c) 2003, Alexander R. Pruss
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    *
    */


    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
    #include <string.h>
    #include <ctype.h>
    #include <time.h>
    #include "palmtypes.h"

    #ifdef __GNUCC__
    #define STRICMP strcasecmp
    #else
    #define STRICMP strcasecmp
    #endif

    struct {
    char* name;
    unsigned short fontID;
    } fontList[] = {
    { "hrTinyFont", 0 },
    { "hrTinyBoldFont", 1 },
    { "hrSmallFont", 2 },
    { "hrSmallSymbolFont", 3 },
    { "hrSmallSymbol11Font", 4 },
    { "hrSmallSymbol7Font", 5 },
    { "hrSmallLedFont", 6 },
    { "hrSmallBoldFont", 7 },
    { "hrStdFont", 8 },
    { "hrBoldFont", 9 },
    { "hrLargeFont", 10 },
    { "hrSymbolFont", 11 },
    { "hrSymbol11Font", 12 },
    { "hrSymbol7Font", 13 },
    { "hrLedFont", 14 },
    { "hrLargeBoldFont", 15 },
    { "stdFont", 0 },
    { "boldFont", 1 },
    { "largeFont", 2 },
    { "symbolFont", 3 },
    { "symbol11Font", 4 },
    { "symbol7Font", 5 },
    { "ledFont", 6 },
    { "largeBoldFont", 7 },
    { "stdFixedFont_sony", 137 },
    { "stdFixedFont_palm", 144 },
    { "narrowFixedFont", 129 }
    };

    #define MAX_RESOURCE_SIZE (2 * 65000l)
    #define MAX_SAFE_RESOURCE_SIZE (2 * 65000l)

    #define NUM_FONTS ( sizeof( fontList ) / sizeof( *fontList ) )

    #define TIME_UNIX_TO_PALM 2082844800
    #define resourceAttribute 0x0001
    #define backupAttribute 0x0008
    DatabaseHdrType header;
    RsrcEntryType resourceEntries[ MAX_RESOURCES ];
    RecordEntryType dbEntries[ MAX_RESOURCES ];
    void* resourceData[ MAX_RESOURCES ];
    long resourceSizes[ MAX_RESOURCES ];
    int numResources = 0;
    int pdbFormat = 0;
    int littleEndian = 0;
    int version = 1;
    int splitUp = 0;
    int ignoreMissing = 0;
    int handleExtended = 0;

    void err(char *s, ...)
    {
    va_list argptr;
    va_start(argptr,s);
    vfprintf(stderr,s,argptr);
    va_end(argptr);
    fputc('\n',stderr);
    exit(1);
    }


    void *mymalloc(unsigned long length)
    {
    void *p;
    if ( length == 0 ) err("malloc(0) requested.");
    p=malloc(length);
    if(NULL==p) err("Out of memory: %u bytes requested.",length);
    return p;
    }

    FILE *myfopen(char *s,char *t)
    {
    FILE *f=fopen(s,t);
    if(NULL==f)
    err("Error opening %s.\n",s);
    return f;
    }



    Boolean IsLittleEndian( void )
    {
    Word w = 1;
    return *(Byte*)&w;
    }


    void ReverseDWord( Byte* data )
    {
    Byte temp;

    temp = data[ 3 ];
    data[ 3 ] = data[ 0 ];
    data[ 0 ] = temp;

    temp = data[ 2 ];
    data[ 2 ] = data[ 1 ];
    data[ 1 ] = temp;
    }


    void ReverseWord( Byte* data )
    {
    Byte temp;
    temp = data[ 1 ];
    data[ 1 ] = data[ 0 ];
    data[ 0 ] = temp;
    }



    /* Makes sure p, which points to a structure in the
    native format on which this code is executing is
    put into big endian format. sizes specifies the
    variable sizes in p. The format of "sizes" is an
    ASCII string of size-identifiers: B for byte (no fix-up),
    W for word (two bytes) and D for dword (four bytes),
    each optionally followed by a count. Thus, W3BBD means:
    three words, two bytes, and a dword. */
    int ToBigEndian( void* p, char* sizes )
    {
    Byte *data;
    int count;
    data = p;
    while ( *sizes ) {
    char type;
    type = sizes[ 0 ];
    sizes++;
    if ( *sizes && isdigit( *sizes ) ) {
    count = ( *sizes - '0' );
    sizes++;
    while ( *sizes && isdigit( *sizes ) ) {
    count = count * 10 + ( *sizes - '0' );
    sizes++;
    }
    }
    else
    count = 1;
    while ( count-- ) {
    switch( type ) {
    case 'B':
    data++;
    break;
    case 'W':
    if ( littleEndian )
    ReverseWord( data );
    data += 2;
    break;
    case 'D':
    if ( littleEndian )
    ReverseDWord( data );
    data += 4;
    break;
    default:
    err( "Internal error: Unknown size type %c.\n", type );
    }
    }
    }
    return data - ( Byte* ) p;
    }






    void PRCReset( void )
    {
    int i;
    for ( i = 0 ; i < numResources ; i++ ) {
    free( resourceData[ i ] );
    }
    numResources = 0;
    }


    void PRCInit( char* dbName, char* type, char* creator )
    {
    DWord t;
    PRCReset();
    t = TIME_UNIX_TO_PALM + time( NULL );
    memset( &header, 0, sizeof( header ) );
    strncpy( header.name, dbName, dmDBNameLength - 1 );
    header.name[ dmDBNameLength - 1 ] = 0;
    header.attributes = ( pdbFormat ? 0 : resourceAttribute ) | backupAttribute;
    header.creationDate = t;
    header.modificationDate = t;
    header.lastBackupDate = 0;
    header.version = version;
    strncpy( header.type, type, 4 );
    strncpy( header.creator, creator, 4 );
    }



    void PRCDump( char* fileName )
    {
    FILE* f = myfopen( fileName, "wb" );
    int i;
    DWord sizeSoFar;
    DWord resourceListSize;
    DWord padding;
    int header_size;
    header.recordList.numRecords = numResources;
    header_size = ToBigEndian( &header, DatabaseHdrType_sizes );
    fwrite( &header, 1, header_size, f );
    if ( pdbFormat )
    resourceListSize = RecordEntryType_size * numResources;
    else
    resourceListSize = RsrcEntryType_size * numResources;
    if ( ( resourceListSize + header_size ) % 4 != 0 ) {
    padding = 4 - ( ( resourceListSize + header_size ) % 4 );
    }
    else
    padding = 0;
    sizeSoFar = header_size + resourceListSize + padding;
    for ( i = 0; i < numResources ; i++ ) {
    if ( pdbFormat ) {
    dbEntries[ i ].localChunkID = sizeSoFar;
    dbEntries[ i ].uniqueID[ 2 ] = ( unsigned )i % 256;
    dbEntries[ i ].uniqueID[ 1 ] = ( ( unsigned )i / 256 ) % 256;
    dbEntries[ i ].uniqueID[ 0 ] = ( unsigned )i / 65536;
    }
    else {
    resourceEntries[ i ].localChunkIDHigh = sizeSoFar >> 16;
    resourceEntries[ i ].localChunkIDLow = sizeSoFar & 0xFFFF;
    }
    sizeSoFar += resourceSizes[ i ];
    // printf("%d - %ld - %ld\n", i, resourceSizes[ i ], sizeSoFar );
    }
    for ( i = 0 ; i < numResources ; i++ ) {
    if ( pdbFormat ) {
    ToBigEndian( &dbEntries[ i ], RecordEntryType_sizes );
    fwrite( &dbEntries[ i ], RecordEntryType_size, 1, f );
    }
    else {
    ToBigEndian( &resourceEntries[ i ], RsrcEntryType_sizes );
    fwrite( &resourceEntries[ i ], RsrcEntryType_size, 1, f );
    }
    }
    while ( padding-- )
    fputc( 0, f );
    for ( i = 0 ; i < numResources ; i++ ) {
    fwrite( resourceData[ i ], 1, resourceSizes[ i ], f );
    }
    fclose( f );
    PRCReset();
    }



    void PRCAdd( Byte* type, Word id, void* record, long size )
    {
    resourceData[ numResources ] = mymalloc( size );
    memcpy( resourceData[ numResources ], record, size );
    if ( pdbFormat ) {
    dbEntries[ numResources ].attributes = 0;
    }
    else {
    resourceEntries[ numResources ].id = id;
    memcpy( resourceEntries[ numResources ].type, type, 4 );
    }
    resourceSizes[ numResources ] = size;
    numResources++;
    }


    char buffer[ MAX_RESOURCE_SIZE ];


    unsigned short GetFontID( char* s )
    {
    int j;
    unsigned short fontID;
    for ( j = 0 ; j < NUM_FONTS ; j++ ) {
    if ( ! STRICMP( s, fontList[ j ].name ) ) {
    fontID = fontList[ j ].fontID;
    break;
    }
    }
    if ( j == NUM_FONTS ) {
    for ( j = 0 ; s[ j ] ; j++ )
    if ( ! isxdigit( s[ j ] ) )
    err( "Error in font specification `%s'.\n", s );
    sscanf( s, "%hx", &fontID );
    }
    return fontID;
    }


    int main( int argc, char** argv )
    {
    int i;
    int firstArg;
    char *fnameStart;
    int size;
    FILE* f;
    if ( argc < 6 )
    err( "Usage:\n"
    " %s outname.prc dataBaseName TypeID CreatorID [ rsrcXXXX.bin | -f rsrcXXXX toFont ] ...\n"
    "or:\n"
    " %s -d outname.pdb dataBaseName TypeID CreatorID record0.bin ...\n\n"
    "Fonts may be specified either by name identifier or in hex. This is only for\n"
    "generating font remap packages for software using Plucker font packages.\n",
    argv[ 0 ], argv[ 0 ] );

    littleEndian = IsLittleEndian();

    firstArg = 1;
    while ( argv[ firstArg ][ 0 ] == '-' ) {
    if ( ! strcmp( argv[ firstArg ], "-d" ) ) {
    pdbFormat = 1;
    firstArg++;
    }
    else if ( ! strcmp( argv[ firstArg ], "-s" ) ) {
    firstArg++;
    splitUp = 1;
    }
    else if ( ! strcmp( argv[ firstArg ], "-i" ) ) {
    firstArg++;
    ignoreMissing = 1;
    }
    else if ( ! strcmp( argv[ firstArg ], "-x" ) ) {
    firstArg++;
    handleExtended = 1;
    }
    }

    if ( pdbFormat )
    splitUp = 0;

    PRCInit( argv[ firstArg + 1 ], argv[ firstArg + 2 ], argv[ firstArg + 3 ] );
    for ( i = firstArg + 4 ; i < argc ; i++ ) {
    unsigned id;
    char hex[ 5 ];
    if ( ! pdbFormat && ! strncmp( argv[ i ], "-f", 2 ) ) {
    unsigned short fontID;

    if ( argc <= i + 2 ) {
    err( "Need to specify both fromFont and toFont.\n" );
    }

    fnameStart = argv[ i + 1 ];
    fontID = GetFontID( argv[ i + 2 ] );
    i += 2;
    buffer[ 0 ] = 0x40;
    buffer[ 1 ] = 0;
    buffer[ 2 ] = fontID >> 8;
    buffer[ 3 ] = fontID & 0xFF;
    size = 4;
    }
    else {
    char *p;
    if ( ! pdbFormat && strlen( argv[ i ] ) < 8 )
    err( "Filename %s not in required rsrcXXXX format.\n", argv[ i ] );
    fnameStart = argv[ i ];
    if ( NULL != ( p = strrchr( fnameStart, ':' ) ) ) {
    fnameStart = p + 1;
    }
    if ( NULL != ( p = strrchr( fnameStart, '\\' ) ) ) {
    fnameStart = p + 1;
    }
    if ( NULL != ( p = strrchr( fnameStart, '/' ) ) ) {
    fnameStart = p + 1;
    }
    if ( ignoreMissing ) {
    f = fopen( argv[ i ], "rb" );
    if ( f == NULL )
    continue;
    }
    else {
    f = myfopen( argv[ i ], "rb" );
    }
    if ( splitUp ) {
    char type[ 5 ];

    size = fread( buffer, 1, MAX_SAFE_RESOURCE_SIZE, f );

    strncpy( hex, fnameStart+4, 4 );
    hex[ 4 ] = 0;
    sscanf( hex, "%x", &id );
    strncpy( type, fnameStart, 4 );
    PRCAdd( type, ( Word )id, buffer, size );

    if ( MAX_SAFE_RESOURCE_SIZE <= size ) {
    size = fread( buffer, 1, MAX_RESOURCE_SIZE, f );
    type[ 0 ] = '2';
    PRCAdd( type, ( Word )id, buffer, size );
    }

    fclose( f );
    continue;
    }
    else {
    size = fread( buffer, 1, MAX_RESOURCE_SIZE, f );
    }
    fclose( f );
    }
    strncpy( hex, fnameStart+4, 4 );
    hex[ 4 ] = 0;
    sscanf( hex, "%x", &id );
    PRCAdd( fnameStart, ( Word )id, buffer, size );
    }
    PRCDump( argv[ firstArg ] );
    return 0;
    }

    --------------------------------------------------------
     
  7. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #7
    Can you put the code in a [ CODE ] block please? it would make the thread easier to read. Anyway does the code compile without errors*? If so does the program work?

    * I just realised this is very unclear to a non-programmer. Warning's don't actually stop the code compiling, though if you have them they often make the program unfunctional and likely to crash when you run it, errors mean that the compiler cannot compile the code at all, so there is no chance of being able to run the program.
     
  8. thread starter macrumors 6502a

    iTwitch

    Joined:
    Mar 30, 2006
    Location:
    East of the Mississippi
    #8
    Code:
     block???? Yes it compiles without errors, no the program doesn't work.
    
    I'm using 'make -f convert-make' to compile and 'convert-make' contains :
    --------------------------------------------
    all: toprc bmp2palm
    
    toprc: toprc.c
    	gcc -O toprc.c -o toprc
    
    bmp2palm: bmp2palm.c
    	gcc -O bmp2palm.c -o bmp2palm
    -----------------------------------------------
    
    bmp2palm.c is in my inital message and toprc.c is in the message before yours.
    
    All I had to do to get this to compile under linux/gcc was change two occurrences of 'stricmp' to 'strcasecmp', but when I tried it on my iBook/gcc I got errors due to a fcloseall() call. I had hoped using 'fclose()' instead of 'fcloseall()' would have worked but there seems to be bigger issues. So much for portable code. :eek:
     
  9. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #9
    Yeah a [CODE] block, sorry i was having a mental block on stopping parsing stuff.

    I think (according to this, shout me down as I don't really know what I'm talking about.) that the errors about signedness, can be ignored and are the compiler being fussy.

    with the MAX_SAFE things you could try commenting them out, and see if it still works.

    Again I would also try commenting out the troublesome lines in palmtypes.h, it seems like they are repeating something...

    Sorry, I don't really know what I'm talking about on this one :(.
     
  10. macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #10
    The simplest solution is to comment out fcloseall() call in CleanUp(), like this:
    /* fcloseall() */

    fcloseall() is called only by CleanUp() function. And CleanUp() is only called before program exits (exit(1) and return 0; from main). So this invocation of fcloseall() is redundant because exit() function flushes all open streams and closes the streams.
     

Share This Page