Skip to main content
Known Participant
September 14, 2009
Answered

to replace "\" with "/" in a string using FrameMaker API

  • September 14, 2009
  • 1 reply
  • 1405 views

How to replace "\folder1\folder2\folder3" with "folder1/folder2/folder3" using framemaker API ?

This topic has been closed for replies.
Correct answer Russ Ward

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

1 reply

Legend
September 14, 2009

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

Known Participant
September 15, 2009

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

Russ WardCorrect answer
Legend
September 15, 2009

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