Announcement

Collapse
No announcement yet.

Convert c style text

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    One thing to bear in mind, if you're planning to do your own conversions, is that the escape character may be escaped. It's not safe to just use a REPLACE statement, since "'\\r" should mean "\r", where "m\r" turns into "m"+$CR ...in fact, you'll often see escaped escapes in C strings that contain paths.

    You'll get better results doing a sequential scan through the string.

    Comment


    • #22
      > is that the escape character may be escaped

      That's because 'c' compilers do 'escaping' at compile time to allow for the shorthand "\n" or "\t" notation in the programmer's source code.

      The PB compilers don't use shorthand stuff requiring escaping at compile time; eg here "\n" and "\t" would be "$LF" and "$TAB" in the source code.

      (Except maybe for $DQ? Since I'm unsure is one more reason I avoid using inline literals).

      MCM
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #23
        Tom, as long as you are aware of escaped escape characters, you can get around them. Instead of doing a REPLACE "\\" WITH "\" in sFormat you could do a REPLACE "\\" WITH $NUL IN sFormat before doing all the other escape character replaces and then do a REPLACE $NUL WITH "\" IN sFormat when you are done. In your example of "\\r" the "\\" would get replaced with a null so the string would then be $NUL + "r". After replacing all the other escaped characters the null would be replaced with "\" leaving you with the string "\r".

        To be safer, instead of hard coding a null, you could first do a loop looking for a character or character sequence that doesn't exist in sFormat and using that as nulls might appear in sFormat. Because I found myself writing this sort of loop a number of times I ended up writing a routine called GetUniqueDelimiter that will find a single character that doesn't exist in the string passed in but I never did get around to adding the multiple character sequences. Comes in handy when you load a file and want to change all the end of line characters to a whatever you want. You get a unique delimiter that doesn't exist in the file and replace CRLF, CR and LF with that delimiter and then replace that delimiter with whatever end of line you want and the rest of the data remains intact.

        However, as you said, doing a sequential scan through the string might be better in this case seeing as many of the format codes can be prefixed and suffixed with other characters. Might as well just deal with the escaped characters (starting with \) when you run into them while looking for format codes (starting with %).
        Jeff Blakeney

        Comment

        Working...
        X