Code cleaning

This commit is contained in:
djes
2016-08-02 14:41:11 +02:00
parent 767472053d
commit 1806be17b0
2 changed files with 19 additions and 130 deletions

View File

@@ -1060,13 +1060,15 @@ Procedure.s str2curl(string.s)
ProcedureReturn ""
EndIf
EndProcedure
; IDE Options = PureBasic 5.40 LTS Beta 5 (Windows - x86)
; EnableUnicode
; EnableXP
; EnableBuildCount = 0
; IDE Options = PureBasic 5.42 LTS (Windows - x86)
; CursorPosition = 1048
; FirstLine = 1008
; CursorPosition = 1031
; FirstLine = 986
; Folding = --
; EnableUnicode
; EnableXP

143
osm.pb
View File

@@ -125,7 +125,6 @@ Module OSM
Moving.i
Dirty.i ;To signal that drawing need a refresh
;CurlMutex.i ;CurlMutex.i ;seems that I can't thread curl ! :(((((
MainDrawingThread.i
List TilesThreads.TileThread()
@@ -143,107 +142,7 @@ Module OSM
;- *** CURL specific ***
; Global *ReceiveHTTPToMemoryBuffer, ReceiveHTTPToMemoryBufferPtr.i, ReceivedData.s
IncludeFile "libcurl.pbi" ; https://github.com/deseven/pbsamples/tree/master/crossplatform/libcurl
;
; ProcedureC ReceiveHTTPWriteToMemoryFunction(*ptr, Size.i, NMemB.i, *Stream)
;
; Protected SizeProper.i = Size & 255
; Protected NMemBProper.i = NMemB
;
; If *ReceiveHTTPToMemoryBuffer = 0
; *ReceiveHTTPToMemoryBuffer = AllocateMemory(SizeProper * NMemBProper)
; If *ReceiveHTTPToMemoryBuffer = 0
; Debug "Problem allocating memory"
; End
; EndIf
; Else
; *ReceiveHTTPToMemoryBuffer = ReAllocateMemory(*ReceiveHTTPToMemoryBuffer, MemorySize(*ReceiveHTTPToMemoryBuffer) + SizeProper * NMemBProper)
; If *ReceiveHTTPToMemoryBuffer = 0
; Debug "Problem reallocating memory"
; End
; EndIf
; EndIf
;
; CopyMemory(*ptr, *ReceiveHTTPToMemoryBuffer + ReceiveHTTPToMemoryBufferPtr, SizeProper * NMemBProper)
; ReceiveHTTPToMemoryBufferPtr + SizeProper * NMemBProper
;
; ProcedureReturn SizeProper * NMemBProper
;
; EndProcedure
;
; Procedure.i CurlReceiveHTTPToMemory(URL$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="")
;
; Protected *Buffer, curl.i, Timeout.i, res.i
;
; ;Debug "ReceiveHTTPToMemory" + URL$ + ProxyURL$ + ProxyPort$ + ProxyUser$ + ProxyPassword$
;
; If Len(URL$)
;
; curl = curl_easy_init()
;
; If curl
;
; Timeout = 3
;
; curl_easy_setopt(curl, #CURLOPT_URL, str2curl(URL$))
; curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
; curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
; curl_easy_setopt(curl, #CURLOPT_HEADER, 0)
; curl_easy_setopt(curl, #CURLOPT_FOLLOWLOCATION, 1)
; curl_easy_setopt(curl, #CURLOPT_TIMEOUT, Timeout)
;
; If Len(ProxyURL$)
; ;curl_easy_setopt(curl, #CURLOPT_HTTPPROXYTUNNEL, #True)
; If Len(ProxyPort$)
; ProxyURL$ + ":" + ProxyPort$
; EndIf
; ; Debug ProxyURL$
; curl_easy_setopt(curl, #CURLOPT_PROXY, str2curl(ProxyURL$))
; If Len(ProxyUser$)
; If Len(ProxyPassword$)
; ProxyUser$ + ":" + ProxyPassword$
; EndIf
; ;Debug ProxyUser$
; curl_easy_setopt(curl, #CURLOPT_PROXYUSERPWD, str2curl(ProxyUser$))
; EndIf
; EndIf
;
; ;TODO improve with https://github.com/Progi1984/RWrappers/tree/master/LibCurl
; curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToMemoryFunction())
; LockMutex(OSM\CurlMutex)
; res = curl_easy_perform(curl)
; UnlockMutex(OSM\CurlMutex)
;
; If res = #CURLE_OK
;
; *Buffer = AllocateMemory(ReceiveHTTPToMemoryBufferPtr)
; If *Buffer
; CopyMemory(*ReceiveHTTPToMemoryBuffer, *Buffer, ReceiveHTTPToMemoryBufferPtr)
; FreeMemory(*ReceiveHTTPToMemoryBuffer)
; *ReceiveHTTPToMemoryBuffer = #Null
; ReceiveHTTPToMemoryBufferPtr = 0
; Else
; ; Debug "Problem allocating buffer"
; EndIf
; ;curl_easy_cleanup(curl) ;Was its original place but moved below as it seems more logical to me.
; Else
; ; Debug "CURL NOT OK"
; EndIf
;
; curl_easy_cleanup(curl)
;
; Else
; ; Debug "Can't Init CURL"
; EndIf
;
; EndIf
;
; ; Debug "Curl Buffer : " + Str(*Buffer)
;
; ProcedureReturn *Buffer
;
; EndProcedure
ProcedureC ReceiveHTTPWriteToFileFunction(*ptr, Size.i, NMemB.i, FileHandle.i)
@@ -256,8 +155,8 @@ Module OSM
Protected *Buffer, curl.i, Timeout.i, res.i
Protected FileHandle.i
Debug "ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$
Debug " to file : " + DestFileName$
;Debug "ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$
;Debug " to file : " + DestFileName$
FileHandle = CreateFile(#PB_Any, DestFileName$)
@@ -296,9 +195,9 @@ Module OSM
curl_easy_setopt(curl, #CURLOPT_WRITEDATA, FileHandle)
curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToFileFunction())
;LockMutex(OSM\CurlMutex) ;Not any more necessary
res = curl_easy_perform(curl)
;UnlockMutex(OSM\CurlMutex)
If res <> #CURLE_OK
Debug "CURL NOT OK"
EndIf
@@ -330,7 +229,6 @@ Module OSM
OSM\ZoomMax = 18
OSM\MoveStartingPoint\x = - 1
OSM\TileSize = 256
;OSM\CurlMutex = CreateMutex()
OSM\Dirty = #False
OSM\Drawing\Mutex = CreateMutex()
OSM\Drawing\Semaphore = CreateSemaphore()
@@ -528,24 +426,16 @@ Module OSM
EndProcedure
Procedure.i GetTileFromHDD(CacheFile.s)
Protected nImage.i
Protected nImage.i
If FileSize(CacheFile) > 0
nImage = LoadImage(#PB_Any, CacheFile)
If IsImage(nImage)
Debug "Loadimage " + CacheFile + " -> Success !"
;Debug "Loadimage " + CacheFile + " -> Success !"
ProcedureReturn nImage
EndIf
EndIf
Debug "Loadimage " + CacheFile + " -> Failed !"
;Debug "Loadimage " + CacheFile + " -> Failed !"
ProcedureReturn -1
EndProcedure
Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s)
@@ -554,18 +444,15 @@ Module OSM
Protected nImage.i = -1
Protected FileHandle.i
Protected TileURL.s = OSM\ServerURL + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png"
; Protected CacheFile.s = "OSM_" + Str(Zoom) + "_" + Str(XTile) + "_" + Str(YTile) + ".png"
Debug "Check if we have this image on Web"
Protected TileURL.s = OSM\ServerURL + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png"
;Debug "Check if we have this image on Web"
If Proxy
;*Buffer = CurlReceiveHTTPToMemory(TileURL, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$)
FileHandle = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$)
If FileHandle
nImage = GetTileFromHDD(CacheFile)
Else
Debug "File " + TileURL + " not correctly received with Curl and proxy"
;Debug "File " + TileURL + " not correctly received with Curl and proxy"
EndIf
Else
*Buffer = ReceiveHTTPMemory(TileURL) ;TODO to thread by using #PB_HTTP_Asynchronous
@@ -573,16 +460,16 @@ Module OSM
If *Buffer
nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer))
If IsImage(nImage)
Debug "Load from web " + TileURL + " as Tile nb " + nImage
;Debug "Load from web " + TileURL + " as Tile nb " + nImage
SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG)
FreeMemory(*Buffer)
Else
Debug "Can't catch image " + TileURL
;Debug "Can't catch image " + TileURL
nImage = -1
;ShowMemoryViewer(*Buffer, MemorySize(*Buffer))
EndIf
Else
Debug "ReceiveHTTPMemory's buffer is empty"
;Debug "ReceiveHTTPMemory's buffer is empty"
EndIf
EndIf
@@ -1170,8 +1057,8 @@ CompilerEndIf
; IDE Options = PureBasic 5.42 LTS (Windows - x86)
; ExecutableFormat = Console
; CursorPosition = 146
; FirstLine = 117
; CursorPosition = 435
; FirstLine = 448
; Folding = -------
; EnableUnicode
; EnableThread