Archive for February, 2010

Tool Review: CppDepends – C\C++ Static Analysis Tool

0

I just want to introduce a new tool that I met recently. Its the CppDepends! its a wonderful tool which can do static code analysis and generate lot of metrics. It has got CQL( Code Query Language) by which you can write sql like queries to get code metrics.

Have a look at the complete feature list. You can download the tool from here. I think its worth a trial. Try it!

How to Tokenize String? Different Tricks in Trade!

3

I still remember my first project which I did seven years back. In that I got a task to split a string which contains ids separated by slash. I wrote a big snippet of string parser code by using pointers. But now when i see the following tricks, i feel – how childish was my first code snippet.


Some of the tricks are as follows.

1) strtok()

If you want plain api and no object oriented fanciness, then strtok is for you. Download the source from here.

#include "string.h"
...

// String to be splitted.
char String[] = "Long.Live_Visual.C++";

// Seperators.
char Seperators[] = "._";

// Start Tokenizing.
char* Token = strtok(String, Seperators);

// Loop until end.
while(Token != NULL)
{
 cout << Token << endl;

 // Tokenize the remaning string.
 Token = strtok(0, Seperators);
};

2) istringstream
C++ Streams are good option for string splitup. But it has only one drawback – one one delimiter can be specified. Download the source from here.

#include "iostream"
#include "sstream"
#include "string"
...

string String = "Long.Live.Visual.C++";
char Seperator = '.';

// Create input string stream.
istringstream StrStream(String);
string Token;

while(getline(StrStream, Token, Seperator))
{
 cout << Token << endl;
}

3) CString::Tokenize()

Wanna MFC way? Then CString::Tokenize() is for you. Download the source from here.

// String.
CString String = _T("long.live_Visual.C++");

// Token seperators.
CString Seperator = _T("._");
int Position = 0;
CString Token;

// Get first token.s
Token = String.Tokenize(Seperator, Position);

while(!Token.IsEmpty())
{
 wcout << Token.GetBuffer() << endl;
 Token.ReleaseBuffer();

 // Get next token.
 Token = String.Tokenize(Seperator, Position);
}


Do you know any other mind blowing tokenizing tricks? Then, share with us.

C++ Union – Is it still Relevant?

2

Do you remember the old days, where memory was a premium? At that time, unions were used to save memory by merging multiple variables. Gone are the days where we had memory constraints. Now we have GB’s of RAM itself. So are unions just overhead to language now? Or is it still useful?


The merging property of unions can be used for parsing values. For instance have a look at the MessageParser below. If you want to deal with byte streams with specific message format, then unions will be really helpful.

#pragma pack(1)

// Message layout.
struct MessageLayout
{
 char Signature[3];
 WORD HeaderLen;
 WORD Param1;
 BYTE Param2;
};

// Union to parse header from byte streams.
union MessageHeaderParser
{
 // Byte stream.
 BYTE Bytes[8];
 MessageLayout Layout;
};

int _tmain(int argc, _TCHAR* argv[])
{
 // DWORD Parser.
 BYTE Bytes[] = { 'M','Z', 0, // Header Signature.
                  10,0,       // Header Length
                  20,0,       // WORD param
                  30 };       // BYTE Param.

 MessageHeaderParser Parser;
 memcpy(&Parser.Bytes, Bytes, 8);

 // Get the HIWORD by using standard windows macro.
 cout << "Signature  :" << Parser.Layout.Signature << endl;
 cout << "Msg Length :" << Parser.Layout.HeaderLen << endl;
 cout << "WORD param :" << Parser.Layout.Param1 << endl;
 cout << "BYTE param :" << (int)Parser.Layout.Param2 << endl;

 _getch();
 return 0;
}

Download the code from here.

BTW, Do you know that the famous Audi brand is a union of 4 old legendary car companies? The four rings represents each of the four companies. Interesting, the history is. isn’t it?

How to set Focus to Different Control on Dialog Startup?

1

Do you want to set the focus to another control on displaying Dialog? Or tried SetFocus() to another control in OnInitDialog() and want to know why its not working? The answer for your ‘Focus’ question is here.

If you are setting the default focus to another control in dialog, then OnInitDialog() should return FALSE. Have a look at the code snippet below.

BOOL CStartupFocusDlg::OnInitDialog()
{
 ...
 // Set focus to your control.
 CWnd* pWnd = GetDlgItem(IDC_EDIT2);
 pWnd->SetFocus();

 // return TRUE;  // Wizard Generated code.
 // Return FALSE if you set focus to different control
 return FALSE;
}


Download the Sample, if you want to see it in action. Please note that sample is compiled in Visual C++ 2008.

How to Get Project Build Time?

0

Many times i lost my temper by waiting for the re-build to be finished. So i just attempted to tune and reduce the build time by removing unnecessary includes. At that time I just wondered how to get the build time?


Just follow the steps to enable the ‘Build Time’.

1) Take – Tools > Options.
2) Now take - Project and Solutions > VC++ Project Settings.
3) Now enable the ‘Build Timing’ option and rebuild your project.


This article seems useful – How do you reduce compile time, and linking time for Visual C++ projects?. Have a look at it.

WeSeeTips Reloaded!!!

5

Dears Visual C++ Enthusiasts ,

At last the migration is successfully completed. It was a bit painful though. Encountered a lot of issues, but lucky was able to solve it without much trouble. Now WeSeeTips is back in new style. Enjoy!

You have already noticed that for last few months there were hardly any update. My sincere apologies to my readers that I was damn busy due to several personal reasons. But I’ve a chest full of interesting tips to show you. Now enjoy the show!

BTW, what do you think about the new theme? Do you like it? Please don’t hesitate to write a few lines in comment section. Thanks for your time!

For WeSeeTips,
Jijo.

WeSeeTips moving to new Home!

0

Dears,

Its been a very long time, I know. But rejoice! WeSeeTips is moving to its new home. Due to several personal and technical reasons, the migration was a bit delayed. Anyway, We will be back soon in new style and new tips to rock you. Keep your fingers crossed!

For WeSeeTips,
Jijo.

Go to Top