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

Variadic template class to add numbers recursively during compilation

 The idea of having a class to add numbers (variable parameters) during compilation time recursively. Also wanted to restrict types to a single type while sending parameters to class member function. That said, if we mix int, float and double types to add function shall result in compilation error. How do we achieve this. The below is the code which actually helps to achieve this: <code> #include < fmt/format.h > template < typename T> class MyVarSumClass{     private :         T _sum = 0 ;     public :         template < typename ... TRest>         T add(T num, TRest... nums){             static_assert (std::conjunction<std::is_same<TRest, T>...>{}); /* Assert fails                if types are different */             _sum += num;             return add(nums...); // Next parameter packs gets picked recursively         }         // Base case         T add(T num){             _sum += num;             return _sum;         } }; int main() {     My

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(

A concept to a product (Kimidori [ 黄緑]) - Part 2

In the previous part , we have seen KIMIDORI [ 黄緑] detect if a URL is malicious. In this part, we will see the details that KIMIDORI [ 黄緑] fetches out of the URL provided. As an example, provided a safe URL, https://www.azuresys.com/, and let's see what it brings out: As we can see, the link is safe and the link is active, which means we can just click on the link to open it on IE.  Now it's time to look into the URL report (still under development):  We have URLs IP, Location, and HTTP Status code. The Report part is a sliding window, the Show Report button shows as well as hides the report. Show / Hide Report is a toggle button. Let's see if we get the same details for any bad (phishing / malicious) URL: Took an URL example from a phishing link and tested it. The tool detected it as not a good link (Screen Shot Below) & link does not activate unlike a safe URL: Now let's see the report part for more details including domain registration details: It looks like it&