ABAP UX2DOS

UNIX TO DOS (UX2DOS) for ABAP.

[Author: Frank Kempf updated August 2010]

For those to stumble over text file processing on Windows of files that were generated on UNIX Systems.

Imagine that:

You run a multi OS Landscape and need to process text files that were generated from a SAP Sys running UNIX.

UNIX delivers the files with just a LINEFEED whereas WINDOWS uses CARRIAGE RETURN plus LINEFEED as a row delimiter.
Therefore some Windows based processing programms (and some ABAPs) will break when feeding them a UNIX file.

Since your SAP System might not provide a UX2DOS or UNIX2DOS command, you could also convert the files by using ABAP programming.

 

The Code below is ment to work - not to perform at speed of light.
It will output to a binary opened file byte by byte which is considered slow, but saves code that would be neccessary to build up a write buffer.

The code is taken from one of my projects and might need some adjustments - feel free to use it.

 

Created July 2009, Frank Kempf (2112portals.com)
Modified January 2010, Frank Kempf(2112portals.com)

*&---------------------------------------------------------------------*
*&      Form  UX2DOS
*&---------------------------------------------------------------------*
*      Converting a UX File to DOS Format (CRLF)
*----------------------------------------------------------------------*
*      -->IV_FILENAME  text

*----------------------------------------------------------------------*
FORM UX2DOS
  using IV_FILENAME TYPE CSEQUENCE.
  DATA: lv_xCR type xstring.
  DATA: iRet type i.
  DATA: lv_str_filename type string.
  DATA: XOUT TYPE XSTRING.
  DATA: xline type xstring,
        xchar type x.
  DATA: actLen type i.
  DATA: pos type i,
        pos_prev type i,
        lv_nCount(10) type n.
  FIELD-SYMBOLS: <lfs_x_in>  type xstring,
                 <lfs_x_out> type xstring,
                 <lfs_x_prv> type x.

  lv_xCR =  '0D0A'.

lv_str_filename = IV_FILENAME.
  assign  xout to <lfs_x_out> .
   TRY.
OPEN DATASET lv_str_filename FOR INPUT IN BINARY MODE .
CATCH CX_SY_FILE_AUTHORITY .
write: 'UX2DOS NO ACCESS TO File [', lv_str_filename, ']'.
RETURN.
ENDTRY.
  IF SY-SUBRC <> 0.
    ULINE.
    write: 'UX2DOS File [', lv_str_filename, '] could not be opened.'.
    RETURN.
  ENDIF.
  "Read full file into buffer and get actlen = filelen
  READ DATASET lv_str_filename INTO xline ACTUAL LENGTH actlen.
  CLOSE DATASET lv_str_filename.
* Reopen same file binary mode
  OPEN DATASET lv_str_filename FOR OUTPUT IN BINARY MODE .

  pos = -1. "because pos is incremented right after do - and we need a ZERO.
  DO actlen times.
    add 1 to   pos.
    assign xline to  <lfs_x_in> .
    if <lfs_x_in> is not assigned.
      exit.
    endif.
    pos_Prev = pos - 1.
     if pos_Prev >= 0.
       assign  xchar to <lfs_x_prv>.
       <lfs_x_prv> = <lfs_x_in>+pos_Prev.
     endif.
* Check if substitution is neccessary at all
    if <lfs_x_in>+pos(1) = '0A' AND <lfs_x_prv> <> '0D'.
      TRANSFER lv_xCR to lv_str_filename length 2.
      ADD 1 to lv_nCount.
      continue.
    endif.

    TRANSFER <lfs_x_in>+pos to lv_str_filename length 1.
  ENDDO.

  CLOSE DATASET lv_str_filename.
  ULINE.
  write:  'UX2DOS File [', lv_str_filename, '] [', lv_nCount, '] linefeeds replaced with CRLF.'.

ENDFORM.                                                    "UX2DOS
Letzte Aktualisierung ( Donnerstag, 5. August 2010 )