Пример #1
0
int main(int argc, char **argv) {
    int ret;
    URL url;
    Downloader downloader;
    Task task;
    Proxy proxy;
    char *ptr = NULL;
    // init_config
    // parse arguments
    // parse url
    // ftp or http ?
    // new download thread

    signal(SIGPIPE, SIG_IGN);
#ifdef HAVE_SSL
    SSL_load_error_strings();
    SSLeay_add_ssl_algorithms();
#endif

    while (1) {
        int option_index = 0;

        ret = getopt_long(argc, argv, short_options,
                long_options, &option_index);
        if (ret == -1) break;

        switch (ret) {
            case 'b':
                global_debug = true;
                break;
            case 'c':
                task.tryCount = atoi(optarg);
                break;
            case 'd':
                task.set_local_dir(optarg);
                break;
            case 'f':
                task.set_local_file(optarg);
                break;
            case 'h':
                print_help();
                return 0;
                break;
            case 'i':
                task.retryInterval = atoi(optarg);
                break;
            case 'n':
                task.threadNum = atoi(optarg);
                break;
            case 'r':
                task.set_referer(optarg);
                break;
            case 't':
                task.timeout = atoi(optarg);
                break;
            case 'v':
                cout << "Mytget " VERSION << endl;
                return 0;
            case 'x':
                ptr = StrDup(optarg);
                break;
            case 'H':
                task.set_host(optarg);
                break;
            case '?':
            default:
                print_help();
                return -1;
        }
    }

    if (ptr == NULL) {
        ptr = StrDup(getenv("proxy"));
    }
    if (ptr) {
        if (url.set_url(ptr) < 0) {
            delete[] ptr;
            cerr << "!!!Please check your http_proxy set" << endl;
            print_help();
            return -1;
        }
        delete[] ptr;
        if (url.get_protocol() != HTTP) {
            cerr << "!!!The proxy type is not supported" << endl;
            return -1;
        }
        proxy.set_type(HTTP_PROXY);
        proxy.set_host(url.get_host());
        proxy.set_port(url.get_port());
        proxy.set_user(url.get_user());
        proxy.set_password(url.get_password());
        task.proxy = proxy;
    }

    if (optind >= argc) {
        print_help();
        return -1;
    }

    while (optind < argc) {
        if (url.set_url(argv[optind++]) < 0) {
            print_help();
            return -1;
        }
        task.url = url;
        downloader.task = task;
        downloader.run();
    }

    return 0;
}