*&---------------------------------------------------------------------*
*&  Include           ZUTIL_SRC_COMPRESS                               *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& This file is part of ZUTIL.                                         *
*&                                                                     *
*& ZUTIL is free software: you can redistribute it and/or modify       *
*& it under the terms of the GNU General Public License as published   *
*& by the Free Software Foundation, either version 3 of the License,   *
*& or any later version.                                               *
*&                                                                     *
*& ZUTIL is distributed in the hope that it will be useful,            *
*& but WITHOUT ANY WARRANTY; without even the implied warranty of      *
*& MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
*& GNU General Public License for more details.                        *
*&                                                                     *
*& You should have received a copy of the GNU General Public License   *
*& along with ZUTIL. If not, see <http://www.gnu.org/licenses/>.       *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&  Author:     Ruediger von Creytz     ruediger.creytz@globalbit.net  *
*&  Copyright:  globalBIT, LLC          http://www.globalbit.net       *
*&                                                                     *
*&---------------------------------------------------------------------*


*-----------------------------------------------------------------------
* get_src_compressed
*-----------------------------------------------------------------------
FORM get_src_compressed
  USING
    it_src TYPE table
    value(i_compress_better) TYPE flag
  CHANGING
    ct_compressed TYPE table.

  DATA:
  ls_src TYPE string,
  l_is_header TYPE flag VALUE abap_true,
  l_length TYPE i,
  l_pos TYPE i,
  l_is_string TYPE flag,
  l_chars(72) TYPE c,
  l_str TYPE string,
  l_str2 TYPE string,
  l_srclen TYPE i,
  l_strlen TYPE i,
  l_line TYPE string,
  l_linelen TYPE i.

  REFRESH ct_compressed.

* drop header for better compression
  IF i_compress_better = abap_true.
    l_is_header = abap_false.
  ENDIF.

  LOOP AT it_src INTO ls_src.
* drop empty rows
    IF ls_src IS INITIAL.
      l_is_header = abap_false.
      CONTINUE.
    ENDIF.
* drop comments if not head information
    IF ls_src+0(1) = '*'.
      IF l_is_header = abap_true.
        APPEND ls_src TO ct_compressed.
      ENDIF.
      CONTINUE.
    ENDIF.
* remove comments from row
    IF ls_src CA '"'.
      PERFORM split_src_string
        USING
          ls_src
          '"'
        CHANGING
          l_str
          l_str2.
      ls_src = l_str.
    ENDIF.
* condense line
    PERFORM get_src_line_compressed
      CHANGING ls_src.
* add line
    IF NOT l_line IS INITIAL.
      l_srclen = strlen( ls_src ).
      l_linelen = strlen( l_line ).
      l_length = l_srclen + l_linelen.
      IF l_length < 71.
        CONCATENATE l_line ls_src INTO l_line SEPARATED BY space.
        CONTINUE.
      ELSE.
        DO.
          PERFORM split_src_string
            USING
              ls_src
              space
            CHANGING
              l_str
              l_str2.
          l_strlen = strlen( l_str ).
          l_linelen = strlen( l_line ).
          l_length = l_strlen + l_linelen.
          IF l_length < 71.
            CONCATENATE l_line l_str INTO l_line
                SEPARATED BY space.
            IF NOT l_str2 IS INITIAL AND l_str2+0(1) = '*'.
              CLEAR l_chars.
              CLEAR ls_src.
              l_chars+1 = l_str2.
              ls_src = l_chars.
            ELSE.
              ls_src = l_str2.
            ENDIF.
          ELSE.
            EXIT.
          ENDIF.
          IF ls_src IS INITIAL.
            EXIT.
          ENDIF.
        ENDDO.
      ENDIF.
    ENDIF.
    IF NOT l_line IS INITIAL.
      APPEND l_line TO ct_compressed.
    ENDIF.
    l_line = ls_src.
    l_is_header = abap_false.
  ENDLOOP.
  IF NOT l_line IS INITIAL.
    APPEND l_line TO ct_compressed.
  ENDIF.

ENDFORM.                    "get_src_compressed


*-----------------------------------------------------------------------
* get_src_line_compressed
*-----------------------------------------------------------------------
FORM get_src_line_compressed
  CHANGING
    c_line TYPE string.

  DATA:
  l_str TYPE string,
  l_str1 TYPE string,
  l_str2 TYPE string,
  l_line_new TYPE string.

  l_str = c_line.

  DO.
    PERFORM split_src_string
      USING
        l_str
        space
      CHANGING
        l_str1
        l_str2.

    IF l_line_new IS INITIAL.
      l_line_new = l_str1.
    ELSE.
      CONCATENATE l_line_new l_str1 INTO l_line_new SEPARATED BY space.
    ENDIF.

    IF l_str2 IS INITIAL.
      EXIT.
    ENDIF.
    l_str = l_str2.
  ENDDO.

  c_line = l_line_new.

ENDFORM.                    "get_src_line_compressed


*-----------------------------------------------------------------------
* split_src_string
*-----------------------------------------------------------------------
FORM split_src_string
  USING
    value(i_src) TYPE string
    value(i_sep) TYPE char1
  CHANGING
    c_str1 TYPE string
    c_str2 TYPE string.

  DATA:
  l_src TYPE string,
  l_is_string TYPE flag VALUE abap_false,
  l_pos TYPE i VALUE 0,
  l_char TYPE char1,
  l_srclen TYPE i.


  CLEAR:
  c_str1,
  c_str2.

  l_src = i_src.

  SHIFT l_src LEFT DELETING LEADING space.

  SPLIT l_src AT i_sep INTO c_str1 c_str2.

  IF c_str1 CA '''' AND c_str2 CA ''''.
    CLEAR: c_str1, c_str2.
    l_srclen = strlen( l_src ).
    DO.
      l_char = l_src+l_pos(1).
      CONDENSE l_char.
      CASE l_char.
        WHEN ''''.
          IF l_is_string = abap_true.
            l_is_string = abap_false.
          ELSE.
            l_is_string = abap_true.
          ENDIF.
        WHEN i_sep.
          IF l_is_string = abap_false.
            c_str1 = l_src+0(l_pos).
            ADD 1 TO l_pos.
            c_str2 = l_src+l_pos.
            EXIT.
          ENDIF.
      ENDCASE.
      ADD 1 TO l_pos.
      IF l_pos = l_srclen.
        c_str1 = l_src.
        EXIT.
      ENDIF.
    ENDDO.
  ENDIF.

  SHIFT c_str2 LEFT DELETING LEADING space.
  IF c_str1 NA ''''.
    CONDENSE c_str1.
  ENDIF.
  IF c_str2 NA ''''.
    CONDENSE c_str2.
  ENDIF.

ENDFORM.                    "split_src_string
