Click here to Skip to main content
15,896,348 members
Articles / Programming Languages / C++

UTF-8 UTILITY FUNCTIONS IN C++ (Platform Independent Code)

Rate me:
Please Sign up or sign in to vote.
3.09/5 (14 votes)
6 Apr 2007CPOL2 min read 83.1K   1.7K   33  
This article describes the basics of UTF-8 and provides some utility functions for handling UTF-8. The code can be compiled for Windows as well as Linux.
/*
* Written by Boby Thomas Pazheparampil. (March 2007.)
* Platform independent code.  (I hope so)
* Tested with Windows 2000, XP, Cygwin and Linux Debian.
*/


#include "utf_util.h"


void Hex2UTF()
{

	string sInput;

	cout<<"Hex2UTF convertor.\nEnter Hex values.\nType exit to quit\n";
	while(1)
	{
		clrscr();
		char acInput[75] = {0};
		scanf("%s",acInput);

		sInput = acInput;


		if(sInput.compare("exit") == 0)
			break;

		string sResult = convertHex2UTF(sInput);

		cout<<sResult.c_str();

	} 
}


void UTF2Hex()
{

	string sInput;


	cout<<"UTF2Hex convertor.\nEnter UTF values.\nType exit to quit\n";
	while(1)
	{
		clrscr();
		char acInput[75] = {0};
		scanf("%s",acInput);

		sInput = acInput;


		if(sInput.compare("exit") == 0)
			break;

		string sResult = convertUTF2Hex(sInput);
		if(sResult.compare("error") == 0)
			cout<<"Invalid UTF character\n";
		else if((convertHex2UTF(sResult).compare(sInput) != 0) && (convertHex2UTF(sResult).compare("0"+sInput) != 0) )
			cout<<"Invalid UTF character.\n";
		else
			cout<<sResult.c_str();

	} 
}



void displayMenu()
{
	cout<<"============================================\n";
	cout<<"Select the option\n";
	cout<<"============================================\n";
	cout<<"1. Self-Test\n";
	cout<<"2. Hex2UTF\n";
	cout<<"3. UTF2Hex\n";
	cout<<"4. ValidateUTFFile\n";
	cout<<"x. exit\n";
}

void ValidateUTFFile()
{

	cout<<"Enter File Name:";

	char acInput[MAX_PATH] = {0};
	scanf("%s",acInput);

	string sInput = acInput;

	if(sInput.length() ==0)
	{
		cout<<"Failed to open file "<<sInput.c_str()<<"\n";
		return;
	}



	if(generateUTFFileDetails(sInput))
		cout<<"Completed the validation. No invalid UTF character found.See utfdetails_"<<sInput.c_str()<<" for details\n";
	else
		cout<<"Completed the validation with errors.See utfdetails_"<<sInput.c_str()<<" for details\n";

	fflush(stdin);
	getchar();
	fflush(stdin);
}

void testAll()
{

	TESThex2binary();

	TESTbinary2hex();

	TESTconvertHex2UTF();

	TESTconvertUTF2Hex();

	TESTfindLengthUTF();

	TESTgenerateUTFFileDetails();
}

int main()
{

	cout<<"============================================\n";
	cout<<"     written by Boby Thomas                 \n";	
	cout<<"============================================\n";

	cout<<"\n\n\n";
	cout<<"============================================\n";
	cout<<"        UTF UTILITY\n";
	cout<<"============================================\n";

	bool isExit = false;


	while(1)
	{
		displayMenu();
		fflush(stdin);
		
		char acInput[30] = {0};
		scanf("%s",acInput);

		switch(acInput[0])
		{
		case '1':
			testAll();
			break;
		case '2':
			Hex2UTF();
			break;
		case '3':
			UTF2Hex();
			break;
		case '4':
			ValidateUTFFile();
			break;
		case 'x':
			isExit = true;
			break;
		}

		if(isExit)
			break;
	}

	return 1;

}

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 The Code Project Open License (CPOL)


Written By
Software Developer (Senior) DWS
Australia Australia

Comments and Discussions