fb:porticula NoPaste
Datei mit libcurl herunterladen und Zeilennummer finden
Uploader: | Sebastian |
Datum/Zeit: | 09.11.2012 14:51:24 |
#include once "curl.bi"
#include once "crt/string.bi"
declare function write_callback cdecl ( byval buffer as byte ptr, _
byval size as integer, _
byval nitems as integer, _
byval outstream as any ptr _
) as integer
declare function findLineOffsetInString ( _
Byref completeFile As String, _
ByVal searchLineNumber As Integer _
) As Integer
dim as CURL ptr curl
dim as CURLcode res
dim shared as String recvData
'' init
curl = curl_easy_init( )
if( curl = 0 ) then
print "Fehler bei curl_easy_init."
sleep
end 1
end if
'' set url and callback
curl_easy_setopt( curl, CURLOPT_URL, "http://www.freebasic-portal.de/" )
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, @write_callback )
'' execute..
res = curl_easy_perform( curl )
'' shutdown
curl_easy_cleanup( curl )
Print "Die heruntergeladenen Daten befinden sich im Stringpuffer."
Print "Es wurden " & Len(recvData) & " Bytes empfangen."
dim as integer lineNumberToLookFor = 139
dim as integer posOfLineNumberX = findLineOffsetInString ( recvData, lineNumberToLookFor )
If (posOfLineNumberX >= 0) Then
Print "Zeile " & lineNumberToLookFor & " beginnt im String an Stelle " & posOfLineNumberX & "."
Else
Print "Die gesuchte Zeile wurde nicht gefunden."
Sleep: End
End if
dim as integer endOfLine = Instr( posOfLineNumberX+1, recvData, chr(10) )
dim as string whiteSpaces = chr(8)+chr(10)+chr(13)+chr(32)
Print "Die Zeile endet an Position " & endOfLine & "."
dim as string resultLine = trim( _
mid(recvData, posOfLineNumberX+1, endOfLine-posOfLineNumberX), _
ANY !" \b\t\r\n" _
)
print "Hier die gesuchte Zeile:"
print ">>";
color 12
print resultLine;
color 7
print "<<"
print
print "Beliebige Taste zum Beenden druecken."
sleep
end
function write_callback cdecl ( byval buffer as byte ptr, _
byval size as integer, _
byval nitems as integer, _
byval outstream as any ptr _
) as integer
dim as zstring ptr zstr = 0
dim as integer bytes = size * nitems
' Passenden Puffer erzeugen
zstr = callocate( bytes + 1 )
' Daten aus cURLs Puffer in unserem Puffer zwischenspeichern
memcpy( zstr, buffer, bytes )
zstr[bytes] = 0
' Daten an empfangenen String anhaengen
recvData &= *zstr
' Pufferspeicher wieder freigeben
deallocate ( zstr )
function = bytes
end function
function findLineOffsetInString (Byref completeFile As String, ByVal searchLineNumber As Integer) As Integer
if (searchLineNumber <= 1) then return 0
dim as ubyte char
dim as integer lineCount = 1
for i as integer = 0 to Len(completeFile) - 1
char = completeFile[i]
if (char = 10) then
lineCount += 1
if (lineCount = searchLineNumber) then
if (i = (Len(completeFile) - 1)) then
return -1
else
return (i+1)
end if
end if
end if
next i
return -1
end function