home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database / CLIPR502.DOS / SOURCE / SAMPLE / STUFF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-15  |  2.3 KB  |  126 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.  
  15. /***
  16. *
  17. *    bcopy()
  18. *
  19. *    shift 'count' bytes of memory
  20. *
  21. */
  22.  
  23. HIDE void bcopy(BYTEP to, BYTEP from, USHORT count)
  24.  
  25. {
  26.     while ( count-- )
  27.         *to++ = *from++;
  28. }
  29.  
  30.  
  31.  
  32.  
  33. /***
  34. *
  35. *  StuffC()
  36. *
  37. *  Remove characters from a string and insert characters
  38. *    from a second string (C callable).
  39. *
  40. *    StuffC( str, len, pos, del, iStr, iLen )
  41. *
  42. *    Remove 'del' characters from 'str' starting at 'pos',
  43. *    insert all characters from 'iStr' in their place.  'len'
  44. *    is the logical length of 'str' and 'iLen' is the logical
  45. *    length of 'iStr'.  The lengths need not be the same and
  46. *    either can be zero.
  47. *
  48. */
  49.  
  50. HIDE void StuffC( BYTEP str, USHORT len, USHORT pos, USHORT del,
  51.                        BYTEP iStr, USHORT iLen )
  52.  
  53. {
  54.     long oLen;
  55.     BYTEP sResult;
  56.    HANDLE hResult;
  57.  
  58.     /* convert origin */
  59.     if ( pos > 0 )
  60.         pos--;
  61.  
  62.     /* limit params */
  63.     if ( pos > len )
  64.         pos = len;
  65.  
  66.     if ( del > len - pos )
  67.         del = len - pos;
  68.  
  69.     /* use long to verify size without overflow */
  70.     oLen = (long)len + (long)iLen - (long)del;
  71.     if ( oLen > 0L && oLen < 65500L )
  72.     {
  73.         /* allocate workspace from virtual memory */
  74.       hResult = _xvalloc( (USHORT)oLen + 1, 0 );
  75.       sResult = _xvlock ( hResult );
  76.  
  77.         /* build the sResult string */
  78.         bcopy( sResult, str, pos );
  79.         bcopy( &sResult[pos], iStr, iLen );
  80.         bcopy( &sResult[pos + iLen], &str[pos + del], len - (pos + del) );
  81.         sResult[oLen] = NIL;
  82.  
  83.         /* return string to Clipper */
  84.         _retclen(sResult, (USHORT)oLen);
  85.       _xvunlock( hResult );
  86.       _xvfree( hResult );
  87.     }
  88.     else
  89.         _retc("");
  90. }
  91.  
  92.  
  93.  
  94.  
  95. /***
  96. *
  97. *    STUFF()
  98. *
  99. *    Replace 'del' number of characters in 'str' starting at 'pos'
  100. *        with the entire 'iStr' string.
  101. *
  102. *    result = STUFF(str, pos, del, iStr)
  103. *
  104. *        result      -   character string.       
  105. *        str         -   character string.
  106. *        pos         -   numeric.
  107. *        del         -   numeric.
  108. *        iStr        -   character string.
  109. *
  110. */
  111.  
  112. CLIPPER STUFF(void)
  113.  
  114. {
  115.     if (PCOUNT == 4 && ISCHAR(1) &&
  116.         ISNUM(2) && ISNUM(3) && ISCHAR(4) )
  117.     {
  118.         StuffC(_parc(1), _parclen(1), _parni(2),
  119.                _parni(3), _parc(4), _parclen(4));
  120.     }
  121.     else
  122.         _retc("");
  123. }
  124.  
  125.  
  126.