midrange.com code scratchpad
Name:
Remove Comma-separated-variable blanks
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
06/04/2015 07:56:59 pm
IP:
Logged
Description:
A COBOL utility to remove the extraneous space between quote delimiters. However currently. the record delimiter characters at the end of each line are being changed from a <CRLF> to a <CR>.
Code:
  1.       *PARMS BNDDIR(QC2LE)
  2.        process nomonoprc apost xref
  3.        Identification Division.
  4.        program-id.   RMVCSVBC.
  5.       *author.       Michael Quigley.
  6.       *installation. The Way International.
  7.       *date-written. 02/12/2015.
  8.  
  9.       * NOTICE:  All computer programs,  data bases and other work
  10.       *          product used in the compilation of this work,  as
  11.       *          well as the work itself,  are the property of The
  12.       *          Way International, publication or copying without
  13.       *          prior written permission prohibited.   All rights
  14.       *          reserved.
  15.  
  16.       *-------------------------------------------------------------
  17.       *
  18.       *    Purpose:    This program reads a CSV stream file from the IFS
  19.       *                and removes single blanks from any field values.
  20.       *
  21.       *    Notes:  The largest file this routine can handle is around
  22.       *            16 MB. Modifying the code can enable it to handle
  23.       *            much larger files. If opening and reading a file
  24.       *            larger than 2 GB is needed. o_LargeFile will need
  25.       *            to be set.
  26.       *            Additional logic will be also be needed to loop
  27.       *            through the larger file processing chunks of the file
  28.       *            rather than processing the entire file at one time.
  29.       *
  30.       *-------------------------------------------------------------
  31.       *    Modifications:
  32.       *
  33.  
  34.  
  35.        Environment Division.
  36.        Configuration section.
  37.        special-names.
  38.            .
  39.  
  40.       *
  41.        Data Division.
  42.  
  43.       *
  44.        Working-storage section.
  45.  
  46.       *-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  47.       *   Working fields
  48.       *
  49.  
  50.        01  Full-path-name              pic x(2048).
  51.  
  52.       * The following are the largest a COBOL element can be.
  53.        01  File-data-in                pic x(16711568).
  54.        01  File-data-out               like File-data-in.
  55.  
  56.        01  In-pointer                  pic 9(9)    binary.
  57.        01  Out-pointer                 like In-pointer.
  58.        01  Work-pointer                like In-pointer.
  59.        01  Move-length                 like In-pointer.
  60.  
  61.       * Define a constant for the Quote blank quote
  62.        01  Quote-blank-quote           constant x'7F407F'.
  63.  
  64.       *--------------------------------------------
  65.       * Links to other routines APIs
  66.  
  67.       *-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  68.       * System APIs
  69.       * - - - - - - - - - - - - - - - - - - - - - -
  70.  
  71.       *-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  72.       * Links to ILE C library and other C routines
  73.       * - - - - - - - - - - - - - - - - - - - - - -
  74.       * Standard UNIX error handling
  75.        01  Get-errno                   constant        '__errno'.
  76.        01  Errno-pointer               pointer.
  77.  
  78.       *--------------------------------------------
  79.       * Stream File APIs
  80.  
  81.       * - - - - - - - - - - - - - - - - - - - - - -
  82.       * File information (statistics?)
  83.        01  Get-file-information        constant        'stat'.
  84.  
  85.        01  Information-buffer.
  86.            05  Mode-permissions        pic 9(9)    binary.
  87.            05  Internal-file-number    pic 9(9)    binary.
  88.            05  Number-of-links         pic 9(4)    binary.
  89.            05  Reserved-2              pic 9(4)    binary.
  90.            05  Owner-user-id           pic 9(9)    binary.
  91.            05  Group-id                pic 9(9)    binary.
  92.            05  Entry-size              pic s9(9)   binary.
  93.            05  Last-time-accessed      pic s9(9)   binary.
  94.            05  Last-time-changed       pic s9(9)   binary.
  95.            05  Last-status-change      pic s9(9)   binary.
  96.            05  File-system-device      pic s9(9)   binary.
  97.            05  Entry-block-size        pic 9(9)    binary.
  98.            05  Entry-bytes-allocated   pic 9(9)    binary.
  99.            05  Entry-object-type       pic x(11).
  100.            05  Reserved-3              pic x.
  101.            05  Code-page               pic 9(4)    binary.
  102.            05  Entry-CCSID             pic 9(4)    binary.
  103.            05  Device-id               pic 9(9)    binary.
  104.            05  Number-of-links-long    pic 9(9)    binary.
  105.            05  Device-id-long          pic 9(18)   binary.
  106.            05  File-system-id          pic 9(18)   binary.
  107.            05  Mounted-file-system-id  pic 9(9)    binary.
  108.            05  Reserved-1              pic x(32).
  109.            05  Internal-generation-id  pic 9(9)    binary.
  110.  
  111.       * - - - - - - - - - - - - - - - - - - - - - -
  112.       * IFS manipulation APIs
  113.        01  Open-file                   constant        'open'.
  114.        01  Close-stream-file           constant        'close'.
  115.        01  Read-file-data              constant        'read'.
  116.        01  Position-in-file            constant        'lseek'.
  117.        01  Write-file-data             constant        'write'.
  118.        01  File-truncate               constant        'ftruncate'.
  119.  
  120.       * - - - - - - - - - - - - - - - - - - - - - -
  121.       * General arguments
  122.        01  Bytes-to-process            pic s9(9)   binary.
  123.        01  Return-value                pic s9(9)   binary.
  124.            88  Processed-okay                          value 0.
  125.            88  Process-error                           value -1.
  126.  
  127.       * - - - - - - - - - - - - - - - - - - - - - -
  128.       * Open specific arguments
  129.        01  File-descriptor             pic s9(9)   binary.
  130.        01  Oflag                       pic s9(9)   binary.
  131.  
  132.       * o_Flag values (access control)
  133.        01  o_RdWr                      constant        4.
  134.        01  o_Trunc                     constant        64.
  135.        01  o_TextData                  constant        16777216.
  136.       *01  o_LargeFile                 constant        536870912.
  137.  
  138.       * - - - - - - - - - - - - - - - - - - - - - -
  139.       * Position in stream file            lseek()
  140.        01  Seek-set                    constant        0.
  141.        01  Offset                      pic s9(9)   binary.
  142.        01  Whence                      like Offset.
  143.  
  144.       *--------------------------------------------
  145.        Linkage section.
  146.        01  Parm-path.
  147.            05  Parm-path-length        pic s9(4)   binary.
  148.            05  Parm-path-data          pic x(1408).
  149.  
  150.       * - - - - - - - - - - - - - - - - - - - - - -
  151.       * UNIX-type API error handling
  152.        01  Error-number                pic 9(9)    binary.
  153.  
  154.        Procedure Division
  155.                using   Parm-path
  156.            .
  157.  
  158.        0000-Main.
  159.            perform 0010-Initialize
  160.  
  161.            perform 1000-Process-file
  162.  
  163.            perform 0050-Close-stream-file
  164.  
  165.            goback
  166.            .
  167.  
  168.       *
  169.        0010-Initialize.
  170.            move Parm-path-data(1:Parm-path-length)
  171.                                to  Full-path-name
  172.            move x'00' to Full-path-name(Parm-path-length + 1:1)
  173.            perform 0020-Get-entry-information
  174.            perform 0030-Open-stream-file
  175.            .
  176.  
  177.        0020-Get-entry-information.
  178.            call procedure Get-file-information
  179.                    using       Full-path-name
  180.                                Information-buffer
  181.                    returning   Return-value
  182.            end-call
  183.  
  184.            if Process-error
  185.                perform 7000-Get-errno
  186.                display 'Error number ', Error-number, ' encountered.'
  187.                move zero to Entry-size
  188.            end-if
  189.            .
  190.  
  191.        0030-Open-stream-file.
  192.            compute Oflag
  193.                =   o_RdWr
  194.                +   o_TextData
  195.            end-compute
  196.  
  197.            call procedure Open-file
  198.                    using
  199.                        by reference    Full-path-name
  200.                        by value        Oflag
  201.                    returning   File-descriptor
  202.            end-call
  203.            if File-descriptor < 0
  204.                perform 7000-Get-errno
  205.                display 'File open error ', Error-number
  206.                goback
  207.            end-if
  208.            .
  209.  
  210.        0050-Close-stream-file.
  211.            call procedure Close-stream-file
  212.                    using
  213.                        by value        File-descriptor
  214.                    returning Return-value
  215.            end-call
  216.  
  217.            if Process-error
  218.                perform 7000-Get-errno
  219.                display 'File close error', Error-number
  220.                goback
  221.            end-if
  222.            .
  223.  
  224.        1000-Process-file.
  225.            perform 1010-Read-file-data
  226.            move 1  to  In-pointer
  227.                        Out-pointer
  228.            move spaces to File-data-out
  229.            perform 2100-Scan-file
  230.       * The file has been completely scanned. See if the last
  231.       *    data has been processed.
  232.            if Work-pointer + 3 <= Entry-size
  233.                perform 2000-Remove-blanks
  234.                        until In-pointer + 2 > Entry-size
  235.            end-if
  236.            if In-pointer < Work-pointer
  237.            and Work-pointer <= Entry-size
  238.                perform 2200-Move-file-data
  239.            end-if
  240.  
  241.            perform 1020-Write-file-data
  242.  
  243.            perform 1030-Truncate-file
  244.            .
  245.  
  246.        1010-Read-file-data.
  247.            call procedure Read-file-data
  248.                    using
  249.                        by value        File-descriptor
  250.                        by reference    File-data-in
  251.                        by value        Entry-size
  252.                    returning   Return-value
  253.            end-call
  254.  
  255.            if Process-error
  256.                perform 7000-Get-errno
  257.                display 'Error reading file: ', Error-number
  258.                goback
  259.            end-if
  260.            .
  261.  
  262.        1020-Write-file-data.
  263.            move zero       to Offset
  264.            move Seek-set   to Whence
  265.            call procedure Position-in-file
  266.                    using
  267.                        by value        File-descriptor
  268.                                        Offset
  269.                                        Whence
  270.                    returning   Return-value
  271.            end-call
  272.  
  273.            if Process-error
  274.                perform 7000-Get-errno
  275.               display 'Error positioning file for write: ', Error-number
  276.                goback
  277.            end-if
  278.  
  279.       * Out-pointer is one past the last byte moved.
  280.       * So reduce the bytes to write by 1
  281.            compute Bytes-to-process
  282.                =   Out-pointer
  283.                -   1
  284.            end-compute
  285.            call procedure Write-file-data
  286.                    using
  287.                        by value        File-descriptor
  288.                        by reference    File-data-out
  289.                        by value        Bytes-to-process
  290.                    returning   Return-value
  291.            end-call
  292.  
  293.            if Process-error
  294.                display 'Error writing file: ', Error-number
  295.                goback
  296.            end-if
  297.            .
  298.  
  299.        1030-Truncate-file.
  300.            call procedure File-truncate
  301.                    using
  302.                        by value        File-descriptor
  303.                                        Bytes-to-process
  304.                    returning   Return-value
  305.            end-call
  306.  
  307.            if Process-error
  308.                display 'Error truncating file: ', Error-number
  309.                goback
  310.            end-if
  311.            .
  312.  
  313.        2000-Remove-blanks.
  314.            perform 2200-Move-file-data
  315.            if Work-pointer < Entry-size
  316.                compute In-pointer
  317.                    =   Work-pointer
  318.                    +   2
  319.                end-compute
  320.                perform 2100-Scan-file
  321.            end-if
  322.            .
  323.  
  324.        2100-Scan-file.
  325.            perform
  326.                    varying Work-pointer
  327.                    from In-pointer by 1
  328.                    until   File-data-in(Work-pointer:3) = '" "'
  329.                    or      Work-pointer  + 2 > Entry-size
  330.                continue
  331.            end-perform
  332.            .
  333.  
  334.        2200-Move-file-data.
  335.            If Work-pointer > Entry-size
  336.                compute Move-length
  337.                    =   Entry-size
  338.                    -   In-pointer
  339.                    +   1
  340.                end-compute
  341.            else
  342.                compute Move-length
  343.                    =   Work-pointer
  344.                    -   In-pointer
  345.                    +   1
  346.                end-compute
  347.            end-if
  348.            move File-data-in(In-pointer:Move-length)
  349.                    to File-data-out(Out-pointer:Move-length)
  350.  
  351.            compute Out-pointer
  352.                =   Out-pointer
  353.                +   Move-length
  354.            end-compute
  355.            .
  356.  
  357.       *--------------------------------------------
  358.       * Unix Errno handling
  359.        7000-Get-errno.
  360.            call procedure Get-errno
  361.                    returning   Errno-pointer
  362.            end-call
  363.  
  364.            set address of Error-number to Errno-pointer
  365.            . 
© 2004-2019 by midrange.com generated in 0.007s valid xhtml & css