Differenze tra le versioni di "Sovrascrivere una stringa con uno o più caratteri mediante le funzioni esterne del API di Libglib-2.0"

Da Gambas-it.org - Wikipedia.
Riga 5: Riga 5:
  
 
Mostriamo un esempio, nel quale una stringa preesistente verrà sovrascritta con la parola "GAMBAS" a partire dal 5° carattere (num. indice 4) della stringa medesima:
 
Mostriamo un esempio, nel quale una stringa preesistente verrà sovrascritta con la parola "GAMBAS" a partire dal 5° carattere (num. indice 4) della stringa medesima:
 +
Public Struct GString
 +
  gstr As Pointer
 +
  len As Long
 +
  allocated_len As Long
 +
End Struct
 +
 
  Library "libglib-2.0"
 
  Library "libglib-2.0"
 
   
 
   
 
  <FONT Color=gray>' ''GString * g_string_new (const gchar *init)''
 
  <FONT Color=gray>' ''GString * g_string_new (const gchar *init)''
 
  ' ''Creates a new GString, initialized with the given string.''</font>
 
  ' ''Creates a new GString, initialized with the given string.''</font>
  Private Extern g_string_new(init As String) As Pointer
+
  Private Extern g_string_new(init As String) As GString
 
   
 
   
 
  <FONT Color=gray>' ''GString * g_string_overwrite (GString *string, gsize pos, const gchar *val)''
 
  <FONT Color=gray>' ''GString * g_string_overwrite (GString *string, gsize pos, const gchar *val)''
 
  ' ''Overwrites part of a string, lengthening it if necessary.''</font>
 
  ' ''Overwrites part of a string, lengthening it if necessary.''</font>
  Private Extern g_string_overwrite(GString As Pointer, gpos As Integer, gval As String) As Pointer
+
  Private Extern g_string_overwrite(gstr As GString, gpos As Integer, gval As String) As GString
 
   
 
   
 
  <FONT Color=gray>' ''gchar * g_string_free (GString *string, gboolean free_segment)''
 
  <FONT Color=gray>' ''gchar * g_string_free (GString *string, gboolean free_segment)''
 
  ' ''Frees the memory allocated for the GString.''</font>
 
  ' ''Frees the memory allocated for the GString.''</font>
  Private Extern g_string_free(GString As Pointer, free_segment As Boolean) As String
+
  Private Extern g_string_free(gstr As GString, free_segment As Boolean) As String
 
   
 
   
 
   
 
   
Riga 23: Riga 29:
 
   
 
   
 
   Dim s As String
 
   Dim s As String
   Dim p1, p2 As Pointer
+
   Dim gs As New GString
 
    
 
    
 
   s = "abcde fghilm nopqrstuvz"
 
   s = "abcde fghilm nopqrstuvz"
 
    
 
    
   p1 = g_string_new(s)
+
   gs = g_string_new(s)
 
        
 
        
   p2 = g_string_overwrite(p1, 4, "GAMBAS")
+
   gs = g_string_overwrite(gs, 4, "GAMBAS")
 
      
 
      
   Print String@(Pointer@(p2))
+
   Print String@(Pointer@(gs.gstr))
 
      
 
      
  <FONT Color=gray>' ''Va in chiusura liberando memoria occupata precedentemente dai due "Puntatori" utilizzati:''</font>
+
  <FONT Color=gray>' ''Va in chiusura liberando memoria occupata dalla Struttura "GString":''</font>
   g_string_free(p2, True)
+
   g_string_free(gs, True)
  g_string_free(p1, True)
 
 
    
 
    
 
  '''End'''
 
  '''End'''
Riga 44: Riga 49:
  
 
Mostriamo un esempio, nel quale una stringa verrà sovrascritta a partire dalla posizione di indice 6 con i soli primi tre caratteri della stringa "GAMBAS":
 
Mostriamo un esempio, nel quale una stringa verrà sovrascritta a partire dalla posizione di indice 6 con i soli primi tre caratteri della stringa "GAMBAS":
 +
Public Struct GString
 +
  gstr As Pointer
 +
  len As Long
 +
  allocated_len As Long
 +
End Struct
 +
 
  Library "libglib-2.0"
 
  Library "libglib-2.0"
 
   
 
   
 
  <FONT Color=gray>' ''GString * g_string_new (const gchar *init)''
 
  <FONT Color=gray>' ''GString * g_string_new (const gchar *init)''
 
  ' ''Creates a new GString, initialized with the given string.''</font>
 
  ' ''Creates a new GString, initialized with the given string.''</font>
  Private Extern g_string_new(init As String) As Pointer
+
  Private Extern g_string_new(init As String) As GString
 
   
 
   
 
  <FONT Color=gray>' ''GString * g_string_overwrite_len (GString *string, gsize pos, const gchar *val, len As Long)''
 
  <FONT Color=gray>' ''GString * g_string_overwrite_len (GString *string, gsize pos, const gchar *val, len As Long)''
 
  ' ''Overwrites part of a string, lengthening it if necessary.''</font>
 
  ' ''Overwrites part of a string, lengthening it if necessary.''</font>
  Private Extern g_string_overwrite(GString As Pointer, gpos As Integer, gval As String) As Pointer
