midrange.com code scratchpad |
Name:
Using memcpy to compress blanks out of an array
|
Scriptlanguage:
Plain Text
|
Tabwidth:
4
|
Date:
04/27/2011 01:31:20 am
|
IP:
Logged
|
|
Description:
This program will compress out the blanks from a simple array. It uses memcpy() to shift entries around.
This is intended as a demonstration of concept, and lacks practical application as a standalone entity.
|
Code:
- H OPTION(*NOSHOWCPY:*NOEXPDDS:*NODEBUGIO:*SRCSTMT)
- H DATFMT(*ISO) TIMFMT(*ISO)
- H BndDir('QC2LE')
- H DFTACTGRP(*NO)
-
- D ARRMEMCPY PR ExtPgm('ARRMEMCPY')
-
- D ARRMEMCPY PI
-
-
- D MemCpy PR * ExtProc('__memcpy')
- D ToAddr * value
- D FromAddr * value
- D copyLen 10I 0 value
-
-
- D compressArray PR ExtProc('compressArray')
-
- D MAX_DIM C 6
-
- D line S 30 DIM(6) Inz(*Blanks)
-
-
- /Free
-
- line(2) = ' data 1' ;
- line(5) = ' data here' ;
- line(6) = ' f=data 2' ;
- compressArray() ;
-
- *INLR = *On ;
-
- /End-free
-
-
- P compressArray B
- D compressArray PI
-
-
- D pSrc S *
- D pTgt S *
- D len S 10I 0
- D i S 3U 0
- D j S 3U 0
- D skipped S 3U 0 Inz(*Zero)
- D nextEnt S 3U 0
-
- /Free
-
- i = 1 ;
- nextEnt = MAX_DIM + 1 ;
- DoU i = *Zero or i >= nextEnt ; // Essentially forever.
- i = %Lookup(*Blanks: line: i) ;
- If i > *Zero and i < nextEnt ; // We found a target
- skipped = *Zero ;
- pTgt = %Addr(line(i)) ; // We'll copy TO here...
- For j = i+1 to nextEnt - 1 ; // Find our source (FROM) addr
- skipped += 1 ; // Number skipped (blank) lines
- If line(j) <> *Blank ; // We found our source
- pSrc = %Addr(line(j)) ; // Set our FROM addreess
- // Length is number of elements
- // from j to end of array *
- // length of each element
- len = %Size(LINE) * (nextEnt - j) ;
- memcpy(pTgt: pSrc: len) ;
- nextEnt -= skipped ; // Last possible nonblank + 1
-
- // Now we need to overlay blank lines at the end
- // Number of lines to clear is the value in SKIPPED.)
-
- line(nextEnt) = *Blanks ;
- If skipped > 1 ;
- pSrc = %Addr(line(nextEnt)) ; // Blank line
- pTgt = pSrc + %Size(line) ; // Next entry
- len = %Size(line) * (skipped - 1) ;
- memcpy(pTgt: pSrc: len) ;
- EndIF ;
- Leave ; // Out of inner FOR loop
- EndIF ; // Found non-blank after blank
- EndFOR ; // Find next blank
- If j >= nextEnt ; // No more non-blank entries
- Leave ; // We're done!
- EndIF ;
- EndIF ; // End: found a target
- EndDO ;
- Return ;
-
- /End-free
-
- P compressArray E
-
|
|
|