Read a text file

Ken Ray kray at sonsothunder.com
Mon Jan 17 22:56:37 EST 2005


On 1/17/05 9:05 PM, "Richard Gaskin" <ambassador at fourthworld.com> wrote:

> I don't have the url offhand (in a bit of a rush this evening), but it
> wasn't hard to find last time I looked it up.

Here's the relevant code from Alex (something I've had in Scripter's
Scrapbook for a while now in case I need it):

--WATCH FOR LINE BREAKS

function CSV2Tab3 pData
   local tNuData -- contains tabbed copy of data

   local tReturnPlaceholder
   -- replaces cr in field data to avoid line breaks which would be misread
   -- as records; replaced later during display

   local tEscapedQuotePlaceholder
   -- used for keeping track of quotes in data

   local tInQuotedText
   -- flag set while reading data between quotes
  
   put numtochar(11) into tReturnPlaceholder -- vertical tab as placeholder
   put numtochar(2)  into tEscapedQuotePlaceholder
   -- used to simplify distinction between quotes in data and those in
   -- delimiters
   
   -- Normalize line endings:
   replace crlf with cr in pData          -- Win to UNIX
   replace numtochar(13) with cr in pData -- Mac to UNIX
   
   -- Put placeholder in escaped quote (non-delimiter) chars:
   replace ("\"&quote) with tEscapedQuotePlaceholder in pData
   replace quote&quote with tEscapedQuotePlaceholder in pData
   --
   put space before pData   -- to avoid ambiguity of starting context
   split pData by quote
   put False into tInsideQuoted
   put 0 into tCounter
   repeat for each line tKey in (the keys of pData)
     add 1 to tCounter
     put pData[tCounter] into k
     if (tInsideQuoted) then
       replace cr with tReturnPlaceholder in k
       put k after tNuData
       put False into tInsideQuoted
     else
       replace comma with tab in k
       put k after tNuData
       put true into tInsideQuoted
     end if
   end repeat
   --
   delete char 1 of tNuData -- remove the leading space
   replace tEscapedQuotePlaceholder with quote in tNuData
   return tNuData
end CSV2Tab3

HTH,

Ken Ray
Sons of Thunder Software
Web site: http://www.sonsothunder.com/
Email: kray at sonsothunder.com




More information about the use-livecode mailing list