Monday, October 06, 2025

Convert Serialized GtkTextBuffer contents to ASCII

 The contents of a GtkTextBuffer can be saved as serialized binary file. Whilst converting such a file to ascii may be problematic if the buffer contains binary image data, saving text can be achieved quite simply as follows:

 

$txt save myfile.ser 

set file [open "myfile.ser" rb]
set DATA [read $file]
close $file

set fp [open myfile.txt w]
puts $header
set i 0
foreach line [split $DATA \n] {
    if {$i == 0} {
        puts $fp ":GTKTEXTBUFFERCONTENTS-0001 <text_view_markup>"
        set i 1
        continue}
    puts $fp $line
}
close $fp