
   /*****************************************
    *                                       *
    *        stripcrlf.c                    *
    *                                       *
    *        © 27-Mar-2002                  *
    *        Alfred Faust                   *
    *                                       *
    *                                       *
    *        This program strip the         *
    *        CR/LF from a unix-style        *
    *        text file                      *
    *                                       *
    *                                       *
    *        Usage:                         *
    *        stripcrlf                      *
    *                                       *
    *        The program opens a requester  *
    *        to choose input and output.    *
    *                                       *
    *                                       *
    *****************************************/



#include <stdio.h>
#include <string.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/asl.h>
#include <proto/asyncio.h>
#include <proto/intuition.h>
#include <intuition/intuition.h>
#include <utility/tagitem.h>

#define SOURCETITLE "Choose sourcefile"
#define DESTTITLE   "Choose destinationdir"

struct FileRequester *filereq;
struct Library *AslBase = NULL;
struct Library *AsyncIOBase = NULL;
struct EasyStruct message;

struct EasyStruct message ={
    20,
    0,
    NULL,
    "Exit StripCr ?\n(No = Continue stripping)",
    "No|Yes"
};

static UBYTE * getname(UBYTE *reqentry, LONG sw){


    LONG len = strlen((BYTE *)reqentry) + 2;
    UBYTE * name = NULL;

	if (reqentry){
	    name = (UBYTE *) AllocVec(len, (MEMF_ANY | MEMF_CLEAR));
	    if (name){
		if (sw){
		    sprintf(name, "%s2", reqentry);
		}
		else sprintf(name, "%s", reqentry);

		return(name);
	    }
	    else return(NULL);
	}
	else return(NULL);
}

static UBYTE * createfilename(UBYTE * drawer, UBYTE * file){

    LONG drawerlen = 0,
	 filelen = 0;
    UBYTE * filename;
	if (drawer && file){
	    drawerlen = ((strlen ((BYTE *)drawer))+1);
	    filelen = ((strlen ((BYTE *)file))+1);
	    filename = (UBYTE *)AllocVec((drawerlen + filelen),(MEMF_ANY | MEMF_CLEAR));
	    if (filename){
		strcpy(filename, drawer);
		AddPart(filename, file,(drawerlen + filelen));
		return(filename);
	    }
	    else return(NULL);
	}
	else return(NULL);
}


