home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database / CLIPR503.W96 / STUFF._ / STUFF.
Text File  |  1995-06-20  |  3KB  |  148 lines

  1. /***
  2. *
  3. *  Stuff.c
  4. *
  5. *  Clipper STUFF() function
  6. *
  7. *  Copyright (c) 1990-1993, Computer Associates International, Inc.
  8. *  All rights reserved.
  9. *
  10. */
  11.  
  12. #include "Extend.api"
  13. #include "Vm.api"
  14. #include "error.api"
  15. #include "error.ch"
  16.  
  17. #define E_SWAPMEM         5300
  18.  
  19. /***
  20. *
  21. *   bcopy()
  22. *
  23. *   shift 'count' bytes of memory
  24. *
  25. */
  26.  
  27. HIDE void bcopy(BYTEP to, BYTEP from, USHORT count)
  28.  
  29. {
  30.    while ( count-- )
  31.       *to++ = *from++;
  32. }
  33.  
  34.  
  35.  
  36.  
  37. /***
  38. *
  39. *  StuffC()
  40. *
  41. *  Remove characters from a string and insert characters
  42. *   from a second string (C callable).
  43. *
  44. *   StuffC( str, len, pos, del, iStr, iLen )
  45. *
  46. *   Remove 'del' characters from 'str' starting at 'pos',
  47. *   insert all characters from 'iStr' in their place.  'len'
  48. *   is the logical length of 'str' and 'iLen' is the logical
  49. *   length of 'iStr'.  The lengths need not be the same and
  50. *   either can be zero.
  51. *
  52. *  If a VM segment can not be allocated, it will generate an error.
  53. *
  54. */
  55.  
  56. HIDE void StuffC( BYTEP str, USHORT len, USHORT pos, USHORT del,
  57.                   BYTEP iStr, USHORT iLen )
  58.  
  59. {
  60.    long oLen;
  61.    BYTEP sResult;
  62.    HANDLE hResult;
  63.    ERRORP  pError;
  64.  
  65.    pError = _errNew();
  66.    /* convert origin */
  67.    if ( pos > 0 )
  68.       pos--;
  69.  
  70.    /* limit params */
  71.    if ( pos > len )
  72.       pos = len;
  73.  
  74.    if ( del > len - pos )
  75.       del = len - pos;
  76.  
  77.    /* use long to verify size without overflow */
  78.    oLen = (long)len + (long)iLen - (long)del;
  79.    if ( oLen > 0L && oLen < 65500L )
  80.    {
  81.       /* allocate workspace from virtual memory */
  82.       if ( ( ( hResult = _xvalloc( (USHORT)oLen + 1, 0 ) ) != NULL ) &&
  83.          ( ( sResult = _xvlock( hResult ) ) != NULL ) )
  84.         {
  85.            /* build the sResult string */
  86.            bcopy( sResult, str, pos );
  87.            bcopy( &sResult[pos], iStr, iLen );
  88.            bcopy( &sResult[pos + iLen], &str[pos + del], len - (pos + del) );
  89.            sResult[oLen] = NIL;
  90.  
  91.            /* return string to Clipper */
  92.           _retclen(sResult, (USHORT)oLen);
  93.           _xvunlock( hResult );
  94.           _xvfree( hResult );
  95.          }
  96.       else
  97.          {
  98.            _errPutGenCode( pError, EG_MEM );
  99.            _errPutSubCode( pError, E_SWAPMEM );
  100.            _errPutSubSystem( pError, "BASE" );
  101.            _errPutFlags( pError, EF_CANDEFAULT );
  102.            _errPutSeverity( pError, ES_ERROR );
  103.  
  104.            _errLaunch( pError );
  105.  
  106.          }
  107.    }
  108.    else
  109.       _retc("");
  110.  
  111.    _errRelease( pError );
  112. }
  113.  
  114.  
  115.  
  116.  
  117. /***
  118. *
  119. *   STUFF()
  120. *
  121. *   Replace 'del' number of characters in 'str' starting at 'pos'
  122. *      with the entire 'iStr' string.
  123. *
  124. *   result = STUFF(str, pos, del, iStr)
  125. *
  126. *      result      -   character string.
  127. *      str         -   character string.
  128. *      pos         -   numeric.
  129. *      del         -   numeric.
  130. *      iStr        -   character string.
  131. *
  132. */
  133.  
  134. CLIPPER STUFF(void)
  135.  
  136. {
  137.    if (PCOUNT == 4 && ISCHAR(1) &&
  138.       ISNUM(2) && ISNUM(3) && ISCHAR(4) )
  139.    {
  140.       StuffC(_parc(1), _parclen(1), _parni(2),
  141.             _parni(3), _parc(4), _parclen(4));
  142.    }
  143.    else
  144.       _retc("");
  145. }
  146.  
  147.  
  148.