[Gambas-user] Using External Libraries
Randall Morgan
rmorgan62 at ...626...
Sun Nov 20 01:57:51 CET 2011
Hi All,
First, I have read, reread, and re-re-re-read the page at:
http://gambasdoc.org/help/howto/extern?v3 and I still seem to be missing
something...
I am trying to use TidyLib in Gambas 3. I have been able to create
documents and buffers but seem to be unable to use the buffers I create
with Tidy.
Tidy uses the C functions alloc and realloc internally for memory
allocation. It stores information in structures of various types and
returns a pointer
to the structures it creates (often these structures are not much more than
grouped pointers to other objects).
In theory, I should be able to accept a pointer to a structure and then
pass that pointer to other tidy functions just as i would in C.
My file is quit long so I am only posting the relevant code here.
If I define a pointer to a buffer and then pass the pointer to
tidyInitBuf(myptr) I get a success return code and printing the pointer
value
reveals the pointer is no longer null. However, any attempt to pass the
pointer back to one of tidy's other functions results in a SEG Fault
error when the buffer is used. What am I missing here?
'======================================
' Gambas class file
'' @Class: HTMLTidy
'' @File: HTMLTidy.class
'' @Ver: 0.1.0
'' @Date:
'' @ Desc: A wrapper for HtmlTidy's libtidy c library
''
'' @Note: Not yet ready for prime time...
'=====================================
Export
Library "libtidy" ' Declare external library
' --- Tidy Types Defines ---
' Some of these Defs are just for reference and may not be used
' in the application
' -- Tidy Buffers
Public Struct TidyBufferType
allocator As Pointer ' Memory allocator
bp As Pointer ' Pointer To bytes
size As Integer ' #bytes currently In use
allocated As Integer ' #bytes allocated
tNext As Integer ' Offset Of current Input position
End Struct
' -- Tidy Nodes
Public Struct NodeType
nparent As Pointer ' tree structure
nprev As Pointer
nnext As Pointer
ncontent As Pointer
nlast As Pointer
attributes As Pointer ' pointer to attribute value structure
nwas As Pointer ' Old tag before it was changed
ntag As Pointer ' tag's dictionary definition
element As Pointer ' name (NULL for text nodes)
nstart As Integer ' start of span into text array
nend As Integer ' end of span onto text array
ntype As Integer ' TextNode, StartTag, EndTag, etc...
nline As Integer ' Current line of document
ncolumn As Integer ' current column of document
nclosed As Boolean ' true if closed by explicit end tag
nimplicit As Boolean ' true if inferred
nlinebreak As Boolean ' true if followed by a line break
otext As String ' Used on with TIDY_STORE_ORIGINAL_TEXT
End Struct
' --- Tidy Memory and Structure Allocation methods ---
' TidyDoc tidyCreate(void)
Extern tidyCreate() As Pointer
' Initialize Buffer
Extern tidyBufInit(buf As Pointer)
' Free Ouptut and error buffers
Extern tidyBufFree(pOutput As Pointer)
' Capture diagnostics in error buffer
' tidySetErrorBuffer( tdoc, &errbuf ); Capture diagnostics
Extern tidySetErrorBuffer(tdoc As Pointer, errbuf As Pointer) As Integer
'-- Tidy Set Option methods ---
' Set Boolean Option value
' tidyOptSetBool( tdoc, TidyXhtmlOut, yes ); // Convert to XHTML
Extern tidyOptSetBool(tdoc As Pointer, Option As Pointer, value As Boolean)
As Boolean
'--- Tidy Document methods ---
' Parse the Input
' tidyParseString( tdoc, input ); Parse the input
Extern tidyParseString(tdoc As Pointer, sInput As Pointer) As Integer
' Tidy it up!
Extern tidyCleanAndRepair(tdoc As Pointer) As Integer
' Get Error Codes, if any
Extern tidyRunDiagnostics(tdoc As Pointer) As Integer
' Pretty Print
Extern tidySaveBuffer(tdoc As Pointer, pOutput As Pointer) As Integer
Public pTidyDoc As Pointer
Public pErrBuf As Pointer
pTidyDoc = tidyCreate()
pErrBuff = tidyInitBuf(VarPtr(pErrBuf)) ' tidy should be allocating memory
and returning a pointer to a buffer structure. In fact tidy does return a
success code.
' However the following line results in a Severe Error code being returned
by tidy.
tidySetErrorBuf(pTidyDoc, pErrBuff)
'After this call all further calls to any tidy function that makes use of
the error buffer results in a segment fault.
' If I allow tidy to create and manage it's own error buffer( which it does
using the same functions I called manually, then everything runs smoothly.
' So what have I missed in the use of the pointer?
Lastly, in order to read data from a structure that I have obtained a
pointer to from a tidy function, do I need to use READ?
For example, the tidyGetRoot() method returns a pointer to a TidyNode
structure that tidy has created in memory. How can I best access the
various
elements in the node structure and make them avaliable as a Gambas 3
variable. Perhaps members of a Gambas class or structure.
Sorry for the lengthy post and thanks to anyone who can provide a little
insight for me...
--
If you ask me if it can be done. The answer is YES, it can always be done.
The correct questions however are... What will it cost, and how long will
it take?
More information about the User
mailing list