midrange.com code scratchpad
Name:
Memory processing routines
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
12/01/2011 12:58:39 am
IP:
Logged
Description:
Here is a sample module which uses the PRCMEM routines, showing how you would use them 'next to' the equivalent RPG BIF. This sample only shows some of the basic stuff.....
Code:
  1.      H DEBUG(*YES)
  2.       *=====================================================================
  3.      D ThisProgram     C                   'PRCMEMTSTR'
  4.       *---------------------------------------------------------------------
  5.       /COPY QRPGLECPY,PRCMEM_P                   ‚Memory processing
  6.      D memptr          S               *
  7.      D memlen          S             10I 0
  8.      D newptr          S               *
  9.      D datalen         S             10I 0
  10.      D pos             S             10I 0
  11.      D data            S            500A
  12.      D newdata500      S            500A
  13.      D newdata50       S             50A
  14.      D newdata1000     S           1000A
  15.       *---------------------------------------------------------------------
  16.      D main            PR                  Extpgm(ThisProgram)
  17.      D main            PI
  18.       *=====================================================================
  19.       *‚MAINLINE
  20.       *=====================================================================
  21.       /free
  22.  
  23.         //‚Allocate the memory
  24.  
  25.         memlen = 500;
  26.         memptr = %alloc( memlen );
  27.         if memptr = *null;
  28.           exsr *pssr;
  29.         endif;
  30.  
  31.         //‚Initialize (clear) the variable/memory
  32.  
  33.         clear data;
  34.  
  35.         minit( memptr : memlen );
  36.  
  37.         //‚Initialize to a repeated single-character value
  38.  
  39.         data = *all'X';
  40.  
  41.         minit( memptr : memlen : 'X' );
  42.  
  43.         //‚Set all of the memory to a certain value
  44.  
  45.         data = *all'ABCDEFG';
  46.  
  47.         minit( memptr : memlen : 'ABCDEFG' );
  48.  
  49.         //‚Change part of the memory
  50.  
  51.         %subst( data : 20 : 10 ) = '1234567890';
  52.  
  53.         mset( memptr : memlen : '1234567890' : 20 );
  54.  
  55.         //‚Change part of the memory (with trailing blanks)
  56.  
  57.         %subst( data : 50 : 12 ) = '1234567890';
  58.  
  59.         mset( memptr : memlen : '1234567890' : 50 : 12 );
  60.  
  61.         //‚Retrieve a substring
  62.  
  63.         newdata50 = %subst( data : 30 : 50 );
  64.  
  65.         mcopy( memptr : memlen : %addr( newdata50 ) : 50 : 30 );
  66.  
  67.         //‚Get pointer to copy of memory
  68.  
  69.         newptr = mdup( memptr : memlen );
  70.  
  71.         dealloc(en) newptr;
  72.  
  73.         //‚Copy memory into same-sized variable
  74.  
  75.         mcopy( memptr : memlen : %addr( newdata500 ) );
  76.  
  77.         //‚Copy memory into smaller variable
  78.  
  79.         mcopy( memptr : memlen : %addr( newdata50 ) : 50 );
  80.  
  81.         //‚Copy memory into larger variable
  82.  
  83.         mcopy( memptr : memlen : %addr( newdata1000 ) : 1000 );
  84.  
  85.         //‚Scan for a string in memory and replace it (same length)
  86.  
  87.         pos = %scan( '123' : data );
  88.         dow pos <> 0;
  89.           data = %replace( '***' : data : pos );
  90.           pos = %scan( '123' : data );
  91.         enddo;
  92.  
  93.         pos = mscan( '123' : memptr : memlen );
  94.         dow pos <> 0;
  95.           datalen = mreplace( '***' : memptr : memlen : pos );
  96.           pos = mscan( '123' : memptr : memlen );
  97.         enddo;
  98.  
  99.         //‚Scan for a string in memory and replace it (smaller length)
  100.  
  101.         pos = %scan( '456' : data );
  102.         dow pos <> 0;
  103.           data = %replace( '#' : data : pos : 3 );
  104.           pos = %scan( '456' : data );
  105.         enddo;
  106.  
  107.         pos = mscan( '456' : memptr : memlen );
  108.         dow pos <> 0;
  109.           datalen = mreplace( '#' : memptr : memlen : pos : 3 );
  110.           pos = mscan( '456' : memptr : memlen );
  111.         enddo;
  112.  
  113.         //‚Scan for a string in memory and replace it (larger length)
  114.  
  115.         pos = %scan( '789' : data );
  116.         dow pos <> 0;
  117.           data = %replace( '&&&&&' : data : pos : 3 );
  118.           pos = %scan( '789' : data );
  119.         enddo;
  120.  
  121.         pos = mscan( '789' : memptr : memlen );
  122.         dow pos <> 0;
  123.           datalen = mreplace( '&&&&&' : memptr : memlen : pos : 3 );
  124.           pos = mscan( '789' : memptr : memlen );
  125.         enddo;
  126.  
  127.         //‚Scan for a string in memory and prefix it (zero length)
  128.  
  129.         pos = %scan( '0' : data );
  130.         if pos <> 0;
  131.           data = %replace( 'xxxxx' : data : pos : 0 );
  132.         endif;
  133.  
  134.         pos = mscan( '0' : memptr : memlen );
  135.         if pos <> 0;
  136.           datalen = mreplace( 'xxxxx' : memptr : memlen : pos : 0 );
  137.         endif;
  138.  
  139.         //‚Deallocate the memory and exit
  140.  
  141.         dealloc(en) memptr;
  142.  
  143.         return;
  144.  
  145.         begsr *pssr;
  146.           return;
  147.         endsr;
  148.  
  149.       /end-free
  150.  
  151.  
© 2004-2019 by midrange.com generated in 0.006s valid xhtml & css