Wednesday 8 February 2017

Convert old ANSI VC++ project to generic (i.e. unicode as well as MBCS) VC++ project

Introduction

In today’s world most of the programmer writes their application code using UNICODE character set, but still there are lots of applications running on legacy code. To convert legacy code into Unicode is cumbersome job. But still it is necessary to convert legacy code into Unicode. So here I am presenting an application which will help to convert legacy code into Unicode. Application I have submitted is not useful to convert legacy code into Unicode 100% but it will help lot.

Article references make me to write this utility

While going through the article, "Neatpad overview - Design & Implementation of a Win32 Text Editor" present on catch22.net I come to the part "Introdution to Unicode". This part of the article makes me to write this utility.
http://www.catch22.net/tuts/introduction-unicode

Settings

Before going for processing or after processing your application make following change into your project:  For that right click on your project and select project properties. In Configuration properties select general and change value of Character Set to “Use Unicode Character Set”.

Using the code

Class I have written in this article, "CANSICodeFileToUnicodeCodeFile" is main part of this utility which takes responsiblity to convert source code so it will able to run as UNICODE. This class contains following functions which do main job of conversion:

ReplaceAnsiSubStringWithGenericSubString: 

This function search for ANSI string in project submitted by user for conversion and convert it into generic string.
void CANSICodeFileToUnicodeCodeFile::ReplaceAnsiSubStringWithGenericSubString(
                                                        /*[IN]*/std::wstring strStringToProcess)
/* =======================================================================================
Function :      CANSICodeFileToUnicodeCodeFile::ReplaceAnsiSubStringWithGenericSubString
Author:         Satish Jagtap
Description :   Ansi character string is enclosed in "". To work with both MBCS build and 
                Unicode build we need to enclose string within _T(""), which makes string generic.
Access :        private
Return :        nothing
Parameters :    [IN] std::wstring strStringToProcess    - String to work on.
Call to:        1) ReplaceWholeWordInString
Call from:      1) MakeANSIFileToUnicodeReady
Usage :         The function is used to replace "" with _T("").
Date:           15-04-2015
========================================================================================*/
{
    int nLength = strStringToProcess.length();

    for(int nLoopIndex = 0; nLoopIndex <= nLength; nLoopIndex++)
    {
        static wstring strOriginalStringToProcess = strStringToProcess;
        bool bIsWideString = true;

        std::wstring::size_type nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\"'));
        if(nFirstQuotePosition == string::npos)
        {
            m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;

            return;
        }

        int nLastThreeChars = nFirstQuotePosition - 3;

        //check if "_T(" is already present. If present assign false to bIsWideString.
        if (nLastThreeChars >= 0)
        {
            if(strStringToProcess.substr((nFirstQuotePosition - 3), 3) == _T("_T("))
            {
                bIsWideString = false;
            }
        }

        if(bIsWideString)
        {
            strStringToProcess.erase(nFirstQuotePosition, 1);
            strStringToProcess.insert(nFirstQuotePosition, _T("_T(\""));

            nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\"'));
            std::wstring::size_type nSecondQuotePosition = strStringToProcess.find_first_of(_T('\"'), 
                                                                            (nFirstQuotePosition + 1));

            if(nSecondQuotePosition != string::npos)
            {
                strStringToProcess.erase(nSecondQuotePosition, 1);
                strStringToProcess.insert(nSecondQuotePosition, _T("\")"));

                nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\"'));
                nSecondQuotePosition = strStringToProcess.find_first_of(_T('\"'), 
                                                              (nFirstQuotePosition + 1));
                std::wstring strTemp = strStringToProcess.substr (0, (nSecondQuotePosition + 2));
                strStringToProcess.erase(0, (nSecondQuotePosition + 2));

                m_strReplaceAnsiSubStringWithGenericSubString += strTemp;

                nLength = strStringToProcess.length();

                if(nLoopIndex >= nLength)
                {
                    nLoopIndex = 0;
                }
            }
            else
            {
                m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;
            }
        }
        else
        {
            nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\"'));
            std::wstring::size_type nSecondQuotePosition = strStringToProcess.find_first_of(_T('\"'), 
                                                                             (nFirstQuotePosition + 1));

            if(nSecondQuotePosition != string::npos)
            {
                std::wstring strTemp = strStringToProcess.substr (0, (nSecondQuotePosition + 2));
                strStringToProcess.erase(0, (nSecondQuotePosition + 2));

                m_strReplaceAnsiSubStringWithGenericSubString += strTemp;

                nLength = strStringToProcess.length();
            }
            else
            {
                m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;
            }
        }
    }
}

ReplaceUnicodeSubStringWithGenericSubString: 

This function search for UNICODE string in project submitted by user for conversion and convert it into generic string.
void CANSICodeFileToUnicodeCodeFile::ReplaceUnicodeSubStringWithGenericSubString(
                                                     /*[IN]*/std::wstring strStringToProcess)
/* ===============================================================================================
Function :      CANSICodeFileToUnicodeCodeFile::ReplaceUnicodeSubStringWithGenericSubString
Author:         Satish Jagtap
Description :   Unicode character string is generally prefixed with L means string is enclosed in L"". 
                To work with both MBCS build and Unicode build we need to enclose string within _T(""), 
                which makes string generic.
Access :        private
Return :        nothing
Parameters :    [IN] std::wstring strStringToProcess - String to work on.
Call to:        1) ReplaceWholeWordInString
Call from:      1) MakeANSIFileToUnicodeReady
Usage :         The function is used to replace L"" with _T("").
Date:           15-04-2015
================================================================================================*/
{
    int nLength = strStringToProcess.length();

    for(int nLoopIndex = 0; nLoopIndex <= nLength; nLoopIndex++)
    {
        static wstring strOriginalStringToProcess = strStringToProcess;
        bool bIsWideString = true;

        std::wstring::size_type nFirstQuotePosition  = strStringToProcess.find_first_of(_T("L\""));
        if(nFirstQuotePosition == string::npos)
        {
            m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;

            return;
        }
    
        int nLastThreeChars = nFirstQuotePosition - 3;

        //check if "_T(" is already present. If present assign false to bIsWideString.
        if (nLastThreeChars >= 0)
        {
            if(strStringToProcess.substr((nFirstQuotePosition - 3), 3) == _T("_T("))
            {
                bIsWideString = false;
            }
        }

        if(bIsWideString)
        {
            strStringToProcess.erase(nFirstQuotePosition, 1);
            strStringToProcess.insert(nFirstQuotePosition, _T("_T("));

            nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\"'));
            std::wstring::size_type nSecondQuotePosition = strStringToProcess.find_first_of(_T('\"'), (nFirstQuotePosition + 1));

            if(nSecondQuotePosition != string::npos)
            {
                strStringToProcess.erase(nSecondQuotePosition, 1);
                strStringToProcess.insert(nSecondQuotePosition, _T("\")"));
    
                nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\"'));
                nSecondQuotePosition = strStringToProcess.find_first_of(_T('\"'), (nFirstQuotePosition + 1));
                std::wstring strTemp = strStringToProcess.substr (0, (nSecondQuotePosition + 2));
                strStringToProcess.erase(0, (nSecondQuotePosition + 2));

                m_strReplaceAnsiSubStringWithGenericSubString += strTemp;

                nLength = strStringToProcess.length();

                if(nLoopIndex >= nLength)
                {
                    nLoopIndex = 0;
                }
            }
            else
            {
                m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;
            }
        }
        else
        {
            nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\"'));
            std::wstring::size_type nSecondQuotePosition = strStringToProcess.find_first_of(_T('\"'), 
                                                                             (nFirstQuotePosition + 1));

            if(nSecondQuotePosition != string::npos)
            {
                std::wstring strTemp = strStringToProcess.substr (0, (nSecondQuotePosition + 2));
                strStringToProcess.erase(0, (nSecondQuotePosition + 2));

                m_strReplaceAnsiSubStringWithGenericSubString += strTemp;

                nLength = strStringToProcess.length();
            }
            else
            {
                m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;
            }
        }
    }
}

ReplaceAnsiCharWithGenericCharInString: 

This function search for ANSI char in project submitted by user for conversion and convert it into generic char.
void CANSICodeFileToUnicodeCodeFile::ReplaceAnsiCharWithGenericCharInString(
                                                           /*[IN]*/std::wstring strStringToProcess)
