utorok 9. októbra 2012

ABAP: OPEN DATASET FOR OUTPUT IN TEXT MODE

ABAP: OPEN DATASET FOR OUTPUT IN TEXT MODE - LF vs CRLF

Problem:
Downloading text file to UNIX file system, which must be later uploaded to Windows file system = problem with white chars CR, LF.

http://sap.ittoolbox.com/groups/technical-functional/sap-abap/convert-unix-file-to-windows-file-4879404?reftrk=no&trdref=4e6577736c6574746572


Read especially the part:

Lou,
That's not always true. You can set the CRLF with windows encoding directly in the unix/linux server. Open dataset puts LF only if you aren't too specific in the way you use it.

From abaphelp. -> Open DataSet - Mode ->
"

... WITH WINDOWS LINEFEED



So, we can specify directly type of LF - 'NT' or 'UNIX' or we can say WITH ... LINEFEED, which should be NATIVE/SMART/UNIX/WINDOWS

ABAP: CRM Reporting


Helpfull package for CRM Reporting - CRM_REPORT



ABAP: How to replace white chars in text (string)

Flow:
1) Convert ASCII text to UTF-8 hexadecimal
2) Replace 'white char' with SPACE
3) Convert UTF-8 hexadecimal  back to ASCII text
4) Delete redundant SPACEs from ASCII text


  DATA:
        lv_nazov_proj TYPE string.
  CLEAR: lv_nazov_proj.
  lv_nazov_proj = ls_gap_data-nazov_proj.

  PERFORM replace_white_chars
    CHANGING lv_nazov_proj.



*&---------------------------------------------------------------------*
*&      Form  REPLACE_WHITE_CHARS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_LV_NAZOV_PROJ  text
*----------------------------------------------------------------------*
form REPLACE_WHITE_CHARS
  changing cv_text.
*  WARNING! Don't CLEAR CV_TEXT!!!

  DATA:
        lr_conv_out TYPE REF TO cl_abap_conv_out_ce,
        lr_conv_in  TYPE REF TO cl_abap_conv_in_ce,
        lv_len TYPE i,
        lv_buffer TYPE xstring,
        lv_hex_cr(1TYPE x VALUE '0D',  "CR
        lv_hex_lf(1TYPE x VALUE '0A',  "LF
        lv_hex_ht(1TYPE x VALUE '09',  "Horizontal Tab
        lv_hex_vt(1TYPE x VALUE '0B',  "Vertical Tab
        lv_hex_sp(1TYPE x VALUE '20'.  "SPACE

* 1) cv_text ASCII --> Hexa
  lv_len = STRLEN( cv_text ).

  lr_conv_out = cl_abap_conv_out_ce=>create(
            encoding = 'UTF-8'
            endian = 'L'  ).

  CALL METHOD lr_conv_out->write( data = cv_text n = lv_len ).

  lv_buffer = lr_conv_out->get_buffer( ).

* 2) Replace 'CR, LF, VerticalTab, HorizontalTab' --> SPACE
  REPLACE ALL OCCURRENCES OF:
    lv_hex_cr IN lv_buffer WITH lv_hex_sp IN BYTE MODE,
    lv_hex_lf IN lv_buffer WITH lv_hex_sp IN BYTE MODE,
    lv_hex_ht IN lv_buffer WITH lv_hex_sp IN BYTE MODE,
    lv_hex_vt IN lv_buffer WITH lv_hex_sp IN BYTE MODE.

* 3) Hexa --> ASCII
  lr_conv_in = cl_abap_conv_in_ce=>create(
             encoding = 'UTF-8'
             endian   = 'L'
             input    = lv_buffer   ).

  CALL METHOD lr_conv_in->read(
       EXPORTING n    = lv_len
       IMPORTING data = cv_text ).

* 4) Delete redundant SPACEs
  CONDENSE cv_text.

endform.                    " REPLACE_WHITE_CHARS