Hey,
I'm a beginning C++ programmer and I'm writing a console file-search application that uses a DataBase with a list of files (like locate from the linux shell). Below is the sourcecode that I've written so far. I'm posting it here because I don't know any good programmers myself and I would really like to get some tips and comments to push me in the right direction.
Besides that, I've also got a few specific questions:
1)
The inline function UnDefBit sets a bit to zero by doing the following:
lineofbits = ((2^32)-1 - 2^bitnumber) AND lineofbits.
I was wondering if there is an easier way to do this because this seems kind of time-consuming to me, and I'd rather not load the math.h library.
2)
Someone told me that I should use a DWORD for storing rows of bits. Why should I use a DWORD instead of a long int?
Thanks for the help!Code:#include <windows.h> #include <iostream.h> #include <math.h> DWORD OptionFlags = 0; inline bool BitTrue(DWORD a, DWORD b) { return (a & (1 << b)) ? true : false; } //Is bit 1? inline void DefBit(DWORD &a, DWORD b) { a = (a | (1 << b)); } //define bit inline void UnDefBit(DWORD &a, DWORD b) { a = DWORD(4294967295 - pow(2, b)) & a; } //undefine bit DWORD LogicDrives(bool HDsOnly) //All Logical drives or Harddisks only { DWORD DriveList = GetLogicalDrives(); if (HDsOnly) { char DriveCheck[4] = "*:\\"; for (int a = 0; a < 26; a++) { if (BitTrue(DriveList, a)) { DriveCheck[0]= 'a'+a; UnDefBit(DriveList, a); if (GetDriveType(DriveCheck)==3) DefBit(DriveList, a); //Check if it's a HD } } } return DriveList; } DWORD DrivesFromArgs(int argc, char *argv[]) //Drives from arguments { DWORD DriveList = 0; char DrivesString[26]; for (int a = 1; a < argc; a++) //Get DrivesString from arguments { if (argv[a][0] == '/' && argv[a][1] == 'd') { strcpy(DrivesString, argv[a + 1]); break; } } for (a = 0; a < 26; a++) //Create DriveList from DrivesString[] { if (DrivesString[a] >= 'a' && DrivesString[a] <= 'z') DefBit(DriveList, DrivesString[a] - 'a'); else break; } return DriveList; } DWORD Options(int argc, char *argv[]) //Get Options from arguments { DWORD Flags=0; for(int a = 1; a < argc; a++) { if (argv[a][0] == '/') { switch (argv[a][1]) { case 'c': DefBit(Flags, 0); break; //Create DB case 'v': DefBit(Flags, 1); break; //Verbose mode case 'd': DefBit(Flags, 2); break; //Specified drives in args case 'a': DefBit(Flags, 3); break; //All logical drives } } } return Flags; } void Say(int LineNum, int a=0, DWORD Flags=0) //Output a line { int b = 0; switch (LineNum) //will always be said { case 0: cout << "Start of program" << endl; return; case 1: cout << "End of program" << endl; return; } if (BitTrue(OptionFlags, 1)) //will only be said in verbose mode { switch (LineNum) { case 100: cout << a << " arguments: "; for(;b < 10;b++) cout << BitTrue(Flags, b); cout << endl; return; case 101: cout << "Creating database" << endl; return; case 102: cout << "Drives to index: "; for(;b < 26;b++) cout << BitTrue(Flags, b); cout << endl; return; } } } void CreateDatabase(int argc, char *argv[]) //Creates a database { Say(101); DWORD DrivesToIndex; if (BitTrue(OptionFlags, 2)) DrivesToIndex = DrivesFromArgs(argc, argv); //Get drives from arguments else if (BitTrue(OptionFlags, 3)) DrivesToIndex = LogicDrives(false); //Do All logic drives else DrivesToIndex = LogicDrives(true); //Do all HDs Say(102,0,DrivesToIndex); } int main(int argc, char *argv[]) //main { Say(0); OptionFlags = Options(argc, argv); //get options Say(100, argc-1, OptionFlags); if (BitTrue(OptionFlags, 0)) CreateDatabase(argc, argv); Say(1); return 0; }



LinkBack URL
About LinkBacks


