summary refs log blame commit diff stats
path: root/compiler/modulegraphs.nim
blob: c081a099a343b7bfff599d770ef86423f5bdc84f (plain) (tree)
1
2
3
4
span>UUID): bool = result = false if a[0] == b[0] and a[1] == b[1] and a[2] == b[2] and a[3] == b[3]: result = true proc `$`(bstr: LPWSTR): string = var buffer: char var count = WideCharToMultiByte(CP_UTF8, 0, bstr, -1, addr(buffer), 0, nil, nil) if count == 0: raiseOsError(osLastError()) else: result = newStringOfCap(count + 8) let res = WideCharToMultiByte(CP_UTF8, 0, bstr, -1, addr(result[0]), count, nil, nil) if res == 0: raiseOsError(osLastError()) result.setLen(res - 1) proc toBstring(str: string): LPWSTR = var buffer: OLECHAR var count = MultiByteToWideChar(CP_UTF8, 0, unsafeAddr(str[0]), -1, addr(buffer), 0) if count == 0: raiseOsError(osLastError()) else: result = cast[LPWSTR](alloc0((count + 1) * sizeof(OLECHAR))) let res = MultiByteToWideChar(CP_UTF8, 0, unsafeAddr(str[0]), -1, result, count) if res == 0: raiseOsError(osLastError()) proc freeBstring(bstr: LPWSTR) = dealloc(bstr) proc getStatus(scode: ULONG): DownloadStatus = case scode of 0: result = statusError of BINDSTATUS_PROXYDETECTING: result = statusProxyDetecting of BINDSTATUS_REDIRECTING: result = statusRedirecting of BINDSTATUS_COOKIE_SENT: result = statusCookieSent of BINDSTATUS_FINDINGRESOURCE: result = statusResolving of BINDSTATUS_CONNECTING: result = statusConnecting of BINDSTATUS_SENDINGREQUEST: result = statusRequesting of BINDSTATUS_MIMETYPEAVAILABLE: result = statusMimetypeAvailable of BINDSTATUS_BEGINDOWNLOADDATA: result = statusBeginDownloading of BINDSTATUS_DOWNLOADINGDATA: result = statusDownloading of BINDSTATUS_ENDDOWNLOADDATA: result = statusEndDownloading of BINDSTATUS_CACHEFILENAMEAVAILABLE: result = statusCacheAvailable else: result = statusUnsupported proc addRef(self: PIBindStatusCallback): ULONG {.gcsafe, stdcall.} = inc(self.objectRefCount) result = self.objectRefCount proc release(self: PIBindStatusCallback): ULONG {.gcsafe, stdcall.} = dec(self.objectRefCount) result = self.objectRefCount proc queryInterface(self: PIBindStatusCallback, riid: ptr UUID, pvObject: ptr pointer): HRESULT {.gcsafe,stdcall.} = pvObject[] = nil if riid[] == IID_IUnknown: pvObject[] = cast[pointer](self) elif riid[] == IID_IBindStatusCallback: pvObject[] = cast[pointer](self) if not isNil(pvObject[]): discard addRef(self) result = S_OK else: result = E_NOINTERFACE proc onStartBinding(self: PIBindStatusCallback, dwReserved: DWORD, pib: pointer): HRESULT {.gcsafe, stdcall.} = result = S_OK proc getPriority(self: PIBindStatusCallback, pnPriority: ptr LONG): HRESULT {.gcsafe, stdcall.} = result = E_NOTIMPL proc onLowResource(self: PIBindStatusCallback, dwReserved: DWORD): HRESULT {.gcsafe, stdcall.} = result = S_OK proc onStopBinding(self: PIBindStatusCallback, hresult: HRESULT, szError: LPWSTR): HRESULT {.gcsafe, stdcall.} = result = S_OK proc getBindInfo(self: PIBindStatusCallback, grfBINDF: ptr DWORD, pbindinfo: ptr BINDINFO): HRESULT {.gcsafe, stdcall.} = var cbSize = pbindinfo.cbSize zeroMem(cast[pointer](pbindinfo), cbSize) pbindinfo.cbSize = cbSize grfBINDF[] = self.binfoFlags result = S_OK proc onDataAvailable(self: PIBindStatusCallback, grfBSCF: DWORD, dwSize: DWORD, pformatetc: pointer, pstgmed: pointer): HRESULT {.gcsafe, stdcall.} = result = S_OK proc onObjectAvailable(self: PIBindStatusCallback, riid: REFIID, punk: pointer): HRESULT {.gcsafe, stdcall.} = result = S_OK proc onProgress(self: PIBindStatusCallback, ulProgress: ULONG, ulProgressMax: ULONG, ulStatusCode: ULONG, szStatusText: LPWSTR): HRESULT {.gcsafe, stdcall.} = var message: string if optUseProgressCallback in self.options: if not isNil(szStatusText): message = $szStatusText else: message = "" self.progressCallback(getStatus(ulStatusCode), uint(ulProgress), uint(ulProgressMax), message) result = S_OK proc newBindStatusCallback(): IBindStatusCallback = result = IBindStatusCallback() result.vtable = cast[ptr IBindStatusCallbackVTable]( alloc0(sizeof(IBindStatusCallbackVTable)) ) result.vtable.QueryInterface = queryInterface result.vtable.AddRef = addRef result.vtable.Release = release result.vtable.OnStartBinding = onStartBinding result.vtable.GetPriority = getPriority result.vtable.OnLowResource = onLowResource result.vtable.OnStopBinding = onStopBinding result.vtable.GetBindInfo = getBindInfo result.vtable.OnDataAvailable = onDataAvailable result.vtable.OnObjectAvailable = onObjectAvailable result.vtable.OnProgress = onProgress result.objectRefCount = 1 proc freeBindStatusCallback(v: var IBindStatusCallback) = dealloc(v.vtable) proc downloadToFile*(szUrl: string, szFileName: string, options: set[DownloadOptions] = {}, progresscb: DownloadProgressCallback = nil) = ## Downloads from URL specified in ``szUrl`` to local filesystem path ## specified in ``szFileName``. ## ## szUrl ## URL to download, international names are supported. ## szFileName ## Destination path for downloading resource. ## options ## Downloading options. Currently only 2 options supported. ## progresscb ## Callback procedure, which will be called throughout the download ## process, indicating status and progress. ## ## Available downloading options: ## ## optUseCache ## Try to use Windows cache when downloading. ## optIgnoreSecurity ## Ignore HTTPS security problems, e.g. self-signed HTTPS certificate. ## var bszUrl = szUrl.toBstring() var bszFile = szFileName.toBstring() var bstatus = newBindStatusCallback() bstatus.options = {} if optUseCache notin options: bstatus.options.incl(optUseCache) let res = DeleteUrlCacheEntry(bszUrl) if res == 0: let err = osLastError() if err.int notin {ERROR_ACCESS_DENIED, ERROR_FILE_NOT_FOUND}: freeBindStatusCallback(bstatus) freeBstring(bszUrl) freeBstring(bszFile) raiseOsError(err) bstatus.binfoFlags = BINDF_GETNEWESTVERSION or BINDF_RESYNCHRONIZE or BINDF_PRAGMA_NO_CACHE or BINDF_NO_UI or BINDF_SILENTOPERATION if optIgnoreSecurity in options: bstatus.binfoFlags = bstatus.binfoFlags or BINDF_IGNORESECURITYPROBLEM if not isNil(progresscb): bstatus.options.incl(optUseProgressCallback) bstatus.progressCallback = progresscb let res = URLDownloadToFile(nil, bszUrl, bszFile, 0, addr bstatus) if FAILED(res): freeBindStatusCallback(bstatus) freeBstring(bszUrl) freeBstring(bszFile) raiseOsError(OSErrorCode(res)) freeBindStatusCallback(bstatus) freeBstring(bszUrl) freeBstring(bszFile) when isMainModule: proc progress(status: DownloadStatus, progress: uint, progressMax: uint, message: string) {.procvar,gcsafe.} = const downset: set[DownloadStatus] = {statusBeginDownloading, statusDownloading, statusEndDownloading} if status in downset: var message = "Downloaded " & $progress & " of " & $progressMax & "\c" stdout.write(message) else: echo "Status [" & $status & "] message = [" & $message & "]" downloadToFile("https://nim-lang.org/download/mingw64-6.3.0.7z", "test.zip", {optUseCache}, progress)