Voilà un programme pour colorer vos codes écrit en C.
Il n'est aps très évolué, mais vous pouvez l'améliorer en rajoutant des fonctions, des mots clé ou des types de variables dans les DATA.
Voilà le code (fais en qq heures) :
Code : Tout sélectionner
Global nomFichier.s
Global etat.b
Global out.s, temp.s
Enumeration
#NORMAL
#COMMENT
#TEXTE1
#TEXTE2
EndEnumeration
etat = #NORMAL
NewList Type.s()
Restore Type:
Read type$
While type$
AddElement(Type())
Type() = type$
Read type$
Wend
NewList KeyWord.s()
Restore KW:
Read type$
While type$
AddElement(KeyWord())
KeyWord() = type$
Read type$
Wend
NewList Function.s()
Restore fct:
Read type$
While type$
AddElement(Function())
Function() = type$
Read type$
Wend
Procedure.b GetCar(str$, ind.w)
If ind > Len(str$) Or ind < 1
ProcedureReturn ""
Else
ProcedureReturn PeekB(@str$ + ind - 1)
EndIf
EndProcedure
Procedure.s ReplaceWord(mot$)
;Debug mot$
ForEach Type()
If mot$ = Type()
ProcedureReturn "<font class='type'>" + mot$ + "</font>"
EndIf
Next
ForEach KeyWord()
If mot$ = KeyWord()
ProcedureReturn "<font class='keyWord'>" + mot$ + "</font>"
EndIf
Next
ForEach Function()
If mot$ = Function()
ProcedureReturn "<font class='fonction'>" + mot$ + "</font>"
EndIf
Next
ProcedureReturn mot$
EndProcedure
Procedure ConvertTextC()
temp = ReplaceString(temp, "<" , "<")
tmp$ =""
If Len(temp) And etat = #Normal
j = 1
For k = 1 To Len(temp)
c.s = Mid(temp, k, 1)
If c="*" Or c="[" Or c="(" Or c=")" Or c="]" Or c=" " Or c="," Or c=";" Or Asc(c)=9 Or c="!"
If k > j
tmp$ = tmp$ + ReplaceWord(Mid(temp, j, k-j)) + c
j = k+1
Else
tmp$ + c
j + 1
EndIf
EndIf
Next
;Debug temp
;Debug tmp$
out + tmp$ + ReplaceWord(Mid(temp, j, Len(temp)+1-j))
Else
out + temp
EndIf
temp = ""
EndProcedure
Procedure.s ConvertLine(line$)
out = ""
temp = ""
c.b
i.l = 1
While i <= Len(line$)
c = GetCar(line$,i)
Select c
Case 35 ; #
If etat = #NORMAL
ConvertTextC()
;<
j = FindString(line$, "/", i+1)
If j = 0
j = Len(line$)+1
Else
j - 1
EndIf
out + "<font class='diese'>"+ ReplaceString(Mid(line$, i, j-i), "<", "<") + "</font>"
i = j - 1
Else
temp + Chr(c)
EndIf
Case 34 ; "
If etat = #TEXTE1
out + temp + Chr(c) + "</font>"
temp = ""
etat = #Normal
ElseIf etat = #Normal
ConvertTextC()
out + "<font class='texte'>"+Chr(c)
etat = #Texte1
Else
temp = temp + Chr(c)
EndIf
Case 39 ; '
If etat = #TEXTE2
out + temp + "'</font>"
temp = ""
etat = #Normal
ElseIf etat = #Normal
ConvertTextC()
out + "<font class='texte'>'"
etat = #Texte2
Else
temp = temp + Chr(c)
EndIf
Case 47 ; /
If etat = #COMMENT
If GetCar(line$,i-1) = 42
out + temp + "/</font>"
temp = ""
etat = #Normal
EndIf
ElseIf etat = #Normal
If GetCar(line$,i+1) = 42
ConvertTextC()
out + "<font class='comment'>/"
etat = #Comment
ElseIf GetCar(line$,i+1) = 47
ConvertTextC()
out + "<font class='comment'>" + Mid(line$, i, Len(line$)+1-i) + "</font>"
i = Len(line$)
EndIf
Else
temp = temp + Chr(c)
EndIf
Default
temp = temp + Chr(c)
EndSelect
i + 1
Wend
ConvertTextC()
ProcedureReturn out
EndProcedure
Procedure WriteHeader()
UseFile(2)
WriteStringN("<html>")
WriteStringN("<title>"+GetFilePart(nomFichier)+"</title>")
WriteStringN("<body>")
WriteStringN("<style>")
WriteStringN("body {")
WriteStringN(" ")
WriteStringN(" color: #000000;}")
WriteStringN(".keyWord {")
WriteStringN(" font-weight: bold;")
WriteStringN(" color: #0000FF;}")
WriteStringN(".diese {")
WriteStringN(" font-weight: none;")
WriteStringN(" color: #A06000;}")
WriteStringN(".type {")
WriteStringN(" font-weigt: normal;")
WriteStringN(" color: #A000FF;}")
WriteStringN(".texte {")
WriteStringN(" font-weigth: normal;")
WriteStringN(" color: #808080;}")
WriteStringN(".comment {")
WriteStringN(" font-weigth: normal;")
WriteStringN(" color: #00A080;}")
WriteStringN(".fonction {")
WriteStringN(" font-weigth: normal;")
WriteStringN(" color: #A06060;}")
WriteStringN("</style>")
WriteStringN("")
WriteStringN("<pre>")
EndProcedure
Procedure WriteFooter()
UseFile(2)
WriteStringN("</pre>")
WriteStringN("</body>")
WriteStringN("</html>")
EndProcedure
Procedure WriteBody()
While ~Eof(1)
UseFile(1)
a$ = ReadString()
UseFile(2)
WriteStringN(ConvertLine(a$))
Wend
EndProcedure
Procedure GetFileC()
file.s = OpenFileRequester("Fichier C à ouvrir", "", "Tous les fichier C|*.c;*.h;*.cpp", 0)
If file
nomFichier = file
ProcedureReturn #True
EndIf
EndProcedure
Procedure CreateColoredFileC()
If FileSize(nomFichier)>0 And OpenFile(1, nomFichier)
outFile.s = nomFichier + ".html"
If OpenFile(2, outFile)
WriteHeader()
WriteBody()
WriteFooter()
CloseFile(1)
CloseFile(2)
ProcedureReturn #True
EndIf
EndIf
MessageRequester("Erreur", "An error as occured", 0)
End
EndProcedure
GetFileC()
CreateColoredFileC()
RunProgram(nomFichier + ".html")
DataSection
Type:
Data.s "int", "char", "double", "void", "FILE", "struct"
Data.s ""
KW:
Data.s "if", "else", "elseif", "switch", "case", "default"
Data.s "for", "while", "do", "return", "break", "sizeof", "typedef"
Data.s ""
fct:
Data.s "malloc", "free", "fscanf", "feof", "fclose", "fopen", "scanf", "printf"
Data.s "strcmp", "strcpy", "localtime", "time"
Data.s ""
EndDataSection
Lionel