home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 04 / chkdupe.cpp < prev    next >
Text File  |  1992-02-05  |  9KB  |  347 lines

  1. // chkdupe.cpp
  2.  
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6.  
  7. #pragma hdrstop
  8. #include"finddupe.h"
  9. #include"chkdupe.h"
  10. #include"msgbox.h"
  11.  
  12. extern unsigned _stklen = 8192;
  13.  
  14. ChkDupe::ChkDupe(void)
  15.     {
  16.     if(GetSwitchArg('?'))
  17.         terminate();
  18.  
  19.         // create settings object and initialize
  20.     Settings.SetName("SHOWDUPE.INI");
  21.  
  22.     for(int i = 0; i < NUMSETTINGS; i++)
  23.         Settings.AddLine(MySettings[i].name,MySettings[i].value);
  24.  
  25.     // open .INI file here and override settings
  26.     Settings.Read();
  27.  
  28.     if(!FilesExist())
  29.         if(GetArgCount() == 0)
  30.             {
  31.             char searchdirs[200];
  32.             if(!strlen(Settings.GetLine(MySettings[SEARCHDIRS].name,
  33.                     searchdirs)))
  34.                 terminate();
  35.             }
  36.  
  37.     InitApp();   
  38.     
  39.     ShowDupes();
  40.     }
  41.  
  42. static char *colorstrings[16] = 
  43.     {   
  44.     "BLACK", "BLUE", "GREEN", "CYAN", 
  45.     "RED", "MAGENTA", "BROWN", "LIGHTGRAY",
  46.     "DARKGRAY", "LIGHTBLUE", "LIGHTGREEN", "LIGHTCYAN", 
  47.     "LIGHTRED", "LIGHTMAGENTA", "YELLOW", "WHITE" 
  48.     };
  49.  
  50. BYTE GetColor(char *color)
  51.     {
  52.     for(int i = 0; i < 16; i++)
  53.         if(!stricmp(color,colorstrings[i]))
  54.             return i;
  55.     return 0xff;
  56.     }
  57.  
  58.  
  59.  
  60. BOOL ChkDupe::FilesExist(void)
  61.     {
  62.     FindFile f1("DUPES.IDX",ALL_FILES);
  63.     if(f1.Found())
  64.         {
  65.         FindFile f2("DUPES.DAT",ALL_FILES);
  66.         if(f2.Found())
  67.             return TRUE;
  68.         }
  69.     return FALSE;
  70.     }
  71.  
  72. void ChkDupe::ShowDupes(void)
  73.     {       
  74.     screen.Cursor(SCR_CURSOR_OFF);
  75.     screen.AtSay(0,0,ShowDupe);
  76.     screen.AtSay(screen.Lines()-1,0,
  77.         "Move <Sp>Tag <Ret>View <Del>Delete <F7>Purge "
  78.         "<F9>Build <F10>Compare  <Esc>Exit");
  79.  
  80.     pathW = new Window("Path:",MakePosition(2,2,4,76));
  81.     pathW->Open();
  82.  
  83.     DWORD position = MakePosition(6,2,(screen.Lines() > 25 ? 42 : 18),
  84. #if defined(DEBUG)
  85. 60);
  86. #else
  87. 46);
  88. #endif
  89.  
  90.     Status = new Window("Status",MakePosition(6,53,(screen.Lines() > 25 ? 42 : 18),
  91.         25));
  92.     Status->Open();
  93.  
  94.     if(!FilesExist())
  95.         {
  96.         if(!FindDuplicates())
  97.             return;
  98.         }
  99.     else if(GetArgCount())      // files exist but command-line spec
  100.         {
  101.         MsgBoxPrompt m("Command-Line Arguments Specified...","Re-build duplicate file tables? (Y/N)");
  102.         if(m.Yes())
  103.             FindDuplicates();
  104.         }
  105.  
  106.     char *title = "   File        Date      Time       Size        ";
  107.     myScroller = new MyScroller(pathW,title,position);
  108.  
  109.     myScroller->Init((screen.Lines() > 25 ? 39 : 15)*2,50);
  110.  
  111.     myScroller->SetColors(
  112.         MakeBG(colors[SCROLL_CBG])+colors[SCROLL_CFG],
  113.         MakeBG(colors[SCROLL_SCBG])+colors[SCROLL_SCFG],
  114.         MakeBG(colors[SCROLL_SNBG])+colors[SCROLL_SNFG],
  115.         MakeBG(colors[SCROLL_NBG])+colors[SCROLL_NFG],
  116.         MakeBG(colors[SCROLL_DNBG])+colors[SCROLL_DNFG],
  117.         MakeBG(colors[SCROLL_DCBG])+colors[SCROLL_DCFG]);
  118.  
  119.     myScroller->Open();
  120.  
  121.     while(ScreenMgr(keyboard.Get()));
  122.  
  123. //    screen.Cursor(SCR_CURSOR_ON);
  124.     delete myScroller;
  125.  
  126.     delete Status;
  127.     delete pathW;
  128.     }
  129.  
  130.  
  131. BOOL ChkDupe::FindDuplicates(void)
  132.     {
  133.     char oldtitle[50];
  134.     pathW->GetTitle(oldtitle);
  135.  
  136.     PopWin pop("Finding Duplicates...",MakePosition(10,20,5,26));
  137.     pop.Open();
  138.     pop.AtSay(1,0,"   Please stand by.");
  139.     FindDupe finddupe(Status, pathW);
  140.     char searchdirs[200];
  141.     BOOL retval = finddupe.FindDuplicates(
  142.         Settings.GetLine(MySettings[SEARCHDIRS].name,searchdirs));
  143.     pop.Close();
  144.     pathW->SetTitle(oldtitle);
  145.  
  146.     if(!retval && finddupe.numFound == 0)
  147.         {
  148.         MsgBox("Finding Duplicates","No duplicates found");
  149.         return (FilesExist() ? TRUE : FALSE);
  150.         }
  151.  
  152.     return retval;
  153.     }
  154.  
  155. BOOL ChkDupe::ScreenMgr(int key)
  156.     {
  157.     switch(key)
  158.         {
  159.         case KEY_SPACE:             // select or de-select line
  160.             myScroller->Select();
  161.             break;
  162.  
  163.         case KEY_DEL:               // delete all selected entries and re-display
  164.             myScroller->Delete();
  165.             break;
  166.  
  167.         case KEY_ENTER:             // call file viewer to view this file
  168.             {
  169.             char *args[3];
  170.             char progname[80];
  171.             char filename[80];
  172.  
  173.             if(myScroller->IsDeleted())
  174.                 break;
  175.             args[0] = Settings.GetLine(MySettings[VIEWER].name,progname);
  176.             if(!args[0])
  177.                 args[0] = MySettings[VIEWER].value;
  178.             args[1] = myScroller->CurFileName(filename);
  179.             args[2] = NULL;
  180.             screen.Exec(args[0],args);
  181.             }
  182.             break;
  183.  
  184.         case KEY_ESC:
  185.             return FALSE;
  186.  
  187.         case KEY_F7:
  188.             {
  189.             PopWin pop("Purging Files...",MakePosition(10,20,5,26));
  190.             pop.Open();
  191.             pop.AtSay(1,0,"   Please stand by.");
  192.             myScroller->Purge();
  193.             pop.Close();
  194.             myScroller->Paint();
  195.             }
  196.             break;
  197.  
  198.         case KEY_F9:
  199.             if(GetArgCount() == 0)
  200.                 {
  201.                 char searchdirs[200];
  202.                 if(!strlen(Settings.GetLine(MySettings[SEARCHDIRS].name,searchdirs)))
  203.                     {
  204.                     MsgBox("Unable to find duplicates","No search directories specified");
  205.                     break;
  206.                     }       
  207.                 }
  208.             myScroller->CloseFiles();
  209.             if(!FindDuplicates())
  210.                 return FALSE;
  211.             myScroller->ReInit();   // reopen files, reinitialize scroller
  212.             break;
  213.  
  214.         case KEY_F10:
  215.             char *comptitle = "File Compare";
  216.             if(myScroller->CompareSetup())
  217.                 {
  218.                 PopWin pop("Comparing Files...",MakePosition(10,20,5,26));
  219.                 pop.Open();
  220.                 pop.AtSay(1,0,"   Please stand by.");
  221.                 if(myScroller->FileCompare())
  222.                     MsgBox(comptitle,"Files are Identical!");
  223.                 else
  224.                     MsgBox(comptitle,"Files differ!");
  225.                 pop.Close();
  226.                 Status->Puts("Compare complete.");
  227.                 }
  228.             else
  229.                 Status->Puts("Compare pending...");
  230.             break;
  231.  
  232.         case KEY_F1:
  233.             {
  234.             PopWin pop("ShowDupe Help",MakePosition(5,5,16,70));
  235.             pop.Open();
  236.             pop.Printf(ShowDupeHelp);
  237.             pop.Printf("\nPress any key to continue...");
  238.             keyboard.Get();
  239.             pop.Close();
  240.             }
  241.             break;
  242.  
  243.         case KEY_CTRLENTER:
  244.             screen.Exec("COMMAND.COM",NULL,
  245.                 "To return to SHOWDUPE, type \'exit\'.");
  246.             break;
  247.  
  248.         default:
  249.             myScroller->KeyProcess(key);
  250.             break;
  251.         }
  252.     return TRUE;
  253.     }
  254.  
  255. ChkDupe::~ChkDupe(void)
  256.     {
  257.     }
  258.  
  259. void ChkDupe::terminate(void)    
  260.     {
  261.     printf(ShowDupe);
  262.     printf("\n");
  263.     printf(ShowDupeHelp);
  264.     exit(0);
  265.     }
  266.  
  267. void ChkDupe::terminate(char *msg)
  268.     {
  269.     printf(msg);
  270.     exit(0);
  271.     }
  272.  
  273. void ChkDupe::InitApp(void)
  274.     {
  275.     char temp[30];
  276.     int mode;
  277.  
  278.     Settings.GetLine(MySettings[SCREENMODE].name,temp);
  279.     if(!stricmp(temp,"EGAVGA"))
  280.         mode = VIDMODE_EGAVGA;
  281.     else
  282.         mode = VIDMODE_DEFAULT;
  283.  
  284.     for(int i = 0; i < MAXCOLORS; i++)
  285.         {
  286.         Settings.GetLine(MySettings[i].name,temp);
  287.         if((colors[i] = GetColor(temp)) == 0xff)
  288.             colors[i] = GetColor(MySettings[i].value);
  289.         }
  290.  
  291.     screen.Init(mode,MakeBG(colors[0])+colors[1],MakeBG(colors[2])+colors[3]);
  292.  
  293.     screen.Paint();
  294.     Window::SetDefaultColors(MakeBG(colors[4])+colors[5]);
  295.     }
  296.  
  297. void ChkDupe::ShowStats(void)
  298.     {
  299. #ifdef NOTYET
  300.     char lastfile[13];
  301.     lastfile[0] = '\0';
  302.  
  303.     unsigned total_files = 0, dupe_files = 0, org_files = 0;
  304.     long total_size = 0L, dupe_size = 0L, org_size = 0L;
  305.     long curr_offset = IndexFile.CurPosition();
  306.  
  307.     IndexFile.Offset(0L);
  308.  
  309.     while(IndexFile.Read(sizeof(OutBuf),&OutBuf) != 0)
  310.         {
  311.         GetData(&DataFile,OutBuf.offset);
  312.  
  313.         total_size += DataBuf.size;
  314.         total_files++;
  315.  
  316.         if(!strcmp(OutBuf.buf,lastfile))        // if a duplicate
  317.             {
  318.             dupe_files++;
  319.             dupe_size += DataBuf.size;
  320.             }
  321.         else
  322.             {
  323.             org_files++;
  324.             org_size += DataBuf.size;
  325.             }
  326.  
  327.         strcpy(lastfile,OutBuf.buf);
  328.         }
  329.  
  330.     printf("ChkDupe has found:\nOut of %u files which comprise %ld bytes\n",
  331.         total_files,total_size);
  332.     printf("A total of %u files have a total of %u duplicates\n",
  333.         org_files,dupe_files);
  334.     printf("These duplicates occupy a total of %ld bytes\n\n",dupe_size);
  335.  
  336.     IndexFile.Offset(curr_offset);
  337. #endif
  338.     }
  339.  
  340. void main(void)
  341.     {
  342.     ChkDupe myApp;
  343.     }
  344.  
  345.  
  346.  
  347.