home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_01 / 3n01028a < prev    next >
Text File  |  1991-11-26  |  2KB  |  73 lines

  1.  
  2. /*** Listing #2
  3. */
  4. #include <string.h>
  5.  
  6. /*** iDBCCharRplc - Replaces a character in a string.
  7. *
  8. *   written by: John G. Nelson, Pacific Software Publishing Inc.
  9. *   copyright:  Pacific Software Publishing Inc.
  10. *   date:       10/91
  11. *   purpose:    Allows a single (DBC or sbc) character in a DBC
  12. *               string to be replaced by another (DBC or sbc) character.
  13. *               The replacement only takes place if there is
  14. *               sufficient room in the string.
  15. *   parameters: pdbcString : Pointer to DBC string.
  16. *               iOffset    : Byte position of character to be replaced.
  17. *               pdbcChar   : Incoming character.
  18. *               iMaxLen    : Length of pdbcField in bytes.
  19. *   return:     iOffset    : Replacement does not have enough room.
  20. *               iNewOffset : Offset indicating position of character
  21. *                            immediately following newly replaced character.
  22. */
  23. int iDBCRplcChar(
  24.     DBC *pdbcString,/*  DBC string */
  25.     int iOffset,    /*  Offset     */
  26.     DBC *pdbcChar,  /*  DBC        */
  27.     int iMaxLen)    /*  Max bytes  */
  28.     {
  29.     int  iInChSz, iOutChSz;
  30.     int  iRoom;     /* bytes slots */
  31.     int  iStrLen;
  32.     int  i;
  33.  
  34.     iStrLen  = strlen(pdbcString);
  35.     iRoom    = iMaxLen - iStrLen;
  36.     iInChSz  = (bIsDBC(pdbcChar)) ? 2 : 1;
  37.     iOutChSz = (bIsDBC(&pdbcString[iOffset])) ? 2 : 1;
  38.  
  39.     /* check if there is enough room for replacement*/
  40.     if ((iInChSz - iOutChSz) > iRoom)  {
  41.         /* not room to accomodate replacement       */
  42.         return(iOffset);
  43.     }
  44.  
  45.     /* enough room for replacement                  */
  46.  
  47.     if (-1 == (iInChSz - iOutChSz))  {
  48.  
  49.         /* DBC by SBC replacement case              */
  50.         /* shift left to delete 2nd byte of OutDBC  */
  51.         memmove(&pdbcString[iOffset+1],
  52.                 &pdbcString[iOffset+2],
  53.                 iStrLen-iOffset);
  54.  
  55.     } else if (1 == (iInChSz-iOutChSz))  {
  56.  
  57.         /* SBC by DBC replacement case              */
  58.         /* shift right to make room for 2nd byte    */
  59.         memmove(&pdbcString[iOffset+2],
  60.                 &pdbcString[iOffset+1],
  61.                 iStrLen-iOffset);
  62.     }
  63.  
  64.     /* replace character                            */
  65.     for (i=0; i<iInChSz; i++)  {
  66.         pdbcString[iOffset+i] = pdbcChar[i];
  67.     }
  68.  
  69.     return (iOffset+iInChSz);
  70.  
  71. } /* iDBCCharRplc */
  72.  
  73.