[bb] Read Comma Delimited String by Ziltch [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : Read Comma Delimited String
Author : Ziltch
Posted : 1+ years ago

Description : ReadCSVString$(Stream) works like ReadString(stream) but looks for data enclosed in quotes not in blitz basic's string format.

Example:


  type quotes
    field quote$
    field Author$
    field era$
    field Num
  end type

  quotefile=readfile("smallquotes.txt")
  while not eof(quotefile)
    tq.quotes  = new quotes
    tqquote$  = ReadCSVString$(quotefile)
    tqAuthor$ = ReadCSVString$(quotefile)
    tqera$    = ReadCSVString$(quotefile)
    count = count + 1
    tqNum = count
;    debuglog count + " "+tqquote$+" "+tqAuthor$+" "+tqera$
  wend
  closefile(quotefile)


smallquotes.txt
"Beware the Jabberwock, my son!","Lewis Carroll","1832-98"
"Though it rain daggers with their points downward.","Burton, Robert","1576-1640"
"A String","Another String","etc"

The code looks for data inside quotes so the data can be delimited by any character, not just commas. TAB delimited should work as well.

Make sure the date does not contain double quotes " in the strings as this will confuse it! [/i]

Code :
Code (blitzbasic) Select
Function ReadCSVString$(Stream)
; ADAmor Ziltch Nov 2003

  local qte$,Ccnt,byt,char$
  qte$=chr(34)
  OutString$ = ""
  Ccnt = 0
  while char$<>qte$ and eof(stream)=0
    Ccnt = Ccnt + 1
    byt= readbyte(stream)
    char$=chr(byt)
  wend
  char$=""
  while char$<>qte$ and eof(stream)=0
    Ccnt = Ccnt + 1
    byt= readbyte(stream)
    char$=chr(byt)
    if char$ <> qte$ then OutString$=OutString$+char$
  wend
 
  return OutString$
End Function


Comments : none...