Using boost::mapped_file with wchar * - c++

I want to ask how to use boost::iostreams::mapped_file with wchar_t *. Currently, I found the following:
boost::iostreams::mapped_file reader("input.txt" , mapped_file::readonly);
char const * it = reader.const_begin();
char const * endit = reader.const_end();
As I see, the interface does only allow char *, but I need to read a Vietnamese corpus (encoding UTF-16 LE). The reason I ask is that all previous assignments I use this, so If there is someway I can reuse these codes again.

Related

How to convert char to wchar_t*?

As the title states I have a simple char that retrieves a full path name of a file I am looking for and I need to convert it to const wchar_t. How can I achieve this? Here is an example of the code:
int main()
{
char filename[] = "poc.png";
char fullFilename[MAX_PATH];
GetFullPathName(filename, MAX_PATH, fullFilename, nullptr);
const wchar_t *path = fullFilename;
}
As you can see I am trying to get the filename to convert but I couldn't find a way to do so. What would be the most simple solution to this?
Your code doesn't show any need to convert between char and wchar_t. Most likely you don't actually need to convert the character types. If you want to use the wchar_t-friendly GetFullPathNameW, then just use wchar_t instead of char.
int main()
{
wchar_t fullFilename[MAX_PATH];
GetFullPathNameW(L"poc.png", MAX_PATH, fullFilename, nullptr);
const wchar_t *path = fullFilename;
return 0;
}
If you really do need to convert between wchar_t-based C-style strings and char-based C-style strings, then you can use the APIs MultiByteToWideChar and WideCharToMultiByte.

char to ACHAR + objectARX

I am trying to convert previous code to VS 2010. The code I am trying to convert is mentioned below. The function addCommand is defined like
addCommand(const ACHAR * cmdGroupName, const ACHAR * cmdGlobalName, const ACHAR * cmdLocalName, Adesk::Int32 commandFlags, AcRxFunctionPtr FunctionAddr,AcEdUIContext *UIContext=NULL, int fcode=-1, HINSTANCE hResourceHandle=NULL, AcEdCommand** cmdPtrRet=NULL)
The third required argument is of type ACHAR. The function is called in the following way.
char cmdLocRes[65];
// If idLocal is not -1, it's treated as an ID for
// a string stored in the resources.
if (idLocal != -1) {
// Load strings from the string table and register the command.
::LoadString(_hdllInstance, idLocal, cmdLocRes, 64);
acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLocRes, cmdFlags, cmdProc);
My problem is that the variable cmdLocRes is of type char but the argument needs to be of type ACHAR.
How can I convert the same ?
ACHAR is a typedef (made by Autodesk in file AdAChar.h) of wchar_t. So the question is how to convert a char to wchar_t.
In a wider context this problem is because of the existence of unicode. Linux and Windows programmers normally discuss it without understanding each other. As I do not understand it, too, I cannot explain it. There are threads for the eager beaver: What's "wrong" with C++ wchar_t and wstrings? What are some alternatives to wide characters?
The folling might give you an idea how to convert it.
// Convert char to wchar_t
char cmdLocRes[65];
// Remark: Make sure cmdLocRes contains elements!
cmdLocRes[0] = 'A';
cmdLocRes[1] = '\0';
// Get a wstringstream
std::wstringstream str;
// Write the char array to the wstringstream
str << cmdLocRes;
// Get a wstring from the wstringstream
std::wstring wstr = str.str();
// Get a wchar_t from the wstring
const wchar_t *chr1 = wstr.c_str();
const ACHAR *chr2 = wstr.c_str(); // We see that wchar_t == ACHAR!
Better think of using wchar_t cmdLocRes[65] instead of char cmdLocRes[65]!
Sorry for the code style, but this text field is another great example for how not to do it. It took me longer to try to format the code block (and please look at it!!!) than to write the answer. Jesus!!!

Problem: How to convert CString into const char * in C++ MFC

How do I convert CString into const char *? I have tried everything found on the internet but I still cant convert them.
Please help.
Thank you.
CString casts to const char * directly
CString temp;
temp = "Wow";
const char * foo = (LPCSTR) temp;
printf("%s", foo);
will print 'foo'
Newer version of MFC also support the GetString() method:
CString temp;
temp = "Wow";
const char * foo = temp.GetString();
printf("%s", foo);
Short answer: Use the CT2CA macro (see ATL and MFC String Conversion Macros). This will work regardless of your project's 'Character Set' setting.
Long answer:
If you have the UNICODE preprocessor symbol defined (i.e., if TCHAR is wchar_t), use the CT2CA or CW2CA macro.
If you don't (i.e., if TCHAR is char), CString already has an operator to convert to char const* implicitly (see CSimpleStringT::operator PCXSTR).
If your application is not Unicode, you can simple typecast to const char *. Use the GetBuffer() method if you need a char * that you can modify.
If your application is Unicode and you really want a char *, then you'll need to convert it. (You can use functions like MultiByteToWideChar().)
I know it's late, but I couldn't use the marked-as-answer solution. I search all over the internet and nothing worked for me. I muscled through it to get a solution:
char * convertToCharArr(CString str) {
int x = 0;
string s = "";
while (x < str.GetLength()) {
char c = str.GetAt(x++);
s += c;
}
char * output = (char *)calloc(str.GetLength() + 1, sizeof(char));
memcpy(output, s.c_str(), str.GetLength() + 1);
return output;
}
First : define char *inputString; in your_file.h
Second : define in yourFile.cpp :
CString MyString;
MyString = "Place here whatever you want";
inputString = new char[MyString.GetLength()];
inputString = MyString.GetBuffer(MyString.GetLength());
The last two sentences convert a CString variable to a char*; but be carefull, with CString you can hold millons of charcteres but with char* no. You have to define the size of your char* varible.

Tinyxml to print attributes

I'm trying to get std::string from attribute's value with TinyXml.
The only thing I can get is a const char * val, and I can't find any way to convert from const char * to a std::string.
so two possible answers to that:
1. How to get a string of an attribute with TinyXml?
2. How to convert const char * val to string val.
this is the code I have now:
TiXmlElement* data;
data->Attribute("some_name"); // return const char * which seems like unconvertible.
After googeling, I tried this:
char * not_const= const_cast<char *> (data->Attribute("some_name"));
There are no errors in the code itself, but after compiling and running I get exceptions.
std::string has a constructor that takes char const*. You don't need a char* for that.
std::string str = data->Attribute("some_name");
However, be aware that std::string doesn't like NULL values, so don't give it any.

What is the easiest way to convert a char array to a WCHAR array?

In my code, I receive a const char array like the following:
const char * myString = someFunction();
Now I want to postprocess it as a wchar array since the functions I use afterwards don't handle narrow strings.
What is the easiest way to accomplish this goal?
Eventually MultiByteToWideChar? (However, since it is a narrow string which I get as input, it doesn't have multibyte characters => probably not the most beautiful solution)
const char * myString = someFunction();
const int len = strlen(myString);
std::vector<wchar_t> myWString (len);
std::copy(myString, myString + len, myWString.begin());
const wchar_t * result = &myWString[0];
MultiByteToWideChar will work unless you are using extended characters in your narrow string. If its a plain alpha numeric string then it should work fine.
you can also look at mbstowcs which is a little less convoluted but doesn't offer the same amount of control.