PDA

View Full Version : PARAM_ENUM_NAMES length



jarno
04-25-2005, 02:20 AM
The spec, and the implementation, says that the length of the PARAM_ENUM_NAMES parameter capability is the number of enums.

However, the example code (nullxcode, v4l, winaudio) sets the length to the length of the concatenated enum name string.

This could cause problems when attempting to get the name of an invalid enum as the search of the enum name string falls off the end.

---JvdL---

ehsan2004
04-26-2005, 07:17 AM
I haven't taked a look at the examples. As you said, the lengh of the string is set to the concatenated enum strings. However, each enum strings are separated by NULL characters. If you want to search an enum in the string, you should use from these NULL characters.
Here's a function that you can accomplish this task:

bool searchString( char* searchAnString, char* allStrings )
{
char* endOfString; //pointer to the last string element
int index = 0; //index for spaces
endOfString = allStrins + strlen( allStrings );

//loop while we haven't reached the end of the string
while( allStrings < endOfString )
{
/*returns an integer value specifying the length of the initial segment of *Allstrings* that consists entirely of characters not in NULL character*/
index = strcspn( allStrings, "\0" );

//we found searchStr
if( (strlen( searchAnString ) == index ) && (strncmp( searchAnString, allStrings, index ) == 0 ) )
{
return true;
}

/*we didn't find searchAnString, move pointer to the next string to search*/
allStrings += ( index + 2 );
}

return false;
}

Now we can get the enum strings and find a string in the enum strings.
Here's a code that shows this task:

//...
MLpv* parameterCap;
mlPvGetCapabilities( someObjectId, someParameterId, & parameterCap );

MLpv* enumNames = mlPvFind( parameterCap, ML_PARAM_ENUM_NAMES_BYTE_ARRAY );

//ensure that this feature is supported
if( enumNames )
{
if( enumNames->lenght )
{
char* enumNamesString = (char*)enumNames->value.pByte;
char* searchAnString;
//searchAnString = string that you want to search
bool returnValue= false;
returnValue = searchString( searchAnString, enumNamesStrings );
}
}

//...
Now if an invalid enum isn't in the string, the funcion searchString returns false.There's no problem here...

If you found an error in the codes, let me know.
references:
1.OpenML specification
2.OpenML programmers guide
3.OpenGL game programming
4.MSDN january 2001

-Ehsan-

[ April 26, 2005: Message edited by: Ehsan Kamrani ]

jarno
04-26-2005, 04:51 PM
I'm quite capable of searching an array of double nul terminated strings thank you. Besides, there is code in mlpublic.c to do just that already (_valueInt32ToString() for example).

I wasn't asking about how to do it. I was pointing out that the length of the parameter is set incorrectly in the example devices, and that this could lead to mysterious crashes.

---JvdL---

ehsan2004
04-26-2005, 06:04 PM
I thought that you are corresponding the lenght component to searching the invalid enumeration.I only claimed that there's no correspondence between the lenght component and searching the enum--And also searching the invalid enum didn't produce an error.
In the above code, if enumeNames->lenght is valid(exept zero, there's no difference between the other values), then the searchString searches the enum values(valid or invalid values) and returns a value.No error is generated...
<BLOCKQUOTE><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><HR>Originally posted by Jarno van der Linden:
This could cause problems when attempting to get the name of an invalid enum as the search of the enum name string falls off the end.
<HR></BLOCKQUOTE>
Maybe i can't understand your meaning of that quote or maybe i'm completely incorrect :(

But,I didn't say anything about the first and second paragraphs of your message.
Like always,Fabrice can check the codes and replyes to you ;)
-Ehsan-

[ April 27, 2005: Message edited by: Ehsan Kamrani ]

fjaubert
05-27-2005, 01:40 AM
Indeed, this is an inconsistency between the spec and the current sample implementations. I'll check to see which we should be fixing.

Thank you for pointing this out.