Skip to main content

MBCS/Unicode enabled C++ string class.

TCHAR, the generic text mapping data type. This is Microsoft specific extension and is not ANSI-compatible. I have used this extension to create my small prototype MBCS/Unicode compatible class. This class consists of all the basic functionality required to represent a minimal string class. I have given the name for this class as "CStringUNI".
// Header File
#pragma once

const int INIT_ALLOC_SIZE = 10;

class CStringUNI
{
private:
 TCHAR *m_szBuffer;
 TCHAR *AllocateMemory(size_t size);

public:
 CStringUNI();
 CStringUNI(const CStringUNI&);
 CStringUNI(const TCHAR*);
 CStringUNI& operator=(const TCHAR*);
 CStringUNI& operator=(const CStringUNI&);
 CStringUNI& operator+=(const CStringUNI&);
 
 // Access Operator
 TCHAR operator[](const size_t n)const;

 TCHAR *GetBuffer() const;

 bool operator==(const TCHAR*) const;
 bool operator==(const CStringUNI&) const;
 virtual ~CStringUNI();

 friend CStringUNI operator+(const CStringUNI& lhs, const CStringUNI& rhs);
};
// CPP file, the actual implemenation file.

#include "StdAfx.h"
#include "StringUNI.h"
#include &ltstring.h&gt

TCHAR *CStringUNI::AllocateMemory(size_t size)
{
 m_szBuffer = new TCHAR[size];

 return m_szBuffer;
}

CStringUNI::CStringUNI()
{
 m_szBuffer = NULL;
}

CStringUNI::CStringUNI(const CStringUNI& source)
{
 const size_t nSizeSource = _tcslen(source.m_szBuffer);
 AllocateMemory(nSizeSource + 1);
 memset(m_szBuffer, 0, nSizeSource);
 _tcscpy(m_szBuffer, source.m_szBuffer);
}

CStringUNI::CStringUNI(const TCHAR* source)
{
 AllocateMemory(_tcslen(source)+ 1);
 memset(m_szBuffer, 0, _tcslen(source)+ 1);
 _tcscpy(m_szBuffer, source);
}

CStringUNI& CStringUNI::operator=(const TCHAR* source)
{
 size_t nSourcelen = _tcslen(source);
 
 if(m_szBuffer != NULL)
 {
  delete[] m_szBuffer;
  m_szBuffer = NULL;
 }

 AllocateMemory(nSourcelen + 1);
 memset(m_szBuffer, 0, (nSourcelen + 1));
 _tcscpy(m_szBuffer, source);

 return *this;
}

CStringUNI& CStringUNI::operator=(const CStringUNI& source)
{
 return operator=(source.m_szBuffer);
}

CStringUNI& CStringUNI::operator+=(const CStringUNI& source)
{
 size_t nSourceLen = _tcslen(source.m_szBuffer);
 size_t nBufferLen = _tcslen(m_szBuffer);

 TCHAR *pTempChar = new TCHAR[nSourceLen + nBufferLen + 1];
 _tcscpy(pTempChar, m_szBuffer);
 _tcscat(pTempChar, source.m_szBuffer);

 if(m_szBuffer != NULL)
 {
  delete []m_szBuffer;
  m_szBuffer = NULL;
 }

 m_szBuffer = pTempChar;
 
 return *this;
}

CStringUNI operator+(const CStringUNI& lhs, const CStringUNI& rhs)
{
 return CStringUNI(lhs) += rhs;
}

bool CStringUNI::operator ==(const TCHAR* source)const
{
 return (!_tcscmp(m_szBuffer, source));
}

bool CStringUNI::operator==(const CStringUNI& source) const
{
 return (!_tcscmp(source.m_szBuffer, m_szBuffer));
}

TCHAR CStringUNI::operator[](const size_t n)const
{
 return m_szBuffer[n];
}

TCHAR * CStringUNI::GetBuffer()const
{
 return m_szBuffer;
}

CStringUNI::~CStringUNI(void)
{
 if(m_szBuffer != NULL)
 {
  delete [] m_szBuffer;
  m_szBuffer = NULL;
 }
}
The following lines demonstrates the usage of this class.
// effString.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "StringUNI.h"
#include &ltlocale.h&gt
#include &ltwindows.h&gt

int _tmain(int argc, _TCHAR* argv[])
{
 
 TCHAR *locale = _tsetlocale(LC_ALL, L"Japanese");
 CStringUNI objString(L"日本語がわかりません");

 CStringUNI objString1(L"日本語でなんと言いますか");

 objString += objString1;
 
 wprintf(L"%s\n", objString.GetBuffer());
 return 0;
}

In the above mentioned lines, I tried to create two different objects of type CStringUNI and then concatenated two objects and displayed the cancatenated string on the output window.
Once we run this program, after setting locale for the system to Japanese, we will get following output: -

Comments

Popular posts from this blog

Reversing char array without splitting the array to tokens

 I was reading about strdup, a C++ function and suddenly an idea came to my mind if this can be leveraged to aid in reversing a character array without splitting the array into words and reconstructing it again by placing spaces and removing trailing spaces. Again, I wanted an array to be passed as a function argument and an array size to be passed implicitly with the array to the function. Assumed, a well-formed char array has been passed into the function. No malformed array checking is done inside the function. So, the function signature and definition are like below: Below is the call from the client code to reverse the array without splitting tokens and reconstructing it. Finally, copy the reversed array to the destination.  For GNU C++, we should use strdup instead _strdup . On run, we get the following output: Demo code

A simple approach to generate Fibonacci series via multi-threading

T his is a very simple approach taken to generate the Fibonacci series through multithreading. Here instead of a function, used a function object. The code is very simple and self-explanatory.  #include <iostream> #include <mutex> #include <thread> class Fib { public:     Fib() : _num0(1), _num1(1) {}     unsigned long operator()(); private:     unsigned long _num0, _num1;     std::mutex mu; }; unsigned long Fib::operator()() {     mu.lock(); // critical section, exclusive access to the below code by locking the mutex     unsigned long  temp = _num0;     _num0 = _num1;     _num1 = temp + _num0;     mu.unlock();     return temp; } int main() {     Fib f;          int i = 0;     unsigned long res = 0, res2= 0, res3 = 0;     std::cout << "Fibonacci series: ";     while (i <= 15) {         std::thread t1([&] { res = f(); }); // Capturing result to respective variable via lambda         std::thread t2([&] { res2 = f(); });         std::thread t3(

Close a Window Application from another application.

 This is just a demo application code to show how the WM_CLOSE message can be sent to the target process which has a titled window to close the application. To achieve this, either we can use SendMessage or PostMessage APIs to send required Windows messages to the target application. Though both the APIs are dispatching WM_XXXXX message to target application two APIs has some differences, these are as below: 1. SendMessage () call is a blocking call but PostMessage is a non-blocking call(Asynchronous) 2. SendMessage() APIs return type is LRESULT (LONG_PTR) but PostMessage() APIs return type is BOOL(typedef int). In Short, SendMessage () APIs return type depends on what message has been sent to the Windowed target process. For the other one, it's always a non-zero value, which indicates the message has been successfully placed on the target process message queue. Now let's see how can I close a target windowed application "Solitaire & Casual Games" from my custom-