Read\Write EncodedString - New Version
Posted: Fri Aug 29, 2008 2:36 pm
Hello everyone,
I have created two small procedures to Read and Write string on binary file using ASCII code and Matrices calculations. Have fun !
Regards
Guimauve
I have created two small procedures to Read and Write string on binary file using ASCII code and Matrices calculations. Have fun !
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Nom du projet : Read\Write EncodedString
; Nom du fichier : Lib_EncodedString.pb
; Version du fichier : 2.0.0
; Programmation : OK
; Programmé par : Guimauve
; Date : 07-08-2008
; Mise à jour : 15-08-2008
; Codé pour PureBasic V4.20
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Déclaration de la Structure <<<<<
Structure Vector3L
i.l
j.l
k.l
EndStructure
; <<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les mutateurs <<<<<
Macro SetVector3Li(VectorA, P_i)
VectorA\i = P_i
EndMacro
Macro SetVector3Lj(VectorA, P_j)
VectorA\j = P_j
EndMacro
Macro SetVector3Lk(VectorA, P_k)
VectorA\k = P_k
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les observateurs <<<<<
Macro GetVector3Li(VectorA)
VectorA\i
EndMacro
Macro GetVector3Lj(VectorA)
VectorA\j
EndMacro
Macro GetVector3Lk(VectorA)
VectorA\k
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Lecture fichier Binaire <<<<<
Macro ReadVector3L(FileID, VectorA)
ReadData(FileID, VectorA, SizeOf(Vector3L))
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Écriture fichier Binaire <<<<<
Macro WriteVector3L(FileID, VectorA)
WriteData(FileID, VectorA, SizeOf(Vector3L))
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Déclaration de la Structure <<<<<
Structure Matrix33L
e11.l
e12.l
e13.l
e21.l
e22.l
e23.l
e31.l
e32.l
e33.l
EndStructure
; <<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les mutateurs <<<<<
Macro SetMatrix33Le11(MatrixA, P_e11)
MatrixA\e11 = P_e11
EndMacro
Macro SetMatrix33Le12(MatrixA, P_e12)
MatrixA\e12 = P_e12
EndMacro
Macro SetMatrix33Le13(MatrixA, P_e13)
MatrixA\e13 = P_e13
EndMacro
Macro SetMatrix33Le21(MatrixA, P_e21)
MatrixA\e21 = P_e21
EndMacro
Macro SetMatrix33Le22(MatrixA, P_e22)
MatrixA\e22 = P_e22
EndMacro
Macro SetMatrix33Le23(MatrixA, P_e23)
MatrixA\e23 = P_e23
EndMacro
Macro SetMatrix33Le31(MatrixA, P_e31)
MatrixA\e31 = P_e31
EndMacro
Macro SetMatrix33Le32(MatrixA, P_e32)
MatrixA\e32 = P_e32
EndMacro
Macro SetMatrix33Le33(MatrixA, P_e33)
MatrixA\e33 = P_e33
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les observateurs <<<<<
Macro GetMatrix33Le11(MatrixA)
MatrixA\e11
EndMacro
Macro GetMatrix33Le12(MatrixA)
MatrixA\e12
EndMacro
Macro GetMatrix33Le13(MatrixA)
MatrixA\e13
EndMacro
Macro GetMatrix33Le21(MatrixA)
MatrixA\e21
EndMacro
Macro GetMatrix33Le22(MatrixA)
MatrixA\e22
EndMacro
Macro GetMatrix33Le23(MatrixA)
MatrixA\e23
EndMacro
Macro GetMatrix33Le31(MatrixA)
MatrixA\e31
EndMacro
Macro GetMatrix33Le32(MatrixA)
MatrixA\e32
EndMacro
Macro GetMatrix33Le33(MatrixA)
MatrixA\e33
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les mutateurs ligne par ligne <<<<<
Macro SetMatrix33LLine1(MatrixA, P_e11, P_e12, P_e13)
SetMatrix33Le11(MatrixA, P_e11)
SetMatrix33Le12(MatrixA, P_e12)
SetMatrix33Le13(MatrixA, P_e13)
EndMacro
Macro SetMatrix33LLine2(MatrixA, P_e21, P_e22, P_e23)
SetMatrix33Le21(MatrixA, P_e21)
SetMatrix33Le22(MatrixA, P_e22)
SetMatrix33Le23(MatrixA, P_e23)
EndMacro
Macro SetMatrix33LLine3(MatrixA, P_e31, P_e32, P_e33)
SetMatrix33Le31(MatrixA, P_e31)
SetMatrix33Le32(MatrixA, P_e32)
SetMatrix33Le33(MatrixA, P_e33)
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Product Matrix Vector : R = A * B <<<<<
Macro ProductMatrix33LVector3L(VectorR, MatrixA, VectorB)
SetVector3Li(VectorR, GetMatrix33Le11(MatrixA) * GetVector3Li(VectorB) + GetMatrix33Le12(MatrixA) * GetVector3Lj(VectorB) + GetMatrix33Le13(MatrixA) * GetVector3Lk(VectorB))
SetVector3Lj(VectorR, GetMatrix33Le21(MatrixA) * GetVector3Li(VectorB) + GetMatrix33Le22(MatrixA) * GetVector3Lj(VectorB) + GetMatrix33Le23(MatrixA) * GetVector3Lk(VectorB))
SetVector3Lk(VectorR, GetMatrix33Le31(MatrixA) * GetVector3Li(VectorB) + GetMatrix33Le32(MatrixA) * GetVector3Lj(VectorB) + GetMatrix33Le33(MatrixA) * GetVector3Lk(VectorB))
EndMacro
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< ReadEncodedString <<<<<
ProcedureDLL.s ReadEncodedString(FileID.l)
Inverse.Matrix33L
VectorCoded.Vector3L
VectorDecoded.Vector3L
MatrixID.b = ReadByte(FileID)
Select MatrixID
Case 0
SetMatrix33LLine1(Inverse, -7, -12, 14)
SetMatrix33LLine2(Inverse, 3, 5, -6)
SetMatrix33LLine3(Inverse, 31, 54, -63)
Case 1
SetMatrix33LLine1(Inverse, -24, -47, -33)
SetMatrix33LLine2(Inverse, 2, 4, 3)
SetMatrix33LLine3(Inverse, 19, 37, 26)
Case 2
SetMatrix33LLine1(Inverse, -30, -39, -22)
SetMatrix33LLine2(Inverse, 23, 30, 17)
SetMatrix33LLine3(Inverse, -19, -25, -14)
Case 3
SetMatrix33LLine1(Inverse, 1, -13, 11)
SetMatrix33LLine2(Inverse, 1, -18, 15)
SetMatrix33LLine3(Inverse, -1, 17, -14)
Case 4
SetMatrix33LLine1(Inverse, -3, -1, 2)
SetMatrix33LLine2(Inverse, -10, -3, 7)
SetMatrix33LLine3(Inverse, -42, -13, 30)
Case 5
SetMatrix33LLine1(Inverse, 3, -3, 5)
SetMatrix33LLine2(Inverse, -14, 13, -21)
SetMatrix33LLine3(Inverse, 5, -5, 8)
Case 6
SetMatrix33LLine1(Inverse, -77, -83, 25)
SetMatrix33LLine2(Inverse, -65, -70, 21)
SetMatrix33LLine3(Inverse, 40, 43, -13)
Case 7
SetMatrix33LLine1(Inverse, 35, -96, -73)
SetMatrix33LLine2(Inverse, -3, 8, 6)
SetMatrix33LLine3(Inverse, 12, -33, -25)
Case 8
SetMatrix33LLine1(Inverse, 54, 29, -63)
SetMatrix33LLine2(Inverse, -24, -13, 28)
SetMatrix33LLine3(Inverse, -13, -7, 15)
Case 9
SetMatrix33LLine1(Inverse, 5, 27, -34)
SetMatrix33LLine2(Inverse, 0, 1, -1)
SetMatrix33LLine3(Inverse, 3, 15, -19)
Case 10
SetMatrix33LLine1(Inverse, 0, 1, -1)
SetMatrix33LLine2(Inverse, 1, -54, 49)
SetMatrix33LLine3(Inverse, 0, 9, -8)
Case 11
SetMatrix33LLine1(Inverse, 3, -25, 51)
SetMatrix33LLine2(Inverse, 3, -26, 53)
SetMatrix33LLine3(Inverse, -4, 34, -69)
Case 12
SetMatrix33LLine1(Inverse, 18, -13, -2)
SetMatrix33LLine2(Inverse, 69, -50, -8)
SetMatrix33LLine3(Inverse, -113, 82, 13)
EndSelect
MaxChar.l = ReadLong(FileID)
For Index = 0 To MaxChar - 1 Step 3
ReadVector3L(FileID, VectorCoded)
ProductMatrix33LVector3L(VectorDecoded, Inverse, VectorCoded)
String.s = String + Chr(GetVector3Li(VectorDecoded)) + Chr(GetVector3Lj(VectorDecoded)) + Chr(GetVector3Lk(VectorDecoded))
Next
ProcedureReturn String
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< WriteEncodedString <<<<<
ProcedureDLL WriteEncodedString(FileID.l, String.s)
Matrix.Matrix33L
VectorCoded.Vector3L
VectorDecoded.Vector3L
MatrixID = Random(12)
Select MatrixID
Case 0
SetMatrix33LLine1(Matrix, -9, 0, -2)
SetMatrix33LLine2(Matrix, -3, -7, 0)
SetMatrix33LLine3(Matrix, -7, -6, -1)
Case 1
SetMatrix33LLine1(Matrix, 7, -1, 9)
SetMatrix33LLine2(Matrix, -5, -3, -6)
SetMatrix33LLine3(Matrix, 2, 5, 2)
Case 2
SetMatrix33LLine1(Matrix, -5, -4, 3)
SetMatrix33LLine2(Matrix, 1, -2, -4)
SetMatrix33LLine3(Matrix, 5, 9, 3)
Case 3
SetMatrix33LLine1(Matrix, 3, -5, -3)
SetMatrix33LLine2(Matrix, 1, 3, 4)
SetMatrix33LLine3(Matrix, 1, 4, 5)
Case 4
SetMatrix33LLine1(Matrix, -1, -4, 1)
SetMatrix33LLine2(Matrix, -6, 6, -1)
SetMatrix33LLine3(Matrix, -4, -3, 1)
Case 5
SetMatrix33LLine1(Matrix, -1, -1, -2)
SetMatrix33LLine2(Matrix, 7, -1, -7)
SetMatrix33LLine3(Matrix, 5, 0, -3)
Case 6
SetMatrix33LLine1(Matrix, 7, -4, 7)
SetMatrix33LLine2(Matrix, -5, 1, -8)
SetMatrix33LLine3(Matrix, 5, -9, -5)
Case 7
SetMatrix33LLine1(Matrix, 2, -9, -8)
SetMatrix33LLine2(Matrix, 3, -1, -9)
SetMatrix33LLine3(Matrix, -3, -3, 8)
Case 8
SetMatrix33LLine1(Matrix, 1, 6, -7)
SetMatrix33LLine2(Matrix, -4, -9, 0)
SetMatrix33LLine3(Matrix, -1, 1, -6)
Case 9
SetMatrix33LLine1(Matrix, -4, 3, 7)
SetMatrix33LLine2(Matrix, -3, 7, 5)
SetMatrix33LLine3(Matrix, -3, 6, 5)
Case 10
SetMatrix33LLine1(Matrix, 9, 1, 5)
SetMatrix33LLine2(Matrix, -8, 0, 1)
SetMatrix33LLine3(Matrix, -9, 0, 1)
Case 11
SetMatrix33LLine1(Matrix, 8, -9, -1)
SetMatrix33LLine2(Matrix, 5, 3, 6)
SetMatrix33LLine3(Matrix, 2, 2, 3)
Case 12
SetMatrix33LLine1(Matrix, 6, 5, 4)
SetMatrix33LLine2(Matrix, 7, 8, 6)
SetMatrix33LLine3(Matrix, 8, -7, -3)
EndSelect
WriteByte(FileID, MatrixID)
MaxChar.l = Len(String)
WriteLong(FileID, MaxChar)
For Index = 0 To MaxChar - 1 Step 3
SetVector3Li(VectorDecoded, Asc(Mid(String, Index + 1, 1)))
SetVector3Lj(VectorDecoded, Asc(Mid(String, Index + 2, 1)))
SetVector3Lk(VectorDecoded, Asc(Mid(String, Index + 3, 1)))
ProductMatrix33LVector3L(VectorCoded, Matrix, VectorDecoded)
WriteVector3L(FileID, VectorCoded)
Next
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< CODE D'ESSAI <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<
Dim Texte.s(10)
Texte(0) = "Allô le monde !"
Texte(1) = "Comment ça va ?"
Texte(2) = "Ca va super bien, et vous ?"
Texte(3) = "Ça va pas pire du tout."
Texte(4) = "Test d'une nouvelle version de EncodedString."
Texte(5) = "PureBasic it's the best of the best."
Texte(6) = "Jeudi 7 août 2008"
Texte(7) = "Atlantis"
Texte(8) = "C'est les vacances !"
Texte(9) = "J'aime les belles filles, spécialement les déesses nordiques !"
Texte(10) = "C'est la fin des vacances, noooonnnnn !"
If CreateFile(0, "TestEncodedString.dat")
For Index = 0 To 10
WriteEncodedString(0, Texte(Index))
Next
CloseFile(0)
EndIf
If ReadFile(1, "TestEncodedString.dat")
For Index = 0 To 10
Debug ReadEncodedString(1)
Next
CloseFile(1)
EndIf
; <<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< FIN DU FICHIER <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<
Guimauve