Trail of Code

Mahmoud, the developer, talks to Steve, the tester, about source control.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged with:  

Verify a String is a Number in C++

The following C++ function verifies that a string contains a number only. In the case, a number is a non-negative whole number from 0 to n.

#include <iostream>
#include <string>

using namespace std;

bool isNumber(string str)
{
  // Zero length
  if ( !str.length() )
    return false;

  // Starts with a zero for numbers more than 1 digit
  if ( (str.length() > 1) && ( str[0] == '0' ) )
    return false;

  for ( int i = 0; i < str.length(); i++ )
  {
    if ( !isdigit(str[i]) )
      return false;
  }

   return true;
}

int main(int argc, char **argv)
{
  cout << "0:    " << isNumber("0") << endl;

  cout << "123:  " << isNumber("123") << endl;

  cout << "0123: " << isNumber("0123") << endl;

  cout << "1.23: " << isNumber("1.23") << endl;

  cout << "-123: " << isNumber("-123") << endl;

  return 0;
}

% 0:    1
% 123:  1
% 0123: 0
% 1.23: 0
% -123: 0

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged with:  

Smells Like Software

Mahmoud, the developer, tells Steve, the tester, his software is almost as good as heaven.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged with:  

How to Prune Third Party DLLs

A good rule of thumb when developing tests is to store only what is needed.  Don’t include content that isn’t necessary for the execution of your tests.  Today I was given a test application that required third party DLLs.  The DLLs were so numerous that they consumed nearly 1G of disk space.  This was unacceptable for two reasons.   First, it would take longer to install the tests over a network environment.  Second, disk space was very limited due to the nature of the product.

I figured that the test application didn’t require every DLL, so I decided to prune the DLL directory leaving only the necessary files.  At first, I was going to remove one DLL at a time.  For each DLL, I would run the test application.  If the application ran successfully, not complaining about missing the DLL, then I would remove it.  It’s not a perfect process because the test application may still need the DLL in some other part of the program not executed.

Just then a friendly developer told me about a free tool called the Dependency Walker.  All you need to do is load a Windows .exe file in this application and it will tell you the DLL dependencies.  For example, I tried the write.exe program that comes with Windows.  It worked great as you can barely see here.

I then tried it on my test application.  Dependency Walker allowed me to export the list as a CSV formatted file for spreadsheets.  I kept what I need and saved tons of space.  Steve Miller wrote the program.  You can find his Texas Ranger Dependency Walker program here:

http://dependencywalker.com/

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged with:  

Parsing Command Line Arguments in C++

Tonight I needed to build a simple parser for command line arguments in a C++ program. So I built this rudimentary parser along with a usage message function. They both seem to go hand-in-hand. The code is very simple and it’s not robust; however, it should help you get started. You can find many examples on the web. Parsing command line arguments can be a complex endeavor if you want to support hundreds of features. I try to be a minimalist though — besides why do I need to argue with the command line in the first place? These arguments just lead to the execution of the program anyway. What a horrible way to go for a test program.

#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

void showHelp(const char *pName, const char *msg, int exitCode)
{
  cout << pName << " Usage: " << endl;

  cout << "-F  Sets Some Flag to True" << endl;

  cout << "-l specifies a log path" << endl;

  cout << "-Hh displays help" << endl;

  cout << msg << endl;

  exit(exitCode);
}

int main(int argc, char **argv)
{
  char argSwitch = '-';
  bool bSomeFlag = false;
  char logPath[255] = "";

  for (int i = 0; i < argc; i++)
  {
    // This is a switch
    if ( argv[i][0] == argSwitch ) 
    {
      // A valid single letter switch
      if ( (argv[i][2] == '\0') && 
        ((argv[i][1] >= 65) && (argv[i][1] <= 90))  ||
        ((argv[i][1] >= 97) && (argv[i][1] <= 122)) )
      {
        switch (argv[i][1]) 
        {

        // Upper case switch, usually means a flag
        case 'F':

          bSomeFlag = true;

          break;

        // A lowercase letter indicating an argument w/value
        case 'l':

          // Make sure has value and not a switch
          if ( (i < (argc - 1)) && (argv[i+1][0] != argSwitch) )
          {
            // Shouldn't use strcpy due to buffer overflow...
            strcpy(logPath, argv[i + 1]);
          }
          else
          {
            showHelp(argv[0], "Missing expected value", -1);          
          }

          i++;

          break;

        case 'H':

        case 'h':

          showHelp(argv[0], "", 0);

        default:

          showHelp(argv[0], "Unknown argument", -1);
        }
      }
      // The argument is not syntactically valid
      else
      {
        showHelp(argv[0], "Invalid command line argument switch", -1);
      }
    } 
  }

  if ( bSomeFlag )
    cout << "The flag is true" << endl;
  else
    cout << "The flag is false" << endl;

  if ( strlen(logPath) ) 
    cout << "The logpath is " << logPath << endl;
  else
    cout << "No logpath given" << endl;

  return 0;
}

% demo.exe -F -l c:\foobar
The flag is true
The logpath is c:\foobar

% demo.exe -l c:\foobar
The flag is false
The logpath is c:\foobar

% demo.exe -F
The flag is true
No logpath given

% demo.exe
The flag is false
No logpath given

% demo.exe -h
demo.exe Usage:
-F  Sets Some Flag to True
-l specifies a log path
-Hh displays help

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged with:  
Page 4 of 6« First...23456

Random Site Quotes

I am a Yankee soldier with sympathetic tendencies towards the southern cause.
© 2009-2011 TestDeveloper.com
Powered by WordPress
Content provided by John L. Whiteman and his Slam Carrot Long Ears Gang