Help:Converters

From C64 Diskmag Wiki
Jump to: navigation, search

Converter main program

Here is the commented version of the conv_hirado_05-11.py converter.

import binascii, os, sys, string
#whic python library is needed

from convert import hirado05
#import the specific converter

from d64 import d64
#import the d64 manager codeblock

for magpostfix in [ "05", "06", "07", "08", "09", "10", "11"] :
    magname = "hirado" + magpostfix
# the d64s whic are convetable with this are hirado05.d64, hirado06.d64...   


    print "Processing", magname
    f = open(magname + ".d64", "rb")
    disk = f.read()
    f.close()
    img = d64.D64image(disk)
    dir = img.get_dir()
    
    #reads the d64 in
    
    for entry in dir :
        filename = entry[0]
        if filename[0] in string.digits and filename[1] in string.digits :
        #the following block is started with that file, if the filename first and second char is a number
        #of-course, if the coding or somethin changed through the evolution of disk mag, maybe the file names, or the
        #char tabel conversions changed by the time, and you need different code blocks
            
            print filename
            
            data = img.get_file_by_name(filename)
            #reads the specific prg file into data
            
            data = data[4:]
            #cuts the first 3 bytes from file, mainly enough 2  "[3:]" because of load address
            
            data = hirado05.unpack(data)
            #calls the specific unpack function
            
            data = hirado05.translate(data)
            #calls the char specific translation function
            
            data = hirado05.webready(data)
            #call the specific transformation function which is changing the special chars to a web displayable format
            
            of = open(magname + "-" + filename[0:2] + ".txt", "wb")
            of.write(data)
            of.close()
            #writes the converted results
 

Some examples for filename checking:
if filename[0:2] in ("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8") :
if filename[0] == "H" and filename[1] in string.digits :
if filename[0] in string.letters and filename[1] == "\xdd" :

Skipping files:
this code skips the file if it is on the disk 09 and starts with O

if filename[0] in string.letters and filename[1] == "\xdd" :
            if magpostfix == "09" and filename[0] == "O" :
                continue

Converter blocks in the exteranl converter

You can define here the special conversion, like extracting, char changing, converting to web format.

#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import binascii, struct, sys
        
table = "áabcdefghijklmnopqrstuvwxyz[é]íó" + \
        " !\"öő%&'()*+,-./0123456789:;úüű?" + \
        "_ABCDEFGHIJKLMNOPQRSTUVWXYZ_____"  + \
        " !\"#$%&'()*+,-./0123456789:;<=>?" + \
        "ÁABCDEFGHIJKLMNOPQRSTUVWXYZ_É_ÍÓ"+ \
        " !\"ÖŐ%&'()*+,-./0123456789:;ÚÜŰ?" + \
        "_ABCDEFGHIJKLMNOPQRSTUVWXYZ_____" + \
        " !\"#$%&'()*+,-./0123456789:;<=>?" 

#this is the replace table of the bytes.
#Following the guidelines
#normal text - lower case
#flasghing, animated - upper case
                  
                  
#this code block simply transaltes the data, with the help of the prevoiusly defined table.
def translate(data) :    
    outdata = ""
    line = ""
    for i in xrange(len(data)) :
        char = ord(data[i])
        line += table[char]
        
        if table[char] == "_" :
            print line, hex(char)
       
        if i % 40 == 39 :
            outdata += line.rstrip() + "\n"
            line = ""
            
    return outdata + line.rstrip() + "\n"

#this block simply unpacks the data
#in this case, if $a0 found, the next byte tells how many spaces needed
#(this is a realy simple byte packing)
def unpack(data) :
    outdata = ""
    i = 0
    while i < len(data) :
        if data[i] == "\xa0" :
            count = ord(data[i + 1])
            if count == 0 :
                print "count is zero!"
                sys.exit()
            outdata += (" " * count)
            i += 2        
        else :
            outdata += data[i]
            i += 1
            
    return outdata

#this fnction does the web fomrating
#replaces with escaped blocks the special chars used 
#and adds the pre tag pair.

def webready(data) :
    data = data.replace("&", "&amp;")
    data = data.replace("<", "&lt;")
    data = data.replace(">", "&gt;")
    
#convert lower accented chars
    data = data.replace("á", "&aacute;")
    data = data.replace("é", "&eacute;")
    data = data.replace("í", "&iacute;")
    data = data.replace("ó", "&oacute;")    
    data = data.replace("ö", "&ouml;")
    data = data.replace("ő", "&#337;")    
    data = data.replace("ú", "&uacute;")
    data = data.replace("ü", "&uuml;")
    data = data.replace("ű", "&#369;")    
    
#convert upper accented chars
    data = data.replace("Á", "&Aacute;")
    data = data.replace("É", "&Eacute;")
    data = data.replace("Í", "&Iacute;")
    data = data.replace("Ó", "&Oacute;")    
    data = data.replace("Ö", "&Ouml;")
    data = data.replace("Ő", "&#336;")    
    data = data.replace("Ú", "&Uacute;")
    data = data.replace("Ü", "&Uuml;")
    data = data.replace("Ű", "&#368;")   
    
    data = "<pre>" + data + "</pre>"
    return data

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox