Click here to Skip to main content
15,894,907 members
Articles / Programming Languages / C++

Resource Comparer

Rate me:
Please Sign up or sign in to vote.
4.85/5 (19 votes)
23 Aug 2004Ms-RL6 min read 98.8K   1.9K   37  
Compares two .RC-files and shows important differences. Useful if you create programs in multiple languages.
#include "stdafx.h"
#include "globals.h"

CString PercentChecker(const CString s1, const CString s2);
CString FullChecker(const CString s1, const CString s2);

void Log(const CString& text)
{
	TRACE(_T("Log: %s\n"), text);
}

CString GetWord(const stdstring& s2, const int num, const stdstring separators)
{
	CString temp = s2.c_str();
	temp.Replace(_T("\"\""), _T("\\\""));
	stdstring s=temp;

	int start=0, end=0, found=0;
	bool inQuote=false, lastInWord=false;

	for(int i=0; i<=(int)s.size(); i++)
	{
		//if(!inQuote && (s[i] == ' ' || s[i] == '\t' || s[i] == ',' || s[i] == '\0'))
		if(!inQuote && (separators.find(s[i]) != stdstring::npos || s[i] == '\0'))
		{
			if(lastInWord)
			{
				end=i-1;
				found++;

				if(found > num)
				{
					stdstring ret = s.substr(start, end-start+1);

					if(ret == _T("\\\"")) return _T("");

					return (CString) ret.c_str();
					/*CString temp = ret.c_str();
					temp.Replace(_T("\t"), _T(" "));
					temp.TrimLeft();
					temp.TrimRight();
					return temp;*/
				}

				lastInWord = false;

			}
		}
		else if (s[i] == '\"' && (i==0 || s[i-1] != '\\'))
		{
			inQuote = !inQuote;

			if(!inQuote)
			{
				end=i-1;
				found++;

				if(found > num)
				{
					stdstring ret = s.substr(start, end-start+1);

					if(ret == _T("\\\"")) return _T("");

					return (CString) ret.c_str();
					/*CString temp = ret.c_str();
					temp.Replace(_T("\t"), _T(" "));
					temp.TrimLeft();
					temp.TrimRight();
					return temp;*/
				}

				lastInWord = false;
			}
		}
		//else if(!lastInWord && s[i] != ' ' && s[i] != '\t' && s[i] != ',')
		else if(!lastInWord && separators.find(s[i]) == stdstring::npos)
		{
			lastInWord=true;
			start = i;
		}
	}

	return _T("");

	/*
	const stdstring separators=" ,\t";

	int start=0, end=-1, found=0;



	while(found <= num)
	{
		start = s.find_first_not_of(separators, end+1);

		if(start == stdstring::npos)
			return _T("");

		found++;

		end = s.find_first_of(separators, start);

		if(end == stdstring::npos)
		{
			if(found <= num)
				return _T("");

			end = s.size();
		}	
		else
			end--;
	}


	stdstring ret = s.substr(start, end-start+1);

	return (CString) ret.c_str();*/
}

bool GotoLine(CTextFileRead &file, const stdstring& word, const int num)
{
	while(!file.Eof())
	{
		stdstring line;
		//getline(file, line);
		file.ReadLine(line);

		if(GetWord(line, num) == word.c_str())
			return true;
	}

	return false;
}

int Count(const CString &s, const CString &what)
{
	int count = 0, pos = 0;

	while( 1 )
	{
		pos = s.Find(what, pos);

		if(pos != -1)
		{
			count++;
			pos += what.GetLength();
		}
		else
			return count;
	}

	return 0;
}

CString NextPercentWord(const CString& s, int &pos)
{
	int start = s.Find(_T("%"), pos);

	if(start == -1)
		return "";

	int end=start+1;

	while(end<s.GetLength())
	{
		TCHAR ch = s.GetAt(end);

		if(isalnum(ch) || (start+1==end && ch =='%'))
			end++;
		else
		{
			CString ret = s.Mid(start, end-start);
			pos=end;
			return ret;
		}
	}

	//return "";
	CString ret = s.Mid(start, end-start);
	pos=end;
	return ret;
}

CString PercentChecker(const CString s1, const CString s2)
{
	int c1=Count(s1, "%"), c2=Count(s2, "%");
	CString word[2];
	int pos[2] = {0, 0};

	if(c1 != c2)
	{
		CString temp;
		temp.Format(_T("Different number of %%-signs ('%s', '%s')"), s1, s2);

		return temp;
	}

	if(c1>0)
	{
		do
		{
			word[0] = NextPercentWord(s1, pos[0]);
			word[1] = NextPercentWord(s2, pos[1]);

			if(word[0] != word[1])
			{
				CString temp;
				temp.Format(_T("Different %%-words (%s and %s)"), word[0], word[1]);

				return temp;
			}

		}while(!word[0].IsEmpty());
	}

	return "";
}

CString NextSlashWord(const CString& s, int &pos)
{
	int start = s.Find(_T("\\"), pos);

	if(start == -1)
		return "";

	//return "";
	CString ret = s.Mid(start, 2);
	pos=start+2;
	return ret;
}

CString SlashChecker(const CString s1, const CString s2)
{
	int c1=Count(s1, "\\"), c2=Count(s2, "\\");
	CString word[2];
	int pos[2] = {0, 0};

	if(c1 != c2)
	{
		CString temp;
		temp.Format(_T("Different number of \\-signs ('%s', '%s')"), s1, s2);

		return temp;
	}

	if(c1>0)
	{
		do
		{
			word[0] = NextSlashWord(s1, pos[0]);
			word[1] = NextSlashWord(s2, pos[1]);

			if(word[0] != word[1])
			{
				CString temp;
				temp.Format(_T("Different \\-characters (%s and %s)"), word[0], word[1]);

				return temp;
			}

		}while(!word[0].IsEmpty());
	}

	return "";
}


CString FullChecker(const CString s1, const CString s2)
{
	CString temp;
	
	if(s1 != s2)
	{	
		temp.Format(_T("Strings doesn't match: 1: '%s', 2: '%s')"), s1, s2);
	}

	return temp;
}

CString StringCheck(const CString &s1, const CString &s2, int method)
{
	if(method == 0)
		return _T("");
	else if(method == 1)
		return PercentChecker(s1, s2);
	else if(method == 2)
		return SlashChecker(s1, s2);
	else if(method == 3)
	{
		CString a = PercentChecker(s1, s2), b = SlashChecker(s1, s2);
		
		if(a.IsEmpty() && b.IsEmpty())
			return _T("");
		
		if(a.IsEmpty())
			return b;
		if(b.IsEmpty())
			return a;

		return a + _T(", ") + b;
	}
	else
		return FullChecker(s1, s2);
}

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, along with any associated source code and files, is licensed under Microsoft Reciprocal License


Written By
PEK
Sweden Sweden
PEK is one of the millions of programmers that sometimes program so hard that he forgets how to sleep (this is especially true when he has more important things to do). He thinks that there are not enough donuts in the world. He likes when his programs works as they should do, but dislikes when his programs is more clever than he is.

Comments and Discussions