Differenze tra le versioni di "Ottenere un file RTF da un file UTF8 e viceversa con le funzioni esterne del API di Librtfcomp"

Da Gambas-it.org - Wikipedia.
(Creata pagina con 'La libreria '''Librtfcomp''', scritta da J. A. Gow, consente di convertire in modo semplice un file ''UTF8'' in un file ''RTF'' (''Rich Text Format'') e viceversa. Per poter ...')
 
 
Riga 26: Riga 26:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
   
 
   
  Dim buffEx As Pointer
+
  Dim buffEx As Pointer
  Dim opt As New RTFOPTS
+
  Dim opt As New RTFOPTS
  Dim FileEn As String
+
  Dim FileEn As String
  Dim outlen, ris As Integer
+
  Dim outlen, ris As Integer
  Dim buffEn, bb As Byte[]
+
  Dim buffEn, bb As Byte[]
  Dim flEn As File
+
  Dim flEn As File
  Dim stEx As Stream
+
  Dim stEx As Stream
 
    
 
    
   FileEn = "''/percorso/del/file/di/testo/UTF8''"
+
   FileEn = "<FONT Color=darkgreen>''/percorso/del/file/di/testo/UTF8''</font>"
 
      
 
      
 
   With opt
 
   With opt
Riga 59: Riga 59:
 
     bb.Read(stEx)
 
     bb.Read(stEx)
 
   
 
   
     File.Save("''/percorso/del/file.rtf''", bb.ToString(0, bb.Count))
+
     File.Save("<FONT Color=darkgreen>''/percorso/del/file.rtf''</font>", bb.ToString(0, bb.Count))
 
   Else
 
   Else
 
     Error.Raise("Byte da convertire assenti !")
 
     Error.Raise("Byte da convertire assenti !")
Riga 68: Riga 68:
 
   flEn.Close
 
   flEn.Close
 
   
 
   
  '''End'''
+
  End
  
  
Riga 86: Riga 86:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
   
 
   
  Dim buffEx As Pointer
+
  Dim buffEx As Pointer
  Dim opt As New RTFOPTS
+
  Dim opt As New RTFOPTS
  Dim FileEn As String
+
  Dim FileEn As String
  Dim outlen, ris As Integer
+
  Dim outlen, ris As Integer
  Dim buffEn, bb As Byte[]
+
  Dim buffEn, bb As Byte[]
  Dim flEn As File
+
  Dim flEn As File
  Dim stEx As Stream
+
  Dim stEx As Stream
     
+
     
   FileEn = "''/percorso/del/file.rtf''"
+
   FileEn = "<FONT Color=darkgreen>''/percorso/del/file.rtf''</font>"
 
      
 
      
 
   With opt
 
   With opt
Riga 119: Riga 119:
 
     bb.Read(stEx)
 
     bb.Read(stEx)
 
      
 
      
     File.Save("''/percorso/del/file/UTF8''", bb.ToString(0, bb.Count))
+
     File.Save("<FONT Color=darkgreen>''/percorso/del/file/UTF8''</font>", bb.ToString(0, bb.Count))
 
   Else
 
   Else
 
     Error.Raise("Byte da convertire assenti !")
 
     Error.Raise("Byte da convertire assenti !")
Riga 128: Riga 128:
 
   flEn.Close
 
   flEn.Close
 
   
 
   
  '''End'''
+
  End

Versione attuale delle 16:31, 29 giu 2024

La libreria Librtfcomp, scritta da J. A. Gow, consente di convertire in modo semplice un file UTF8 in un file RTF (Rich Text Format) e viceversa.

Per poter fruire in Gambas delle risorse fornite da questa libreria, bisogna installare e richiamare la libreria dinamica condivisa: "librtfcomp.so.0.0.0"


Convertire un file UTF8 in un file RTF

Per convertire un file UTF8 in un file RTF, potremo utilizzare questo codice:

Private HEADER As String = "\\ansi \\deff0{\\fonttbl{\\f0\\fnil\\fcharset0\\fprq0 Tahoma;}{\\f1\\froman\\fcharset2\\fprq2" &
                           "Symbol;}{\\f2\\fswiss\\fcharset204\\fprq2;}}{\\colortbl;\\red0\\green0\\blue0;\\red128\\green128" &
                           "\\blue128;\\red192\\green192\\blue192;\\red255\\green255\\blue255;\\red255\\green0\\blue0;\\red0" &
                           "\\green255\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red255\\green0\\blue255;" &
                           "\\red255\\green255\\blue0;\\red128\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0" &
                           "\\blue128;\\red0\\green128\\blue128;\\red128\\green0\\blue128;\\red128\\green128\\blue0;}\x0a"

Library "librtfcomp:0.0.0"

Public Struct RTFOPTS
  lenOpts As Integer
  isCompressed As Integer
End Struct

' int LZRTFConvertUTF8ToRTF(unsigned char ** rtfout, unsigned int * lenOut, unsigned char * utfin, unsigned int len, unsigned char * rtfhdr, unsigned int hdrlen, RTFOPTS * options)
' Convert an UTF8-encoded string to a RTF encoded one.
Private Extern LZRTFConvertUTF8ToRTF(utfout As Pointer, lenout As Pointer, utfin As Byte[], lenI As Integer, rtfhdr As String, hdrlen As Integer, options As RTFOPTS) As Integer


Public Sub Main()

  Dim buffEx As Pointer
  Dim opt As New RTFOPTS
  Dim FileEn As String
  Dim outlen, ris As Integer
  Dim buffEn, bb As Byte[]
  Dim flEn As File
  Dim stEx As Stream
 
  FileEn = "/percorso/del/file/di/testo/UTF8"
   
  With opt
    .lenOpts = Object.SizeOf(RTFOPTS)
    .isCompressed = 0
  End With
   
  flEn = Open FileEn For Read
  buffEn = New Byte[Stat(FileEn).Size]
  buffEn.Read(flEn, 0, Stat(FileEn).Size)
 
  If buffEn.Count > 0 Then
    Print "Vi sono "; buffEn.Count; " byte in entrata da convertire in RTF"
 
    ris = LZRTFConvertUTF8ToRTF(VarPtr(buffEx), VarPtr(outlen), buffEn, buffEn.Count, HEADER, Len(HEADER), opt)
    If ris <> 0 Then Error.Raise("Impossibile effettuare la conversione !")

    Print "Conversione effettuata, byte in uscita: "; outlen

    bb = New Byte[outlen]
    stEx = Memory buffEx For Read Write
    bb.Read(stEx)

    File.Save("/percorso/del/file.rtf", bb.ToString(0, bb.Count))
  Else
    Error.Raise("Byte da convertire assenti !")
  Endif

' Va in chiusura:
  stEx.Close
  flEn.Close

End


Convertire un file RTF in un file UTF8

Per convertire un file RTF in un file UTF8, potremo utilizzare questo codice:

Library "librtfcomp:0.0.0"

Public Struct RTFOPTS
  lenOpts As Integer
  isCompressed As Integer
End Struct

' int LZRTFConvertRTFToUTF8(unsigned char ** utfout, unsigned int * utflen, unsigned char * rtfin, unsigned int rtflen, RTFOPTS * options)
' Convert an RTF-encoded string to a UTF-8 encoded one.
Private Extern LZRTFConvertRTFToUTF8(utfout As Pointer, utflen As Pointer, rtfin As Byte[], rtflen As Integer, options As RTFOPTS) As Integer


Public Sub Main()

  Dim buffEx As Pointer
  Dim opt As New RTFOPTS
  Dim FileEn As String
  Dim outlen, ris As Integer
  Dim buffEn, bb As Byte[]
  Dim flEn As File
  Dim stEx As Stream
     
  FileEn = "/percorso/del/file.rtf"
   
  With opt
    .lenOpts = Object.SizeOf(RTFOPTS)
    .isCompressed = 0
  End With
  
  flEn = Open FileEn For Read
  buffEn = New Byte[Stat(FileEn).Size]
  buffEn.Read(flEn, 0, Stat(FileEn).Size)
  
  If buffEn.Count > 0 Then
    Print "Vi sono "; buffEn.Count; " byte in entrata da convertire in UTF8"
     
    ris = LZRTFConvertRTFToUTF8(VarPtr(buffEx), VarPtr(outlen), buffEn, buffEn.Count, opt)
    If ris <> 0 Then Error.Raise("Impossibile effettuare la conversione !")
     
    Print "Conversion effettuata, byte in uscita: "; outlen
     
    bb = New Byte[outlen]
    stEx = Memory buffEx For Read Write
    bb.Read(stEx)
    
    File.Save("/percorso/del/file/UTF8", bb.ToString(0, bb.Count))
  Else
    Error.Raise("Byte da convertire assenti !")
  Endif

' Va in chiusura:
  stEx.Close
  flEn.Close

End