int main (void){

    struct TagItem filereqtags[7];
    UBYTE *infilename = NULL, //with complete path
	  *outfilename = NULL, //with complete path
	  *infile = NULL, //only file
	  *inpath = NULL, //only path
	  *outfile = NULL,
	  *outpath = NULL;          

    LONG readbyte = 0,
	 result = 0,
	 argselected = 0;
    int i = 0;

    struct WBArg *wbarg = NULL;

    struct AsyncFile *readfile = NULL,
		     *writefile = NULL;

    /* we fill the filerequester tags with neccessary datas */

    filereqtags[0].ti_Tag  = ASLFR_TitleText;
    filereqtags[0].ti_Data = (ULONG)DESTTITLE;

    filereqtags[1].ti_Tag  = ASLFR_InitialDrawer;
    filereqtags[1].ti_Data = (ULONG)"RAM:";

    filereqtags[2].ti_Tag  = ASLFR_DoMultiSelect;
    filereqtags[2].ti_Data = FALSE;

    filereqtags[3].ti_Tag  = ASLFR_DoPatterns;
    filereqtags[3].ti_Data = FALSE;

    filereqtags[4].ti_Tag  = ASLFR_InitialFile;
    filereqtags[4].ti_Data = (ULONG)"";

    filereqtags[5].ti_Tag  = ASLFR_InitialPattern;
    filereqtags[5].ti_Data = (ULONG)"";

    filereqtags[6].ti_Tag  = TAG_DONE;

    /* we open the neccessary libraries */

    if(!(AslBase = (struct Library *)OpenLibrary("asl.library",0L))){
	goto exitit;
    }

    if(!(AsyncIOBase = (struct Library *)OpenLibrary("asyncio.library",0L))){
	goto exitit;
    }


    /* we allocate a asl request as a FileRequester */
    filereq = (struct FileRequester *)AllocAslRequest(ASL_FileRequest, 0L);

start2: /* startpoint, if we don't leave the program and NO outpath was choosen */

    /* making the AslRequester for th outpath appear -
    if we leave the  requester with a selection ... */

    if(AslRequest(filereq, filereqtags)){

	outpath = getname(filereq->fr_Drawer,0);

    }
    else {
	/* leaved the requester with "Cancel" */
	goto exitit;
    }


    /* alter the tags for use as source requester */

    filereqtags[0].ti_Data = (ULONG)SOURCETITLE;  //new title
    filereqtags[2].ti_Data = TRUE;                //multiselect
    filereqtags[3].ti_Data = TRUE;                //pattern


start:  /* startpoint, if we don't leave the program and outpath was choosen*/

    /* making the AslRequester appear -
    if we leave the  requester with a selection ... */

    if(AslRequest(filereq, filereqtags)){

	/* multiple selection ? */

	if (filereq->fr_NumArgs){
	    argselected = filereq->fr_NumArgs ;
	    wbarg = filereq->fr_ArgList;
	    filereq->fr_File = wbarg->wa_Name;

	}

	/* we create the string with complete path of the infile */

	inpath = getname(filereq->fr_Drawer,0);
	infile = getname(filereq->fr_File,0);
	infilename = createfilename(inpath, infile);

	/* same for the outpath, but the filename dependend if the
	sourcedir is the same like outdir or not */

	if (!(strcmp(inpath, outpath))){

	    /* Outdir == Indir -> we append a "2" at the filename */

	    outfile = getname(filereq->fr_File,1);
	}
	else{
	    outfile = getname(filereq->fr_File,0);
	}

	outfilename = createfilename(outpath, outfile);

    }
    else {
	/* leaved the requester with "Cancel" */
	goto exitit;
    }

    /* mainloop */

    for(i = 0;;){

	if ((argselected >1) && (i == 0)){
	    goto jump;
	}

	/* open the infile for reading and the outfile for writing */

	readfile = OpenAsync((STRPTR)infilename, MODE_READ, 8192);
	writefile = OpenAsync((STRPTR)outfilename, MODE_WRITE, 8192);

	/* the core of the program */

	for(;;){ /* loop for stripping */
	    /* reading one byte into a long  - leftaligned*/

	    result = (UBYTE)ReadAsync(readfile, &readbyte, 1);

	    /* if EOF - End of file - we leave the loop */

	    if (result == 0) break;

	    /* if byte is not a CR - ! the comparisationvalue is a long ! */

	    else{
		if (readbyte != 0x0D000000){

		    /* write one byte to the outfile */

		    WriteAsync(writefile, &readbyte, 1);
		}
	    }
	}


	/* close the opened files */
	if (readfile) CloseAsync(readfile);
	readfile = NULL;
	if (writefile) CloseAsync(writefile);
	writefile = NULL;
	if (i == argselected) break;

jump:
	/* mor than one file selected */

	if ((argselected > 1) && (wbarg)){
	    if (infile){

		/* freeing the infilerecources at first */

		FreeVec(infile);
		infile = NULL;
	    }

	    /* new infile from the List */

	    infile = getname(wbarg->wa_Name,0);

	    if ((infile) && (inpath)){

		/* freeing the infilenamerecources at first */

		if (infilename){
		    FreeVec(infilename);
		    infilename = NULL;
		}

		/* new infilename  */

		infilename = createfilename(inpath, infile);

		/* all the stuff for the outfilename */

		if(outfile){
		    FreeVec(outfile);
		    outfile = NULL;
		}
		if (!(strcmp(inpath, outpath))){
		    outfile = getname(wbarg->wa_Name,1);
		}
		else{
		    outfile = getname(wbarg->wa_Name,0);
		}
		if(outfilename){
		    FreeVec(outfilename);
		    outfilename = NULL;
		}
		outfilename = createfilename(outpath, outfile);
		printf("Working on file: %s\n", (UBYTE *)infile);
	    }
	    /* if no infile or inpath */

	    else break;

	    /* go to the next entry in the filelist */

	    wbarg++;
	}
	else  break;
	i ++;
    }

exitit:

    if (result = EasyRequest(NULL, &message, NULL)){

	Delay(20);

	if (infile) {
	    FreeVec(infile);
	    infile = NULL;
	}
	if (outfile) {
	    FreeVec(outfile);
	    outfile = NULL;
	}
	if (infilename) {
	    FreeVec(infilename);
	    infilename = NULL;
	}
	if (outfilename) {
	    FreeVec(outfilename);
	    outfilename = NULL;
	}
	if (outpath) {

	    /* alter the tags for use as source requester */

	    filereqtags[0].ti_Data = (ULONG)SOURCETITLE;   //new title
	    filereqtags[1].ti_Data = (ULONG)inpath;        //init drawer
	    filereqtags[2].ti_Data = TRUE;                 //multiselect
	    filereqtags[3].ti_Data = TRUE;                 //pattern
	    filereqtags[4].ti_Data = (ULONG)"";            //init file
	    filereqtags[5].ti_Data = (ULONG)"#?";          //init pattern
	    Delay(10);
	    goto start;
	}
	else{
	    /* alter the tags for use as destination path requester */

	    filereqtags[0].ti_Data = (ULONG)DESTTITLE;     //new title
	    filereqtags[1].ti_Data = (ULONG)"RAM:";        //init drawer
	    filereqtags[2].ti_Data = FALSE;                //multiselect
	    filereqtags[3].ti_Data = FALSE;                //pattern
	    filereqtags[4].ti_Data = (ULONG)"";            //init file
	    Delay(10);
	    goto start2;
	}
    }


    /* we free all allocated ressorces */

    if (infile) FreeVec(infile);
    if (outfile) FreeVec(outfile);
    if (inpath) FreeVec(inpath);
    if (outpath) FreeVec(outpath);
    if (infilename) FreeVec(infilename);
    if (outfilename) FreeVec(outfilename);
    if (filereq) FreeAslRequest(filereq);

    /* close the opened libraries */

    if (AslBase) CloseLibrary((struct Library *)AslBase);
    if (AsyncIOBase) CloseLibrary((struct Library *)AsyncIOBase);

    /* close the opened files */
    if (readfile) CloseAsync(readfile);
    if (writefile) CloseAsync(writefile);

}
