home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name stcmid -- Replace characters in the middle of a string.
- *
- * Synopsis amov = stcmid(ptarget,psource,cpos,cnt,tarsize)
- *
- * int amov Actual number of characters replaced
- * char *ptarget Pointer to string whose characters
- * are to be replaced.
- * char *psource Pointer to source string
- * int cpos Position within ptarget where replace-
- * ment begins
- * int cnt Number of characters of psource to
- * move into ptarget
- * int tarsize Maximum length of the target string
- *
- * Description STCMID replaces cnt characters of ptarget with the
- * characters of psource starting at character position
- * cpos (the first character is at position 0). If cpos is
- * greater than the length of ptarget, the characters of
- * psource are concatenated to ptarget. If cnt is less
- * than the length of psource, the leftmost characters of
- * psource are used; if cnt is greater than the length of
- * psource, the remaining positions are filled with blanks.
- * In any case, no more characters are transferred than the
- * maximum length of ptarget permits.
- *
- * Returns amov The number of characters extracted
- * ptarget The altered string
- *
- * Version 3.0 (C)Copyright Blaise Computing Inc. 1983, 1984, 1986
- *
- **/
-
- #include <string.h>
-
- #include <bstring.h>
-
- #define BLANK ' '
-
- int stcmid(ptarget,psource,cpos,cnt,tarsize)
- register char *ptarget,*psource;
- int cpos,cnt,tarsize;
- {
- register int i;
- int npos,n,tlen,len;
-
- /* First set up reasonable values for the number of characters and */
- /* the position within ptarget. */
-
- tlen = (int) strlen(ptarget);
- npos = min(max(0,cpos),tlen);
- n = min(cnt,tarsize - npos);
- len = (int) strlen(psource);
-
- ptarget += npos;
- for (i = 1; i <= n; i++) /* Copy the string */
- if (i > len)
- *ptarget++ = BLANK;
- else
- *ptarget++ = *psource++;
-
- if ((npos + n) >= tlen)
- *ptarget = '\0'; /* String extension occurred. */
-
- return(i - 1);
- }