wcstombs并为堆上的字符数组分配内存

问题描述 投票:0回答:1

我正在读一个带有单个宽字符行的文件。但是,我永远不知道它将会持续多久。我已经把它读成std::wstringinString,并且设法用空气创建多字节字符串(Q1 - 这些被称为r值吗?)。 Q2 - 现在,如何在堆中为此分配内存并获取指向它的智能指针?我不想使用newmalloc(并最终调用freedelete)或任何常量来将其存储在堆栈中(因为我永远不知道最大长度)。 Q3 - 我可以在这里使用make_sharedmake_unique功能模板吗? Q4 - 具体来说,我可以获得像shared_ptr<char>这样指针指向堆上分配的char数组吗?

我试过类似下面的东西,

std::shared_ptr<char> MBString(const_cast<char*>(std::string(inString.begin(), inString.end()).c_str()));

那没起效。我在互联网上尝试了一些建议,但我还不知道该怎么办。

Q5 - 更不用说宽字符到多字节转换了,一般来说,如何在堆上分配一个任意长度的char字符串并获得一个智能指针呢?

std::wfstream inFile(L"lengthUnkown.txt", std::ios::in);
std::wstring inString;
inFile >> inString;
std::wcout << inString << std::endl; //prints correctly
std::cout << (const_cast<char*>(std::string(inString.begin(), inString.end()).c_str())) << std::endl; //this prints the line correctly as expected
//convert wide character string to multi-byte on the heap pointed, to by MBString 
//std::cout << MBString << std::endl; //I want to print the multi-byte string like this
return 0;
c++11 memory-management smart-pointers wstring widechar
1个回答
1
投票

资源不是最优但可靠:

wchar_t* mb2wstr(const char* inval) {
    size_t size = std::strlen(inval);
    #define OUTSZ (size+1)*sizeof(wchar_t)
    auto buf = (wchar_t*)std::malloc(OUTSZ);
    std::memset(buf, 0, OUTSZ);
    std::setlocale(LC_CTYPE,""); //  необходима, чтобы отработала "mbstowcs"
    size = std::mbstowcs(buf, inval, size);
    if ( size == (size_t)(-1) ) {
        std::free(buf);
        buf = nullptr;
    } else {
        buf = (wchar_t*)std::realloc(buf,OUTSZ);
    }
    return buf;
    #undef OUTSZ
}

char* wstr2mb(const wchar_t* inval) {
    size_t size = std::wcslen(inval);
    #define OUTSZ (size+1)*MB_CUR_MAX // Maximum length of a multibyte character in the current locale
    auto buf = (char*)std::malloc(OUTSZ);
    std::memset(buf, 0, OUTSZ);
    std::setlocale(LC_CTYPE,""); //  необходима, чтобы отработала "wcstombs"
    size = std::wcstombs(buf, inval, size*sizeof(wchar_t));
    if ( size == (size_t)(-1) ) {
        std::free(buf);
        buf = nullptr;
    } else {
        buf = (char*)std::realloc(buf,size+1);
    }
    return buf;
    #undef OUTSZ
}

const std::string pwchar2string(const wchar_t* inval) {
    char* tmp = wstr2mb(inval);
    string out{tmp};
    std::free(tmp);
    return out;
}
const std::wstring pchar2wstring(const char* inval) {
    wchar_t* tmp = mb2wstr(inval);
    wstring out{tmp};
    std::free(tmp);
    return out;
}

const wstring string2wstring(const string& value) {
    return pchar2wstring(value.c_str());
}
const string wstring2string(const wstring& value) {
    return pwchar2string(value.c_str());
}
const wchar_t* char2wchar(const char* value) {
    return pchar2wstring(value).c_str();
}
const char* wchar2char(const wchar_t* value) {
    return pwchar2string(value).c_str();
}
© www.soinside.com 2019 - 2024. All rights reserved.