home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / xbase / library / clipper / memory / corrupt1 / insstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-06  |  4.2 KB  |  90 lines

  1. /***************************************************************************/
  2. /* InsertStr function, replaces Clipper version of insert string that is   */
  3. /* included in this zip file.                                              */
  4. /* The primary advantages of this method are:                              */
  5. /*      Speed: runs over twice as fast than the clipper version,           */
  6. /*          faster on larger strings.                                      */
  7. /*      Works: as noted in the attached documentation, the Clipper         */
  8. /*          version corrupts memory, this version doesn't.                 */
  9. /***************************************************************************/
  10. /*                                                                         */
  11. /*  Author:                                                                */
  12. /*  Brian Connelly                                                         */
  13. /*  CIS 70673, 1652                                                        */
  14. /***************************************************************************/
  15. /*                                                                         */
  16. /*  Requires:                                                              */
  17. /*  Clipper 5.01 {Summer '87, add '#include <nandef.h>' at top}            */
  18. /*  MSC 5.1 or greater.                                                    */
  19. /***************************************************************************/
  20. /*                                                                         */
  21. /*  To compile:                                                            */
  22. /*  This version was compiled w/ MSC 6.0a, command line:                   */
  23. /*      cl  /AL /W4 /DMSC /Zp1 /Otclaz /FPa /Gsh insstr.c                  */
  24. /*  all other versions compile the same, except no :                       */
  25. /*      /Ocz    /Gh                                                        */
  26. /*  The /Gh switch is very important to MSC 6.0a, it is an UNDOCUMENTED    */
  27. /*      switch that compiles 5.1 link compatible code. This is needed      */
  28. /*      to link into clipper programs.                                     */
  29. /***************************************************************************/
  30. /*                                                                         */
  31. /*  Clipper Usage:                                                         */
  32. /*  cSample := "abc"                                                       */
  33. /*  lRc := insertStr(cSample, "Z", 2)                                      */
  34. /*  ? lRc, cSample                                                         */
  35. /*  Output:                                                                */
  36. /*  .T. aZc                                                                */
  37. /*  Note:                                                                  */
  38. /*  the string being inserted into must already have enough space          */
  39. /*  allocated for the insertion. This is confirmed in the function.        */
  40. /***************************************************************************/
  41.  
  42.  
  43. #include <extend.h>
  44.  
  45.  
  46. CLIPPER insertstr( void /* char *cBuffer, char *cNewStr, int iPos */ )
  47. {
  48.  
  49.     unsigned short iBufferLen, iInsertLen, iPos, i, j;
  50.     char *cBuffer, *cNewStr;
  51.  
  52.  
  53.     // check parameters
  54.     if ( (PCOUNT != 3) || !ISBYREF(1) || !ISCHAR(1) || !ISCHAR(2) || !ISNUM(3) ) {
  55.         _retl(0);   // return .F.
  56.         return;
  57.     }
  58.  
  59.     // verify a valid iPos value
  60.     if ( (iPos = (unsigned short) _parnl(3)) < 1) {
  61.         _retl(0);   // return .F.
  62.         return;
  63.     }
  64.  
  65.     // copy to local vars
  66.     iBufferLen = _parclen(1);
  67.     cBuffer    = _parc(1);
  68.     iInsertLen = _parclen(2);
  69.     cNewStr    = _parc(2);
  70.  
  71.     // check for insert past end of string
  72.     if (iPos >= iBufferLen) {
  73.         _retl(0);   // return .F.
  74.         return;
  75.     }
  76.  
  77.     // begin processing
  78.  
  79.     // truncate insert that would cause overflow
  80.     if ( (iInsertLen + iPos - 1) > iBufferLen )
  81.         iInsertLen -= ( (iInsertLen + iPos) - iBufferLen + 1);
  82.  
  83.     // actual update
  84.     for(i = --iPos, j = 0; j < iInsertLen ; i++, j++)
  85.         cBuffer[i] = cNewStr[j];
  86.  
  87.     _retl(1);       // return .T.
  88.     return;
  89. }
  90.