/*
 * Usage:       LsClip
 * Function:    List the content of the clipboard.
 * Description: This is an example of the standard way of using the 
 *              iffparser in the most simple way, i.e. in the 
 *              RAWSTEP-mode when you want to read data from the 
 *              clipboard (and files).
 *
 * Written by:  Michael Jansson. 1990-12-08
 */


#include <libraries/iffparse.h>
#include <proto/iffparse.h>

#define NAME        "Error in LsClip: "
#define ERR_LIB     NAME "Could not open iffparse.library!"
#define ERR_MEM     NAME "Out of memory!"
#define ERR_CLIP    NAME "Could not open the clipboard!"
#define ERR_OPEN    NAME "Could not open primary clip in clipboard!"
#define ERR_FAILED  NAME "Failed to read the clipboard (Error Code: %ld)!\n"
#define LEVEL       ". "
#define SUCCESS     0L

struct Library *IFFParseBase;

void
main(void)
{
    struct ContextNode *chunk = NULL;
    struct IFFHandle *iff = NULL;
    UBYTE buf1[5], buf2[5];
    long i, error;
    

    /* Open all the stuff that is needed! */
    if ((IFFParseBase=OpenLibrary("iffparse.library", 0L))==NULL) {
        puts(ERR_LIB);
        goto die;
    }
    if ((iff = AllocIFF())==NULL) {
        puts(ERR_MEM);
        goto die;
    }
    if ((iff->iff_Stream=(ULONG)OpenClipboard(0L))==0L) {
        puts(ERR_CLIP);
        goto die;
    }
    InitIFFasClip(iff);
    if (OpenIFF(iff, IFFF_READ)) {
        puts(ERR_OPEN);
        goto die;
    }

    /* Let's do it...*/
    do {
        switch(error=ParseIFF(iff, IFFPARSE_RAWSTEP)) {
        case IFFERR_EOC:/* End of a context (chunk). */
            break;
        case IFFERR_EOF:/* End of the stream. */
            goto die;
            break;
        case SUCCESS:   /* Entering a new context (chunk). */
            chunk = CurrentChunk(iff);
            for (i=0; i<iff->iff_Depth; i++)
                printf(LEVEL);
            printf("%s %ld %s\n", 
                IDtoStr(chunk->cn_ID, buf1), 
                chunk->cn_Size, 
                IDtoStr(chunk->cn_Type, buf2));
            break;
        default:        /* Everything else is a genuin error. */
            printf(ERR_FAILED, error);
            goto die;
            break;
        }
    } while (TRUE);


    /* Make a clean exit. */
die:
    if (iff) {
        CloseIFF(iff);
        if (iff->iff_Stream)
           CloseClipboard((struct ClipboardHandle *)iff->iff_Stream);
        FreeIFF(iff);
    }
    if (IFFParseBase)
        CloseLibrary(IFFParseBase);
    exit(0);
}
