Sisentäjä
Author: Jonhux
        Added: 27. elokuuta 2011 kello 9.50
        Edited: 27. elokuuta 2011 kello 9.50
        Category: Sovellukset
    
Description
Vanha tekemäni sisentäjä, josta Legend teki oman versionsa :)
Käyttäkää varoen ja omalla vastuulla :D
    Code
Select all1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82  | // Jonhu 23.2.2010
lahde$ = "sotku.cb"
kohde$ = "sotku2.cb"
Print "Sisentää tiedostoa " + lahde$
Sisenna( lahde$, kohde$ )
Print "Tiedosto "+ lahde$ + " sisennetty kohteeseen: "+kohde 
WaitKey
Function Sisenna( file1$, file2$ )
    
    If FileExists( file1$ ) = 0 Then MakeError "Tiedostoa "+file1+" ei loytynyt"
    
    r = OpenToRead( file1$ )
    w = OpenToWrite( file2$ )
    
    While Not EOF(r)
        
        rivi$ = ReadLine(r)
        muutos = Sisennys( rivi$ )
        If muutos < 0 Then sisennys_kohta = sisennys_kohta + muutos    
        WriteLine w, GetVali( sisennys_kohta ) + Trim(rivi$)  
        If muutos > 0 Then sisennys_kohta = sisennys_kohta + muutos
    Wend
    
    CloseFile w
    CloseFile r
EndFunction
Function GetVali( nro, vali$ = "    " )
    If nro = 0 Then Return ""
    For a=1 To nro
        vali2$ = vali2$ + vali$
    Next a
    Return vali2$
EndFunction
Function Sisennys( rivi$ )
    stri$ = Trim( GetWord( rivi$, 1, " ") )
    
    // tutkitaan selvät tapaukset
    Select stri$
        Case "For"    : Return 1
        Case "While"  : Return 1
        Case "Repeat" : Return 1
        Case "Function":Return 1
        Case "Else"   : Return 1
        Case "Type"   : Return 1
        Case "Lock"   : Return 1
     
        Case "Next"   : Return -1
        Case "Wend"   : Return -1
        Case "Forever": Return -1
        Case "Until"  : Return -1
        Case "Unlock" : Return -1
        
        Case "Drawtoimage"  : Return 1
        Case "Drawtoscreen" : Return -1
   EndSelect
   
   // Jos if sana joudutaan parsimaan yksiriviset iffit pidemmistä
   If stri$ = "If" Then
        sanoja = CountWords( rivi$, " " )
        For a = 1 To sanoja
            If Trim( GetWord( rivi$, a, " " ) ) = "Then" Then 
                If a = sanoja Then Return 1 
                Return 0
            EndIf
        Next a
        Return 1
   EndIf
   
   // joku end-alkuinen / end ( endif, endfunction, endtype, endselect... )
   If LSet(stri$,3) = "End" Then Return -1
   
   // ei löytynyt mitään sanaa jolla olisi merkitystä sisennyksen kanssa..
   Return False
   
EndFunction
 | 
Comments
#20 Sent by: VesQ, 6. syyskuuta 2011 kello 18.36
Sama CountWordsille. Mutta kätevä koodinpätkä tämä on kuitenkin :)
Leave a comment
            You must be logged in to comment.
        
    
        
#19 Sent by: VesQ, 6. syyskuuta 2011 kello 18.36
GetWord kusee vähän ääkkösten kanssa niin kannattaa käyttää GetWord2-funktiota.
http://www.cbrepository.com/codes/code/11/