+
  Private Extern g_string_overwrite(gstr As GString, gpos As Integer, gval As String) As GString
 
   
 
   
 
  <FONT Color=gray>' ''gchar * g_string_free (GString *string, gboolean free_segment)''
 
  <FONT Color=gray>' ''gchar * g_string_free (GString *string, gboolean free_segment)''
 
  ' ''Frees the memory allocated for the GString.''</font>
 
  ' ''Frees the memory allocated for the GString.''</font>
  Private Extern g_string_free(GString As Pointer, free_segment As Boolean) As String
+
  Private Extern g_string_free(gstr As GString, free_segment As Boolean) As String
 
   
 
   
 
   
 
   
Riga 62: Riga 73:
 
   
 
   
 
   Dim s As String
 
   Dim s As String
   Dim p1, p2 As Pointer
+
   Dim gs As New GString
 
    
 
    
 
   s = "abcde fghilm nopqrstuvz"
 
   s = "abcde fghilm nopqrstuvz"
 
    
 
    
   p1 = g_string_new(s)
+
   gs = g_string_new(s)
 
        
 
        
   p2 = g_string_overwrite_len(p1, 6, "GAMBAS", 3)
+
   gs = g_string_overwrite_len(gs, 6, "GAMBAS", 3)
 
      
 
      
   Print String@(Pointer@(p2))
+
   Print String@(Pointer@(gs.gstr))
 
      
 
      
  <FONT Color=gray>' ''Va in chiusura liberando memoria occupata precedentemente dai due "Puntatori" utilizzati:''</font>
+
  <FONT Color=gray>' ''Va in chiusura liberando memoria occupata dalla Struttura "GString":''</font>
   g_string_free(p2, True)
+
   g_string_free(gs, True)
  g_string_free(p1, True)
 
 
    
 
    
 
  '''End'''
 
  '''End'''

Versione delle 06:49, 28 ott 2015

E' possibile sovrascrivere una parte ben determinata di una stringa con uno o più caratteri, a cominciare da una specificata posizione, utilizzando alcune funzioni della libreria esterna libglib-2.0 .

Sarà necessario aver installato nel sistema e richiamare nell'applicazione Gambas la libreria: "libglib-2.0.so"


Mostriamo un esempio, nel quale una stringa preesistente verrà sovrascritta con la parola "GAMBAS" a partire dal 5° carattere (num. indice 4) della stringa medesima:

Public Struct GString
  gstr As Pointer
  len As Long
  allocated_len As Long
End Struct

Library "libglib-2.0"

' GString * g_string_new (const gchar *init)
' Creates a new GString, initialized with the given string.
Private Extern g_string_new(init As String) As GString

' GString * g_string_overwrite (GString *string, gsize pos, const gchar *val)
' Overwrites part of a string, lengthening it if necessary.
Private Extern g_string_overwrite(gstr As GString, gpos As Integer, gval As String) As GString

' gchar * g_string_free (GString *string, gboolean free_segment)
' Frees the memory allocated for the GString.
Private Extern g_string_free(gstr As GString, free_segment As Boolean) As String


Public Sub Main()

 Dim s As String
 Dim gs As New GString
 
  s = "abcde fghilm nopqrstuvz"
 
  gs = g_string_new(s)
      
  gs = g_string_overwrite(gs, 4, "GAMBAS")
   
  Print String@(Pointer@(gs.gstr))
   
' Va in chiusura liberando memoria occupata dalla Struttura "GString":
  g_string_free(gs, True)
 
End


Sovrascrivere una stringa con una determinata quantità di caratteri di un'altra stringa

E' anche possibile sovrascrivere una stringa originaria con un numero determinato di caratteri, iniziando da sinistra, facendit parte di un'altra stringa.

Mostriamo un esempio, nel quale una stringa verrà sovrascritta a partire dalla posizione di indice 6 con i soli primi tre caratteri della stringa "GAMBAS":

Public Struct GString
  gstr As Pointer
  len As Long
  allocated_len As Long
End Struct

Library "libglib-2.0"

' GString * g_string_new (const gchar *init)
' Creates a new GString, initialized with the given string.
Private Extern g_string_new(init As String) As GString

' GString * g_string_overwrite_len (GString *string, gsize pos, const gchar *val, len As Long)
' Overwrites part of a string, lengthening it if necessary.
Private Extern g_string_overwrite(gstr As GString, gpos As Integer, gval As String) As GString

' gchar * g_string_free (GString *string, gboolean free_segment)
' Frees the memory allocated for the GString.
Private Extern g_string_free(gstr As GString, free_segment As Boolean) As String


Public Sub Main()

 Dim s As String
 Dim gs As New GString
 
  s = "abcde fghilm nopqrstuvz"
 
  gs = g_string_new(s)
      
  gs = g_string_overwrite_len(gs, 6, "GAMBAS", 3)
   
  Print String@(Pointer@(gs.gstr))
   
' Va in chiusura liberando memoria occupata dalla Struttura "GString":
  g_string_free(gs, True)
 
End