There are already variations above on a one or two of these, but I just completed my first LR project where I had to correlate with two or three variations of the original string, both performing additional hex char conversions beyond that provided by web_convert_param(), as well as then stripping chars from those original correlations (we use Actuate's web app to produce some of our reports, which required these conversion variants).
At any rate all of these functions encapsulate the needed buffer allocations and are buffer safe--assuming you don't run out of memory. The output string is always saved to an LR parameter specified in the call. I tested all pretty thoroughly prior to using them without incident on my project.
-Terry Horwath
Code:
/******************************************************************************
*
* gEncodeHTMLString()
*
* Converts inStr to encoded HTML, which is saved in {outParmName} parameter.
* If the {outParmName} parameter does not exist it will be created; if it
* exists it will be overwritten.
*
* Example: gEncodeHTMLString("blah", "a+b=c++def=="));
*
* places "a%2Bb%3Dc%2B%2Bdef%3D%3D" in {blah} parameter
*
******************************************************************************
*/
void gEncodeHTMLString(char *outParmName, char *inStr)
{
char *srcString;
int bufSize=(strlen(inStr)*3)+100; //buffer overrun is impossible
srcString = (char *)calloc(bufSize, sizeof(char));
strcpy(srcString, "SourceString=");
strcat(srcString, inStr);
web_convert_param(outParmName, srcString, "SourceEncoding=HTML", "TargetEncoding=URL", LAST);
free(srcString);
}
/******************************************************************************
*
* gEncodeStringChar()
*
* Parses the string pointed to by *inStr, converting all cDelim chars
* to it's ASCII hex equivlent in the form:
*
* Example: when cDelim = '_', it is converted to "%5f"
*
* The converted string is saved to the *outParmName parameter. If the
* {outParmName} parameter does not exist it will be created; if it
* exists it will be overwritten.
*
******************************************************************************
*/
void gEncodeStringChar(char *outParmName, char *inStr, char cDelim) {
char c, *buf;
int i, j=0, bufSize=(strlen(inStr)*3)+100; //buffer overrun is impossible
buf = (char *)calloc(bufSize, sizeof(char));
//lr_output_message("buf[] size is: %d", bufSize);
for (i=0; j<bufSize; i++) {
if ((c=inStr[i]) == '\0') {
break; //input string has been fully converted
}
if (c == cDelim) {
sprintf(buf, "%s%%%02x", buf, cDelim);
j+=2;
} else {
buf[j] = c;
}
j++;
//lr_output_message("c: '%c', cDelim: '%c', buf[]: %s", c, cDelim, buf);
}
lr_save_string(buf, outParmName);
free(buf);
}
/******************************************************************************
*
* gStripStringChar()
*
* Parses the string pointed to by *inStr, deleting all its cDeleteMe chars
* from the string.
*
* The converted string is saved to the *outParmName parameter. If the
* {outParmName} parameter does not exist it will be created; if it
* exists it will be overwritten.
*
******************************************************************************
*/
void gStripStringChar(char *outParmName, char *inStr, char cDeleteMe) {
char c, *buf;
int i, j=0, bufSize=strlen(inStr)+1; //buffer overrun is impossible
buf = (char *)calloc(bufSize, sizeof(char));
for (i=0; j<bufSize; i++) {
if ((c=inStr[i]) == '\0') {
buf[j]='\0';
break; //input string has been fully parsed
}
if (c != cDeleteMe) {
buf[j++] = c;
}
//lr_output_message("c: '%c', cDeleteMe: '%c', buf[]: %s", c, cDeleteMe, buf);
}
lr_save_string(buf, outParmName);
free(buf);
}