!+
!	FIX_CRLFS.TPU - Routine to turn CRLFs into line breaks 
!			and remove leading CRs and trailing CRLFs 
!-

procedure eve_fix_crlfs 

LOCAL	the_range;

    on_error
        if (ERROR <> tpu$_STRNOTFOUND) then
            message("Error (" + str(ERROR) + ") at line " + str(ERROR_LINE));
            return;
        endif;
    endon_error;

!
! First remove the CRLFs. If they are not at the EOL, add a line break.
!
    position(beginning_of(current_buffer));
    loop
        the_range := search(ascii(13)+ascii(10), FORWARD);
        exitif (the_range = 0);
        erase(the_range);
        position(beginning_of(the_range));
        if (current_character <> "") then
            split_line;
        endif;
    endloop;
!
! Next remove naked LFs. If they are not at the EOL, add a line break.
!
    position(beginning_of(current_buffer));
    loop
        the_range := search(ascii(10), FORWARD);
        exitif (the_range = 0);
        erase(the_range);
        position(beginning_of(the_range));
        if (current_character <> "") then
            split_line;
        endif;
    endloop;
!
! Finally, remove naked CRs. If they are not at the BOL, add a line break.
!
    position(beginning_of(current_buffer));
    loop
        the_range := search(ascii(13), FORWARD);
        exitif (the_range = 0);
        position(end_of(the_range));
        if (current_offset <> 0) then
            split_line;
        endif;
        erase(the_range);
    endloop;

endprocedure

