Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

RTF "parcer" - just thought somebody may need that
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



Report this thread to moderator Post Follow-up to this message
Old Post
Vlad Kozin
09-29-04 01:55 AM


Re: RTF "parcer" - just thought somebody may need that
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..

Report this thread to moderator Post Follow-up to this message
Old Post
Ken Halter
09-29-04 01:55 AM


Re: RTF "parcer" - just thought somebody may need that
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
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Veign
09-29-04 01:55 AM


Re: RTF "parcer" - just thought somebody may need that
"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.



Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Johnson [MVP:VB]
09-29-04 01:55 AM


Re: RTF "parcer" - just thought somebody may need that
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
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Jim Edgar
09-29-04 08:55 AM


Re: RTF "parcer" - just thought somebody may need that
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..



Report this thread to moderator Post Follow-up to this message
Old Post
Vlad Kozin
09-29-04 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Visual Basic archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:35 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.