'------------------------------------------------------------------------------ ' cURL.inc - cURL library for PowerBasic. ' currently supported: FTP UPLOAD with text-progressbar ' Usage: ErrCode = UPLOAD(hDlg, TxPgBarId, "myfile", "/temp") '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ GLOBAL curlFile, curlRdir AS STRING : GLOBAL curlStatus AS LONG '------------------------------------------------------------------------------ FUNCTION UPLOAD (BYVAL hDlg AS DWORD, BYVAL PgBarId AS LONG, _ BYVAL file AS STRING, BYVAL rdir AS STRING) AS LONG curlFile = file curlRdir = rdir curlStatus = %STILL_ACTIVE DIALOG POST hDlg, %WM_APP, PgBarId, 0 WHILE curlStatus = %STILL_ACTIVE DIALOG DOEVENTS SLEEP 0 WEND FUNCTION = curlStatus END FUNCTION '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ MACRO UPLOAD_CALLBACK STATIC hConsole, curlPid AS DWORD STATIC curlTimer, curlPg AS LONG LOCAL timeleft, curlErr AS STRING LOCAL curlBuffer AS ASCIIZ * 512 LOCAL curlPct, curlBps AS LONG LOCAL hInfo AS DWORD LOCAL curlCod AS LONG SELECT CASE AS LONG CB.MSG CASE %WM_APP curlErr = DIR$(EXE.PATH$ + "curl.exe") : DIR$ CLOSE IF ISFALSE LEN(curlErr) THEN ? "cURL executable not found", %MB_ICONERROR, EXE.NAME$ curlStatus = 1 EXIT FUNCTION END IF ' Get text-progressbar id from message curlPg = CB.WPARAM SET_TEXT_PROGRESSBAR CB.HNDL, curlPg, 0, "", %RGB_DARKGREEN, %GREEN ' Spawn a cURL process via Shell curlPid = SHELL ("curl -T " + curlFile _ + " -u " + $USR + ":" + $PWD _ + " ftp://" + $FTP _ + "/" + TRIM$(curlRdir, "/") + "/" _ , 0) SLEEP 1000 ' Attach the corresponding console and get its handle AttachConsole(curlPid) hConsole = GetStdHandle(%STD_OUTPUT_HANDLE) ' Start the 1 second timer to read console content curlTimer = SetTimer(CB.HNDL, %WM_USER, 1000, BYVAL 0) DIALOG POST CB.HNDL, %WM_TIMER, %WM_USER, 0 CASE %WM_DESTROY ' If parent dialog (this program) is killed then kill child process (cURL) as well IF ISTRUE curlTimer THEN KillTimer CB.HNDL, curlTimer IF ISTRUE hConsole THEN FreeConsole () hConsole = OpenProcess(%PROCESS_TERMINATE, %FALSE, curlPid) TerminateProcess(hConsole, 0) CloseHandle(hConsole) END IF CASE %WM_TIMER ' Check status of cURL process hInfo = OpenProcess(%PROCESS_QUERY_INFORMATION, %FALSE, curlPid) GetExitCodeProcess(hInfo, curlCod) CloseHandle(hInfo) ' Get cURL progress (%) readConsoleOutputCharacter (hConsole, BYVAL VARPTR (curlBuffer), 80*4, BYVAL 0, curlBps) curlPct = VAL(MID$(curlBuffer, 161, 3)) ' cURL is finished: end timer + show result IF curlCod <> %STILL_ACTIVE THEN KillTimer CB.HNDL, curlTimer : curlTimer = 0 IF curlCod <> 0 THEN curlErr = TRIM$(MID$(curlBuffer, 239)) SET_TEXT_PROGRESSBAR CB.HNDL, curlPg, curlPct, "", %RGB_DARKRED, %RED ELSE SET_TEXT_PROGRESSBAR CB.HNDL, curlPg, curlPct, "" END IF FreeConsole () : hConsole = 0 curlStatus = curlCod EXIT FUNCTION END IF ' cURL is active: get more info timeleft = LTRIM$(MID$(curlBuffer, 228, 5)) : IF LEFT$(timeleft,1) = "0" THEN timeleft = MID$(timeleft,2) curlBps = VAL(MID$(curlBuffer, 234)) ' Display info SET_TEXT_PROGRESSBAR CB.HNDL, curlPg, curlPct, SIZEKB(curlBps) + "/s (" + timeleft + " left)" END SELECT END MACRO '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ FUNCTION SIZEKB(fs AS LONG) AS STRING ' File size, in B, KB or MB LOCAL n AS LONG n = fs ' filesize IF n < 1024 THEN FUNCTION = TRIM$(n) + " B" ELSE n \= 1024 IF n < 1024 THEN FUNCTION = TRIM$(n) + " KB" ELSE n \= 1024 FUNCTION = TRIM$(n) + " MB" END IF END IF END FUNCTION '------------------------------------------------------------------------------