Example #1
0
File: option.c Project: richwu/tbox
/* //////////////////////////////////////////////////////////////////////////////////////
 * main
 */ 
tb_int_t tb_demo_utils_option_main(tb_int_t argc, tb_char_t** argv)
{
    // init option
    tb_option_ref_t option = tb_option_init("option", "the option command test demo", g_options);
    if (option)
    {
        // done option
        if (tb_option_done(option, argc - 1, &argv[1]))
        {
            // done dump
            tb_option_dump(option);

            // done help
            if (tb_option_find(option, "help"))
                tb_option_help(option);
            // done version
            else if (tb_option_find(option, "version"))
            {
                tb_version_t const* version = tb_version();
                if (version) tb_trace_i("version: tbox-v%u.%u.%u.%llu", version->major, version->minor, version->alter, version->build);
            }
            else
            {
                // done integer
                if (tb_option_find(option, "i"))
                    tb_trace_i("integer: %lld", tb_option_item_sint64(option, "i"));
                // done string
                if (tb_option_find(option, "s"))
                    tb_trace_i("string: %s", tb_option_item_cstr(option, "s"));
#ifdef TB_CONFIG_TYPE_HAVE_FLOAT
                // done float
                if (tb_option_find(option, "f"))
                    tb_trace_i("float: %f", tb_option_item_float(option, "f"));
#endif
                // done boolean
                if (tb_option_find(option, "b"))
                    tb_trace_i("boolean: %s", tb_option_item_bool(option, "b")? "y" : "n");
                // done demo
                if (tb_option_find(option, "demo"))
                    tb_trace_i("demo: %s", tb_option_item_cstr(option, "demo"));
                // done file0
                if (tb_option_find(option, "file0"))
                    tb_trace_i("file0: %s", tb_option_item_cstr(option, "file0"));
                // done file1
                if (tb_option_find(option, "file1"))
                    tb_trace_i("file1: %s", tb_option_item_cstr(option, "file1"));

                // done more
                tb_size_t more = 0;
                while (1)
                {
                    tb_char_t name[64] = {0};
                    tb_snprintf(name, 63, "more%lu", more++);
                    if (tb_option_find(option, name))
                        tb_trace_i("%s: %s", name, tb_option_item_cstr(option, name));
                    else break;
                }
            }
        }
        else tb_option_help(option);
    
        // exit option
        tb_option_exit(option);
    }

    return 0;
}
Example #2
0
static tb_bool_t tb_demo_spider_init(tb_demo_spider_t* spider, tb_int_t argc, tb_char_t** argv)
{
    // check
    tb_assert_and_check_return_val(spider && argc && argv, tb_false);

    // done
    tb_bool_t ok = tb_false;
    do
    {
#ifdef TB_CONFIG_MODULE_HAVE_OBJECT
        // init option
        spider->option = tb_option_init("spider", "the spider demo", g_options);
        tb_assert_and_check_break(spider->option);
 
        // done option
        if (!tb_option_done(spider->option, argc - 1, &argv[1])) break;

        // check
        tb_assert_and_check_break(tb_option_find(spider->option, "home"));

        // init home
        if (!tb_url_init(&spider->home)) break;
        tb_url_set(&spider->home, tb_option_item_cstr(spider->option, "home"));
        tb_trace_d("home: %s", tb_url_get(&spider->home));

        // init only home?
        if (tb_option_find(spider->option, "only"))
            spider->home_only = tb_option_item_bool(spider->option, "only");

        // init root
        tb_char_t const* root = tb_option_item_cstr(spider->option, "directory");

        // init user agent
        spider->user_agent = tb_option_item_cstr(spider->option, "agent");

        // init timeout
        if (tb_option_find(spider->option, "timeout"))
            spider->timeout = tb_option_item_sint32(spider->option, "timeout");

        // init limited rate
        if (tb_option_find(spider->option, "rate"))
            spider->limited_rate = tb_option_item_uint32(spider->option, "rate");
#else

        // check
        tb_assert_and_check_break(argv[1]);

        // init home
        if (!tb_url_init(&spider->home)) break;
        spider->home = argv[1]? argv[1] : tb_null;
        tb_trace_d("home: %s", tb_url_get(&spider->home));

        // init root
        tb_char_t const* root = argv[2];
#endif

        // the home host
        tb_char_t const* host = tb_url_host_get(&spider->home);
        tb_assert_and_check_break(host);

        // init home domain
        tb_char_t const* domain = tb_strchr(host, '.');
        if (domain)
        {
            tb_strlcpy(spider->home_domain, domain, sizeof(spider->home_domain) - 1);
            spider->home_domain[sizeof(spider->home_domain) - 1] = '\0';
        }

        // using the default root
        if (root) tb_strlcpy(spider->root, root, sizeof(spider->root) - 1);
        else 
        {
            // the temporary root
            tb_directory_temp(spider->root, sizeof(spider->root) - 1);

            // append spider
            tb_strcat(spider->root, "/spider");
        }
        tb_trace_d("root: %s", spider->root);

        // using the default user agent
        if (!spider->user_agent) spider->user_agent = TB_DEMO_SPIDER_USER_AGENT;

        // using the default timeout
        if (!spider->timeout) spider->timeout = TB_DEMO_SPIDER_TASK_TIMEOUT;

        // using the default rate
        if (!spider->limited_rate) spider->limited_rate = TB_DEMO_SPIDER_TASK_RATE;

        // strip root tail: '/' or '\\'
        tb_size_t size = tb_strlen(spider->root);
        if (size && (spider->root[size - 1] == '/' || spider->root[size - 1] == '\\')) spider->root[size - 1] = '\0';

        // init state
        spider->state = TB_STATE_OK;

        // init lock
        if (!tb_spinlock_init(&spider->lock)) break;

        // init pool
        spider->pool = tb_fixed_pool_init(tb_null, TB_DEMO_SPIDER_TASK_MAXN >> 2, sizeof(tb_demo_spider_task_t), tb_null, tb_null, tb_null);
        tb_assert_and_check_break(spider->pool);

        // init filter
        spider->filter = tb_bloom_filter_init(TB_BLOOM_FILTER_PROBABILITY_0_001, 3, TB_DEMO_SPIDER_FILTER_MAXN, tb_item_func_str(tb_true));
        tb_assert_and_check_break(spider->filter);

        // register lock profiler
#ifdef TB_LOCK_PROFILER_ENABLE
        tb_lock_profiler_register(tb_lock_profiler(), (tb_pointer_t)&spider->lock, "spider");
#endif

        // ok
        ok = tb_true;

    } while (0);

    // failed? help it
#ifdef TB_CONFIG_MODULE_HAVE_OBJECT
    if (!ok && spider->option) tb_option_help(spider->option);
#endif

    // ok?
    return ok;
}