For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > September 2004 > RTF "parcer" - just thought somebody may need that









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author RTF "parcer" - just thought somebody may need that
Vlad Kozin

2004-09-28, 8:55 pm

Hi everyone,

I was working with RTF files today, got tired viewing rtf internals in
Notepad.
So I really quickly wrote that rtf parcing thing. Thought that a Treeview
can show the rtf hierarchy a little better.

To use the source code below
- create an empty EXE project,
- add microsoft windows common controls via project|components menu,
- paste a treeview on the form, rename it to tvTreeView,
- paste a button on the form, call it cmdGo,
- paste the code to the code window,
- go to project properties, and set the path to an rtf file in the
Make|Command line box,
- press f5.

The code uses recursion, so I don't expect it to handle really large rtf
files.
Take it, break it, use it. It's all yours.

regards,
Vlad
www.vbrocks.net

======== cut here ========

Option Explicit
Option Base 0

Private tmpfn As String
Private aFile() As Byte
Private gTotalBytes As Long
Private pos As Long

Private Sub OpenFile(ByVal fn As String)
Dim fvar As Long
Dim aNode As Node
' read file
If InStr(1, fn, "\") = 0 Then
fn = App.Path & "\" & fn
End If
gTotalBytes = FileLen(fn)
fvar = FreeFile
ReDim aFile(gTotalBytes)
Open fn For Binary Access Read As #fvar
Get #fvar, , aFile
Close #fvar
End Sub

Private Sub cmdGo_Click()
Dim aNode As Node
If tmpfn <> "" Then
Me.tvTreeView.Nodes.Clear
Call OpenFile(tmpfn)
pos = 0
Set aNode = Me.tvTreeView.Nodes.Add(, , "MAIN", "RTF")
aNode.Expanded = True
Call ParseFile(aNode)
End If
End Sub

Private Function ParseFile(pNode As Node)
Dim k As Long
Dim ch As String
Dim aNode As Node
Do While pos <= gTotalBytes
ch = Chr(aFile(pos))
Select Case ch
Case "{"
pos = pos + 1
Set aNode = tvTreeView.Nodes.Add(pNode, tvwChild)
aNode.Expanded = True
aNode.text = "{"
Call ParseFile(aNode)
Case "}"
pNode.text = pNode.text & "}"
Exit Do
Case Else
If Asc(ch) > 31 Then
pNode.text = pNode.text & ch
End If
End Select
pos = pos + 1
Loop
End Function

Private Sub Form_Load()
tmpfn = Command$
End Sub


Ken Halter

2004-09-28, 8:55 pm

Vlad Kozin wrote:
> Hi everyone,
>
> I was working with RTF files today, got tired viewing rtf internals in
> Notepad.


Didn't try the code but.... Cool. Tucked away, ready to google later
and.....

> regards,
> Vlad
> www.vbrocks.net


..... I agree... VB does rock <g>

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
Veign

2004-09-28, 8:55 pm

Look pretty kewl....

--
Chris Hanscom
MVP (Visual Basic)
http://www.veign.com
--

"Vlad Kozin" <vladkozin@hotmail.com> wrote in message
news:uVkBL1ZpEHA.3172@TK2MSFTNGP10.phx.gbl...
> Hi everyone,
>
> I was working with RTF files today, got tired viewing rtf internals in
> Notepad.
> So I really quickly wrote that rtf parcing thing. Thought that a Treeview
> can show the rtf hierarchy a little better.
>
> To use the source code below
> - create an empty EXE project,
> - add microsoft windows common controls via project|components menu,
> - paste a treeview on the form, rename it to tvTreeView,
> - paste a button on the form, call it cmdGo,
> - paste the code to the code window,
> - go to project properties, and set the path to an rtf file in the
> Make|Command line box,
> - press f5.
>
> The code uses recursion, so I don't expect it to handle really large rtf
> files.
> Take it, break it, use it. It's all yours.
>
> regards,
> Vlad
> www.vbrocks.net
>
> ======== cut here ========
>
> Option Explicit
> Option Base 0
>
> Private tmpfn As String
> Private aFile() As Byte
> Private gTotalBytes As Long
> Private pos As Long
>
> Private Sub OpenFile(ByVal fn As String)
> Dim fvar As Long
> Dim aNode As Node
> ' read file
> If InStr(1, fn, "\") = 0 Then
> fn = App.Path & "\" & fn
> End If
> gTotalBytes = FileLen(fn)
> fvar = FreeFile
> ReDim aFile(gTotalBytes)
> Open fn For Binary Access Read As #fvar
> Get #fvar, , aFile
> Close #fvar
> End Sub
>
> Private Sub cmdGo_Click()
> Dim aNode As Node
> If tmpfn <> "" Then
> Me.tvTreeView.Nodes.Clear
> Call OpenFile(tmpfn)
> pos = 0
> Set aNode = Me.tvTreeView.Nodes.Add(, , "MAIN", "RTF")
> aNode.Expanded = True
> Call ParseFile(aNode)
> End If
> End Sub
>
> Private Function ParseFile(pNode As Node)
> Dim k As Long
> Dim ch As String
> Dim aNode As Node
> Do While pos <= gTotalBytes
> ch = Chr(aFile(pos))
> Select Case ch
> Case "{"
> pos = pos + 1
> Set aNode = tvTreeView.Nodes.Add(pNode, tvwChild)
> aNode.Expanded = True
> aNode.text = "{"
> Call ParseFile(aNode)
> Case "}"
> pNode.text = pNode.text & "}"
> Exit Do
> Case Else
> If Asc(ch) > 31 Then
> pNode.text = pNode.text & ch
> End If
> End Select
> pos = pos + 1
> Loop
> End Function
>
> Private Sub Form_Load()
> tmpfn = Command$
> End Sub
>
>



Jeff Johnson [MVP:VB]

2004-09-28, 8:55 pm


"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:OZgNmOapEHA.3068@TK2MSFTNGP15.phx.gbl...

>
> Didn't try the code but.... Cool. Tucked away, ready to google later


The first change I would make to that code is to check for \ in the
preceding character whenever you encounter { or }, as this is RTF's way of
indicating that a literal brace is to be output, so \{ and \} shouldn't be
treated as group delimiters.


Jim Edgar

2004-09-29, 3:55 am

Thanks, I tried it and it is a good base to work with.

Jim Edgar

"Vlad Kozin" <vladkozin@hotmail.com> wrote in message
news:uVkBL1ZpEHA.3172@TK2MSFTNGP10.phx.gbl...
> Hi everyone,
>
> I was working with RTF files today, got tired viewing rtf internals in
> Notepad.
> So I really quickly wrote that rtf parcing thing. Thought that a Treeview
> can show the rtf hierarchy a little better.
>
> To use the source code below
> - create an empty EXE project,
> - add microsoft windows common controls via project|components menu,
> - paste a treeview on the form, rename it to tvTreeView,
> - paste a button on the form, call it cmdGo,
> - paste the code to the code window,
> - go to project properties, and set the path to an rtf file in the
> Make|Command line box,
> - press f5.
>
> The code uses recursion, so I don't expect it to handle really large rtf
> files.
> Take it, break it, use it. It's all yours.
>
> regards,
> Vlad
> www.vbrocks.net
>
> ======== cut here ========
>
> Option Explicit
> Option Base 0
>
> Private tmpfn As String
> Private aFile() As Byte
> Private gTotalBytes As Long
> Private pos As Long
>
> Private Sub OpenFile(ByVal fn As String)
> Dim fvar As Long
> Dim aNode As Node
> ' read file
> If InStr(1, fn, "\") = 0 Then
> fn = App.Path & "\" & fn
> End If
> gTotalBytes = FileLen(fn)
> fvar = FreeFile
> ReDim aFile(gTotalBytes)
> Open fn For Binary Access Read As #fvar
> Get #fvar, , aFile
> Close #fvar
> End Sub
>
> Private Sub cmdGo_Click()
> Dim aNode As Node
> If tmpfn <> "" Then
> Me.tvTreeView.Nodes.Clear
> Call OpenFile(tmpfn)
> pos = 0
> Set aNode = Me.tvTreeView.Nodes.Add(, , "MAIN", "RTF")
> aNode.Expanded = True
> Call ParseFile(aNode)
> End If
> End Sub
>
> Private Function ParseFile(pNode As Node)
> Dim k As Long
> Dim ch As String
> Dim aNode As Node
> Do While pos <= gTotalBytes
> ch = Chr(aFile(pos))
> Select Case ch
> Case "{"
> pos = pos + 1
> Set aNode = tvTreeView.Nodes.Add(pNode, tvwChild)
> aNode.Expanded = True
> aNode.text = "{"
> Call ParseFile(aNode)
> Case "}"
> pNode.text = pNode.text & "}"
> Exit Do
> Case Else
> If Asc(ch) > 31 Then
> pNode.text = pNode.text & ch
> End If
> End Select
> pos = pos + 1
> Loop
> End Function
>
> Private Sub Form_Load()
> tmpfn = Command$
> End Sub
>
>



Vlad Kozin

2004-09-29, 3:55 pm

Thanks :) Receiving good feedbacks, especially from the VB monsters - is
always a pleasure. :)


getting ready to submit more good stuff...
Vlad

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:OZgNmOapEHA.3068@TK2MSFTNGP15.phx.gbl...
> Vlad Kozin wrote:
>
> Didn't try the code but.... Cool. Tucked away, ready to google later
> and.....
>
>
> .... I agree... VB does rock <g>
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> Please keep all discussions in the groups..



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com