Linux GCHAR() and GUNICHAR() like UTF8() and ASCII(...)

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Linux GCHAR() and GUNICHAR() like UTF8() and ASCII(...)

Post by mk-soft »

Sometimes you want to have a *gchar as return value for a Linux libray function. For this, however, a memory of type g_char must be created.

Update v1.01.2
- Added UCS4

Code: Select all

;-TOP by mk-soft, v1.01.2, 19.04.2023

; Free result with g_free_(...)

Procedure GCHAR(String$)
  Protected *gchar, items_read, items_written, *ppError.gerror
  *gchar = g_utf16_to_utf8_(String$, -1, @items_read, @items_written, @*ppError)
  If *ppError
    Debug "GCHAR Error: " + PeekS(*ppError\message, -1, #PB_UTF8)
    g_error_free_(*ppError)
  EndIf
  ProcedureReturn *gchar
EndProcedure

Procedure.s StringFromUCS4(*gunichar)
  Protected r1.s, *gunichar2, items_read, items_written, *ppError.gerror
  *gunichar2 = g_ucs4_to_utf16_(*gunichar, -1, @items_read, @items_written, @*ppError)
  If *ppError
    Debug "GUNICHAR Error: " + PeekS(*ppError\message, -1, #PB_UTF8)
    g_error_free_(*ppError)
  EndIf
  If *gunichar2
    r1 = PeekS(*gunichar2, -1, #PB_Unicode)
    g_free_(*gunichar2)
  EndIf
  ProcedureReturn r1
EndProcedure

Procedure StringToUCS4(String$)
  Protected *gunichar, items_read, items_written, *ppError.gerror
  *gunichar = g_utf16_to_ucs4_(String$, -1, @items_read, @items_written, @*ppError)
  If *ppError
    Debug "GUNICHAR Error: " + PeekS(*ppError\message, -1, #PB_UTF8)
    g_error_free_(*ppError)
  EndIf
  ProcedureReturn *gunichar
EndProcedure

; ****

CompilerIf #PB_Compiler_IsMainFile
  
  Structure ArrayOfLong
    l.l[0]
  EndStructure
  
  Structure ArrayOfUnicode
    u.u[0]
  EndStructure
  
  Structure ArrayOfByte
    StructureUnion
      b.b[0]
      a.a[0]
    EndStructureUnion
  EndStructure
  
  Debug "gunichar to string"
  Dim unichar.l(4)
  unichar(0) = Asc("{")
  unichar(1) = $0001f923
  unichar(2) = Asc("}")
  
  s1.s = StringFromUCS4(@unichar())
  Debug s1
  *unicode.ArrayOfUnicode = @s1
  If *unicode
    i = 0
    While *unicode\u[i]
      Debug "Char " + i + ": " + RSet(Hex(*unicode\u[i], #PB_Unicode), 4, "0")
      i + 1
    Wend
  EndIf
  Debug "----"
  
  Debug "gunichar from string" 
  *gunichar.ArrayOfLong = StringToUCS4(s1)
  If *gunichar
    i = 0
    While *gunichar\l[i]
      Debug "Char " + i + ": " + RSet(Hex(*gunichar\l[i], #PB_Long), 8, "0")
      i + 1
    Wend
    g_free_(*gunichar)
  EndIf
  Debug "----"
  
  Debug "gchar from string"
  *gchar.ArrayOfByte = GCHAR(s1)
  If *gchar
    Debug PeekS(*gchar, -1, #PB_UTF8)
    i = 0
    While *gchar\a[i]
      Debug "Byte " + i + ": " + RSet(Hex(*gchar\a[i], #PB_Ascii), 2, "0")
      i + 1
    Wend
    g_free_(*gchar)
  EndIf
  Debug "----"
  
CompilerEndIf
Last edited by mk-soft on Wed Apr 19, 2023 5:14 pm, edited 6 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: GCHAR(...) like Unicode() and ASCII(...)

Post by mk-soft »

Library example

Code: Select all

;- Example

#TESTDLL = 1

CompilerIf #TESTDLL = 0
  
  Procedure GCHAR(String$)
    Protected *gchar, items_read, items_written, *ppError.gerror
    *gchar = g_utf16_to_utf8_(String$, -1, @items_read, @items_written, @*ppError)
    If *ppError
      Debug "GCHAR Error: " + PeekS(*ppError\message, -1, #PB_UTF8)
      g_error_free_(*ppError)
    EndIf
    ProcedureReturn *gchar
  EndProcedure
  
  ProcedureCDLL MyFunction(*string)
    Protected *gchar, Report.s
    
    Report = "Report: " + PeekS(*string, -1, #PB_UTF8)
    *gchar = GCHAR(Report)
    ProcedureReturn *gchar
    
  EndProcedure

CompilerElse
  
  PrototypeC protoMyFunction(String.p-utf8)
  
  If OpenLibrary(0, "PureBasic.so")
    Define MyFunction.protoMyFunction = GetFunction(0, "MyFunction")
    *gchar = MyFunction("I like PureBasic!")
    Response.s = PeekS(*gchar, -1, #PB_UTF8)
    MessageRequester("Info", Response)
    g_free_(*gchar)
  EndIf
  
CompilerEndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: GCHAR(...) like Unicode() and ASCII(...)

Post by netmaestro »

Can't find structure gerror
BERESHEIT
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: GCHAR(...) like Unicode() and ASCII(...)

Post by mk-soft »

Its only for Linux ...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Linux GCHAR(...) like Unicode() and ASCII(...)

Post by mk-soft »

Update v1.01.1
- Added UCS4
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply