Results 1 to 3 of 3

Thread: CSV Parsing Module using OOP

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    18

    CSV Parsing Module using OOP

    Testing OOP in a CSV parsing Module


    
    Uses "console"
    
    
    'testing oop to create a CSV parsing module
    
    
    
    
    Type ParseCsv
    Private
      mainstring as string
      Delimiter As String  
      posString as long
      
    
    
    Public  
    Function _Create(ByVal txt_in As String) As Long
        '---------------------------------------------------
          me.mainstring=txt_in
          me.Delimiter=","
          
        End Function
    
    
    Function Chg_ParseCVS(ByVal txt_in As String)
        '---------------------------------------------------
          me.mainstring=txt_in
          
        End Function
    
    
    Function ParseInsert (index As Long ) As String
      'add delimiter position inside string ParseInsert("aa,bb,cc", "," ,2) would give aa,,bb,cc
      
      
      
      if (index > 0)  then 
        if (index > parsecount(me.mainstring,me.delimiter) + 1) Then
          function =me.mainstring
          exit function
        end if 
        index= index -1
        if index = 0 then 
          function = me.delimiter + me.mainstring
          exit function 
        end if  
        
        me.posString = instr(1,me.mainstring, me.delimiter,index)
      
        if me.posString=0 Then
          me.mainString = me.mainString+me.delimiter
          '? me.mainString
        else 
          me.mainString=mid$(me.mainstring,1,me.posString-1) + me.delimiter +  mid$(me.mainstring,me.posString )   
          '? me.mainString
          
        end if
      Else
        me.mainstring 
        '? ? me.mainString
        exit Function
      end if     
      
       
    End Function
    
    
    Function ParseDelete(index AS Long) AS String
             'deletes a indexed position inside string
        dim Cnt  AS Long
        dim cntMinusDelimiter as long
        Local Txt AS String
        If Len(me.MainString) = 0 Then
            exit function
            'Function = me.MainString  
        Else
            Cnt = instr(1,me.MainString,me.delimiter,index)
            Txt = Parse$(me.MainString, me.delimiter, index)
            cntMinusDelimiter=cnt-len(txt)-1
            if cnt=0 then
               me.MainString =mid$(me.MainString,1,cntMinusDelimiter)
            else   
              me.MainString =mid$(me.MainString,1,cntMinusDelimiter) + mid$(me.MainString,Cnt+len(me.delimiter)) 
            end if
        End If
    
    
    
    
    End Function
    
    
    Function ParseReplace( ByVal iPos AS Long, ByVal sNewString AS String) AS String
    'from William Burns
    
    
       Local iCount   AS Long
       Local sTextOut AS String
       If Len(me.mainstring) = 0 Then
          Function = me.mainstring  'just return same string on errors
       Else
          For iCount = 1 To ParseCount(me.mainstring, me.delimiter)
             If iCount = iPos Then   'is this the one we want to switch?
                sTextOut = sTextOut + sNewString + me.delimiter
             Else
                sTextOut = sTextOut + Parse$(me.mainstring, me.delimiter, iCount) + me.delimiter
             End If
          Next iCount
          me.mainstring = Left$(sTextOut,Len(sTextOut)-Len(me.delimiter))  'remove traling delimiter
       End If
    End Function
    
    
    
    
    
    
    function PrintOut()
        '---------------------------------------------------
          PrintL " mainstring :", Me.mainstring
          
    End function
    
    
    function ToVariable()as string
        '---------------------------------------------------
          function = Me.mainstring
          
    End function
    
    
    function parseNew(index as long) as string
       function = Parse$(me.mainstring,me.delimiter,index)
    end function
    
    
    
    
    function parseCnt() as long
      function = parsecount(me.mainstring,me.delimiter)
    
    
    end function
    end type
    
    
    dim main as string
    Dim c1 As ParseCsv("aa,bb,cc")  'CSV string (comma delimited)
    
    
    c1.ParseInsert(2)   'insert delimiter into aa,bb,cc like aa,,bb,cc
    c1.PrintOut()
    
    
    c1.ParseReplace(2,"dfa")  'puts dfa into aa,,bb,cc like aa,dfa,bb,cc
    c1.PrintOut()
    
    
    c1.ParseDelete(2)        'deletes the second delimited field
    c1.PrintOut()
    
    
    main = c1.ToVariable()   'retrieves mainstring and puts in variable
    
    
    Printl "main variable:", main
    
    
    'can not use parse$ reserve word as function name
    printl "ParseNew: " + c1.ParseNew(3)  ' the parse$ statement
    
    
    printl "ParseCnt: " + str$(c1.parseCnt()) 'the parsecount statement
    
    
    c1.Chg_ParseCVS("1,2,3")
    c1.PrintOut()
    
    
    Console_WaitKey
    
    Last edited by Gary; 08-09-2023 at 23:37. Reason: Edit some changes and function

Similar Threads

  1. Bracket parsing
    By Petr Schreiber in forum Tokenizer
    Replies: 6
    Last Post: 04-07-2022, 20:04
  2. Parsing CSV data file
    By ErosOlmi in forum General purpose scripts
    Replies: 1
    Last Post: 22-03-2007, 05:35

Members who have read this thread: 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •