简体   繁体   中英

Replacing WCHAR and wchar_t with something generic

I'm trying to convert some VS 2015 C++ code to Android Studio C++ code.

My function looks like this:

int size = 0;
int len = 0;
fread(&size,sizeof(int),1,g_File);

#ifdef VERBOSE      
     printf("fullSentences size = %d\n",size);
#endif

int i1 = 0;
int i2 = 0;
int i3 = 0;
for(int i = 0; i < size; i++)
{
    fread(&len,sizeof(int),1,g_File);
    wchar_t *buff = new WCHAR[len+1];
    fread(buff,sizeof(WCHAR),len,g_File);
    buff[len]=0;
    fread(&i1,sizeof(int),1,g_File);
    fread(&i2,sizeof(int),1,g_File);
    fread(&i3,sizeof(int),1,g_File);
    fullSentences.Add(buff,i1,i2,i3);
    delete buff;

    #ifdef VERBOSE      
        FullSentence fs = fullSentences.Content().back();
        printf("%s\t%d\t%d\n",fs.Text.c_str(),fs.ByteStart,fs.ByteCount);
    #endif
}

I would like to get rid of the WCHAR and wchar_t in order to make the porting easier.

Can anybody suggestion a replacement for these? I would like to avoid having to tell Android Studio what wchar_t is, if possible.

Thank you.

Edit:

Here is the class information:

  //
// -------------------- clsFullSentences -----------------------------
//
vector<FullSentence> &clsFullSentences::Content()
{
    return m_content;
}
vector<wstring> &clsFullSentences::CleanLower()
{
    return m_sCleanLower;
}
void clsFullSentences::LoadSerializedFullSentences(string uFile)
{
    if (!fileExists(stringToWString(uFile)))
    {
        DebugBreak();
    }

    FILE* inFile =  fopen(uFile.c_str(), "rb");

    wchar_t signature[2];
    fread(signature, sizeof(wchar_t), 1, inFile);

    wstring wline;

    //read how many possibleresults we have     
    getLineW(inFile, wline);

    unsigned int count=_wtoi(wline.c_str());

    for (unsigned int i = 0; i < count; i++)
    {
        FullSentence st;

        getLineW(inFile,wline);
        st.Text = wline;

        //getLineW(inFile,wline);
        st.Emotion =0;// _wtoi(wline.c_str());

        getLineW(inFile,wline);
        st.ByteStart = _wtoi(wline.c_str());

        getLineW(inFile,wline);
        st.ByteCount = _wtoi(wline.c_str());

        m_content.push_back(st);
    }

    fclose(inFile);
}
void clsFullSentences::Add(wstring text, int i1, int i2, int i3)
{
    FullSentence fs;
    fs.Text = text;
    fs.Emotion = i1;
    fs.ByteStart = i2;
    fs.ByteCount = i3;
    m_content.push_back(fs);

    wstring sClean;
    sClean=StripPuncToLower(text);

    m_sCleanLower.push_back(sClean);
}

bool getLineW(FILE *inFile, wstring &result)
{
    wchar_t data[2] = { 0, 0 };

    result = L"";
    do
    {
        fread(data, sizeof(wchar_t), 1, inFile);

        if (data[0] > 0)
        {
            if (data[0] != 13)
            {
                if (data[0] != 10)
                {
                    result += data;
                }
                else
                {
                    break;//10 is the end of the line
                }
            }
        }
    } while (!feof(inFile));

    if (result.size() > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

If you want this code to work on Windows and Android builds then you will find that on windows wchar_t is 2 bytes, while on android it is 4bytes . So you will not be able to write a file on windows, and later read it properly on android. In such case you would have to use char16_t on android and convert it to 'your choosen' string type under android.

(edit: even better, if you can - make sure all the files are written as utf8 strings)

As for the 'choosen string type' I would suggest to use utf8, so instead of std::wstring use std::string (encoded using utf8). From my experience NDK team was discouraging use of wchar_t from the very begining (missing w* functions from c library etc.) I am not sure how it is now.

I work on a project which was originally coded with MFC, and then ported to android. We used from the very begining a TCHAR macro. Which as you know resolves to char on non unicode builds and to wchar_t on unicode builds. So the idea is to use TCHAR everywhere then under android TCHAR should resolve to char, while on windows use wchar_t (I assume you use unicode build under windows?).

I am not saying this is a best solution to share code between windows and android platform, there are lots of problems like conversions to utf8 on android, these are done with if-defs.

Use std::string that contains text in UTF-8 encoding. You may also want some lightweight library that helps to deal with checks and conversions of such text. Avoid using wchar_t and std::wstring in code that is meant to be portable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM