Copy link to clipboard
Copied
How to replace "\folder1\folder2\folder3" with "folder1/folder2/folder3" using framemaker API ?
Hi Asha,
That should work as well. Note the following, though:
- your version will always be case-sensitive, which is OK for what you needed
- maybe you've left some code out, but what you have shown is missing some important allocation/deallocation steps for use with F_StrCat(). You have to allocate more space before concatenating strings and have to deallocate all unused pointers when done or before using them again. It might work for now, but I can assure you that improper allocation routines ar
...Copy link to clipboard
Copied
Hi Asha,
Here is a function that I use for substring replacement, using all FDK library functions. If you use standard C libraries instead, you could probably reduce this to a line or two.
To use it, you would send something like:
path = F_StrCopyString("\\folder1\\folder2\\folder3");
strReplace(path, "\\", "/", False);
There might be better ways to do this. I'm not really that much of a programmer.
Russ
//returns the index of the first change, or -1 if no changes.
IntT ws_StrReplace(StringT *mainString,
StringT searchString,
StringT replaceString,
IntT considerCase)
{
StringT strBuf,
returnString,
appendString;
IntT i,
firstIndex = -1;
//if the search string is empty, there is nothing to do.
if(F_StrIsEmpty(searchString))
returnString = F_StrCopyString(*mainString);
else
{
//otherwise, initialize the buffer
returnString = F_StrCopyString("");
for(i = 0; i < F_StrLen(*mainString); i++)
{
strBuf = F_StrCopyString(*mainString);
//Truncate the string from the beginning
F_StrReverse(strBuf, 10000);
F_StrTrunc(strBuf, (F_StrLen(strBuf) - i));
F_StrReverse(strBuf, 10000);
//and lop it down to the size of the search string
if(F_StrLen(strBuf) > F_StrLen(searchString))
F_StrTrunc(strBuf, F_StrLen(searchString));
//if they are the same, we are doing the replacement
if((considerCase && F_StrCmp(strBuf, searchString) == 0) ||
(!considerCase && F_StrICmp(strBuf, searchString) == 0))
{
appendString = F_StrCopyString(replaceString);
//set the return value
if(firstIndex < 0) firstIndex = i;
//jimmy the loop so we step past the length of the replacement
//string the next time around
i += F_StrLen(searchString) - 1;
}
//otherwise, we are just appending 1 character on.
else
{
F_StrTrunc(strBuf, 1);
appendString = F_ApiCopyString(strBuf);
}
//now, concatenate
//rciReturnString = F_Realloc(rciReturnString,
//F_StrLen(rciReturnString) + F_StrLen(rciAppendString), NO_DSE);
returnString = (StringT) F_Realloc(returnString,
(F_StrLen(returnString) + F_StrLen(appendString))*sizeof(StringT), NO_DSE);
F_StrCat(returnString, appendString);
F_ApiDeallocateString(&strBuf);
F_ApiDeallocateString(&appendString);
} //end main else
}
//all done
F_ApiDeallocateString(mainString);
*mainString = F_StrCopyString(returnString);
F_ApiDeallocateString(&returnString);
return firstIndex;
}
Copy link to clipboard
Copied
Hi Russ,
Thank you for sharing the code.
I tried the following using F_StrTok and it worked.
StringT tok, str1;
//str contains "folder1\folder2\folder3"
tok = F_StrTok(str, (StringT)"\\");
str1 = F_StrCopyString(tok);
while (tok != NULL)
{
tok = F_StrTok(NULL, (StringT)"\\");
if(tok !=NULL)
{
F_StrCat(str1,(StringT)"/");
F_StrCat(str1,tok);
}
}
// str1 will contain the replaced string "folder1/folder2/folder3"
Thanks,
Asha
Copy link to clipboard
Copied
Hi Asha,
That should work as well. Note the following, though:
- your version will always be case-sensitive, which is OK for what you needed
- maybe you've left some code out, but what you have shown is missing some important allocation/deallocation steps for use with F_StrCat(). You have to allocate more space before concatenating strings and have to deallocate all unused pointers when done or before using them again. It might work for now, but I can assure you that improper allocation routines are a sure path to memory leaks and crashes in the future.
Russ
Copy link to clipboard
Copied
Hi Russ,
As you had mentioned , I did face memory problem as I had not reallocated the str1 before F_StrCat and I had not deallocated the tok and str1.
I have added those statements in my code.
StringT tok, str1;
//str contains "folder1\folder2\folder3"
tok = F_StrTok(str, (StringT)"\\");
str1 = F_StrCopyString(tok);
while (tok != NULL)
{
tok = F_StrTok(NULL, (StringT)"\\");
if(tok !=NULL)
{
str1 = (StringT)F_Realloc(str1, F_StrLen(str1)+2, NO_DSE);
F_StrCat(str1,(StringT)"/");
str1 = (StringT)F_Realloc(str1, F_StrLen(str1)+F_StrLen(tok)+1, NO_DSE);
F_StrCat(str1,tok);
}
}
// str1 will contain the replaced string "folder1/folder2/folder3"
str = F_StrCopyString(str1);
F_ApiDeallocateString(&tok);
F_ApiDeallocateString(&str1);
if(str) F_Free(str);
Thanks,
Asha
Copy link to clipboard
Copied
Hi Asha,
Looks better. Here's how I would probably do it, although your method may be just fine:
str1 = (StringT) F_Realloc(str1, (F_StrLen(str1) + 1)*sizeof(StringT), NO_DSE);
F_StrCat(str1, "/");
str1 = (StringT) F_Realloc(str1, (F_StrLen(str1) + F_StrLen(tok))*sizeof(StringT), NO_DSE);
F_StrCat(str1, tok);
Like I say, though, I don't know if that's any better. Might be worse even.
Also, make sure you deallocate "str" before using it again, as in:
F_ApiDeallocateString(&str);
str = F_StrCopyString(str1);
Russ