Read file with accented characters ?

Cubist at aol.com Cubist at aol.com
Tue May 23 07:57:10 EDT 2006


In a message dated 5/23/06 1:29:03 AM, Zax <zax at tripoy.com> writes:
>Thanks Devin for your solution.
>I have to say I don't know how to check the line endings in a safe way
>:(
>For exemple, I could write
>------
>    if (numToChar(13) & numToChar(10)) is in it then
>      put "win" into fileFormat
>    else if numToChar(10) is in it then
>      put "nix" into fileFormat
>    else put "mac" into fileFormat
>------
>but it's not very safe because ASCII 10 can sometimes be found in Mac
>files...
   Perhaps something like this might work:

function WhatMyLineEnding DerFile
  # DerFile had better have been read as a binary file;
  # otherwise, Rev will have automagically converted all end-of-line 
characters
  # to whatever is used by *your* machine
  put numToChar (10) into MyLinefeed
  put numToChar (13) into MyReturn

  put the length of DerFile into OldLength

  # how many lines?
  put the number of lines in DerFile into NumOfLines

  # how many linefeeds?
  put DerFile into Fred
  if char -1 of Fred <> MyLinefeed then put 1 into NumOfLFs
  replace MyLinefeed with "" in Fred
  add (OldLength - the length of Fred) to NumOfLFs

  # how many returns?
  put DerFile into Fred
  if char -1 of Fred <> MyReturn then put 1 into NumOfReturns
  replace MyReturn with "" in Fred
  add (OldLength - the length of Fred) to NumOfReturns

  # how many (return+linefeed)s?
  put DerFile into Fred
  if (char -2 to -1 of Fred) <> (MyReturn & MyLinefeed) then put 1 into 
NumOfCRLFs
  replace (MyReturn & MyLinefeed) with "" in Fred
  add (OldLength - the length of Fred) div 2 to NumOfCRLFs

  switch
    case (NumOfCRLFs = NumOfLines)
      put "Win/DOS" into FileSource
    break

    case (NumOfLFs = NumOfLines)
      put "Unix" into FileSource
    break

    case (NumOfReturns = NumOfLines)
      put "Mac" into FileSource
    break

    default
      put "???" into FileSource
  end switch

  return FileSource
end WhatsMyLineEnding

    Hopefully, this handler can identify which flavor of line-endings are 
used in any arbitrary file...



More information about the use-livecode mailing list