static void test_UrlCreateFromPath(void) { size_t i; char ret_url[INTERNET_MAX_URL_LENGTH]; DWORD len, ret; WCHAR ret_urlW[INTERNET_MAX_URL_LENGTH]; WCHAR *pathW, *urlW; for(i = 0; i < sizeof(TEST_URLFROMPATH) / sizeof(TEST_URLFROMPATH[0]); i++) { len = INTERNET_MAX_URL_LENGTH; ret = UrlCreateFromPathA(TEST_URLFROMPATH[i].path, ret_url, &len, 0); ok(ret == TEST_URLFROMPATH[i].ret, "ret %08x from path %s\n", ret, TEST_URLFROMPATH[i].path); ok(!lstrcmpi(ret_url, TEST_URLFROMPATH[i].url), "url %s from path %s\n", ret_url, TEST_URLFROMPATH[i].path); ok(len == strlen(ret_url), "ret len %d from path %s\n", len, TEST_URLFROMPATH[i].path); len = INTERNET_MAX_URL_LENGTH; pathW = GetWideString(TEST_URLFROMPATH[i].path); urlW = GetWideString(TEST_URLFROMPATH[i].url); ret = UrlCreateFromPathW(pathW, ret_urlW, &len, 0); WideCharToMultiByte(CP_ACP, 0, ret_urlW, -1, ret_url, sizeof(ret_url),0,0); ok(ret == TEST_URLFROMPATH[i].ret, "ret %08x from path L\"%s\", expected %08x\n", ret, TEST_URLFROMPATH[i].path, TEST_URLFROMPATH[i].ret); ok(!lstrcmpiW(ret_urlW, urlW), "got %s expected %s from path L\"%s\"\n", ret_url, TEST_URLFROMPATH[i].url, TEST_URLFROMPATH[i].path); ok(len == lstrlenW(ret_urlW), "ret len %d from path L\"%s\"\n", len, TEST_URLFROMPATH[i].path); FreeWideString(urlW); FreeWideString(pathW); } }
char* jm_create_URL_from_abs_path(jm_callbacks* cb, const char* path) { /* worst case: all symbols are 4-byte UTF-8 and need to be %-encoded */ #define MAX_URL_LENGTH (FILENAME_MAX * 4 * 3 + 7) char buffer[MAX_URL_LENGTH]; char* url; size_t urllen; if(!cb) { cb = jm_get_default_callbacks(); } #if defined(_WIN32) || defined(WIN32) { DWORD pathLen = MAX_URL_LENGTH; HRESULT code = UrlCreateFromPathA( path, buffer, &pathLen, 0); if( (code != S_FALSE) && (code != S_OK)) { jm_log_fatal(cb, module,"Could not constuct file URL from path %s", path); return 0; } urllen = pathLen; } #else { size_t i, len = strlen(path); char *curBuf = buffer + 7; unsigned char ch; strcpy(buffer, "file://"); for( i = 0; i < len; i++) { ch = (unsigned char)path[i]; if( (ch == '/') || ((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '_') || (ch == '.') ||(ch == '~')) { *curBuf = ch; curBuf++; continue; } sprintf(curBuf, "%%%2X", (int)ch);/*safe*/ curBuf+=3; } *curBuf = 0; urllen = curBuf - buffer; } #endif url = (char*)cb->malloc(urllen+1); if(!url) { jm_log_fatal(cb, module,"Could not allocate memory"); return 0; } strcpy(url, buffer); return url; }