Help to port a console Method with "infinite parameter"
by elvince · in Torque 3D Professional · 08/14/2010 (11:37 am) · 5 replies
Hi all,
Here is the original code from the resource of SQLITE
I would like to understand how I can port it to the new DefineEngineMethod structure.
Thanks for your help.
Here is the original code from the resource of SQLITE
I would like to understand how I can port it to the new DefineEngineMethod structure.
Thanks for your help.
ConsoleMethod(SQLiteObject, query, S32, 4, 0, "(const char* sql, int mode) Performs an SQL query on the open database and returns an identifier to a valid result set. mode is currently unused, and is reserved for future use.")
{
S32 iCount;
S32 iIndex, iLen, iNewIndex, iArg, iArgLen, i;
char* szNew;
if (argc == 4)
return object->ExecuteSQL(argv[2]);
else if (argc > 4)
{
// Support for printf type querys, as per Ben Garney's suggestion
// Basically what this does is allow the user to insert questino marks into thier query that will
// be replaced with actual data. For example:
// "SELECT * FROM data WHERE id=? AND age<7 AND name LIKE ?"
// scan the query and count the question marks
iCount = 0;
iLen = dStrlen(argv[2]);
for (iIndex = 0; iIndex < iLen; iIndex++)
{
if (argv[2][iIndex] == '?')
iCount++;
}
// now that we know how many replacements we have, we need to make sure we
// have enough arguments to replace them all. All arguments above 4 should be our data
if (argc - 4 == iCount)
{
// ok we have the correct number of arguments
// so now we need to calc the length of the new query string. This is easily achieved.
// We simply take our base string length, subtract the question marks, then add in
// the number of total characters used by our arguments.
iLen = dStrlen(argv[2]) - iCount;
for (iIndex = 1; iIndex <= iCount; iIndex++)
{
iLen = iLen + dStrlen(argv[iIndex+3]);
}
// iLen should now be the length of our new string
szNew = new char[iLen];
// now we need to replace all the question marks with the actual arguments
iLen = dStrlen(argv[2]);
iNewIndex = 0;
iArg = 1;
for (iIndex = 0; iIndex <= iLen; iIndex++)
{
if (argv[2][iIndex] == '?')
{
// ok we need to replace this question mark with the actual argument
// and iterate our pointers and everything as needed. This is no doubt
// not the best way to do this, but it works for me for now.
// My god this is really a mess.
iArgLen = dStrlen(argv[iArg + 3]);
// copy first character
szNew[iNewIndex] = argv[iArg + 3][0];
// copy rest of characters, and increment iNewIndex
for (i = 1; i < iArgLen; i++)
{
iNewIndex++;
szNew[iNewIndex] = argv[iArg + 3][i];
}
iArg++;
}
else
szNew[iNewIndex] = argv[2][iIndex];
iNewIndex++;
}
}
else
return 0; // incorrect number of question marks vs arguments
Con::printf("Old SQL: %s\nNew SQL: %s", argv[2], szNew);
return object->ExecuteSQL(szNew);
}
return 0;
}About the author
Recent Threads
#2
BTW, while it doesn't help you here, what might come in handy at times when having to stick to working with plain strings directly from the console system is to be able to use engineAPI's own marshaling and unmarshaling systems.
For example, instead of
you can do
Note the extra () here on EngineUnmarshallData is required here (due to C++ rules for templates).
To go the other way round (value->string), use EngineMarshallData.
08/14/2010 (5:50 pm)
BTW, while it doesn't help you here, what might come in handy at times when having to stick to working with plain strings directly from the console system is to be able to use engineAPI's own marshaling and unmarshaling systems.
For example, instead of
Point3F p; dSscanf( argv[ i ], "%g %g %g", &p.x, &p.y, &p.z );
you can do
Point3F p = EngineUnmarshallData< Point3F >()( argv[ i ] );
Note the extra () here on EngineUnmarshallData is required here (due to C++ rules for templates).
To go the other way round (value->string), use EngineMarshallData.
#3
I have you documentation always opened. It's really helpful.
marshaling seems a great thing. I will try to keep it in mind when it comes to use string from console. like in your sample.
08/15/2010 (10:16 am)
Thanks reneI have you documentation always opened. It's really helpful.
marshaling seems a great thing. I will try to keep it in mind when it comes to use string from console. like in your sample.
Quote://PS: Minor clarification... engineAPI does not support *console-style* variadic functions.I'm not sure to follow you on this one. Did you meant that DefineEngineMethod &DefineEngineFunction will not support the variadic functions but other functions of engineApi can?
#4
Kind of inept how I said it. engineAPI in fact extends to more than just the console interop wrapper and there is in fact support for variadic functions. However, this neither comes into play with TorqueScript nor will it be supported through the DefineEngineXXX macros. Technically, variadics could be supported by the macros but I don't think the effort is close to being worth it.
08/15/2010 (6:02 pm)
Quote:I'm not sure to follow you on this one. Did you meant that DefineEngineMethod &DefineEngineFunction will not support the variadic functions but other functions of engineApi can?
Kind of inept how I said it. engineAPI in fact extends to more than just the console interop wrapper and there is in fact support for variadic functions. However, this neither comes into play with TorqueScript nor will it be supported through the DefineEngineXXX macros. Technically, variadics could be supported by the macros but I don't think the effort is close to being worth it.
#5
In fact, I think, variadic functions are really "exception" and most of the time, you can manage it by 1 argument that will act as an array with a token as separator.
in all case, it was very instructive.
08/15/2010 (6:35 pm)
Ok I got your point.In fact, I think, variadic functions are really "exception" and most of the time, you can manage it by 1 argument that will act as an array with a token as separator.
in all case, it was very instructive.
Associate Rene Damm
EngineAPI.rtf should give you the details you need.
//PS: Minor clarification... engineAPI does not support *console-style* variadic functions.