Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C++

String Handling in XPCOM

Rate me:
Please Sign up or sign in to vote.
4.78/5 (8 votes)
16 Aug 2010CPOL7 min read 50.2K   313   9  
Using different types of strings in XPCOM.
#include "Sample.h"
#include "nsStringAPI.h"
#include <stdio.h>


//This macro automatically adds the nsISupports entry
NS_IMPL_ISUPPORTS1(CSample, ISample)

CSample::CSample()
{
	/* member initializers and constructor code */
}

CSample::~CSample()
{
	/* destructor code */
}

/* long Add (in long a, in long b); */
NS_IMETHODIMP CSample::Add(PRInt32 a, PRInt32 b, PRInt32 *_retval)
{
	*_retval = a + b;
	
	return NS_OK;
}

/*  string ReverseString(in string s); */
NS_IMETHODIMP CSample::ReverseString(const char *s, char **_retval)
{
	char sTemp[256] = {'\0'};
	
	int nLen = strlen(s);

	*_retval = (char*)NS_Alloc(sizeof(char)*256);
	for (int i = 0; i < nLen; i++)
	{
		sTemp[i] = s[nLen-1-i];
	}

	strcpy(*_retval, sTemp);		
	
	return NS_OK;
}

/* AString ReverseAString (in AString nStr); */
NS_IMETHODIMP CSample::ReverseAString(const nsAString & nStr, nsAString & _retval)
{
	nsAutoString strAuto;
	strAuto.Assign(nStr);
	nsAutoString strAutoTemp;

	int nLen = strAuto.Length();
	
	wchar_t wchTemp[256] = {'\0'};
	
	for (int i = 0; i < nLen; i++)
	{
		wchTemp[i] = strAuto[nLen-1-i];		
	}
	
	_retval.Assign(wchTemp);


	return NS_OK;	
}

/* wstring ReverseUnichar (in wstring nStr); */
NS_IMETHODIMP CSample::ReverseUnichar(const PRUnichar *nStr, PRUnichar **_retval NS_OUTPARAM)
{
	nsAutoString strAuto;
	strAuto.Assign(nStr);

	int nLen = strAuto.Length();
	*_retval = (PRUnichar*)NS_Alloc(nLen*sizeof(PRUnichar));

	PRUnichar nStrTemp[256] = {'\0'};

	for (int i = 0; i < nLen; i++)
	{
		nStrTemp[i] = strAuto[nLen-1-i];		
	}

	wcscpy(*_retval, nStrTemp);		
	return NS_OK;
}

/* ACString ReverseACString (in ACString nStr); */
NS_IMETHODIMP CSample::ReverseACString(const nsACString & nStr, nsACString & _retval NS_OUTPARAM)
{
	nsCAutoString strAuto;
	strAuto.Assign(nStr);
	nsCAutoString strAutoTemp;

	int nLen = strAuto.Length();
	
	char chTemp[256] = {'\0'};
	
	for (int i = 0; i < nLen; i++)
	{
		chTemp[i] = strAuto[nLen-1-i];	
	}

	_retval.Assign(chTemp);


	return NS_OK;
}

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) Eyeball Networks Inc, Dhaka
Bangladesh Bangladesh
I strongly believe, a person's strongest point is to know his/her weak point. I try to be simple.

I have completed BSc in Computer Science & Engineering from Shah Jalal University of Science & Technology, Sylhet, Bangladesh (SUST).


Watch out for my other CodeProject Articles.

Comments and Discussions