/* =============================================================================================
Function :     CANSICodeFileToUnicodeCodeFile::ReplaceAnsiCharWithGenericCharInString
Author:        Satish Jagtap
Description :  Ansi character is generally enclosed in ''. To work with both MBCS build and Unicode 
               build we need to enclose string within _T(''), which makes string generic.
Access :       private
Return :       nothing
Parameters :   [IN] std::wstring strStringToProcess - String to work on.
Call to:       1) ReplaceWholeWordInString
Call from:     1) MakeANSIFileToUnicodeReady
Usage :        The function is used to replace L'' with _T('').
Date:          15-04-2015
===============================================================================================*/
{
    int nLength = strStringToProcess.length();

    for(int nLoopIndex = 0; nLoopIndex <= nLength; nLoopIndex++)
    {
        static wstring strOriginalStringToProcess = strStringToProcess;
        bool bIsWideString = true;

        std::wstring::size_type nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\''));
        if(nFirstQuotePosition == string::npos)
        {
            m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;

            return;
        }
    
        int nLastThreeChars = nFirstQuotePosition - 3;

        //check if "_T(" is already present. If present assign false to bIsWideString.
        if (nLastThreeChars >= 0)
        {
            if(strStringToProcess.substr((nFirstQuotePosition - 3), 3) == _T("_T("))
            {
                bIsWideString = false;
            }
        }

        if(bIsWideString)
        {
            strStringToProcess.erase(nFirstQuotePosition, 1);
            strStringToProcess.insert(nFirstQuotePosition, _T("_T(\'"));

            nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\''));
            std::wstring::size_type nSecondQuotePosition = strStringToProcess.find_first_of(_T('\''), 
                                                                             (nFirstQuotePosition + 1));

            if(nSecondQuotePosition != string::npos)
            {
                strStringToProcess.erase(nSecondQuotePosition, 1);
                strStringToProcess.insert(nSecondQuotePosition, _T("\')"));

    
                nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\''));
                nSecondQuotePosition = strStringToProcess.find_first_of(_T('\''), 
                                                              (nFirstQuotePosition + 1));
                std::wstring strTemp = strStringToProcess.substr (0, (nSecondQuotePosition + 2));
                strStringToProcess.erase(0, (nSecondQuotePosition + 2));

                m_strReplaceAnsiSubStringWithGenericSubString += strTemp;

                nLength = strStringToProcess.length();

                if(nLoopIndex >= nLength)
                {
                    nLoopIndex = 0;
                }
            }
            else
            {
                m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;
            }
        }
        else
        {
            nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\''));
            std::wstring::size_type nSecondQuotePosition = strStringToProcess.find_first_of(_T('\''), 
                                                                             (nFirstQuotePosition + 1));

            if(nSecondQuotePosition != string::npos)
            {
                std::wstring strTemp = strStringToProcess.substr (0, (nSecondQuotePosition + 2));
                strStringToProcess.erase(0, (nSecondQuotePosition + 2));

                m_strReplaceAnsiSubStringWithGenericSubString += strTemp;

                nLength = strStringToProcess.length();
            }
            else
            {
                m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;
            }
        }
    }
}

ReplaceUnicodeCharWithGenericChar: 

This function search for UNICODE char in project submitted by user for conversion and convert it into generic char.
void CANSICodeFileToUnicodeCodeFile::ReplaceUnicodeCharWithGenericChar(
                                                          /*[IN]*/std::wstring strStringToProcess)
/* ================================================================================================
Function :     CANSICodeFileToUnicodeCodeFile::ReplaceUnicodeCharWithGenericChar
Author:        Satish Jagtap
Description :  Unicode character is generally prefixed with L means string is enclosed in L''. To work 
               with both MBCS build and Unicode build we need to enclose string within _T(''), which 
               makes string generic.
Access :       private
Return :       nothing
Parameters :   [IN] std::wstring strStringToProcess - String to work on.
Call to:       1) ReplaceWholeWordInString
Call from:     1) MakeANSIFileToUnicodeReady
Usage :        The function is used to replace L'' with _T('').
Date:          15-04-2015
==================================================================================================*/
{
    int nLength = strStringToProcess.length();

    for(int nLoopIndex = 0; nLoopIndex <= nLength; nLoopIndex++)
    {
        static wstring strOriginalStringToProcess = strStringToProcess;
        bool bIsWideString = true;

        std::wstring::size_type nFirstQuotePosition  = strStringToProcess.find_first_of(_T("L\'"));
        if(nFirstQuotePosition == string::npos)
        {
            m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;

            return;
        }
    
        int nLastThreeChars = nFirstQuotePosition - 3;

        //check if "_T(" is already present. If present assign false to bIsWideString.
        if (nLastThreeChars >= 0)
        {
            if(strStringToProcess.substr((nFirstQuotePosition - 3), 3) == _T("_T("))
            {
                bIsWideString = false;
            }
        }

        if(bIsWideString)
        {
            strStringToProcess.erase(nFirstQuotePosition, 1);
            strStringToProcess.insert(nFirstQuotePosition, _T("_T("));

            nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\''));
            std::wstring::size_type nSecondQuotePosition = strStringToProcess.find_first_of(_T('\''), (nFirstQuotePosition + 1));

            if(nSecondQuotePosition != string::npos)
            {
                strStringToProcess.erase(nSecondQuotePosition, 1);
                strStringToProcess.insert(nSecondQuotePosition, _T("\')"));

    
                nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\''));
                nSecondQuotePosition = strStringToProcess.find_first_of(_T('\''), (nFirstQuotePosition + 1));
                std::wstring strTemp = strStringToProcess.substr (0, (nSecondQuotePosition + 2));
                strStringToProcess.erase(0, (nSecondQuotePosition + 2));

                m_strReplaceAnsiSubStringWithGenericSubString += strTemp;

                nLength = strStringToProcess.length();

                if(nLoopIndex >= nLength)
                {
                    nLoopIndex = 0;
                }
            }
            else
            {
                m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;
            }
        }
        else
        {
            nFirstQuotePosition  = strStringToProcess.find_first_of(_T('\''));
            std::wstring::size_type nSecondQuotePosition = strStringToProcess.find_first_of(_T('\''), (nFirstQuotePosition + 1));

            if(nSecondQuotePosition != string::npos)
            {
                std::wstring strTemp = strStringToProcess.substr (0, (nSecondQuotePosition + 2));
                strStringToProcess.erase(0, (nSecondQuotePosition + 2));

                m_strReplaceAnsiSubStringWithGenericSubString += strTemp;

                nLength = strStringToProcess.length();
            }
            else
            {
                m_strReplaceAnsiSubStringWithGenericSubString += strStringToProcess;
            }
        }
    }
}

