QString encoding with special character - encoding

I m trying to convert QString with special character to const char but I did not succeed.
my function is:
void class::func(const QString& fileName) // fileName = "â.tmp"
{
qDebug()<< fileName; // display "â.tmp"
const char* cfileName = fileName.toAscii().data();
qDebug() << cfileName; // display "a?.tmp"
}
qDebug()<< fileName display the true value that is "â.tmp" but after converting it to const a char*, I do not succeed to have the right value.
In the second time I try to use
const char* cfileName = QString::fromUtf8(fileName.toAscii().data());
but I did not still have the right value, it display the same thing: a?.tmp.
How can I fix this?

ASCII character set does not have the character â, so what you are trying to do is impossible.
You could try this:
const char* cfileName = = fileName.toUtf8().data();

Related

Pass String array as input into external C function

I would like to pass a String vector into an external C function.
In a minimal example I just want to pass the String vectors (or 1D array) through the C function.
My Modelica function looks like:
function testreadstri
input String instri[2];
output String outstri[2];
external "C" test_stri(instri,, size(instri, 1), outstri);
annotation (Include="#include <ebcmysql.cpp>", Library="libmysql");
end testreadstri;
My C fucntion looks like:
void test_stri(const char* thestring, size_t nLines, const char **testresult)
{
//bout = 12.3;
size_t iLines;
//size_t nLines;
iLines = 0;
//nLines = 1;
while ( iLines <= nLines ) {
<LINE_OF_INTEREST>
iLines++;
}
}
I tried for <LINE_OF_INTEREST> the following lines:
testresult[iLines] = thestring[iLines];
strcpy(testresult[iLines], thestring[iLines]);
What works, but of course does not pass the input through as an output, is:
testresult[iLines] = "aTestString";
Is there any possibility to handle Modelica input String vectors in the external C function?
Thanks in advance!
Here's a short, self-contained and compilable example demonstrating both input string and output string handling of a pure external function in Modelica
model Model
function testreadstri
input String instri[2];
output String outstri[2];
external "C" test_stri(instri, size(instri, 1), outstri, size(outstri, 1));
annotation(Include="
#include \"ModelicaUtilities.h\"
#include <stdlib.h>
#include <string.h>
void test_stri(const char** thestring, size_t nLinesIn, const char** testresult, size_t nLinesOut)
{
size_t iLines;
// example for input string handling
for (iLines = 0; iLines < nLinesIn; iLines++) {
ModelicaFormatMessage(\"%s\\n\", thestring[iLines]);
}
// example for output string handling
for (iLines = 0; iLines < nLinesOut; iLines++) {
char* line = ModelicaAllocateStringWithErrorReturn(6);
if (line != NULL) {
strcpy(line, \"result\");
testresult[iLines] = line;
}
}
}");
end testreadstri;
String s[:] = testreadstri({"first", "second"});
end Model;
Yes, this is supported by the Modelica specification, see https://specification.modelica.org/v3.4/Ch12.html#argument-type-mapping.

How to send CString through sockets?

I'm trying to program a chatting program.
I ask you guys for help since I have little problem.
when I try to send a CString formatted strings, it only receives first letter of the string.
I'm using CAsyncSocket for sockets.
I tried it with char* format string, it worked.
Can you guys tell me what is wrong?
My code is like below:
worked.
char* buf = new char[m_strMsg.GetLength()];
buf = "helloworld!";
m_ClientSocket.Send("sended", m_strMsg.GetLength());
m_ClientSocket.Send(buf, 10);
not worked.
CString a = _T("helloworld!");
m_ClientSocket.Send(a,10);
I've also tried:
CString a = _T("helloworld!");
char* buf = new char[a.GetLength()];
buf = (LPSTR)(LPCTSTR)a;
m_ClientSocket.Send(buf,a.GetLength()];
Here is the proper UNICODE-compliant way of doing it:
CStringW sMessage = L"Hello World";
// convert from UTF-16 (UCS-2) to UTF-8
CStringA sMessageA = CW2A(sMessage, CP_UTF8);
const size_t nBytes = sizeof(CStringA::XCHAR) * sMessageA.GetLength();
CByteArray Message;
Message.SetSize( nBytes );
std::memcpy( Message.GetData(), (const BYTE*)(LPCSTR)sMessageA, nBytes );
m_ClientSocket.Send(Message.GetData(), Message.GetSize());

why my PCRE only matches the first result

I want to match all 'abc' in the input string. But I got the following result when input "first abc, second abc, third abc". I also output the ovector:
src: first abc, second abc, third abc
Matches 1
ovector: 6|9|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
My code:
#include <stdio.h>
#include <string.h>
#include "pcre.h"
static const char my_pattern[] = "abc";
static pcre* my_pcre = NULL;
static pcre_extra* my_pcre_extra = NULL;
void my_match(const char* src)
{
printf("src: %s\n", src);
int ovector[30]={0};
int ret = pcre_exec(my_pcre, NULL, src, strlen(src), 0, 0, ovector, 30);
if (ret == PCRE_ERROR_NOMATCH){
printf("None match.\n");
}
else{
printf("Matches %d\n",ret);
}
printf("ovector: ");
for(int i=0;i<sizeof(ovector)/sizeof(int);i++){
printf("%d|",ovector[i]);
}
printf("\n");
return;
}
int main()
{
const char* err;
int erroffset;
my_pcre = pcre_compile(my_pattern, PCRE_CASELESS, &err, &erroffset, NULL);
my_pcre_extra = pcre_study(my_pcre, 0, &err);
my_match("first abc, second abc, third abc");
return 0;
}
How can I get all the 'abc's, thanks.
pcre_exec only finds one match at a time. ovector is for substring matches. int ovector[30]={0}; will give you up to 10 matches (the last third (20-29) is not used), the first pair of numbers is for the whole pattern, the next pair is for the first capturing parentheses and so on. E.g. if you change your pattern to:
`static const char my_pattern[] = "(a(b)c)";`
then in your output you should see
Matches 3
ovector: 6|9|6|9|7|8|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
The function returns the number of captures that matched, in this case three, one for the whole pattern and two subpattern captures. The whole pattern matches at 6-9, the first parentheses match 6-9 too and the second parentheses match 7-8. To get more whole matches (global) you have to use a loop, passing in the offset of the previous match (ovector[1]) each time.
See http://www.pcre.org/pcre.txt and search for How pcre_exec() returns captured substrings

Macros when compiling shader for webgl

In OpenGL ES 2 you can set an array of strings as the shader source
void ShaderSource( uint shader, sizei count, const char **string, const int *length );
Which can be used to "inject" macros into the shader source. The corresponding function in WebGL only takes a single string as it seems.
void shaderSource(WebGLShader shader, DOMString source)
Is the only possibility to use macros to manually insert them in the source string?
I'm not sure what you mean by inject macros
const char* str1 = "foo";
const char* str2 = "bar";
const char* strings[] = { str1, str2, }
glShaderSource(shader, 2, strings, NULL);
is functionally equivalent to
var str1 = "foo";
var str2 = "bar";
var strings = [str1, str2];
gl.shaderSource(shader, strings.join(""));
So, in answer to you question. If you want to insert macros prepend them to the string you supply to gl.shaderSource. There's nothing special about macros and which string they come. As far as GL is concerned it's just one big string.

a problem about using const char* to pass parameter

I first store the 3 value into a pair of map like this:
void AddMenuAtlasTexture( int tag, const char* filename, const char* textureName )
{
map<const char*, const char*> _item;
_item.insert(pair<const char*, const char*>(filename, textureName));
m_texturesToLoad.insert(pair<int, map<const char*, const char*> >(tag, _item));
};
then I pass the value to another function like this:
map<const char*, const char*>::iterator _content;
int _tag = (*m_texturesToLoadIterator).first;
_content = (*m_texturesToLoadIterator).second.begin();
AtlasManagerSingleton->AddAtlas((*_content).first, (*_content).second, _tag);
the "textureName" is an absolute path like this kind: "/Users/eddy/Library/Application Support/iPhone Simulator/User/Applications/5FDE0091-2E93-42FE-BB62-05A16429551D/Ranch.app/../Documents/shop_tex.png"
my problem is the first function can get the "textureName" right, but the second function "AddAtlas" can not get the path, the "(*_content).second" is NULL.
and the "AddAtlas" prototype is:
void AtlasManager :: AddAtlas( const char *a_configFile, const char *a_spriteName, int a_nKey )
I develop this in iPhone dev using XCode.
use make_pair instead of pair<int, map<const char*, const char*> >.
use -> instead of *.
the texture loader is a map int -> const char* -> const char*. I don't see where you used the second index.
Probably this:
AtlasManagerSingleton->AddAtlas((*_content).first, (*_content).second, _tag)
Should be:
AtlasManagerSingleton->AddAtlas(_content->first, _content->second.begin()->second, _tag)