REPORT zini_WRAP_STRINGS.
CLASS zini_text_wrap DEFINITION.
PUBLIC SECTION.
CONSTANTS porcent_justify TYPE i VALUE 80. " 80%
METHODS:
from_read_text IMPORTING i_name TYPE any
i_maxlen TYPE i
i_halign TYPE c
EXPORTING t_out TYPE string_t,
from_string IMPORTING i_string TYPE any
i_maxlen TYPE i
i_halign TYPE c
EXPORTING t_out TYPE string_t,
align_string IMPORTING i_string TYPE string
i_maxlen TYPE i
i_halign TYPE c
RETURNING VALUE(e_string) TYPE string.
ENDCLASS.
CLASS zini_text_wrap IMPLEMENTATION.
METHOD align_string.
CONSTANTS c_dic type c LENGTH 15 value '··^¨ºª#~&%|'.
DATA: l_s TYPE char1024,
t_res TYPE match_result_tab.
CASE i_halign.
WHEN 'J' OR 'j' OR 'w' OR 'W'.
WRITE i_String TO l_s LEFT-JUSTIFIED.
DATA(l_porcen) = strlen( i_string ) * 100 / i_maxlen.
IF l_porcen >= porcent_justify.
DATA(l_len) = strlen( l_s ).
do 10 times.
data(l_join) = c_dic+sy-index(1).
if l_s na l_join.
exit.
endif.
enddo.
TRANSLATE l_s+0(l_len) USING ' ¬'.
DO.
l_len = strlen( l_s ).
FIND ALL OCCURRENCES OF '¬' IN l_s+0(l_len) RESULTS t_res.
SORT t_res BY offset DESCENDING.
DELETE t_res WHERE offset = 0.
LOOP AT t_res INTO DATA(l_indice).
l_s = |{ l_s+0(l_indice-offset) }{ l_join }{ l_s+l_indice-offset }|.
IF strlen( l_s ) >= i_maxlen.
EXIT.
ENDIF.
ENDLOOP.
IF strlen( l_s ) >= i_maxlen.
EXIT.
ENDIF.
ENDDO.
data(l_using) = |{ l_join } ¬ |.
translate l_s USING l_using.
ENDIF.
WHEN 'L' OR 'l' OR 'I' OR 'i'. WRITE i_String TO l_s LEFT-JUSTIFIED.
WHEN 'C' OR 'c'. WRITE i_string TO l_s+0(i_maxlen) CENTERED.
WHEN 'R' OR 'r' OR 'D' OR 'd'. WRITE l_s TO l_s+0(i_maxlen) RIGHT-JUSTIFIED.
WHEN OTHERS. l_s = i_string.
ENDCASE.
e_string = l_s.
ENDMETHOD.
METHOD from_string.
DATA: l_s TYPE string.
REFRESH t_out.
IF i_string IS INITIAL.
APPEND i_string TO t_out.
EXIT.
ENDIF.
SPLIT i_string AT ' ' INTO TABLE DATA(t_tab).
LOOP AT t_TAB INTO DATA(l_tab).
DATA(l_MAX) = strlen( l_s ) + strlen( l_tab ) + 1. "El espacio
IF l_max > i_maxlen.
l_s = align_string( EXPORTING I_string = l_s
i_maxlen = i_maxlen
i_halign = i_halign ).
APPEND l_s TO t_out.
l_s = l_tab.
ELSE.
l_s = |{ l_s } { l_tab }|.
ENDIF.
AT LAST.
l_s = align_string( EXPORTING I_string = l_s
i_maxlen = i_maxlen
i_halign = i_halign ).
APPEND l_s TO t_out.
ENDAT.
ENDLOOP.
ENDMETHOD.
METHOD from_read_text.
DATA: lt_text TYPE STANDARD TABLE OF tline.
DATA: t_table TYPE string_t,
l_s TYPE string,
l_name TYPE thead-tdname.
l_name = i_name .
CALL FUNCTION 'READ_TEXT'
EXPORTING
id = 'ST'
language = sy-langu
name = l_name
object = 'TEXT'
TABLES
lines = lt_text
EXCEPTIONS
id = 1
language = 2
name = 3
not_found = 4
object = 5
reference_check = 6
wrong_access_to_archive = 7
OTHERS = 8.
LOOP AT lt_text INTO DATA(ls_text).
IF ls_text-tdformat = '*'.
APPEND l_S TO t_table.
CLEAR l_s.
ENDIF.
CONCATENATE l_s ls_text-tdline INTO l_s SEPARATED BY ' '.
IF l_s IS INITIAL.
APPEND l_s TO t_table.
ENDIF.
AT LAST.
APPEND l_s TO t_table.
ENDAT.
ENDLOOP.
DATA: l_out TYPE string_t.
LOOP AT t_table INTO l_S.
from_string( EXPORTING i_string = CONV string( l_s )
i_maxlen = i_maxlen
i_halign = i_halign
IMPORTING t_out = l_out ).
APPEND LINES OF l_out TO t_out.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
END-OF-SELECTION.
DATA: t_out TYPE string_t.
DATA(l_zini) = NEW zini_text_wrap( ).
l_zini->FROM_read_text( EXPORTING i_NAME = 'GASTOS20240000005227EG_INF_NFOBSER'
i_maxlen = 80
i_halign = 'J'
IMPORTING t_out = t_out ).
loop at t_out into data(l_out).
if l_out is not INITIAL.
write / l_out.
else.
write / '<--- [ENTER]'.
endif.
endloop.