ReplaceWin32APIsWithGenericEquivalent: 

This function search for Win 32 APIs in project submitted by user for conversion and convert them to generic equivalent.
void CANSICodeFileToUnicodeCodeFile::ReplaceWin32APIsWithGenericEquivalent(
                                                        /*[IN]*/std::wstring strStringToProcess)
/* ==============================================================================================
Function :      CANSICodeFileToUnicodeCodeFile::ReplaceWin32APIsWithGenericEquivalent
Author:         Satish Jagtap
Description :   There are many Win32 APIs having different variants for ANSI/MBCS build and Unicode 
                build. To build application in both build (or Uniocde) we need to replace those APIs 
                with generic Win32 APIs.
Access :        private
Return :        nothing
Parameters :    1) [IN] std::wstring strStringToProcess - String to work on.
Call to:        1) ReplaceWholeWordInString
Call from:      1) MakeANSIFileToUnicodeReady
Usage :         This function is used to replace ansi win32 api to its generic versions.
Date:           15-04-2015
===============================================================================================*/
{
    if(strStringToProcess.length() > 3) //Check if length of source string is greater than 3 characters
    {
        //Replacing Win32 APIs with corresponding Generic equivalent
        ReplaceWholeWordInString(strStringToProcess, _T("_access"), _T("_taccess")); 
        ReplaceWholeWordInString(strStringToProcess, _T("_atoi64"), _T("_tstoi64")); //_atoi64 converts a string to a 64-bit integer.
        ReplaceWholeWordInString(strStringToProcess, _T("_cgets"), _T("cgetts")); //_cgets gets a character string from the console.
        ReplaceWholeWordInString(strStringToProcess, _T("_chdir"), _T("_tchdir")); //_chdir changes the current working directory.
        ReplaceWholeWordInString(strStringToProcess, _T("_chmod"), _T("_tchmod")); //_chmod changes the file-permission settings
        ReplaceWholeWordInString(strStringToProcess, _T("_getdcwd"), _T("_tgetdcwd")); // Gets the full path of the current working directory on the specified drive.
        ReplaceWholeWordInString(strStringToProcess, _T("_ltoa"), _T("_ltot")); //Converts a long integer to a string
        ReplaceWholeWordInString(strStringToProcess, _T("_makepath"), _T("_tmakepath")); //Create a path name from components.
        ReplaceWholeWordInString(strStringToProcess, _T("_mkdir"), _T("_tmkdir")); //Creates a new directory.
        ReplaceWholeWordInString(strStringToProcess, _T("_mktemp"), _T("_tmktemp")); //Creates a unique file name
        ReplaceWholeWordInString(strStringToProcess, _T("_open"), _T("_topen")); //Opens a file
        ReplaceWholeWordInString(strStringToProcess, _T("_popen"), _T("_tpopen")); //Creates a pipe and executes a command.
        ReplaceWholeWordInString(strStringToProcess, _T("_putch"), _T("_puttch")); //Writes a character to the console.
        ReplaceWholeWordInString(strStringToProcess, _T("_putenv"), _T("_tputenv")); //Creates, modifies, or removes environment variables. 
        ReplaceWholeWordInString(strStringToProcess, _T("_rmdir"), _T("_trmdir")); //The _rmdir function deletes the directory specified by dirname.
        ReplaceWholeWordInString(strStringToProcess, _T("_scprintf"), _T("_sctprintf")); //Returns the number of characters in the formatted string.
        ReplaceWholeWordInString(strStringToProcess, _T("_searchenv"), _T("_tsearchenv")); //Uses environment paths to search for a file.
        ReplaceWholeWordInString(strStringToProcess, _T("_snprintf"), _T("_sntprintf")); //Writes formatted data to a string
        ReplaceWholeWordInString(strStringToProcess, _T("_snscanf"), _T("_sntscanf")); //Reads formatted data of a specified length from a string.
        ReplaceWholeWordInString(strStringToProcess, _T("_sopen"), _T("_tsopen")); //Opens a file for sharing.
        ReplaceWholeWordInString(strStringToProcess, _T("_spawnl"), _T("_tspawnl")); //Creates and executes a new process.
        ReplaceWholeWordInString(strStringToProcess, _T("_spawnle"), _T("_tspawnle")); //This API cannot be used in applications that execute in the Windows Runtime.
        ReplaceWholeWordInString(strStringToProcess, _T("_spawnlp"), _T("_tspawnlp")); //Creates and executes a new process.
        ReplaceWholeWordInString(strStringToProcess, _T("_spawnlpe"), _T("_tspawnlpe")); //Creates and executes a new process.
        ReplaceWholeWordInString(strStringToProcess, _T("_spawnv"), _T("_tspawnv")); //Creates and executes a new process.
        ReplaceWholeWordInString(strStringToProcess, _T("_spawnve"), _T("_tspawnve")); //Creates and executes a new process.
        ReplaceWholeWordInString(strStringToProcess, _T("_spawnvp"), _T("_tspawnvp")); //Creates and executes a new process.
        ReplaceWholeWordInString(strStringToProcess, _T("_spawnvpe"), _T("_tspawnvpe")); //Creates and executes a new process.
        ReplaceWholeWordInString(strStringToProcess, _T("_splitpath"), _T("_tsplitpath")); //Break a path name into components.
        ReplaceWholeWordInString(strStringToProcess, _T("_stat64"), _T("_tstat64")); //Get status information on a file
        ReplaceWholeWordInString(strStringToProcess, _T("_stat"), _T("_tstat")); //Get status information on a file
        ReplaceWholeWordInString(strStringToProcess, _T("_stati64"), _T("_tstati64")); //Get status information on a file
        ReplaceWholeWordInString(strStringToProcess, _T("_strdate"), _T("_tstrdate")); //Copy current system date to a buffer
        ReplaceWholeWordInString(strStringToProcess, _T("_strdec"), _T("_tcsdec")); //Moves a string pointer back one character.
        ReplaceWholeWordInString(strStringToProcess, _T("_strdup"), _T("_tcsdup")); //Duplicates strings.
        ReplaceWholeWordInString(strStringToProcess, _T("_strinc"), _T("_tcsinc")); //Advances a string pointer by one character.
        ReplaceWholeWordInString(strStringToProcess, _T("_strlwr"), _T("_tcslwr")); //Converts a string to lowercase
        ReplaceWholeWordInString(strStringToProcess, _T("_strncnt"), _T("_tcsnbcnt")); //Returns the number of characters or bytes within a specified count.
        ReplaceWholeWordInString(strStringToProcess, _T("_strnextc"), _T("_tcsnextc")); //Finds the next character in a string.
        ReplaceWholeWordInString(strStringToProcess, _T("_strnicmp"), _T("_tcsnicmp")); //Compares characters of two strings without regard to case.
        ReplaceWholeWordInString(strStringToProcess, _T("_strninc"), _T("_tcsninc")); //Advances a string pointer by n characters.
        ReplaceWholeWordInString(strStringToProcess, _T("_strnset"), _T("_tcsnset")); //Initializes characters of a string to a given character
        ReplaceWholeWordInString(strStringToProcess, _T("_strrev"), _T("_tcsrev")); //Reverses the characters of a string.
        ReplaceWholeWordInString(strStringToProcess, _T("_strset"), _T("_tcsset")); //Sets characters of a string to a character.
        ReplaceWholeWordInString(strStringToProcess, _T("_strspnp"), _T("_tcsspnp")); //Returns a pointer to the first character in a given string that is not in another given string.
        ReplaceWholeWordInString(strStringToProcess, _T("_strtime"), _T("_tstrtime")); //Copy the time to a buffer.
        ReplaceWholeWordInString(strStringToProcess, _T("_strtoi64"), _T("_tcstoi64")); //Convert a string to an __int64 value.
        ReplaceWholeWordInString(strStringToProcess, _T("_strupr"), _T("_tcsupr")); //Converts a string to uppercase. 
        ReplaceWholeWordInString(strStringToProcess, _T("_tempnam"), _T("_ttempnam")); //Generate names you can use to create temporary files
        ReplaceWholeWordInString(strStringToProcess, _T("_ui64toa"), _T("_ui64tot")); //Converts an integer to a string.
        ReplaceWholeWordInString(strStringToProcess, _T("_ultoa"), _T("_ultot")); //Convert an unsigned long integer to a string. 
        ReplaceWholeWordInString(strStringToProcess, _T("_ungetch"), _T("_ungettch")); //Pushes back the last charcter read from the console.
        ReplaceWholeWordInString(strStringToProcess, _T("_unlink"), _T("_tunlink")); //Delete a file.
        ReplaceWholeWordInString(strStringToProcess, _T("_utime64"), _T("_tutime64")); //Set the file modification time.
        ReplaceWholeWordInString(strStringToProcess, _T("_utime"), _T("_tutime")); //Set the file modification time.
        ReplaceWholeWordInString(strStringToProcess, _T("_vscprintf"), _T("_vsctprintf")); //Returns the number of characters in the formatted string using a pointer to a list of arguments.
        ReplaceWholeWordInString(strStringToProcess, _T("_vsnprintf"), _T("_vsntprintf")); //Write formatted output using a pointer to a list of arguments. 
        ReplaceWholeWordInString(strStringToProcess, _T("asctime"), _T("_tasctime")); //Convert a tm time structure to a character string. 
        ReplaceWholeWordInString(strStringToProcess, _T("atof"), _T("_tstof")); //Convert a string to double.
        ReplaceWholeWordInString(strStringToProcess, _T("isascii"), _T("_istascii")); //Determines whether a particular character is an ASCII character.
        ReplaceWholeWordInString(strStringToProcess, _T("sprintf_s"), _T("_stprintf_s")); //Write formatted data to a string
        ReplaceWholeWordInString(strStringToProcess, _T("sprintf"), _T("_stprintf")); //Write formatted data to a string
        ReplaceWholeWordInString(strStringToProcess, _T("atol"), _T("_tstol")); //convert string to long
        ReplaceWholeWordInString(strStringToProcess, _T("atoi"), _T("_tstoi")); //convert string int
        ReplaceWholeWordInString(strStringToProcess, _T("atof"), _T("_tstof")); //convert string to double
        ReplaceWholeWordInString(strStringToProcess, _T("fopen_s"), _T("_tfopen_s")); //Opens a file.
        ReplaceWholeWordInString(strStringToProcess, _T("strtok_s"), _T("_tcstok_s")); //Finds the next token in a string, by using the current locale or a locale that's passed in.
        ReplaceWholeWordInString(strStringToProcess, _T("ctime"), _T("_tctime")); //Convert a time value to a string and adjust for local time zone settings.
        ReplaceWholeWordInString(strStringToProcess, _T("fgetc"), _T("_fgettc")); //Read a character from a stream.
        ReplaceWholeWordInString(strStringToProcess, _T("fgets"), _T("_fgetts")); //Get a string from a stream.
        ReplaceWholeWordInString(strStringToProcess, _T("fopen"), _T("_tfopen")); //Opens a file.
        ReplaceWholeWordInString(strStringToProcess, _T("fprintf"), _T("_ftprintf")); //Print formatted data to a stream. 
        ReplaceWholeWordInString(strStringToProcess, _T("fputc"), _T("_fputtc")); //Writes a character to a stream.
        ReplaceWholeWordInString(strStringToProcess, _T("fputs"), _T("_fputts")); //Writes a string to a stream.
        ReplaceWholeWordInString(strStringToProcess, _T("freopen"), _T("_tfreopen")); //Reassigns a file pointer. 
        ReplaceWholeWordInString(strStringToProcess, _T("fscanf"), _T("_ftscanf")); //Read formatted data from a stream. 
        ReplaceWholeWordInString(strStringToProcess, _T("getc"), _T("_gettc")); //Read a character from a stream.
        ReplaceWholeWordInString(strStringToProcess, _T("getchar"), _T("_gettchar")); //Reads a character from standard input.
        ReplaceWholeWordInString(strStringToProcess, _T("getenv"), _T("_tgetenv")); //Gets a value from the current environment.
        ReplaceWholeWordInString(strStringToProcess, _T("gets"), _T("_getts")); //Gets a line from the stdin stream.
        ReplaceWholeWordInString(strStringToProcess, _T("isalnum"), _T("_istalnum")); //Determines whether an integer represents an alphanumeric character.
        ReplaceWholeWordInString(strStringToProcess, _T("isalpha"), _T("_istalpha")); //Determines whether an integer represents an alphabetic character.
        ReplaceWholeWordInString(strStringToProcess, _T("iscntrl"), _T("_istcntrl")); //Determines whether an integer represents a control character.
        ReplaceWholeWordInString(strStringToProcess, _T("isdigit"), _T("_istdigit")); //Determines whether an integer represents a decimal-digit character.
        ReplaceWholeWordInString(strStringToProcess, _T("isgraph"), _T("_istgraph")); //Determines whether an integer represents a graphical character.
        ReplaceWholeWordInString(strStringToProcess, _T("isleadbyte"), _T("_istleadbyte")); //Determines whether a character is the lead byte of a multibyte character.
        ReplaceWholeWordInString(strStringToProcess, _T("islower"), _T("_istlower")); //Determines whether an integer represents a lowercase character.
        ReplaceWholeWordInString(strStringToProcess, _T("isprint"), _T("_istprint")); //Determines whether an integer represents a printable character.
        ReplaceWholeWordInString(strStringToProcess, _T("ispunct"), _T("_istpunct")); //Determines whether an integer represents a punctuation character.
        ReplaceWholeWordInString(strStringToProcess, _T("isspace"), _T("_istspace")); //Determines whether an integer represents a space character.
        ReplaceWholeWordInString(strStringToProcess, _T("isupper"), _T("_istupper")); //Determines whether an integer represents an uppercase character.
        ReplaceWholeWordInString(strStringToProcess, _T("isxdigit"), _T("_istxdigit")); //Determines whether an integer represents a character that is a hexadecimal digit.
        ReplaceWholeWordInString(strStringToProcess, _T("main"), _T("_tmain")); //
        ReplaceWholeWordInString(strStringToProcess, _T("perror"), _T("_tperror")); //Print an error message.
        ReplaceWholeWordInString(strStringToProcess, _T("printf"), _T("_tprintf")); //Prints formatted output to the standard output stream. 
        ReplaceWholeWordInString(strStringToProcess, _T("putc"), _T("_puttc")); //Writes a character to a stream.
        ReplaceWholeWordInString(strStringToProcess, _T("putchar"), _T("_puttchar")); //Writes a character to stdout.
        ReplaceWholeWordInString(strStringToProcess, _T("puts"), _T("_putts")); //Writes a string to stdout.
        ReplaceWholeWordInString(strStringToProcess, _T("remove"), _T("_tremove")); //Delete a file.
        ReplaceWholeWordInString(strStringToProcess, _T("rename"), _T("_trename")); //Rename a file or directory.
        ReplaceWholeWordInString(strStringToProcess, _T("scanf"), _T("_tscanf")); //Reads formatted data from the standard input stream. 
        ReplaceWholeWordInString(strStringToProcess, _T("setlocale"), _T("_tsetlocale")); //Sets or retrieves the run-time locale.
        ReplaceWholeWordInString(strStringToProcess, _T("sscanf"), _T("_stscanf")); //Read formatted data from a string.
        ReplaceWholeWordInString(strStringToProcess, _T("strcat"), _T("_tcscat")); //Appends a string.
        ReplaceWholeWordInString(strStringToProcess, _T("strchr"), _T("_tcschr")); //Finds a character in a string, by using the current locale or a specified LC_CTYPE conversion-state category.
        ReplaceWholeWordInString(strStringToProcess, _T("strcmp"), _T("_tcscmp")); //Compare strings.
        ReplaceWholeWordInString(strStringToProcess, _T("_stricmp"), _T("_tcsicmp")); //Performs a case-insensitive comparison of strings.
        ReplaceWholeWordInString(strStringToProcess, _T("strcoll"), _T("_tcscoll")); //Collate two strings
        ReplaceWholeWordInString(strStringToProcess, _T("_stricoll"), _T("_tcsicoll")); //Collate two strings (case insensitive)
        ReplaceWholeWordInString(strStringToProcess, _T("_strncoll"), _T("_tcsnccoll")); //Collate first count characters of two strings
        ReplaceWholeWordInString(strStringToProcess, _T("_strnicoll"), _T("_tcsnicoll")); //Collate first count characters of two strings (case-insensitive)
        ReplaceWholeWordInString(strStringToProcess, _T("strcpy"), _T("_tcscpy")); //Copies a string.
        ReplaceWholeWordInString(strStringToProcess, _T("strcpy_s"), _T("_tcscpy_s")); //Copies a string.
        ReplaceWholeWordInString(strStringToProcess, _T("strcspn"), _T("_tcscspn")); //Returns the index of the first occurrence in a string, of a character that belongs to a set of characters.
        ReplaceWholeWordInString(strStringToProcess, _T("strerror"), _T("_tcserror")); //Gets a system error message string or formats a user-supplied error message string.
        ReplaceWholeWordInString(strStringToProcess, _T("strftime"), _T("_tcsftime")); //Format a time string.
        ReplaceWholeWordInString(strStringToProcess, _T("strlen"), _T("_tcslen")); //Gets the length of a string, by using the current locale or a specified locale. 
        ReplaceWholeWordInString(strStringToProcess, _T("strncat"), _T("_tcsncat")); //Appends characters of a string.
        ReplaceWholeWordInString(strStringToProcess, _T("strncmp"), _T("_tcsncmp")); //Compares up to the specified count of characters of two strings.
        ReplaceWholeWordInString(strStringToProcess, _T("strncpy"), _T("_tcsncpy")); //Copy characters of one string to another.
        ReplaceWholeWordInString(strStringToProcess, _T("strpbrk"), _T("_tcspbrk")); //Scans strings for characters in specified character sets.
        ReplaceWholeWordInString(strStringToProcess, _T("strrchr"), _T("_tcsrchr")); //Scans a string for the last occurrence of a character.
        ReplaceWholeWordInString(strStringToProcess, _T("strspn"), _T("_tcsspn")); //Returns the index of the first character, in a string, that does not belong to a set of characters.
        ReplaceWholeWordInString(strStringToProcess, _T("strstr"), _T("_tcsstr")); //Returns a pointer to the first occurrence of a search string in a string.
        ReplaceWholeWordInString(strStringToProcess, _T("strtod"), _T("_tcstod")); //Convert strings to a double-precision value.
        ReplaceWholeWordInString(strStringToProcess, _T("strtok"), _T("_tcstok")); //Finds the next token in a string, by using the current locale or a specified locale that's passed in.
        ReplaceWholeWordInString(strStringToProcess, _T("strtol"), _T("_tcstol")); //Convert strings to a long-integer value.
        ReplaceWholeWordInString(strStringToProcess, _T("strtoul"), _T("_tcstoul")); //Convert strings to an unsigned long-integer value.
        ReplaceWholeWordInString(strStringToProcess, _T("strxfrm"), _T("_tcsxfrm")); //Transform a string based on locale-specific information.
        ReplaceWholeWordInString(strStringToProcess, _T("system"), _T("_tsystem")); //Executes a command.
        ReplaceWholeWordInString(strStringToProcess, _T("tmpnam"), _T("_ttmpnam")); //Generate names you can use to create temporary files.
        ReplaceWholeWordInString(strStringToProcess, _T("tolower"), _T("_totlower")); //Converts a character to lowercase.
        ReplaceWholeWordInString(strStringToProcess, _T("toupper"), _T("_totupper")); //Convert character to uppercase.
        ReplaceWholeWordInString(strStringToProcess, _T("ungetc"), _T("_ungettc")); //Pushes a character back onto the stream.
        ReplaceWholeWordInString(strStringToProcess, _T("vfprintf"), _T("_vftprintf")); //Write formatted output using a pointer to a list of arguments. 
        ReplaceWholeWordInString(strStringToProcess, _T("vprintf"), _T("_vtprintf")); //Each of the vprintf functions takes a pointer to an argument list, then formats and writes the given data to a particular destination.
        ReplaceWholeWordInString(strStringToProcess, _T("vsprintf"), _T("_vstprintf")); //Write formatted output using a pointer to a list of arguments.
        ReplaceWholeWordInString(strStringToProcess, _T("WinMain"), _T("_tWinMain")); //
    }

    m_strReplaceAnsiSubStringWithGenericSubString = strStringToProcess;
}

