Sunday
Jul222012
SQUID - Handling Compound Data
Sunday, July 22, 2012 at 12:30PM
For simple data in SQUID, you can call SquidRetrieveData and save the result into a parameter. This parameter can then be used throughout your script.
SquidSendData (iSquid, "OrderNumber", "abc123"); lr_save_string ((char*) SquidRetrieveData (iSquid, "OrderNumber"), "pOrderNumber");
Things get a bit more complicated if your data is comprised of multiple parameter values. A common example of this is user name and password.
SquidSendData (iSquid, "User", "user001;pwd001"); // How can I split the data value and save to different parameters?
Technique 1
- Use strtok to parse returned data.
- Straightforward, but can get difficult to manage.
- Puts the responsibility on the person creating the script.
char *ptr; char *token; SquidSendData (iSquid, "User", "user001;pwd001"); ptr = (char*) SquidRetrieveData (iSquid, "User"); token = (char*) strtok (ptr, ";"); lr_save_string (token, "pUserName"); token = (char*) strtok (NULL, ";"); lr_save_string (token, "pUserPassword");
Technique 2
- Create SquidRetrieveData_LR to hide strtok and other implementation details.
- Additional complexity is hidden.
- Improves readability and centralizes the handling of the compound data.
- Simpler interface for scripter.
- Can be used on simple and compound data values.
SquidSendData (iSquid, "User", "user001;pwd001"); SquidRetrieveData_LR (iSquid, "User", ";", "pUserName;pUserPassword"); SquidSendData (iSquid, "OrderNumber", "abc124"); SquidRetrieveData_LR (iSquid, "OrderNumber", ";", "pOrderNumber"); SquidSendData (iSquid, "OrderNumber", "abc125"); SquidRetrieveData_LR (iSquid, "OrderNumber", NULL, "pOrderNumber");
SquidRetrieveData_LR
- Adds 2 parameters to what SquidRetrieveData has:
- strDelimiter - Delimiter used for the data and parameter names. If NULL or not found, the entire data value is returned.
- strParams - List of parameter names to save the data to.
- This function is not in SQUID.dll. You can put this function inside your script or create a .h file that all your scripts can include. This .h file will contain this and other utility type functions.
Challenges
- Vugen does not support stdargs.h. This prevented the use of variable argument lists. Could have used a variable argument list for the parameter names.
- Can't call strtok on 2 different strings and manipulate at the same time.
Assumptions
- Maximum of 50 parameter names.
- Parameter string is less than 1024 characters.
- No checks
- Same number of delimiters in the column value and parameter list.
- Same delimiter used in value and parameter list.
- Column value exists (SquidRetrieveData returns non-NULL value).
- Workarounds
- Use malloc/free to increase above maximums.
- Add logic to check the above conditions and act accordingly.
char strBuffer[1024]; // globally scoped variable int SquidRetrieveData_LR (int iSquid, char *strColumn, char *strDelimiter, char *strParams) { char *ptr; char *token_value; char *token_param[50]; int i=0; // ------------------------------------------------------------------------- // Step 0 - If no delimiter given, return the entire data value. // ------------------------------------------------------------------------- if (strDelimiter == NULL) { lr_save_string ((char*) SquidRetrieveData (iSquid, strColumn), strParams); return LR_PASS; } // ------------------------------------------------------------------------- // Step 1 - Copy the parameter names to a working buffer. // ------------------------------------------------------------------------- strcpy (strBuffer, strParams); // ------------------------------------------------------------------------- // Step 2 - Split the parameter names with the given delimiter. // ------------------------------------------------------------------------- token_param[0] = (char*) strtok (strBuffer, strDelimiter); while (token_param[i] != NULL) { i++; token_param[i] = (char*) strtok (NULL, strDelimiter); } // ------------------------------------------------------------------------- // Step 3 - Retrieve the data from SQUID. // ------------------------------------------------------------------------- ptr = (char*) SquidRetrieveData (iSquid, strColumn); // ------------------------------------------------------------------------- // Step 4 - Split the data and save to the corresponding parameter name. // ------------------------------------------------------------------------- token_value = (char*) strtok (ptr, strDelimiter); i=0; while (token_value != NULL) { lr_save_string (token_value, token_param[i++]); token_value = (char*) strtok (NULL, strDelimiter); } // ------------------------------------------------------------------------- return LR_PASS; }
Reader Comments