*&---------------------------------------------------------------------*
*&  Include           ZDB_UPLOAD_CONVERT_DATA                          *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& This file is part of ZDB.                                           *
*&                                                                     *
*& ZDB_DOWNLOAD 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.                                      *
*&                                                                     *
*& ZDB_DOWNLOAD 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 ZDOWNLOAD. If not, see <http://www.gnu.org/licenses/>.   *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&  Author:     Ruediger von Creytz     ruediger.creytz@globalbit.net  *
*&  Copyright:  globalBIT, LLC          http://www.globalbit.net       *
*&                                                                     *
*&---------------------------------------------------------------------*


*-----------------------------------------------------------------------
* form: csv2data
*-----------------------------------------------------------------------
FORM csv2data USING
                value(i_tabname) TYPE tabname
                it_csv TYPE it_string.
  DATA:
  lt_dd03l TYPE STANDARD TABLE OF dd03l,
  ls_dd02l TYPE dd02l,
  l_fieldname TYPE fieldname,
  l_position TYPE tabfdpos,
  lt_value TYPE it_string,
  l_value TYPE string,
  l_csv TYPE string,
  l_len TYPE i,
  l_cnt TYPE i,
  l_length TYPE string,
  l_quot TYPE flag,
  ls_fs TYPE REF TO data.

  FIELD-SYMBOLS:
  <ls_csv> TYPE string,
  <ls_dd03l> TYPE dd03l,
  <ls_fs> TYPE ANY,
  <l_fs> TYPE ANY,
  <l_value> TYPE ANY.

  SELECT * FROM dd03l INTO TABLE lt_dd03l
      WHERE tabname = i_tabname
      AND as4local = 'A'
      ORDER BY position.

  SELECT SINGLE * FROM dd02l INTO ls_dd02l
      WHERE tabname = i_tabname
      AND as4local = 'A'.

*content
  CREATE DATA ls_fs TYPE (i_tabname).
  ASSIGN ls_fs->* TO <ls_fs>.

  LOOP AT it_csv ASSIGNING <ls_csv>.
    IF sy-tabix = 1.
      CONTINUE.
    ENDIF.

    CLEAR l_cnt.

    SPLIT <ls_csv> AT ';' INTO TABLE lt_value.
    LOOP AT lt_value ASSIGNING <l_value>.
      IF l_quot = abap_true.
        CONCATENATE l_value ';' <l_value> INTO l_value.
      ELSE.
        l_value = <l_value>.
        IF l_value <> space AND l_value+0(1) = '"'.
          l_quot = abap_true.
          l_value = l_value+1.
        ENDIF.
      ENDIF.

      IF l_quot = abap_true.
        l_len = strlen( l_value ).
        IF l_len > 0.
          SUBTRACT 1 FROM l_len.
          IF l_value+l_len(1) = '"'.
            l_value = l_value+0(l_len).
            l_quot = abap_false.
          ENDIF.
        ENDIF.
      ENDIF.

      IF l_quot = abap_false.
        ADD 1 TO l_cnt.
        READ TABLE lt_dd03l ASSIGNING <ls_dd03l> INDEX l_cnt.
        IF sy-subrc <> 0.
*          message 'Cannot import data'.
          EXIT.
        ENDIF.

        IF <ls_dd03l>-fieldname <> 'MANDT'.
          ASSIGN COMPONENT <ls_dd03l>-fieldname OF STRUCTURE <ls_fs>
            TO <l_fs>.
          <l_fs> = l_value.
        ENDIF.
      ENDIF.
    ENDLOOP.

    MODIFY (i_tabname) FROM <ls_fs>.
    CLEAR <ls_fs>.

  ENDLOOP.

ENDFORM.                    "select_csv2data
