CNC_Pete
CNC_Pete

Reputation: 33

VB6 How to avoid text inside parentheses, when adding spaces to a text file?

I want to read in a partly unspaced text file like this: (I've had to make the lines double-spaced for ease of reading on this web page. My problem is with the spacing between the "words", not the lines)

O0377(NO EXTRA SPACES REQUIRED INSIDE ALL PARENTHESES)
(T1 WNMG R0.8) 
(T2 TNMG R0.8  SKEWED OUT) 
(T3 3MM P/BLADE) 
(T5 16MM U/DRILL CUTTING 16.5) 
(CYCLE TIME 1MIN 5SEC) 
G50S3000 
G30U0W0M8(ROUGH OD AND FACE) 
T0101

(NO EXTRA SPACES REQUIRED INSIDE SQUARE BRACKETS) 
[IF#100GT#101GOTO99]
G96G99S180M3 
G0X48.2Z20.
Z3.
N99G1Z-6.2F0.2
X12.F0.3 
G30W0M24 
M30

and then add a space before every letter, except inside parentheses - these are already spaced - and except inside square brackets - no spaces needed there. It should look like this:

O0377(NO EXTRA SPACES REQUIRED INSIDE ALL PARENTHESES) 
(T1 WNMG R0.8)
(T2 TNMG R0.8  SKEWED OUT) 
(T3 3MM P/BLADE) 
(T5 16MM U/DRILL CUTTING 16.5) 
(CYCLE TIME 1MIN 5SEC) 
G50 S3000 

G30 U0 W0 M8 (ROUGH OD AND FACE) 

T0101

(NO EXTRA SPACES REQUIRED INSIDE SQUARE BRACKETS) 
[IF#100GT#101 GOTO99]
G96 G99 S180 M3 
G0 X48.2 Z20.
Z3.
N99 G1 Z-6.2 F0.2
X12. F0.3 
G30 W0 M24 
M30

My problem is that my program puts spaces everywhere, like this:

O0377 ( N O  E X T R A S P A C E S  R E Q U I R E D  I N S I D E  A L L  P A R E N T H E S E S) 
( T1  W N M G  R0.8) 
( T2  T N M G  R0.8   S K E W E D  O U T) 
( T3 3 M M  P/ B L A D E) 
( T5 16 M M  U/ D R I L L  C U T T I N G 16.5) 
( C Y C L E  T I M E 1 M I N 5 S E C) 
G50 S3000 
G30 U0 W0 M8 ( R O U G H  O D  A N D  F A C E) 
T0101

( N O  E X T R A  S P A C E S  R E Q U I R E D  I N S I D E  S Q U A R E  B R A C K E T S) 
[ I F#100 G T#101 G O T O99]
G96 G99 S180 M3 
G0 X48.2 Z20.
Z3.
N99 G1 Z-6.2 F0.2
X12. F0.3 
G30 W0 M24 
M30

This is my code. (Don't know how to make it all look like code...) It's not pretty, but I can understand it, and it doesn't take long to process a typical file.

Private Sub Form_Load()
Dim InputLines, NextLine, OutputLines As String
Dim fileIn, fileOut As Integer

fileIn = FreeFile
Open "C:\Documents and Settings\Owner\Desktop\FileToBeSpaced.txt" For Input As fileIn

fileOut = FreeFile
Open "C:\Documents and Settings\Owner\Desktop\SpacedFile.txt" For Append As fileOut


Do While Not EOF(fileIn)
    Line Input #fileIn, NextLine
    InputLines = InputLines + NextLine + Chr(13) + Chr(10)

    OutputLines = InputLines
    OutputLines = Replace(OutputLines, "A", " A")
    OutputLines = Replace(OutputLines, "B", " B")
    OutputLines = Replace(OutputLines, "C", " C")
    OutputLines = Replace(OutputLines, "D", " D")
    OutputLines = Replace(OutputLines, "E", " E")
    OutputLines = Replace(OutputLines, "F", " F")
    OutputLines = Replace(OutputLines, "G", " G")
    OutputLines = Replace(OutputLines, "H", " H")
    OutputLines = Replace(OutputLines, "I", " I")
    OutputLines = Replace(OutputLines, "J", " J")
    OutputLines = Replace(OutputLines, "K", " K")
    OutputLines = Replace(OutputLines, "L", " L")
    OutputLines = Replace(OutputLines, "M", " M")
    OutputLines = Replace(OutputLines, "N", " N")
    OutputLines = Replace(OutputLines, "O", " O")
    OutputLines = Replace(OutputLines, "P", " P")
    OutputLines = Replace(OutputLines, "Q", " Q")
    OutputLines = Replace(OutputLines, "R", " R")
    OutputLines = Replace(OutputLines, "S", " S")
    OutputLines = Replace(OutputLines, "T", " T")
    OutputLines = Replace(OutputLines, "U", " U")
    OutputLines = Replace(OutputLines, "V", " V")
    OutputLines = Replace(OutputLines, "W", " W")
    OutputLines = Replace(OutputLines, "X", " X")
    OutputLines = Replace(OutputLines, "Y", " Y")
    OutputLines = Replace(OutputLines, "Z", " Z")
    OutputLines = Replace(OutputLines, "(", " (")

Loop

Print #fileOut, OutputLines

Close fileIn
Close fileOut

Unload frmInsertSpaces

End Sub

The "word" spacing is the main issue. I want to avoid unnecessary extra spaces.

And then, obviously I don't want to have to work with a fixed path and filename, like I have in my code at the moment. I'd like to just drop any unspaced file onto an icon or something, and have it spaced, and automatically saved to the desktop. Or even right-click an unspaced file, and do it from there.

If anyone can provide a (preferably uncomplicated) solution, or point me in the right direction, I'd be very grateful. Thanks in advance, Pete.

Upvotes: 0

Views: 104

Answers (1)

Bob77
Bob77

Reputation: 13267

Pretty routine stuff really.

This ought to be close if not precisely what you want. Try to follow the code, which uses an "inline string builder" technique to avoid concatenation performance bottlenecks. This sort of thing is needed so often you ought to become familiar with the approach rather than just copy/pasting code handouts.

Because of your title and poor problem description above I despair of any future reader ever discovering this and applying it to other problems though.

Option Explicit

Private Sub Main()
    Dim InpF As Integer   'Input file number.
    Dim InpText As String 'Input line of text.
    Dim InpP As Long      'Input "pointer" within line.
    Dim NewF As Integer
    Dim NewText As String
    Dim NewP As Long
    Dim Char As String
    Dim EndB As String    'End bracket character.
    Dim TempP As Long
    Dim Length As Long    'Length of bracketed text.

    InpF = FreeFile(0)
    Open "original.txt" For Input As #InpF
    NewF = FreeFile(0)
    Open "new.txt" For Output As #NewF
    Do Until EOF(InpF)
        Line Input #InpF, InpText
        NewText = Space$(Len(InpText) * 3 \ 2) 'Allocate estimated space.
        NewP = 1
        For InpP = 1 To Len(InpText)
            Char = Mid$(InpText, InpP, 1)
            If Char = "(" Or Char = "[" Then
                If Char = "(" Then EndB = ")" Else EndB = "]"
                TempP = InStr(InpP + 1, InpText, EndB)
                If TempP = 0 Then TempP = Len(InpText)
                Length = TempP - InpP + 1
                If Len(NewText) < NewP + Length - 1 Then
                    NewText = NewText & Space$(Length) 'Add more space.
                End If
                Mid$(NewText, NewP, Length) = Mid$(InpText, InpP, Length)
                InpP = InpP + Length
                NewP = NewP + Length
            Else
                If Char Like "[A-Z]" And InpP > 1 Then NewP = NewP + 1
                If Len(NewText) < NewP + 2 Then
                    NewText = NewText & Space$(10) 'Add more space.
                End If
                Mid$(NewText, NewP) = Char
                NewP = NewP + 1
            End If
        Next
        Print #NewF, Left$(NewText, NewP - 1)
    Loop
    Close #NewF
    Close #InpF
End Sub

Upvotes: 1

Related Questions