[Q] GetArray / SetArray usage examples

Are you developing an External using the LiveCode Externals SDK?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

[Q] GetArray / SetArray usage examples

Post by shaosean » Fri Jun 01, 2012 11:58 pm

Does anyone have an example on how to use these external SDK functions?

mwieder
VIP Livecode Opensource Backer
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

Post by mwieder » Wed Jun 06, 2012 5:41 pm

Sure. This any help?

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);
}
GetArray is orthogonal to the above, just get the elements out of p_values, i.e.

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

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: [Q] GetArray / SetArray usage examples

Post by shaosean » Thu Jun 07, 2012 1:52 am

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..

mwieder
VIP Livecode Opensource Backer
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

Post by mwieder » Thu Jun 07, 2012 6:09 am

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.

Post Reply

Return to “Building Externals”