Rate my code

This is a discussion on Rate my code within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm a beginning C++ programmer and I'm writing a console file-search application that uses a DataBase with a list ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    Smile Rate my code

    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?


    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;
    }
    Thanks for the help!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Add another function

    Code:
    inline DWORD Bit(pos) { return 1ul << pos; }                // A bit in the right pos
    inline bool BitTrue(DWORD a, DWORD b) { return (a & Bit(b)) != 0; } //Is bit 1?
    inline void DefBit(DWORD &a, DWORD b) { a |= Bit(b); }      //define bit
    inline void UnDefBit(DWORD &a, DWORD b) { a &= ~Bit(b); }   //undefine bit
    > 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?
    Except DWORD is currently specified in windows.h (which not everyone has)

    Besides, its a rather generic type. You would be better with say
    typedef unsigned long bits_t;
    Which conveys a bit more meaning for what is going on, and is more portable to other platforms.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    6
    Man I feel stupid about my way of writing down a &= ~Bit(b)

    thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  4. rate my code
    By kashifk in forum C Programming
    Replies: 1
    Last Post: 06-07-2003, 12:18 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21