Esempio n. 1
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * main
 */ 
tb_int_t tb_demo_platform_directory_main(tb_int_t argc, tb_char_t** argv)
{
#if 1
    // home
    tb_char_t home[TB_PATH_MAXN] = {0};
    if (tb_directory_home(home, sizeof(home))) tb_trace_i("home: %s", home);

    // current
    tb_char_t current[TB_PATH_MAXN] = {0};
    if (tb_directory_current(current, sizeof(current))) tb_trace_i("current: %s", current);

    // temporary
    tb_char_t temporary[TB_PATH_MAXN] = {0};
    if (tb_directory_temporary(temporary, sizeof(temporary))) tb_trace_i("temporary: %s", temporary);
#elif 0

    // current
    tb_char_t current[TB_PATH_MAXN] = {0};
    if (tb_directory_current(current, sizeof(current))) tb_trace_i("current: %s", current);

    // current
    tb_directory_walk(argv[1]? argv[1] : current, tb_true, tb_true, tb_directory_walk_func, tb_null);
#elif 0
    tb_directory_remove(argv[1]);
#else
//  tb_directory_walk(argv[1], tb_true, tb_true, tb_directory_walk_func, tb_null);
    tb_directory_copy(argv[1], argv[2]);
#endif

    return 0;
}
Esempio n. 2
0
File: path.c Progetto: richwu/tbox
/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */
tb_size_t tb_path_translate(tb_char_t* path, tb_size_t size, tb_size_t maxn)
{
    // check
    tb_assert_and_check_return_val(path, 0);

    // file://?
    tb_char_t* p = path;
    if (!tb_strnicmp(p, "file:", 5)) p += 5;
    // is user directory?
    else if (path[0] == '~')
    {
        // get the home directory
        tb_char_t home[TB_PATH_MAXN];
        tb_size_t home_size = tb_directory_home(home, sizeof(home) - 1);
        tb_assert_and_check_return_val(home_size, 0);

        // check the path space
        tb_size_t path_size = size? size : tb_strlen(path);
        tb_assert_and_check_return_val(home_size + path_size - 1 < maxn, 0);

        // move the path and ensure the enough space for the home directory
        tb_memmov(path + home_size, path + 1, path_size - 1);

        // copy the home directory 
        tb_memcpy(path, home, home_size);
        path[home_size + path_size - 1] = '\0';
    }

    // remove repeat separator
    tb_char_t*  q = path;
    tb_size_t   repeat = 0;
    for (; *p; p++)
    {
        if (tb_path_is_separator(*p))
        {
            // save the separator if not exists
            if (!repeat) *q++ = TB_PATH_SEPARATOR;

            // repeat it
            repeat++;
        }
        else 
        {
            // save character
            *q++ = *p;

            // clear repeat
            repeat = 0;
        }
    }

    // remove the tail separator and not root: '/'
    if (q > path + 1 && *(q - 1) == TB_PATH_SEPARATOR) q--;

    // end
    *q = '\0';

    // is windows path? 
    if (q > path + 1 && tb_isalpha(path[0]) && path[1] == ':')
    {
        // get the upper drive prefix
        path[0] = tb_toupper(path[0]);

        // append the drive separator if not exists
        if (q - path == 2)
        {
            *q++ = TB_PATH_SEPARATOR;
            *q = '\0';
        }
    }

    // trace
    tb_trace_d("translate: %s", path);

    // ok
    return q - path;
}