Code:
/*BUGS AS OF AUGUST 2 2002:
COMPLETE WORKING VERSION, NO BUGS...sort of
*/
#include "stdafx.h"
#include "Part Finder.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// THE APPLICATION OBJECT
CWinApp theApp;
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTION PROTOTYPES
int init(void);
int mainMenu(void);
int startSearch(void);
int returnChoice(void);
int ErrorHandler(string Error);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBALS
int nRetCode = 0;
char choice[4];
int company=25;
bool goAgain=FALSE;
bool stop=FALSE;
char partNumber[25];
char * currentDir=new char [MAX_PATH];
char pathCommand[]="c:\\shop_machines\\puma_part_prg\\PUMA\\";
char * szBuffer=new char[256];
HANDLE hFileRead;
HANDLE hSearchFile;
WIN32_FIND_DATA fileData;
char * tempFileName=new char[15];
bool foundOnce=FALSE;
bool stillFiles=TRUE;
LPDWORD lpNumberOfBytesRead=new unsigned long;
bool noChoice=TRUE;
ofstream fout("c:\\Part Finder\\results\\results.txt");
char * b=new char [sizeof(szBuffer)];
int numOfResults=0;
HWND hwnd;
string ccompany;
int infiniteLoopCount;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MAIN FUNCTION
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
ErrorHandler("\n\nProgram Initialization Failed");
}
else
{
//run the initial greeting
init();
do{
//which company?
mainMenu();
//search here
startSearch();
//want to search again?
returnChoice();
}while(goAgain==TRUE);
}
//clean up memory
currentDir=NULL;
szBuffer=NULL;
tempFileName=NULL;
lpNumberOfBytesRead=NULL;
b=NULL;
delete [] szBuffer;
delete [] currentDir;
delete [] tempFileName;
delete [] b;
delete lpNumberOfBytesRead;
fout.close();
std::cout<<"Press Any Key To Quit"<<endl;
getch();
return nRetCode;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PROGRAM FUNCTION DEFINITIONS
int init(void){
system("cls");
std::cout<<"\nPart Finder Version 3.0\n\n";
std::cout<<"Copyright 2002\nCreated By Drew Peterson";
Sleep(4000);
system("cls");
return 0;
}
int mainMenu(void){
//company menu block,leave tabs alone!
if(goAgain==TRUE){
strcpy(pathCommand,"c:\\shop_machines\\puma_part_prg\\puma\\");
numOfResults=0;
}
while(company>24&&infiniteLoopCount<10) {
std::cout<<"Which Company Is The Part For?"<<endl;
std::cout<<" AMT 0"<<" || Mercury 13"<<endl;
std::cout<<" Barber 1"<<" || Misc. 14"<<endl;
std::cout<<" BeloitCP 2"<<" || Modern 15"<<endl;
std::cout<<" Borg 3"<<" || Nissan 16"<<endl;
std::cout<<" Bosch 4"<<" || Packard 17"<<endl;
std::cout<<" Camcar 5"<<" || Phil 18"<<endl;
std::cout<<" Colt 6"<<" || Simmons 19"<<endl;
std::cout<<" Durst 7"<<" || SSI 20"<<endl;
std::cout<<" Enkel 8"<<" || Tradewind 21"<<endl;
std::cout<<" Goex 9"<<" || Warman 22"<<endl;
std::cout<<" Isthmus 10"<<" || Warner 23"<<endl;
std::cout<<" Kulasik 11"<<" || Witco 24"<<endl;
std::cout<<" Lear 12"<<" ||"<<endl;
std::cout<<"\nEnter Choice Here:";
std::cin>>company,cin.get();
std::cin.clear();
infiniteLoopCount++;
}
if(infiniteLoopCount>=10)
{
system("cls");
ErrorHandler("Oh No!!!\nYou've Been Caught In The Infinite Loop Of Death!!!\n\nNext Time Enter The Number Of The Company, Have A Nice Day\n");
}
//with the below, fileFind will go to the correct directory
//when finding all the files...
//basically takes whatever pathCommand is now c:whatever to c:whatever whatever so you can search
switch(company){
case 0:
strcat(pathCommand,"Amt");
strcpy(currentDir,pathCommand);
ccompany="Amt";
break;
case 1:
strcat(pathCommand,"Barber");
strcpy(currentDir,pathCommand);
ccompany="Barber";
break;
case 2:
strcat(pathCommand,"Beloit");
strcpy(currentDir,pathCommand);
ccompany="BeloitCP";
break;
case 3:
strcat(pathCommand,"Borg");
strcpy(currentDir,pathCommand);
ccompany="Borg";
break;
case 4:
strcat(pathCommand,"Bosch");
strcpy(currentDir,pathCommand);
ccompany="Bosch";
break;
case 5:
strcat(pathCommand,"Camcar");
strcpy(currentDir,pathCommand);
ccompany="Camcar";
break;
case 6:
strcat(pathCommand,"Colt");
strcpy(currentDir,pathCommand);
ccompany="Colt";
break;
case 7:
strcat(pathCommand,"Durst");
strcpy(currentDir,pathCommand);
ccompany="Durst";
break;
case 8:
strcat(pathCommand,"Enkel");
strcpy(currentDir,pathCommand);
ccompany="Enkel";
break;
case 9:
strcat(pathCommand,"Goex");
strcpy(currentDir,pathCommand);
ccompany="Goex";
break;
case 10:
strcat(pathCommand,"Isthms");
strcpy(currentDir,pathCommand);
ccompany="Isthmus";
break;
case 11:
strcat(pathCommand,"Kul");
strcpy(currentDir,pathCommand);
ccompany="Kulasik";
break;
case 12:
strcat(pathCommand,"Lear");
strcpy(currentDir,pathCommand);
ccompany="Lear";
break;
case 13:
strcat(pathCommand,"Merc");
strcpy(currentDir,pathCommand);
ccompany="Mercury";
break;
case 14:
strcat(pathCommand,"Misc");
strcpy(currentDir,pathCommand);
ccompany="Miscellaneous";
break;
case 15:
strcat(pathCommand,"Modern");
strcpy(currentDir,pathCommand);
ccompany="Modern";
break;
case 16:
strcat(pathCommand,"Nissan");
strcpy(currentDir,pathCommand);
ccompany="Nissan";
break;
case 17:
strcat(pathCommand,"Pack");
strcpy(currentDir,pathCommand);
ccompany="Packard";
break;
case 18:
strcat(pathCommand,"Phil");
strcpy(currentDir,pathCommand);
ccompany="Phil";
break;
case 19:
strcat(pathCommand,"Simmon");
strcpy(currentDir,pathCommand);
ccompany="Simmons";
break;
case 20:
strcat(pathCommand,"Ssi");
strcpy(currentDir,pathCommand);
ccompany="SSI";
break;
case 21:
strcat(pathCommand,"Tradwin");
strcpy(currentDir,pathCommand);
ccompany="Tradewind";
break;
case 22:
strcat(pathCommand,"Warman");
strcpy(currentDir,pathCommand);
ccompany="Warman";
break;
case 23:
strcat(pathCommand,"Warner");
strcpy(currentDir,pathCommand);
ccompany="Warner";
break;
case 24:
strcat(pathCommand,"Witco");
strcpy(currentDir,pathCommand);
ccompany="Witco";
break;
}
return 0;
}
int startSearch(void){
//reinitialize in case they searched again
stillFiles=TRUE;
foundOnce=FALSE;
infiniteLoopCount=0;
//get the partNumber to search through the text files for
std::cout<<"\nEnter Part Name Or Number"<<endl;
std::cin.getline(partNumber,75,'\n');
std::cin.clear();
fout<<"\n\n\n\nSearching For "<<partNumber<<" In "<<ccompany<<" Yeilded:\n\n";
std::cout<<"\nSearching";
for(int i=0; i<=3; i++)
{
Sleep(750);
std::cout<<".";
}
std::cout<<"\n\n\n\n";
Sleep(250);
//make currentDir the default directory for the file functions to work in
if(SetCurrentDirectory(currentDir)==0){
ErrorHandler("\n\nUnable To Set Directory");
}
//find the first file with, store info on it in the fileData struct
hSearchFile=FindFirstFile("*",&fileData);
//get the filename of the particular file at this point
strcpy(tempFileName,fileData.cFileName);
FindNextFile(hSearchFile,&fileData);
strcpy(tempFileName,fileData.cFileName);
CharUpper(partNumber);
CloseHandle(hFileRead);
//NOW GO FIND SOME MORE
//this loop will stop when there are no more files in the current directory
do{
//checks to see if there are files
if(FindNextFile(hSearchFile,&fileData)==0){
stillFiles=FALSE;
std::cout<<"\n\nSearch Complete"<<endl;;
}else{
stillFiles=TRUE;
}
if(stillFiles==TRUE)
{
strcpy(tempFileName,fileData.cFileName);
//IF THERE ARE MORE FILES
//same as above
hFileRead = CreateFile(tempFileName, // open found file
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFileRead == INVALID_HANDLE_VALUE)
{
ErrorHandler("\n\nCould Not Open File"); // process error
}
if(ReadFile(hFileRead,szBuffer,256,lpNumberOfBytesRead,NULL)==0){
ErrorHandler("\n\nCould Not Read File"); //process error
}else{
CharUpper(szBuffer);
b=strchr(szBuffer,'(');
if (b==NULL) {
stop=TRUE;
}else{
stop=FALSE;
}
if (stop!=TRUE){
strtok(b,")");
b[0]='\b';
if (strstr(b,partNumber)!=NULL) {
std::cout<<"That Part Was Found In: "<<currentDir<<'\\'<<tempFileName<<endl;
std::cout<<"\"\""<<b<<"\"\n"<<endl;
fout<<"That Part Was Found In: "<<currentDir<<'\\'<<tempFileName<<endl;
fout<<'\"'<<b<<"\"\"\n"<<endl;
foundOnce=TRUE;
}
}
CloseHandle(hFileRead);
numOfResults++;
}
}
}while(stillFiles==TRUE);
FindClose(hSearchFile);
if(foundOnce==FALSE){
std::cout<<"That Part Number Was Not Found"<<endl;;
fout<<"That Part Number Was Not Found"<<endl;
}
if(numOfResults>=6){
ShellExecute(hwnd,"open","results.txt",NULL,"c:\\Part Finder\\results",SW_SHOWNORMAL);
}
return 0;
}
int returnChoice(void){
//see if they want to run the program again
do{
std::cout<<"Do You Wish To Perform Another Search? (Y/N)"<<endl;
std::cin.getline(choice,3,'\n');
std::cin.clear();
if (choice[0]=='y'||choice[0]=='Y'){
goAgain=TRUE;
noChoice=FALSE;
}else if(choice[0]=='n'||choice[0]=='N'){
goAgain=FALSE;
noChoice=FALSE;
}else{
noChoice=TRUE;
}
}while(noChoice==TRUE);
company=25;
if(goAgain==TRUE){
system("cls");
}
return 0;
}
int ErrorHandler (string Error){
//tell about errors , clean memory, and exit
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED|FOREGROUND_INTENSITY );
std::cout<<Error<<endl;
if(currentDir){
currentDir=NULL;
delete [] currentDir;
}
if(szBuffer){
szBuffer=NULL;
delete [] szBuffer;
}
if(tempFileName){
tempFileName=NULL;
delete [] tempFileName;
}
if(lpNumberOfBytesRead){
lpNumberOfBytesRead=NULL;
delete lpNumberOfBytesRead;
}
if(b){
b=NULL;
delete [] b;
}
std::cout<<"Press Any Key To Quit"<<endl;
getch();
exit(1);
}
thanks again