home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / amiga / filereplace.cpp < prev    next >
C/C++ Source or Header  |  1996-05-27  |  4KB  |  225 lines

  1. #include <fstream.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAXBUF 4095
  5.  
  6. void copy(ifstream& infil,ostream& utfil)
  7.     {
  8.     char    ch;
  9.     while (utfil && infil.get(ch))
  10.         {
  11.         utfil.put(ch);
  12.         }
  13.     }
  14.  
  15.  
  16. int replace(ifstream& infil,ofstream& utfil,char* str,char *nystr)
  17.     {
  18.     char    buff[MAXBUF];
  19.     int        pos,ant;
  20.     pos=0;
  21.     ant=0;
  22.     while (utfil && infil.get(buff[pos]))
  23.         {
  24.         while(buff[pos]==str[pos]&&pos<strlen(str) )
  25.             {
  26.             infil.get(buff[++pos]);
  27.             }
  28.  
  29.         if(pos==strlen(str))
  30.             {
  31.             utfil.write(nystr,strlen(nystr));
  32.             utfil.put(buff[pos]);                                    // the missing char
  33.             ant++;
  34.             }
  35.         else
  36.             {
  37.             utfil.write(buff,pos+1);
  38.             }
  39.         pos=0;
  40.         }
  41.     if (ant>0)
  42.         {
  43.         cout<<"Found "<<ant<<" apperece";
  44.         if (ant>1)
  45.             cout<<"s";
  46.         cout<<".\n";
  47.         }
  48.     else
  49.         {
  50.         cout<<"Found no match\n";
  51.         }
  52.     return(ant);
  53.     }
  54.  
  55. void PrepareStr(char *str,char *outstr)
  56.     {
  57.     char    ch;
  58.     int     I,U;
  59.     I=U=0;
  60.     while((ch=str[I++])!=0)
  61.         {
  62.         if (ch=='\\')                    //check special char
  63.             {
  64.             if((ch=str[I++])==0)
  65.                 {                              // END of Str
  66.                 outstr[U++]='\\';        
  67.                 break;
  68.                 }
  69.             else
  70.                 {
  71.                 if (ch=='n')                        // Found /n
  72.                     {
  73.                     outstr[U++]='\n';
  74.                     }
  75.                 else
  76.                     {
  77.                     outstr[U++]='\\';  // fix it if not special char
  78.                     outstr[U++]=ch;
  79.                     }
  80.                 }
  81.             }
  82.         else
  83.             {
  84.             outstr[U++]=ch;
  85.             }
  86.         }
  87.     outstr[U++]=0;    
  88.     }
  89.  
  90. int filereplace(char *file1, char *file2,char *str1,char *str2)
  91.     {
  92.     char    str[MAXBUF];
  93.     char    *filename;
  94.     int rc = 0;                                              // Returvärde från programmet..
  95.     int    ant;
  96.     ifstream    infil;
  97.     ofstream    utfil;
  98.     
  99.     if(strlen(file2)==0)
  100.         {                                                                            // if no destfile then make a tmpfile.
  101.         filename="t:FileReplace.tmp";
  102.         }
  103.     else
  104.         {
  105.         filename=file2;
  106.         }
  107.  
  108.     infil.open(file1);                                        // öppna fil....
  109.     utfil.open(filename);                                        // Destfil
  110.     if (infil && utfil)
  111.         {
  112.         cout<<file1<<" ";
  113.         PrepareStr(str1,str);
  114.         ant=replace(infil,utfil,str,str2);         // Skriv ut...
  115.         infil.close();                                              // Stäng....
  116.         utfil.close();
  117.         
  118.         if(strlen(file2)==0&&ant!=0)
  119.             {                                                                            // if no destfile then make a tmpfile.
  120.             infil.open(filename);                                        // öppna fil....
  121.             utfil.open(file1,ios::trunc);                                        // Destfil
  122.             if (infil && utfil)
  123.                 {
  124.                 copy(infil,utfil);
  125.                 }
  126.             if (infil)
  127.                 infil.close();                                              // Stäng....
  128.             if (utfil)
  129.                 utfil.close();
  130.         }
  131.     }
  132.     else
  133.         {
  134.         if (!infil)
  135.             {
  136.             cout << "Kunde inte öppna " <<  file1 << endl; 
  137.             }
  138.         else
  139.             {
  140.             cout << "Kunde inte öppna " <<  filename << endl;
  141.             }
  142.         if (infil)
  143.             infil.close();                                              // Stäng....
  144.         if (utfil)
  145.             utfil.close();
  146.  
  147.         rc = 1;
  148.         }
  149.     return rc;
  150.     }
  151.  
  152. fnuttparser(ifstream& infil,char *str)
  153.     {
  154.     char    ch;
  155.     int    I;
  156.     infil.get(ch);
  157.     while(ch!='"'&& !infil.eof())                // parsa till ett "
  158.         infil.get(ch);
  159.     I=0;
  160.     infil.get(ch);                                            // fill str with the string
  161.     while(ch!='"'&&I<MAXBUF && !infil.eof())
  162.         {
  163.         str[I++]=ch;                                            // Spara tecken till nesta "
  164.         infil.get(ch);
  165.         }
  166.     str[I]=0;                                                        //och en Null terminator
  167.     }
  168.  
  169.  
  170. int datafil(char *file)
  171.     {
  172.     int rc;
  173.     char    arg1[MAXBUF],arg2[MAXBUF],arg3[MAXBUF],arg4[MAXBUF];
  174.     ifstream    infil;
  175.     rc=0;
  176.     
  177.     infil.open(file);                                        // öppna fil....
  178.     if (infil)
  179.         {
  180.         while(!infil.eof())
  181.             {
  182.             fnuttparser(infil,arg1);
  183.             fnuttparser(infil,arg2);
  184.             fnuttparser(infil,arg3);
  185.             fnuttparser(infil,arg4);
  186.             if(!infil.eof())
  187.                 rc=filereplace(arg1,arg2,arg3,arg4);
  188.             }
  189.             
  190.         }
  191.     else
  192.         cout<<"Error cant open datafil :"<<file<<"\n";
  193.     return(rc);
  194.     }
  195.  
  196.  
  197.  
  198. main(int argc,char *argv[])
  199. {
  200.     int    rc;
  201.     rc=0;
  202.     if (!(argc==2||argc==5))
  203.         {
  204.         cout<<"Error wrong type of arguments.\n";
  205.         cout<<argv[0]<<" [infile] [destfile] [findstring] [Replacesting]\n";
  206.         cout<<"or\n";
  207.         cout<<argv[0]<<" [datafile]\n";
  208.         cout<<"Were datafile    is a file with a series of "<<'"'<<"strings"<<'"'<<" that are grouped\n";    
  209.         cout<<"into 4:s.The data is formed in the same order as the 4 comand line.\n";
  210.         cout<<"In findstring you could write \\n to locate returns.\n";
  211.         cout<<"If destfile ìs "" than infile will be overwritten.\n";
  212.         cout<<"This Program is written by StefanZ as a help programs when he ported mesa to amiga.\n";
  213.         return(0);
  214.         }
  215.     if (argc==5)
  216.         {
  217.         rc=filereplace(argv[1],argv[2],argv[3],argv[4]);
  218.         }
  219.     if (argc==2)
  220.         {
  221.         rc=datafil(argv[1]);
  222.  
  223.         }
  224.     return(rc);
  225. }