tb_size_t tb_directory_home(tb_char_t* path, tb_size_t maxn) { // check tb_assert_and_check_return_val(path && maxn, 0); // the home directory tb_bool_t ok = tb_false; tb_handle_t pidl = tb_null; tb_wchar_t home[TB_PATH_MAXN] = {0}; do { // get the appdata folder location if (S_OK != tb_shell32()->SHGetSpecialFolderLocation(tb_null, 0x1a /* CSIDL_APPDATA */, &pidl)) break; tb_check_break(pidl); // get the home directory if (!tb_shell32()->SHGetPathFromIDListW(pidl, home)) break; // ok ok = tb_true; } while (0); // exit pidl if (pidl) GlobalFree(pidl); pidl = tb_null; // wtoa tb_size_t size = ok? tb_wtoa(path, home, maxn) : 0; // ok? return size != -1? size : 0; }
static tb_void_t tb_directory_walk_impl(tb_wchar_t const* path, tb_bool_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv) { // check tb_assert_and_check_return(path && func); // last tb_long_t last = tb_wcslen(path) - 1; tb_assert_and_check_return(last >= 0); // add \*.* tb_wchar_t temp_w[4096] = {0}; tb_char_t temp_a[4096] = {0}; tb_swprintf(temp_w, 4095, L"%s%s*.*", path, path[last] == L'\\'? L"" : L"\\"); // init info WIN32_FIND_DATAW find = {0}; HANDLE directory = INVALID_HANDLE_VALUE; if (INVALID_HANDLE_VALUE != (directory = FindFirstFileW(temp_w, &find))) { // walk do { // check if (tb_wcscmp(find.cFileName, L".") && tb_wcscmp(find.cFileName, L"..")) { // the temp path tb_long_t n = tb_swprintf(temp_w, 4095, L"%s%s%s", path, path[last] == L'\\'? L"" : L"\\", find.cFileName); if (n >= 0 && n < 4096) temp_w[n] = L'\0'; // wtoa temp n = tb_wtoa(temp_a, temp_w, 4095); if (n >= 0 && n < 4096) temp_a[n] = '\0'; // the file info tb_file_info_t info = {0}; if (tb_file_info(temp_a, &info)) { // do callback if (prefix) func(temp_a, &info, priv); // walk to the next directory if (info.type == TB_FILE_TYPE_DIRECTORY && recursion) tb_directory_walk_impl(temp_w, recursion, prefix, func, priv); // do callback if (!prefix) func(temp_a, &info, priv); } } } while (FindNextFileW(directory, &find)); // exit directory FindClose(directory); } }
tb_size_t tb_directory_curt(tb_char_t* path, tb_size_t maxn) { // check tb_assert_and_check_return_val(path && maxn > 4, 0); // the current directory tb_wchar_t curt[TB_PATH_MAXN] = {0}; GetCurrentDirectoryW(TB_PATH_MAXN, curt); // wtoa return tb_wtoa(path, curt, maxn); }
tb_size_t tb_directory_temp(tb_char_t* path, tb_size_t maxn) { // check tb_assert_and_check_return_val(path && maxn > 4, 0); // the temporary directory tb_wchar_t temp[TB_PATH_MAXN] = {0}; GetTempPathW(TB_PATH_MAXN, temp); // wtoa return tb_wtoa(path, temp, maxn); }
tb_size_t tb_directory_temporary(tb_char_t* path, tb_size_t maxn) { // check tb_assert_and_check_return_val(path && maxn > 4, 0); // the temporary directory tb_wchar_t temporary[TB_PATH_MAXN] = {0}; GetTempPathW(TB_PATH_MAXN, temporary); // wtoa tb_size_t size = tb_wtoa(path, temporary, maxn); // ok? return size != -1? size : 0; }
tb_size_t tb_directory_current(tb_char_t* path, tb_size_t maxn) { // check tb_assert_and_check_return_val(path && maxn > 4, 0); // the current directory tb_wchar_t current[TB_PATH_MAXN] = {0}; GetCurrentDirectoryW(TB_PATH_MAXN, current); // wtoa tb_size_t size = tb_wtoa(path, current, maxn); // ok? return size != -1? size : 0; }
tb_long_t tb_wputs(tb_wchar_t const* string) { // check tb_check_return_val(string, 0); // wtoa tb_char_t line[8192] = {0}; tb_long_t size = tb_wtoa(line, string, 8191); if (size >= 0 && size < 8192) line[size] = '\0'; // print it tb_printl(line); // ok? return tb_wcslen(string); }
tb_long_t tb_wputs(tb_wchar_t const* string) { // check tb_check_return_val(string, 0); // wtoa tb_char_t line[8192] = {0}; tb_long_t size = tb_wtoa(line, string, 8191); tb_assert_and_check_return_val(size != -1, 0); // print it tb_printl(line); // ok? return tb_wcslen(string); }
static tb_wchar_t* tb_printf_object(tb_wchar_t* pb, tb_wchar_t* pe, tb_printf_entry_t e, tb_cpointer_t object) { // the object name tb_char_t data[1024] = {0}; tb_wtoa(data, e.object, tb_arrayn(data)); // find the object func tb_printf_object_func_t func = tb_printf_object_find(data); if (func) { // printf it tb_long_t size = func(object, data, tb_arrayn(data) - 1); if (size >= 0) { // end data[size] = '\0'; // atow size = tb_atow(pb, data, pe - pb); if (size >= 0) pb += size; } else { // invalid if (pb < pe) *pb++ = L'i'; if (pb < pe) *pb++ = L'n'; if (pb < pe) *pb++ = L'v'; if (pb < pe) *pb++ = L'a'; if (pb < pe) *pb++ = L'l'; if (pb < pe) *pb++ = L'i'; if (pb < pe) *pb++ = L'd'; } } else { // null if (pb < pe) *pb++ = L'n'; if (pb < pe) *pb++ = L'u'; if (pb < pe) *pb++ = L'l'; if (pb < pe) *pb++ = L'l'; } return pb; }
tb_long_t tb_wprintf(tb_wchar_t const* format, ...) { // check tb_check_return_val(format, 0); // format line tb_long_t size = 0; tb_wchar_t line[8192] = {0}; tb_vswprintf_format(line, 8191, format, &size); if (size >= 0 && size < 8192) line[size] = L'\0'; // wtoa tb_char_t text[8192] = {0}; size = tb_wtoa(text, line, 8191); tb_assert_and_check_return_val(size != -1, 0); // print it tb_print(text); // ok? return size; }
/* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ static tb_char_t* tb_environment_get_impl(tb_char_t const* name, tb_size_t* psize) { // check tb_assert_and_check_return_val(name, 0); // done tb_bool_t ok = tb_false; tb_size_t size = 0; tb_size_t maxn = 256; tb_char_t* value = tb_null; tb_wchar_t* value_w = tb_null; do { // make value_w value_w = (tb_wchar_t*)tb_malloc0(sizeof(tb_wchar_t) * maxn); tb_assert_and_check_break(value_w); // make name tb_wchar_t name_w[512]; tb_size_t name_n = tb_atow(name_w, name, tb_arrayn(name_w)); tb_assert_and_check_break(name_n != -1); // get it size = (tb_size_t)tb_kernel32()->GetEnvironmentVariableW(name_w, value_w, (DWORD)maxn); if (!size) { // error? if (ERROR_ENVVAR_NOT_FOUND == GetLastError()) { // trace tb_trace_d("environment variable(%s) does not exist", name); } break; } else if (size > maxn) { // grow space value_w = (tb_wchar_t*)tb_ralloc(value_w, sizeof(tb_wchar_t) * (size + 1)); tb_assert_and_check_break(value_w); // get it size = (tb_size_t)tb_kernel32()->GetEnvironmentVariableW(name_w, value_w, (DWORD)size + 1); tb_assert_and_check_break(size); } // make value value = (tb_char_t*)tb_malloc0(sizeof(tb_char_t) * (size + 1)); tb_assert_and_check_break(value); // save value if ((size = tb_wtoa(value, value_w, size)) == -1) break; // save size if (psize) *psize = size; // ok ok = tb_true; } while (0); // failed? if (!ok) { // exit value if (value) tb_free(value); value = tb_null; } // exit value_w if (value_w) tb_free(value_w); value_w = tb_null; // ok? return value; }