home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / REPSTR.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  5KB  |  185 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /***************************************************************************
  4. * @(#)repstr
  5. * @(#)- Replace a pattern in a large amount of data using a copy function
  6. *
  7. ***************************************************************************
  8. *@(#)1993 Erik Bachmann
  9. *
  10. * Released to public domain 27-Oct-95
  11. ***************************************************************************/
  12.  
  13. /*
  14. #define     TEST
  15. */
  16. /*
  17. #define     DEBUG
  18. */
  19.  
  20. #include    <stdio.h>
  21. #include    <string.h>                          /* memset, strlen, memmove */
  22. #include    "bacstd.h"                          /* filesize, strnsub       */
  23.  
  24. #if   defined DEBUG
  25. #     define      BUFFERSIZE  80
  26. #     define      BLOKSIZE          10
  27. #else
  28. #     define      BUFFERSIZE  2000
  29. #     define      BLOKSIZE           250
  30. #endif
  31.  
  32. #if   defined     TEST
  33. #     define      MAXFIELDS   20
  34. #endif
  35.  
  36. /*
  37.  /-------------------------------------\
  38. |        REPSTR                         |------------------------------------|
  39. |\-------------------------------------/
  40. |
  41. | Replace a pattern in a large amount of data using a copy function
  42. |
  43. |
  44. |----------------------------------------------------------------------------|
  45. | CALL:
  46. |    repstr( fpInput, fpOutput, pszCharTable ) ;
  47.  
  48. |
  49. | HEADER:
  50. |    stdio.h
  51. |    string.h
  52. |    bacstd.h
  53. |
  54. | GLOBALE VARIABLES:
  55. |    %
  56. |
  57. | ARGUMENTS:
  58. |    %
  59. |
  60. | PROTOTYPE:
  61. |    int _CfnTYPE repstr(FILE *fpIn, FILE *fpOut, char *PatternTable[][2]) ;
  62. |
  63. | RETURN VALUE:
  64. |    int           No of replacements
  65. |
  66. | MODULE:
  67. |    repstr.c
  68. |----------------------------------------------------------------------------|
  69. |
  70. |
  71. |----------------------------------------------------------------------------|
  72. |1993-06-19/Erik Bachmann
  73. \---------------------------------------------------------------------------|*/
  74.  
  75. int _CfnTYPE repstr(FILE *fpIn, FILE *fpOut, char *PatternTable[][2])
  76. {
  77.       char  szBuffer[BUFFERSIZE+1] ;
  78.       char  szTmpBuffer[BUFFERSIZE+1] ;
  79.  
  80.       long  lFileLength ;
  81.       long  lFilePos = 0 ;
  82.  
  83.       int   i = 0 ;
  84.       int   iStrLength ;
  85.  
  86.       /*----------------------------------------*/
  87.  
  88.       memset(szBuffer, '\0', BUFFERSIZE) ;
  89.  
  90.       iStrLength = fread(szBuffer, BLOKSIZE, 1, fpIn); /* Read first block */
  91.  
  92.       lFilePos = BLOKSIZE ;
  93.  
  94.       iStrLength = fread(&szBuffer[BLOKSIZE], BLOKSIZE, 1, fpIn) ;
  95.  
  96.       lFilePos += BLOKSIZE ;
  97.  
  98.       while( 0 != iStrLength )
  99.       {     /* WHILE able to read */
  100.             i = 0 ;
  101.  
  102.             lFilePos += BLOKSIZE ;
  103.  
  104.             while ( NULL != PatternTable[i][0][0] )
  105.             {     /* For all patterns */
  106.                   while (0 != strnsub(szBuffer, PatternTable[i][0],
  107.                                       PatternTable[i][1], BUFFERSIZE))
  108.                   {
  109.                         ;     /* If found replace */
  110.                   }
  111.                   i++ ;
  112.             }     /* Replace pattern with replacement */
  113.  
  114.             fwrite(&szBuffer, strlen(szBuffer) - BLOKSIZE, 1, fpOut) ;
  115.  
  116.             /* Write szBuffer execept last block */
  117.  
  118.             fflush(fpOut) ;
  119.  
  120.             memmove(&szBuffer, &szBuffer[strlen(szBuffer) - BLOKSIZE],
  121.                     BLOKSIZE+1) ;
  122.  
  123.             memset(&szBuffer[BLOKSIZE], '\0', BLOKSIZE) ;
  124.  
  125.             iStrLength = fread(&szBuffer[BLOKSIZE], BLOKSIZE, 1, fpIn) ;
  126.  
  127.             szBuffer[BLOKSIZE + BLOKSIZE] = '\0' ;
  128.  
  129.       }
  130.  
  131.  
  132.       i = 0 ;
  133.  
  134.       while (PatternTable[i][0][0] != NULL)
  135.       {     /* For all patterns */
  136.             while (0 != strnsub(szBuffer, PatternTable[i][0],
  137.                                 PatternTable[i][1], BUFFERSIZE))
  138.             {
  139.                   ;
  140.             }
  141.             i++ ;
  142.       }
  143.  
  144.       /* Replace pattern with replacement */
  145.  
  146.       fwrite(&szBuffer, strlen(szBuffer), 1, fpOut) ;
  147.  
  148.       /* Write buffer */
  149.  
  150.       lFilePos += strlen(&szBuffer[BLOKSIZE]) ;
  151.  
  152.       return(0) ;
  153. }
  154.  
  155.  
  156. #if defined TEST
  157.  
  158. int main()
  159. {
  160.       char        *pszTegnTabel[MAXFIELDS][2] = {
  161.                         {"XXX", "YYYY"},
  162.                         {"ZZZ", "A"},
  163.                         {"",""}
  164.                         } ;
  165.  
  166.       FILE        *fp1,
  167.                         *fp2 ;
  168.  
  169.       /*--------------------------------*/
  170.  
  171.       fp1 = fopen("test1", "rt") ;
  172.       fp2 = fopen("test2", "wt") ;
  173.  
  174.       if ( (fp1 != NULL) && (fp2 != NULL) )
  175.  
  176.             repstr(fp1, fp2, pszTegnTabel) ;
  177.  
  178.       fclose(fp1) ;
  179.       fclose(fp2) ;
  180.  
  181.       return(0) ;
  182. }
  183.  
  184. #endif
  185.