Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[ABAP] Validate JSON data

While searching on how to validate a given JSON string, I found two options. The first simply returns a Boolean value, the second also returns information about what could be wrong.

DATA(lv_json) = '{'
             &&      '"employee": {'
             &&           |"name" : "Max", |
             &&           |"age"  : 43,    |
             &&      '}'
             && '}'.


" option 1:
DATA(is_valid) = /ui5/cl_json_util=>is_wellformed( lv_json ).

" option 2: 
DATA(lo_reader) = cl_sxml_string_reader=>create( cl_abap_codepage=>convert_to( lv_json ) ).
TRY.
    lo_reader->next_node( ).
    lo_reader->skip_node( ).
  CATCH cx_sxml_parse_error INTO DATA(lx_parse_error).
    WRITE lx_parse_error->get_text( ).
ENDTRY.

[ABAP] Generate a QR-Code

There are many blogs describing how to create a QR-Code in the context of SAPscript or Smartforms (e.g. here, here, here and here). But I was looking for a way to generate a QR-Code and only receive the graphical data stream from it, without the need for any manual steps such a creation via SE73. During my search, I found these two notes, which contained all the information I needed:

  • 2790500: mentions the class CL_RSTX_BARCODE_RENDERER with method QR_CODE
  • 2030263: describes the parameters for a QC-Code creation

Following my little test report:

PARAMETERS p_text TYPE string DEFAULT 'My QR-Code Content'.

DATA: lv_action   TYPE i.
DATA: lv_filename TYPE string.
DATA: lv_fullpath TYPE string.
DATA: lv_path     TYPE string.

TRY.
    cl_rstx_barcode_renderer=>qr_code( EXPORTING i_module_size      = 25                " Size of smallest module (in pixel, max: 32000)
                                                 i_barcode_text     = p_text            " Barcode text
*                                                 i_mode             = 'A'               " Mode ('N', 'A', 'L', 'B', 'K', 'U', '1', '2'; note 2030263)
*                                                 i_error_correction = 'H'               " Error correction ('L', 'M', 'Q', 'H')
*                                                 i_rotation         = 0                 " Rotation (0, 90, 180 or 270)
                                       IMPORTING e_bitmap           = DATA(e_bitmap) ). " Bitmap in BMP format

    DATA(lt_raw_data) = cl_bcs_convert=>xstring_to_solix( e_bitmap ).

    " Save-Dialog
    cl_gui_frontend_services=>file_save_dialog( EXPORTING default_file_name = 'QR-Code'
                                                          default_extension = 'bmp'
                                                          file_filter       = |{ cl_gui_frontend_services=>filetype_all }|
                                                CHANGING  filename          = lv_filename
                                                          path              = lv_path
                                                          fullpath          = lv_fullpath
                                                          user_action       = lv_action ).

    IF lv_action EQ cl_gui_frontend_services=>action_ok.

      " Download file to disk
      cl_gui_frontend_services=>gui_download( EXPORTING filename     = lv_fullpath
                                                        filetype     = 'BIN'
                                                        bin_filesize = xstrlen( e_bitmap )
                                              CHANGING  data_tab     = lt_raw_data ).
    ENDIF.

  CATCH cx_rstx_barcode_renderer INTO DATA(lo_exp).
    MESSAGE lo_exp->get_text( ) TYPE 'I'.
ENDTRY.

[Terminal] Bash script to add leading season and episode numbers by parsing from file names

I had some videos in the format “My Episode #01.mkv” which I wanted to rename to “S01E01 My Episode #01.mkv“. This little script did the job for me:

# Specify the directory containing the files. For current directory use: $(dirname "$0")
directory="/path/to/your/directory"	

# Loop through all .mkv files in the directory
for file in "$directory"/*.{webm,mkv}; do
    # Check if the file exists to avoid errors when no files match
    [ -e "$file" ] || continue
    
    # Extract the base filename (without the directory path)
    filename=$(basename "$file")

    # Use regex to find the episode number (e.g., #01, #02)
    if [[ $filename =~ \#([0-9]+) ]]; then
        episode_number=${BASH_REMATCH[1]}

        # Pad the episode number with a leading zero if it's a single digit
        if [ ${#episode_number} -eq 1 ]; then
            episode_number="0$episode_number"
        fi  
		
        # Construct the new filename
        new_filename="S01E${episode_number} $filename"
        
        # Rename the file
        mv "$file" "$directory/$new_filename"
        echo "Renamed: $filename -> $new_filename"
    fi
done