a problem about using const char* to pass parameter - iphone

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)

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.

Unix network programming /socket programming

I get the following error
In function w_Endline:
/home/prog2/in_out.c:113:19: error: assignment of read-only l ocation ‘*(sent + (sizetype)(endlen * 1ul))’
sent[endlen]='\0';
/home/prog2/in_out.c: In function ‘w_White’:
/home/prog2/in_out.c:119:19: warning: initialization discards ‘const’ qualifier from pointer target type
char* endlen=sent+whitelen;
/home/prog2/in_out.c:120:6: warning: implicit declaration of function ‘isspace’ [-Wimplicit-function-declaration]
while(endlen>sent &&isspace(*endlen))
FILES
1.in_out.c http://ideone.com/nI15F4
void w_Endline(const char* sent)
{
size_t endlen=strlen(sent)-1;
if(sent[endlen]=='\n')
sent[endlen]='\0';
}
void w_White(const char* sent)
{
size_t whitelen=strlen(sent);
char* endlen=sent+whitelen;
while(endlen>sent &&isspace(*endlen))
{
endlen='\0';
--endlen;
}
}
2.in_out.h http://ideone.com/lDxxhY
Dont use const in w_Endline if you are modifying the pointer also you need ctype.h header for isspace() function. Also in w_White function if you are assigning a const pointer the pointer also needs to be const.
char* endlen=sent+whitelen;
should be
const char* endlen=sent+whitelen;// because sent is const
Actually const qualifier means read-only

QString encoding with special character

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();

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.

When is the const version called?

I made "T operator[](int i) const" and "T& operator[](int i)" for class A.
(and I also tried it for "const T& operator[](int i) const" and "T& operator[](int i)")
The operator print a value to distinguish which operator is called.
A a;
int k = a[0];
k = a[0];
const int l = a[0];
result : three calls of non-const version.
How can I call const version?
Should I use const class?
There is no chance to call a function that is const version without using const class?
You can use a const reference:
const A& b=a;
k=b[0];
Or a const cast:
k=const_cast<const A&>(a)[0];