home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / vidhandl / extraia.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-03  |  7.2 KB  |  299 lines

  1. //Copyright (c) By Márcio Afonso Arimura Fialho
  2. //Freeware version, may be distributed freely
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <conio.h>
  7.  
  8. typedef unsigned int word;
  9.  
  10. void putmstr (int n, char *str, FILE *stream)
  11. //saves n characters from a string into a stream
  12.  {
  13.     int c0;
  14.     for(c0=0;c0<n;c0++)
  15.      {
  16.         fputc(*str,stream);
  17.         str++;
  18.      }
  19.  }
  20.  
  21. #include "readname.cpp"
  22.     //readfname defined in readname.cpp appends a extension to a filename if
  23.     //none is given
  24.  
  25. void main (int n, char *ent[2])
  26.  {
  27.     FILE *source,*target; //source file is the archive containing target files
  28.     char sourcepath[132];
  29.     char targetpath[132];
  30.     char *path;
  31.     char *opentag="\x0d\x0a//<FILE=\""; //file opening tag
  32.     char *closetag="\x0d\x0a//</FILE>"; //file closing tag
  33.     int  tagpos=5; //last position where both tags are equal
  34.     int  key;
  35.     int  c0; //counters
  36.     word nlevel; //nesting level
  37.     word sf; //subcontrol
  38.     char c;
  39.     if (n<2) //about the program
  40.      {
  41.         printf ("EXTRAIA 2.0b - Text file extractor for C/C++ sources - Freeware version\n\n");
  42.         printf ("Usage:\tEXTRAIA filename[.SRC] [/B] \twhere:\n\tfilename\tsource archive pathname (.SRC extension optional)\n");
  43.         printf ("\t/B\t\textracts files from archives with binary file tags\n\n");
  44.         printf ("For further help read EXTRAIA.TXT\n\n");
  45.         printf ("By Márcio A. A. Fialho\nhttp://pessoal.iconet.com.br/jlfialho\n");
  46.         printf ("E-mail: jlfialho@iconet.com.br OR jlfialho@yahoo.com");
  47.         return;
  48.      }
  49.     c0=0;
  50.  
  51.     if (*ent[2]=='/') //verifies if it is the /B switch
  52.      {
  53.         ent[2]++;
  54.         if (*ent[2]=='B' || *ent[2]=='b') //instructs the program to read file tags in binary mode
  55.          {
  56.             opentag="\x0d\x0a//\x01"; //replaces file tags by binary ones
  57.             closetag="\x0d\x0a//\x02";
  58.             printf ("Reading archive in binary format\n");
  59.             //if tagpos==4 then archive file is istructed to read binary tags
  60.             tagpos=4;
  61.          }
  62.         else
  63.          {
  64.             ent[2]--;
  65.             printf ("Unknow option %s. ",ent[2]);
  66.             goto jmp1; //display error message
  67.          }
  68.      }
  69.  
  70.     readfname(sourcepath,ent[1],".SRC");
  71.         //reads filename and appends .SRC extension if none is found
  72.  
  73.     source=fopen (sourcepath,"rb");
  74.     if(source==NULL) //source file could not be opened
  75.      {
  76.         printf ("ERROR: FILE \"%s\" DOESN'T EXIST OR COULDN'T BE READ.\n",sourcepath);
  77.       jmp1:
  78.         puts ("Type EXTRAIA with no parameters to obtain help.\a");
  79.         fcloseall();
  80.         return;
  81.      }
  82. //resets program control vars
  83.     nlevel=0;
  84.     c0=0;
  85.     sf=0;
  86.     while (!feof(source)) //program main loop, reads source file
  87.      {
  88.       volta:
  89.         c=(char)fgetc (source);
  90.         if (nlevel==0) //reading the archive control data and comments
  91.          {
  92.             //sf==0; searches for the file open tag
  93.             //sf==1; reads file name and open it if possible
  94.             if (sf==0)
  95.              {
  96.               jmp0_1:
  97.                 if(opentag[c0]==c) //searches for the file open tag //<FILE="????????.SRC"> or //
  98.                  {
  99.                     c0++;
  100.                     if (opentag[c0]==0) //if found then reads file name and open it if possible
  101.                      {
  102.                         sf=1;
  103.                         path=targetpath;
  104.                      }
  105.                  }
  106.                  else if (c0) //if not, try again
  107.                  {
  108.                     c0=0;
  109.                     goto jmp0_1;
  110.                  }
  111.              }
  112.             else if (sf==1)
  113.              {
  114.                 c0=0;
  115.                 do //reads stored file name
  116.                  {
  117.                     *path=c;
  118.                     path++;
  119.                     c0++;
  120.                     if (c0>128)
  121.                         printf ("WARNING: filename %s too big. It has been truncated\n",targetpath);
  122.                     c=(char)fgetc (source);
  123.                  }
  124.                  while (c!='"' && c!=0x0d && c!=0x20 && c!=0x09 && c0<=128);
  125.                     // " or (NL) or (SPACE BAR) or <TAB> is required only if filename doesn't exceed 128 characters
  126.                 *path=0; //puts a null terminator in filename end
  127.  
  128.              //opens target file and verifies if it can be written
  129.                 puts (targetpath); //writes the name of the file that is being extracted
  130.                 target=fopen (targetpath,"rb");
  131.                 key='Y';
  132.                 if (target!=NULL)
  133.                  {
  134.                     printf ("File %s already exist. Overwrite (y/n)?",targetpath);
  135.                     do
  136.                      {
  137.                         key=getche();
  138.                         if (key>0x60 && key<0x7b) //upcase keyboard input
  139.                             key-=0x20;
  140.                         if (key!='N' && key!='Y')
  141.                             printf ("\b\a");
  142.                      }
  143.                      while (key!='Y' && key !='N');
  144.                     puts("");
  145.                  }
  146.                 if (key=='N')
  147.                     goto jmp0_2;
  148.                 if (key=='Y')
  149.                  {
  150.                     fclose(target);
  151.                     target=fopen (targetpath,"wb");
  152.                     if (target==NULL)
  153.                      {
  154.                         printf ("File %s couldn't be written. ");
  155.                       jmp0_2:
  156.                         printf ("Skipping file %s\n",targetpath);
  157.                         fclose (target);
  158.                         nlevel=0; c0=0; sf=0;
  159.                      }
  160.                      else
  161.                      {
  162.                         if (tagpos==5) //waits for '>' to end comments only if not instructed to read binary tags (reading ascii tags only)
  163.                             while (c!='>')//searches for the end of file opentag
  164.                                 c=(char)fgetc(source);
  165.                         while (c!=0x0A)//searches for a newline
  166.                             c=(char)fgetc(source);
  167.                         nlevel=1; c0=0; sf=0;//starts extracting stored file
  168.                      }
  169.                  }
  170.              }
  171.          }
  172.  
  173.         else if (nlevel) //writes into target file
  174.          {
  175.             //if sf==0 searching for file open or close tag
  176.             //if sf==1 verifies if tag is opentag or closetag
  177.             //if sf==2 searching for file open tag
  178.             //if sf==3 searching for file close tag
  179.  
  180.           jmp1_0:
  181.             if (sf==0)
  182.              {
  183.                 if (c0 && c!=opentag[c0]) //if not found opentag completely
  184.                  {
  185.                     if (nlevel==1)
  186.                         putmstr(c0, opentag, target);
  187.                     c0=0; //resets c0
  188.                  }
  189.  
  190.                 if (opentag[c0]==c) //searches for file open tag or close tag
  191.                  {
  192.                     c0++;
  193.                     if (c0==tagpos) //tagpos==5 in ascii mode or 4 in binary mode
  194.                         sf=1;
  195.                  }
  196.              }
  197.             else if (sf==1)
  198.              {
  199.                 if (tagpos==4) //if EXTRAIA is reading archive in binary mode (with binary tags)
  200.                  {
  201.                     if (c==0x01) //file open tag
  202.                      {
  203.                         nlevel++; sf=0; c0=0;
  204.                         if (nlevel==2)
  205.                          {
  206.                             putmstr(5,opentag, target);
  207.                             goto volta;
  208.                          }
  209.                      }
  210.                     else if (c==0x02) //file close tag
  211.                      {
  212.                         nlevel--; sf=0; c0=0;
  213.                         if (!nlevel)
  214.                          {
  215.                             fclose (target);
  216.                             goto volta;
  217.                          }
  218.                      }
  219.                     else
  220.                      {
  221.                         if (nlevel==1)
  222.                             putmstr(c0, opentag,target);
  223.                         c0=0; sf=0;
  224.                      }
  225.                  }
  226.                  else //if EXTRAIA is reading archive in ascii mode (with normal ASCII tags)
  227.                  {
  228.                     if (c=='F')
  229.                      {
  230.                         sf=2;
  231.                         c0++;
  232.                         if (nlevel==1)
  233.                             putmstr (c0, opentag, target);
  234.                      }
  235.                     else if (c=='/')
  236.                      {
  237.                         sf=3;
  238.                         c0++;
  239.                      }
  240.                     else
  241.                      {
  242.                         if (nlevel==1)
  243.                             putmstr(c0, opentag, target);
  244.                         c0=0;    sf=0;
  245.                      }
  246.                  }
  247.              }
  248.             else if (sf==2)
  249.              {
  250.                 if (opentag[c0]==c)
  251.                  {
  252.                     c0++;
  253.                     if (opentag[c0]==0)
  254.                      {
  255.                         nlevel++; c0=0; sf=0;
  256.                      }
  257.                     if (nlevel==1)
  258.                         fputc(c,target);
  259.                  }
  260.                  else
  261.                  {
  262.                     c0=0; sf=0;
  263.                     goto jmp1_0;
  264.                  }
  265.              }
  266.             else if (sf==3)
  267.              {
  268.                 if (closetag[c0]==c)
  269.                  {
  270.                     c0++;
  271.                     if (closetag[c0]==0)
  272.                      {
  273.                         nlevel--; c0=0; sf=0;
  274.                         if (!nlevel)
  275.                          {
  276.                             fclose (target);
  277.                             goto volta;
  278.                          }
  279.                      }
  280.                  }
  281.                  else
  282.                  {
  283.                     if (nlevel==1)
  284.                         putmstr(c0, closetag, target);
  285.                     sf=0; c0=0;
  286.                     goto jmp1_0;
  287.                  }
  288.              }
  289.             if (!c0 || nlevel>1)
  290.                 fputc(c,target);
  291.          }
  292.      }
  293.     fcloseall ();
  294.  }
  295.  
  296. //by Marcio A. A. Fialho
  297. //http: pessoal.iconet.com.br/jlfialho
  298. //E-mail : jlfialho@iconet.com.br (main e-mail) OR
  299. //    (alternate e-mail) jlfialho@yahoo.com