Click here to Skip to main content
15,897,371 members
Articles / Desktop Programming / MFC

Counting Lines in a MS VC++ 6.0 project

Rate me:
Please Sign up or sign in to vote.
4.33/5 (15 votes)
4 Aug 2001 152.8K   1.9K   33  
A program that counts the lines (source, comments, blank) in every file included in a MS VC++ 6.0 project
// THIS FILE IS PROVIDED FOR INFORMATIONAL PURPOSE ONLY.
// NO COMERCIAL USE!!!
// Information: decl_spec@yahoo.com

#include "stdafx.h"
#include "counter.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

_counter_data count(const char* _filedata_, unsigned int _datalen_)
{
	_counter_data data;

	if (!_filedata_)
		return data;

	if (!_datalen_)
		return data;
	
	char *_file_data = new char[_datalen_ * 2];
	strcpy(_file_data, _filedata_);
	if (_file_data[_datalen_-1] != '\n')
		strncpy(_file_data, "\r\n", 2);

	unsigned long _size_ = 0;
	unsigned long _last_ = 0;

	bool _recall_ = false;
	const char *_this_line;

	do
	{
		_this_line = NULL;
		_this_line = findline(_file_data + _last_, _size_);
		if (_this_line)
		{
			char *_token_line = new char[_size_ + 1];
			unsigned long _token_size = tokenize(_file_data + _last_, _size_, _token_line);
			_last_+=_size_ + 2; // "\r\n"
			if (!_token_line || _token_size <= 0)
			{
				data._blank_++;
				delete [] _token_line;
				continue;
			}

			if (isgoodline(_token_line, _token_size, _recall_))
				data._lines_++;
			else
				data._comnt_++;

			delete [] _token_line;
		}
	} while(_this_line);

	delete [] _file_data;
	return data;
}

const char *findline(const char *_buf_, unsigned long &_size_)
{
	const char *p = NULL;
	p = strstr(_buf_, "\r\n");
	if (p)
		_size_ = (p - _buf_);

	return p;
}

unsigned long tokenize(const char *_line_, unsigned long _size_, char *_tokens_)
{
	unsigned long _index_ = 0;
	for(unsigned long i = 0; i < _size_; i++)
	{
		if (_line_[i] != TAB_CHAR && _line_[i] != SPACE_CHAR)
			strncpy(_tokens_+ _index_++, &_line_[i], 1);
	}

	return _index_;
}

bool isgoodline(const char *_token_line, unsigned long _token_size, bool &_recall_)
{
	if (!_token_line)
		return false;

	int _line_sz = _token_size;
	
	char *_start_	= NULL;
	char *_end_		= NULL;

	if (!_recall_)
	{
		if (havecomment_1(_token_line))
			return false;

		if (havecomment_2(_token_line, _start_))
		{
			if (!comment_2_end(_token_line, _end_))
			{
				_recall_ = true;
				return (_start_ - _token_line) ? true : false;
			}

			if ((_end_ - _token_line) == _line_sz - 2)
				return (_start_ - _token_line) ? true : false;
		}
	}
	else
	{
		if (!comment_2_end(_token_line, _end_))
		{
			_recall_ = true;
			return false;
		}

		if ((_end_ - _token_line) == _line_sz - 2)
		{
			_recall_ = false;
			return false;
		}
	}

	return true;
}

bool havecomment_1(const char *_buf_)
{
	bool ret = false;

	if (!strncmp(_buf_, "//", 2))
		ret = true;

	return ret;
}

bool havecomment_2(const char *_buf_, char *&_where_)
{
	bool ret = false;

	const char *_found_ = strstr(_buf_, "/*");
	if (_found_)
		ret = true;

	_where_ = (char*)_found_;

	return ret;
}

bool comment_2_end(const char *_buf_, char *&_where_)
{
	bool ret = false;
	
	char *_found_ =(char*) strstr(_buf_, "*/");
	if (_found_)
		ret = true;

	_where_ = _found_;

	return ret;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions