⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.cpp

📁 dget是一个基于Linux平台的多线程下载工具, 采用C++开发。主要支持FTP, HTTP, MMS, RTSP协议下载, 目前为止主要实现了FTP, HTTP, MMS,RTSP协议的多线程下载
💻 CPP
字号:
/* * by balancesli * balancesli@gmail.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <stdarg.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/time.h>#include <unistd.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <pthread.h>#include <ctype.h>#include <assert.h>#include <errno.h>#include "dLog.h"#include "Utils.h"#include "dget.h"#include "Task.h"#include "listviews.h"/******************* utils routine **********************************/int select_fd(int fd, int maxtime){   	fd_set readfds;   	struct timeval timeout;   	int nfds;   	FD_ZERO(&readfds);   	FD_SET(fd, &readfds);   	timeout.tv_sec = maxtime;   	timeout.tv_usec = 0;   	nfds = select(fd + 1, &readfds, NULL, NULL, &timeout);   	return nfds;}int Recv(int Sock, char * Buf, int Size, int Flags){    int RetVal;    do    {	    do	    {			RetVal = select_fd(Sock, 5);	    } while (RetVal == -1 && errno == EINTR);		RetVal = recv(Sock, Buf, Size, Flags);    }while (RetVal == -1 && errno == EINTR);    return RetVal;}int Send(int Sock, char *Buf, int Size, int Flags){    int RetVal;    do    {		RetVal = send(Sock, Buf, Size, Flags);    }while (RetVal == -1 && errno == EINTR);    return RetVal;}/*******************************************************************Encode the given string to base64 format and place it into store. store will be 0-terminated, and must point to a writable buffer of at least 1+BASE64_LENGTH(length) bytes. Note: Routine stolen from wget (grendel).******************************************************************/void base64_encode(const char *s, char *store, int length){  /* Conversion table. */	char tbl[64] = {	      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',	      'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',	      'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',	      'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',	      'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',	      'o', 'p', 'q', 'r', 's', 't', 'u', 'v',	      'w', 'x', 'y', 'z', '0', '1', '2', '3',	      '4', '5', '6', '7', '8', '9', '+', '/'	};	int i;	unsigned char *p = (unsigned char *) store;    /* Transform the 3x8 bits to 4x6 bits, as required by base64. */	for (i = 0; i < length; i += 3)  	{		*p++ = tbl[s[0] >> 2];		*p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];	    *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];		*p++ = tbl[s[2] & 0x3f];		s += 3;	}	/* Pad the result if necessary... */	if (i == length + 1)		*(p - 1) = '=';	else if (i == length + 2)		*(p - 1) = *(p - 2) = '=';		/* ...and zero-terminate it. */	*p = '\0';}char * GetAuthStr(char * user, char * passwd, char * auth_header){  char *p1, *p2, *ret;  int len = strlen(user) + strlen(passwd) + 1;  int b64len = 4 * ((len + 2) / 3);  p1 = (char *)malloc(len + 1);  sprintf(p1, "%s:%s", user, passwd);  p2 = (char *)malloc(b64len + 1);  /* Encode username:passwd to base64. */  base64_encode(p1, p2, len);  ret = (char *)malloc(strlen(auth_header) + b64len + 11);  sprintf(ret, "%s: Basic %s\r\n", auth_header, p2);  free(p1);  free(p2);  return ret;}void ShowMsg(const char * args, ...){	char p[MAX_MSG_SIZE];	va_list vp;	va_start(vp, args);	vsprintf(p, args, vp);	va_end(vp);	fprintf(stdout, "%s\n", p);}//extern TThread * ThreadQue[MAXTHREADS];void ExitResume(TThread * This){    ShowMsg("Aborting...");    This->Task->TerminateThreads();    Quit("Ok..please resume later with the -r switch");}void ExitNoResume(TThread * This){	TLog * plog = new TLog(This->u->HostFileName);    This->Task->TerminateThreads();    if(This->Task->DeleteDownloads()  == 0)		ShowMsg("Cleaned up the file parts");	else		ShowMsg("unable to cleanup the downloaded file parts");    if(plog->DeleteLogFile() == -1)    {        ShowMsg("Error while deleting the logfile: %s", strerror(errno));    }	delete plog;    Quit("Aborted...ok");}void Quit(const char *args, ...){    char p[MAX_MSG_SIZE];    va_list vp;    va_start(vp, args);    vsprintf(p, args, vp);    va_end(vp);    printf("\n%s\n",p);    exit(0);}int Panic(const char *args, ...){    int i;	char p[MAX_MSG_SIZE];    va_list vp;    va_start(vp, args);    vsprintf(p, args, vp);    va_end(vp);	ShowMsg(p);	exit(1);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -