| Victor Lazzarini wrote:
> when querying a global variable that doesn't exist (yet), would it be
> possible to
> return NULL, so that code can check if it exists?
It is already implemented that way, if you use csoundQueryGlobalVariable().
There are two functions for getting the address of a variable:
csoundQueryGlobalVariableNoCheck():
fast version that does not check if the variable exists; if it does not,
the operation of the function is undefined (which includes the possibility
of a crash, or returning a wrong address).
csoundQueryGlobalVariable():
safe, but slower function that returns NULL if the variable does not exist
Here is the source code of the functions so you can see the difference:
/**
* Get pointer to space allocated with the name "name".
* Returns NULL if the specified name is not defined.
*/
PUBLIC void *csoundQueryGlobalVariable(void *csound, const char *name)
{
ENVIRON *csnd = (ENVIRON*) csound;
GlobalVariableEntry_t *p;
unsigned char h;
/* check if there is an actual database to search */
if (csnd->namedGlobals == NULL)
return NULL;
/* check for a valid name */
if (name == NULL)
return NULL;
if (name[0] == '\0')
return NULL;
/* calculate hash value */
h = name_hash(name);
/* search tree */
p = (GlobalVariableEntry_t*) (csnd->namedGlobals[(int) h]);
if (p == NULL)
return NULL;
while (sCmp(name, (char*) (p->name)) != 0) {
p = (GlobalVariableEntry_t*) p->nxt;
if (p == NULL)
return NULL;
}
return (void*) (p->p);
}
/**
* This function is the same as csoundQueryGlobalVariable(), except the
* variable is assumed to exist and no error checking is done.
* Faster, but may crash or return an invalid pointer if 'name' is
* not defined.
*/
PUBLIC void *csoundQueryGlobalVariableNoCheck(void *csound, const char *name)
{
ENVIRON *csnd = (ENVIRON*) csound;
GlobalVariableEntry_t *p;
unsigned char h;
/* calculate hash value */
h = name_hash(name);
/* search tree */
p = (GlobalVariableEntry_t*) (csnd->namedGlobals[(int) h]);
while (p->nxt != NULL && sCmp(name, (char*) (p->name)) != 0)
p = (GlobalVariableEntry_t*) p->nxt;
return (void*) (p->p);
}
-------------------------------------------------------
This SF.Net email is sponsored by Yahoo.
Introducing Yahoo! Search Developer Network - Create apps using Yahoo!
Search APIs Find out how you can build Yahoo! directly into your own
Applications - visit http://developer.yahoo.net/?fr=offad-ysdn-ostg-q22005
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net |