home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************************/
- /* InsertStr function, replaces Clipper version of insert string that is */
- /* included in this zip file. */
- /* The primary advantages of this method are: */
- /* Speed: runs over twice as fast than the clipper version, */
- /* faster on larger strings. */
- /* Works: as noted in the attached documentation, the Clipper */
- /* version corrupts memory, this version doesn't. */
- /***************************************************************************/
- /* */
- /* Author: */
- /* Brian Connelly */
- /* CIS 70673, 1652 */
- /***************************************************************************/
- /* */
- /* Requires: */
- /* Clipper 5.01 {Summer '87, add '#include <nandef.h>' at top} */
- /* MSC 5.1 or greater. */
- /***************************************************************************/
- /* */
- /* To compile: */
- /* This version was compiled w/ MSC 6.0a, command line: */
- /* cl /AL /W4 /DMSC /Zp1 /Otclaz /FPa /Gsh insstr.c */
- /* all other versions compile the same, except no : */
- /* /Ocz /Gh */
- /* The /Gh switch is very important to MSC 6.0a, it is an UNDOCUMENTED */
- /* switch that compiles 5.1 link compatible code. This is needed */
- /* to link into clipper programs. */
- /***************************************************************************/
- /* */
- /* Clipper Usage: */
- /* cSample := "abc" */
- /* lRc := insertStr(cSample, "Z", 2) */
- /* ? lRc, cSample */
- /* Output: */
- /* .T. aZc */
- /* Note: */
- /* the string being inserted into must already have enough space */
- /* allocated for the insertion. This is confirmed in the function. */
- /***************************************************************************/
-
-
- #include <extend.h>
-
-
- CLIPPER insertstr( void /* char *cBuffer, char *cNewStr, int iPos */ )
- {
-
- unsigned short iBufferLen, iInsertLen, iPos, i, j;
- char *cBuffer, *cNewStr;
-
-
- // check parameters
- if ( (PCOUNT != 3) || !ISBYREF(1) || !ISCHAR(1) || !ISCHAR(2) || !ISNUM(3) ) {
- _retl(0); // return .F.
- return;
- }
-
- // verify a valid iPos value
- if ( (iPos = (unsigned short) _parnl(3)) < 1) {
- _retl(0); // return .F.
- return;
- }
-
- // copy to local vars
- iBufferLen = _parclen(1);
- cBuffer = _parc(1);
- iInsertLen = _parclen(2);
- cNewStr = _parc(2);
-
- // check for insert past end of string
- if (iPos >= iBufferLen) {
- _retl(0); // return .F.
- return;
- }
-
- // begin processing
-
- // truncate insert that would cause overflow
- if ( (iInsertLen + iPos - 1) > iBufferLen )
- iInsertLen -= ( (iInsertLen + iPos) - iBufferLen + 1);
-
- // actual update
- for(i = --iPos, j = 0; j < iInsertLen ; i++, j++)
- cBuffer[i] = cNewStr[j];
-
- _retl(1); // return .T.
- return;
- }
-