ReplaceDatatypesWithGenericEquivalent: 

This function search for datatypes in project submitted by user for conversion and convert them to generic equivalent.
void CANSICodeFileToUnicodeCodeFile::ReplaceDatatypesWithGenericEquivalent(
                                                   /*[IN]*/std::wstring strStringToProcess)
/* ============================================================================================
Function :     CANSICodeFileToUnicodeCodeFile::ReplaceDatatypesWithGenericEquivalent
Author:        Satish Jagtap
Description :  There are many data types having different variants for ANSI/MBCS build and Unicode build.
               To build application in both build (or Uniocde) we need to replace those data types with 
               generic data types.
Access :       private
Return :       nothing
Parameters :   [IN] std::wstring strStringToProcess - String to work on.
Call to:       1) ReplaceWholeWordInString
Call from:     1) MakeANSIFileToUnicodeReady
Usage :        This function is used to replace ansi data tyeps with its generic versions.
Date:          15-04-2015
=============================================================================================*/
{
    if(strStringToProcess.length() > 3) //Check if length of source string is greater than 3 characters
    {
        //Replace data types
        ReplaceWholeWordInString(strStringToProcess, _T("unsigned int"), _T("_TINT"), true);
        ReplaceWholeWordInString(strStringToProcess, _T("char"), _T("_TCHAR"), true);
        ReplaceWholeWordInString(strStringToProcess, _T("_finddata_t"), _T("_tfinddata_t"), true);
        ReplaceWholeWordInString(strStringToProcess, _T("__finddata64_t"), _T("_tfinddata64_t"), true);
        ReplaceWholeWordInString(strStringToProcess, _T("_finddatai64_t"), _T("_tfinddatai64_t"), true);
        ReplaceWholeWordInString(strStringToProcess, _T("signed char"), _T("_TSCHAR"), true);
        ReplaceWholeWordInString(strStringToProcess, _T("unsigned char"), _T("_TUCHAR"), true);
        ReplaceWholeWordInString(strStringToProcess, _T("LPSTR"), _T("LPTSTR"), true);
        ReplaceWholeWordInString(strStringToProcess, _T("LPCSTR"), _T("LPCTSTR"), true);
        ReplaceWholeWordInString(strStringToProcess, _T("LPOLESTR"), _T("LPTSTR"), true);
    }

    m_strReplaceAnsiSubStringWithGenericSubString = strStringToProcess;
}
Along with these mentioned functions other functions are also present in this utility are important and useful. Some of them are useful to take backup of converting project files before process get starts.

Using an utility

User should go through following welcome window containing precautionary note before using this utility.
                  
Now please go through following main window of this utility where actual processing is going to start.
Utility provides following conversion options: 
  • Convert single file
  • Convert a whole project
  • Convert selected files from project
This utility also help user to keep backup of its original content before staring conversion process. Using this utility user will also able to revert back all changes.

Note: This utility is only used to convert C, C++ project develop to run under Windows operating systems.

Points of Interest

In actual word using this utility user will able to build converted projects or files for UNICODE as well as MBCS character code.

No comments:

Post a Comment