[Q] GetArray / SetArray usage examples
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
[Q] GetArray / SetArray usage examples
Does anyone have an example on how to use these external SDK functions?
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: [Q] GetArray / SetArray usage examples
Sure. This any help?
GetArray is orthogonal to the above, just get the elements out of p_values, i.e.
Code: Select all
// platform-specific stuff goes here.
// make macros for safe versions of string statements
// to prevent buffer overflows.
#ifdef TARGET_OS_MAC
#include <Carbon/Carbon.h>
#endif
#ifdef _MACOSX
#define COPYSTRING(dest, src) strlcpy(dest, src, sizeof(dest))
#define FORMATSTRING(dest, format,...) snprintf(dest, sizeof(dest),format, __VA_ARGS__)
#else
#define COPYSTRING(dest, src) strcpy_s(dest, sizeof(dest), src)
#define FORMATSTRING(dest, format,...) sprintf_s(dest, sizeof(dest), format,__VA_ARGS__)
#endif
#define ELEMENT_COUNT 4
ExternalString CharStringToExternalString(char *strInput)
{
static ExternalString strExternal;
strExternal.buffer = strInput;
strExternal.length = strlen(strInput);
return strExternal;
}
ExternalString p_values[ELEMENT_COUNT];
char *p_keys[ELEMENT_COUNT] =
{
"John" \
,"Paul" \
,"George" \
,"Ringo" \
};
// pass an array name as the first argument: revFillAnArray "arrayName"
void revFillAnArray(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
char retValue[1024] = "error";
char p_name[256];
int r_success
// put the array name into p_name
COPYSTRING(p_name, args[0]);
p_values[0] = CharStringToExternalString("Vera");
p_values[1] = CharStringToExternalString("Chuck");
p_values[2] = CharStringToExternalString("Dave");
p_values[2] = CharStringToExternalString("Rita");
SetArray(p_name, ELEMENT_COUNT, p_values, p_keys, &r_success);
retValue[0] = 0; // return empty for success
*pass = False; // don't pass the command up the chain
*error = False; // no unhandled errors
*retstring = strdup(retValue);
}
Code: Select all
int elementCount=ELEMENT_COUNT;
GetArray(p_name, &elementCount, p_values, p_keys, &r_success);
longElementZero = (unsigned char)strtol(p_values[0].buffer, NULL, 0);
Re: [Q] GetArray / SetArray usage examples
Thank you.. I had ended up downloading MetaCard 2.4.1 and using the demo code provided in there, but yours seem a lot tidier and easier to follow, so I will update my code accordingly..
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: [Q] GetArray / SetArray usage examples
Well, mine's pulled and modified from working code,so I hope it does the job for you. Let me know if it doesn't.