/*
 *  IR-Listcommands V1.0
 *
 *  Purpose: List all the nodes in a ir-master project file
 *           (demonstration of how to load a project file)
 */
#include <libraries/irbase.h>

#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/utility.h>

#include <mike_h/packer.h>
#include <stdlib.h>

#define VERSION "1.0"

static void listcommands( char * );

static char version[] = "$VER: IR-ListCommands "VERSION;
static char usage[]   = "IR-Listcommands "VERSION" ©1994 by Michael Watzl\n\nUsage: IR-Listcommands project.irm\n";

static char file_code[]    = "IRMD";    // file code version 1.0 is to be attached

// see GNRENDER_ defines
static char *type_string[] = {"Gfx2-node","Gfx1-node","Text-node"};

// see TYP_ defines
static char *ir_string[]   = {"37 kHz","45 kHz","Unpulsed","66 kHz","Control_L","Rapid","Shell-Command","Conrad-Relaicard","Audio-DMA"};

int main( int argc , char **argv )
{
    int i;

    switch( argc ){
    case 0:         // No WBstart, please; perhaps you can insert an requester here...
        return;
    case 1:
        PutStr( usage );
        return(5);
    default:
        for(i=1;i<argc;i++){
            Printf("----------- Nodes in file \"%s\":\n",argv[i]);
            listcommands( argv[i] );
        }
        return(0);
    }
}

static void
listcommands( char *name )
{
    int  node_counter = 0;
    BPTR fh;

    if( fh = Open( name , MODE_OLDFILE )){
        char temp[1024];

        Read( fh , temp , 4 );                          // read file_code
        if( !Strnicmp( temp ,file_code , 4)){           // compare it
            Read( fh , temp , 3 );                      // read file code version
            if( !Strnicmp( temp , "1.0" , 3)){          // & compare
                unsigned short size;
                unsigned short usize;
                struct GNode gn;                        // if you want to use the nodes later
                                                        // you should better malloc & attach them to a list

                Read( fh , &size , 2 );                // size of packed project header
                Seek( fh , size  , OFFSET_CURRENT );   // skip the project header (not used, 'cause we do not
                                                       // want to open a window...

                while( Read( fh , &size , 2 ) == 2 ){   // get length of packed gnode
                    Read( fh , temp , size );           // get packed gnode

                    // simple ByteRun1 dempack algorithm - not included to the source
                    UnPackData( temp , (char *)&gn , size , sizeof( struct GNode ));

                    Read( fh , &size , 2 );         // size of packed renderdata (eiher imagedata or text)
                    switch( gn.gn_gnrender ){
                    case GNRENDER_TEXT:
                        Read( fh , &usize , 2 );    // get unpacked size of renderdata
                        if( gn.gn_renderdata = malloc( usize )){
                            Read( fh, temp , size );
                            UnPackData( temp , gn.gn_renderdata , size , usize );
                        }
                        break;
                    case GNRENDER_GFX1:
                    case GNRENDER_GFX2:
                    default:
                        gn.gn_renderdata = 0;
                        Seek( fh , 2 , OFFSET_CURRENT );    // don't read the image data in this case
                        Seek( fh , size , OFFSET_CURRENT ); // if you would like to show it - you have
                        break;                              // to alloc chip and set the ImageData pointer...
                    }
                    Printf("#%3ld: gadtype: %s  name: %-25.25s irtype: %s\n",
                        node_counter,
                        gn.gn_gnrender <= 2 ? type_string[ gn.gn_gnrender ] : "Unknown",
                        gn.gn_gnrender == GNRENDER_TEXT ? gn.gn_renderdata:"N/A",
                        gn.gn_type     <= 8 ? ir_string[ gn.gn_type ] : "Unknown"
                    );
                    if(gn.gn_renderdata )  free( gn.gn_renderdata );
                    node_counter++;
                }
            } else Printf("Sorry, I can't read version %s of IR-Master datafiles.\n",temp);
        } else Printf("\"%s\" is not a IR-Master data file.\n",name);
        Close( fh );
    } else Printf("Can't open \"%s\".\n",name);
}

