Altering text within a file

Started by Baggey, November 06, 2021, 13:49:26

Previous topic - Next topic

Baggey

I have found whilst writting my Emulators. Some times you may want to alter a variable Name or field Name. This could occur upto a thousand times or more within your code.

I have Automated this Process 8) As altering Line by Line will take a long time and can be very laborious  ???

The idea was given to me by Midimaster and i have Written my own Version for my own uses which i am sharing.  :D

This is Runable Code! Please note this will Distroy your Origonal FILE!

Code (Blitzmax) Select
' Written by Lee Chatt

    SuperStrict

    Graphics 10,100
     
    Global File:String = "6510_OPcodes.bmx" ' FILE to be Modified
    Global Path:String = ""  ' Leave BLANK if file is in Same Directory!
   
    'load the original file:-
    If FileSize(Path+File)=-1 Then
            Notify "File ~r" + Chr(34) + Path+File  + Chr(34) + "~rnot found!"
            End
    EndIf

    Global Original:String  = LoadText(Path+File)
    Global ReplaceText:String = "C65" ' <--- Text we want to alter
    Global NewText:String = "C6510" ' <--- String we want to Replace OLD TEXT with
    Global SpecialCharacter:Int = Asc(".") ' <--- If Special character after Text will replace!
    Global word:String
    Global FoundText:Int
    Global TextLength:Int
    Global TextNext:Int
    Global FoundSomething:Byte=0
     

    Global wordlength:Int=0
    Global ReplaceTextLength:Int=ReplaceText.Length


    Repeat
       TextLength = Original.Length
       Print
       Print "whole Text length = " + TextLength
       Print
         
       ' find the next position of our keyword and what comes next
       FindText()
       WhatsNext()
       
       If ( TextNext =  32 ) Or ( TextNext =  13 ) Or ( TextNext = SpecialCharacter ) Then
wordlength=FoundText+ReplaceTextLength
AlterText()
FoundSomething=1
'WaitKey()
  Else
GetText()

Print ""
Print "Found at Pos "+FoundText+" ["+word+"]"
Print ""
Print "SO IGNORE!"
Print ""
       End If

            Forever


Function Terminate()

' If Print OUT is OKAY! then uncomment SaveText BELOW!

Print Original
   
' //////////////////////////////////////////////////////
' WARNING THESES LINES WILL OVERWRITE ORIGINAL FILE!
'Rem 
If Foundsomething=1 Then
Print
Print "PRESS ENTER to SaveText!"
Repeat Until KeyHit(key_ENTER)
SaveText Original, Path + File
Print ""
Print "Text Saved!"

Else
Print
Print "NOTHING TO ALTER!"

End If
'End Rem
' WARNING THESE LINES WILL OVERWRITE ORIGINAL FILE!
' //////////////////////////////////////////////////////

End

End Function


Function WhatsNext()

' Chr(34) forces " to be printed

TextNext = Original [ FoundText + ReplaceText.Length ]
If TextNext <> 13 Then
'Print "Whats next = " + TextNext + " ... Which is " + Chr(34) + Chr ( TextNext ) + Chr(34)
Else
'Print "Whats next = " + TextNext + " ... Which is " + Chr(34) + "CR" + Chr(34)
End If

End Function


Function FindText()

FoundText = Original.Find( ReplaceText , FoundText + 1 )
            If FoundText=-1 Then Terminate()

End Function


Function GetText()

' Now copy string Upto newline or Carrige Return CR

For Local line:Int=FoundText To wordlength-1
word:+Chr(Original[Line])
Next

End Function


Function AlterText()

GetText()
Rem
Print ""
Print "Found at Pos "+FoundText+" ["+word+"]"
Print
Print "Length of Program line is "+(Wordlength-FoundText)+" characters Long Until Newline!"
Print
End Rem
word=Right(word,Wordlength-FoundText-ReplaceTextLength)
Local LengthOFword:Int=word.length

word=NewText+word

Rem
Print "New Program Line is ["+word+"]"
Print ""
End Rem

Original=Original[0..FoundText] + word + Original[FoundText+LengthOFword+ReplaceTextLength..TextLength]

word=""

End Function
     


if anyone has a more efficient way of doing the above please share here!

Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 24GB ram 1TB SSD and NVIDIA Quadro K620 . DID Technology stop! Or have we been assimulated!

ZX Spectrum 48k, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip. Im Misunderstood!

Derron

"Find and Replace" of your text editor of choice?
Regex ?

on linux this could also simply be done with "sed" on the command line (so with WSL on windows 10+ too).


did not check what your "specialcharacter" thing does ... but with regex you could require dots too ...


bye
Ron