[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: LF/CR conversion



  To: David Wilson
On Thu, 11 Jan 2007 13:19:06 -0800, "David Wilson" <mcs6502@gmail.com>
wrote:

>Simon D. Williams wrote:
>> Is there an Apple II program (prefereably ProDOS) that can convert text
>> files between DOS, Apple and UNIX formats?
>
>Do you want a ProDOS application (.SYSTEM program) or an external
>command for BASIC.SYSTEM? I think I could write a simple one over the
>weekend.
>
>If the former, should it have wildcard support for bulk conversions
>(this would add some work so would probably have to wait for version
>two). Should it attempt to auto-identify the type of the source file?
>What if anything should be done with "naked" CR (missing the following
>LF) characters in a DOS file?

Simon's original post hasn't shown up on A2Central.com yet so I'm
replying to David's reply.

There are several program out there that will do this conversion for
you but because it is such a simple thing to do you can always write
your own.  Here is some pseudo code that just popped into my head that
will deal with converting from any format to the format of your
choice:

    let OutputEOL$ = whatever end of line you want
    open input file
    open output file
    do
        let Output$ = ""
        get next character from the input file
        is it a CR?
            yes, let Output$ = OutputEOL$
                get next character from the input file
                is it a LF?
                    yes, do nothing and thus skip the LF
                    no, add character read from input file to Output$
                end
            no, is it a LF?
                yes, let Output$ = OutputEOL$
                no, let Ouput$ = character read from input file
        end
        write Output$ to output file
    loop until EndOfFile

Basically this checks to see if the character read is a carriage
return.  If it is, then it is going to add the new end of line string
to the output file. It then checks to see if the next character is a
line feed (as in a DOS file) and if it is, it just ignores it.  But if
it isn't, it adds that character to the output file as well.

If the character read isn't a carriage return it checks to see if it
is a LF.  If it is, then this is a Unix file as we already know the
character before it wasn't a CR otherwise this LF would have been
skipped already.  There is a potential problem here as it is possible
for single LF to be in a file that normally has CR or CR/LF EOLs but I
don't think that will happen much and is more of a problem of whatever
generated the file as opposed to this conversion program's.

Anyway, if the character read is a line feed, then the program is
going to add the new end of line string to the output file.  If the
character wasn't a line feed then the character is just added to the
output file.

I started working on an Applesoft implementation of this code but I
keep forgetting how limiting Applesoft can be at times so it would
take me longer than I have at present to finish it.  I might finish up
anyway just because I was having fun doing it.  :-)

Jeff Blakeney
- to send me e-mail, CUT the obvious from my e-mail address
--- Synchronet 3.13a-Win32 NewsLink 1.83
A2Central.com - Your total source for Apple II computing.