home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_tools / q_search / qsearch.c next >
C/C++ Source or Header  |  1992-06-25  |  7KB  |  289 lines

  1. /*******************************QSEARCH.DLL**********************************
  2. *   Author  - Jeff Simms  72200,3173      Date: June 24, 1992            *
  3. *                                        *
  4. *   FindString Routine from FGREP.C author unknown                *
  5. *   CopyFile Routine from COPY.C author Eric Flo                *
  6. *   FindFile Routine from FILEFIND.C author unknown                *
  7. *                                        *
  8. *                                        *
  9. *   .def and .h files at end    Compiled under MS C 7.0 Compact Model        *
  10. ****************************************************************************/
  11.  
  12. #include <dos.h>
  13. #include <stdio.h>
  14. #include <direct.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <windows.h>
  18. #include "qsearch.h"
  19.  
  20. int FAR PASCAL LibMain( HANDLE hInstance, WORD wDataSegment,
  21.                    WORD wHeapSize, LPSTR lpszCmdLine )
  22. {
  23.     if ( wHeapSize != 0 )
  24.     UnlockData( 0 );
  25.     return 1;
  26. }
  27.  
  28. int FAR PASCAL WEP (int bSystemExit)
  29. {
  30.     return (1);
  31. }
  32.  
  33.  
  34. int _export FAR PASCAL FindString(int iSwitch, LPSTR lpPathName,
  35.                  LPSTR lpSearchString, HWND hListBox)
  36. {
  37.  
  38.     FILE    *hFile;
  39.     char line[132],buff[132],bufftwo[132];
  40.     int    lnum=0,found=0,length;
  41.  
  42.     if (iSwitch > 0)
  43.     AnsiLower(lpSearchString);
  44.  
  45.     if (hFile = fopen(lpPathName, "r")){
  46.  
  47.         while (fgets(line,132,hFile)) {
  48.             lnum++;
  49.            strcpy(bufftwo,line);
  50.  
  51.          if (iSwitch > 0)
  52.            AnsiLower(line);
  53.  
  54.            if (strstr(line,lpSearchString)) {
  55.           if (iSwitch == 2 || iSwitch == -1){
  56.               itoa(lnum,buff,10);
  57.                strcat(buff,": ");
  58.                strcat(buff,bufftwo);
  59.             }
  60.           else
  61.                strcpy(buff, bufftwo);
  62.  
  63.           length = strlen(buff);
  64.           buff[length-1] = '\0';
  65.  
  66.       SendMessage(hListBox,LB_ADDSTRING,0,(LONG)(LPSTR)buff);
  67.                 found++;
  68.                 }
  69.         }
  70.           fclose(hFile);
  71.       }
  72.           if (hFile)
  73.             return found;
  74.           else
  75.             return (-1);
  76. }
  77.  
  78.  
  79. int _export FAR PASCAL CopyFile (LPSTR szSource, LPSTR szDest, BOOL bAttrib)
  80.  
  81. {
  82.  
  83.  
  84.   static int hFileSource, hFileDest;
  85.   OFSTRUCT OpenBuff;
  86.   WORD      wBytesRead,CHUNKSIZE=32000;
  87.   HANDLE   hMem;
  88.   LPSTR    gPtr;
  89.   static unsigned int uTime, uDate, uAttrib, uResult;
  90.  
  91.  
  92.   /* open source file for reading */
  93.   hFileSource = OpenFile (szSource, &OpenBuff, OF_READ | OF_SHARE_DENY_WRITE);
  94.   if (hFileSource == -1)
  95.     return (ERROR_CANNOT_OPEN_SOURCE);
  96.  
  97.   /* open destination file for writing */
  98.   hFileDest = OpenFile (szDest, &OpenBuff, OF_CREATE | OF_WRITE | OF_SHARE_EXCLUSIVE);
  99.   if (hFileDest == -1)
  100.     {
  101.     _lclose (hFileSource);
  102.     return (ERROR_CANNOT_OPEN_DEST);
  103.     }
  104.  
  105.  
  106.   hMem = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, CHUNKSIZE+10);
  107.  
  108.   if (hMem == 0)
  109.     {
  110.     _lclose (hFileSource);
  111.     _lclose (hFileDest);
  112.     return (ERROR_CANNOT_ALLOCATE_MEMORY);
  113.     }
  114.  
  115.   gPtr = GlobalLock (hMem);
  116.  
  117.   if (gPtr == 0)
  118.     {
  119.     GlobalFree (hMem);
  120.     _lclose (hFileSource);
  121.     _lclose (hFileDest);
  122.     return (ERROR_CANNOT_LOCK_MEMORY);
  123.     }
  124.  
  125.   do
  126.     {
  127.     wBytesRead = (WORD) _lread(hFileSource, gPtr, CHUNKSIZE);
  128.     if (wBytesRead == -1)
  129.         {
  130.         GlobalUnlock (hMem);
  131.         GlobalFree (hMem);
  132.         _lclose (hFileSource);
  133.         _lclose (hFileDest);
  134.         return (ERROR_READING_FILE);
  135.         }
  136.     _lwrite(hFileDest,    gPtr, wBytesRead);
  137.     }
  138.   while (wBytesRead == CHUNKSIZE);
  139.  
  140.  
  141.   if (bAttrib)
  142.     {
  143.  
  144.     uResult = _dos_getftime (hFileSource, &uTime, &uDate);
  145.     if (uResult != 0)
  146.         {
  147.         GlobalUnlock (hMem);
  148.         GlobalFree (hMem);
  149.         _lclose (hFileSource);
  150.         _lclose (hFileDest);
  151.         return (ERROR_GETTING_FILE_DATE_TIME);
  152.         }
  153.     uResult = _dos_setftime (hFileDest, uTime, uDate);
  154.     if (uResult != 0)
  155.         {
  156.         GlobalUnlock (hMem);
  157.         GlobalFree (hMem);
  158.         _lclose (hFileSource);
  159.         _lclose (hFileDest);
  160.         return (ERROR_SETTING_FILE_DATE_TIME);
  161.         }
  162.     }
  163.  
  164.   GlobalUnlock (hMem);
  165.   GlobalFree (hMem);
  166.   _lclose (hFileSource);
  167.   _lclose (hFileDest);
  168.  
  169.   if (bAttrib)
  170.     {
  171.     /* Query and set file attributes */
  172.     uResult = _dos_getfileattr ( szSource, &uAttrib);
  173.     if (uResult != 0)
  174.         return (ERROR_GETTING_FILE_ATTRIB);
  175.  
  176.     uResult = _dos_setfileattr (szDest, uAttrib);
  177.     if (uResult != 0)
  178.         return (ERROR_SETTING_FILE_ATTRIB);
  179.     }
  180.  
  181.   return (SUCCESS);
  182.   }
  183.  
  184.  
  185. static char    file[13],dletter[1];
  186. unsigned int    found=0,lbhandle;
  187. int        drive;
  188.  
  189. int _export FAR PASCAL FindFile (LPSTR szDrive,LPSTR szPattern,HWND hListBox)
  190.  
  191. {
  192.  
  193.     char     rootpath[128];
  194.  
  195.     lstrcpy(file,szPattern);
  196.     lbhandle = hListBox;
  197.     found = 0;
  198.     rootpath[0]='\0';
  199.     strcat(rootpath, "\\");
  200.     dletter[0]=(char)toupper(szDrive[0]);
  201.     drive = dletter[0] - 64;
  202.     if (_chdrive(drive))
  203.          return (-1);
  204.  
  205.     do_a_dir(rootpath);
  206.  
  207.     return found;
  208.  
  209. }
  210.  
  211.    void do_a_dir(char currentpath[128])
  212.  
  213. {
  214.     char    nextpath[128];
  215.     char    searchpath[128];
  216.     char    fullname[128];
  217.     unsigned done;
  218.     struct    find_t ffblk;
  219.  
  220.     strcpy(searchpath,currentpath);
  221.     strcat(searchpath,file);
  222.  
  223.     done = _dos_findfirst(searchpath,_A_HIDDEN,&ffblk);
  224.  
  225.     while (!done) {
  226.         strcpy(fullname,dletter);
  227.         strcat(fullname,":");
  228.         strcat(fullname,currentpath);
  229.         strcat(fullname,ffblk.name);
  230.         SendMessage(lbhandle,LB_ADDSTRING,0,(LONG)(LPSTR)fullname);
  231.         found++;
  232.         done = _dos_findnext(&ffblk);
  233.     }
  234.  
  235.     strcpy(searchpath,currentpath);
  236.     strcat(searchpath,"*.*");
  237.  
  238.     done = _dos_findfirst(searchpath,_A_SUBDIR,&ffblk);
  239.  
  240.     while (!done) {
  241.         if (ffblk.attrib == _A_SUBDIR && ffblk.name[0] != '.') {
  242.             strcpy(nextpath,currentpath);
  243.             strcat(nextpath,ffblk.name);
  244.             strcat(nextpath,"\\");
  245.             do_a_dir(nextpath);
  246.         }
  247.         done = _dos_findnext(&ffblk);
  248.     }
  249.  
  250. }
  251.  
  252.  
  253. /*           QSEARCH.DEF
  254.  
  255. LIBRARY        QSEARCH
  256. DESCRIPTION    'STRING SEARCHING DLL'
  257. EXETYPE        WINDOWS
  258. CODE           PRELOAD MOVEABLE DISCARDABLE
  259. DATA           PRELOAD MOVEABLE SINGLE
  260. HEAPSIZE       1024
  261. EXPORTS        WEP
  262.            FindString
  263.            CopyFile
  264.            FindFile
  265.  
  266. */
  267.  
  268. /*        QSEARCH.H
  269.  
  270. int _export FAR PASCAL FindString(BOOL,LPSTR,LPSTR,HWND);
  271. int _export FAR PASCAL CopyFile (LPSTR,LPSTR,BOOL);
  272. int _export FAR PASCAL FindFile (LPSTR,LPSTR,HWND);
  273.  
  274. void do_a_dir(char currentpath[128]);
  275.  
  276. #define SUCCESS                       0
  277. #define ERROR_CANNOT_OPEN_SOURCE      1
  278. #define ERROR_CANNOT_OPEN_DEST        2
  279. #define ERROR_CANNOT_ALLOCATE_MEMORY  3
  280. #define ERROR_CANNOT_LOCK_MEMORY      4
  281. #define ERROR_READING_FILE            5
  282. #define ERROR_GETTING_FILE_DATE_TIME  6
  283. #define ERROR_SETTING_FILE_DATE_TIME  7
  284. #define ERROR_GETTING_FILE_ATTRIB     8
  285. #define ERROR_SETTING_FILE_ATTRIB     9
  286.  
  287.  
  288. */
  289.