Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- (unknown)
Class Method Summary collapse
-
.expand_path(*args) ⇒ Object
File.expand_path(path, dir = nil).
Class Method Details
.expand_path(*args) ⇒ Object
File.expand_path(path, dir = nil)
Converts path
to an absolute path.
This is a custom version of the File.expand_path method for Windows that is much faster than the MRI core method. It also supports “~user” expansion on Windows while the MRI core method does not.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'ext/win32/xpath.c', line 190
static VALUE rb_xpath(int argc, VALUE* argv, VALUE self){
VALUE v_path, v_path_orig, v_dir_orig;
wchar_t* buffer = NULL;
wchar_t* ptr = NULL;
wchar_t* path = NULL;
char* final_path;
int length;
rb_encoding* path_encoding;
rb_econv_t* ec;
const int replaceflags = ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE;
rb_scan_args(argc, argv, "11", &v_path_orig, &v_dir_orig);
if (rb_respond_to(v_path_orig, rb_intern("to_path")))
v_path_orig = rb_funcall2(v_path_orig, rb_intern("to_path"), 0, NULL);
SafeStringValue(v_path_orig);
if (!NIL_P(v_dir_orig))
SafeStringValue(v_dir_orig);
// Dup and prep string for modification
path_encoding = rb_enc_get(v_path_orig);
if (rb_enc_to_index(path_encoding) == rb_utf8_encindex()){
v_path = rb_str_dup(v_path_orig);
}
else{
ec = rb_econv_open(rb_enc_name(path_encoding), "UTF-8", replaceflags);
v_path = rb_econv_str_convert(ec, v_path_orig, ECONV_PARTIAL_INPUT);
rb_econv_close(ec);
}
rb_str_modify_expand(v_path, MAX_WPATH);
// Make our path a wide string for later functions
length = MultiByteToWideChar(CP_UTF8, 0, StringValueCStr(v_path), -1, NULL, 0);
path = (wchar_t*)ruby_xmalloc(length * sizeof(wchar_t));
if(!MultiByteToWideChar(CP_UTF8, 0, StringValueCStr(v_path), -1, path, length)){
ruby_xfree(path);
rb_raise_syserr("MultibyteToWideChar", GetLastError());
}
// Convert all forward slashes to backslashes to Windows API functions work properly
while(wcsstr(path, L"/"))
path[wcscspn(path, L"/")] = L'\\';
// Handle ~ expansion if first character.
if ( (ptr = wcschr(path, L'~')) && ((int)(ptr - path) == 0) ){
wchar_t* home;
// Handle both ~/user and ~user syntax
if (ptr[1] && ptr[1] != L'\\'){
home = find_user(++ptr);
}
else{
#ifdef HAVE_PATHCCHAPPENDEX
HRESULT hr;
home = expand_tilde(path);
hr = PathCchAppendEx(home, MAX_WPATH, ++ptr, 1);
if(hr != S_OK){
ruby_xfree(home);
rb_raise_syserr("PathCchAppendEx", hr);
}
#else
home = expand_tilde(path);
if (!PathAppendW(home, ++ptr)){
ruby_xfree(home);
rb_raise_syserr("PathAppend", GetLastError());
}
#endif
}
path = home;
}
// Directory argument is present
if (!NIL_P(v_dir_orig)){
wchar_t* dir;
VALUE v_dir;
rb_encoding* dir_encoding;
#ifdef HAVE_PATHCCH_H
HRESULT hr;
#endif
dir_encoding = rb_enc_get(v_dir_orig);
// Hooray for encodings
if (rb_enc_to_index(dir_encoding) == rb_utf8_encindex()){
v_dir = rb_str_dup(v_dir_orig);
}
else{
ec = rb_econv_open(rb_enc_name(dir_encoding), "UTF-8", replaceflags);
v_dir = rb_econv_str_convert(ec, v_dir_orig, ECONV_PARTIAL_INPUT);
rb_econv_close(ec);
}
// Prep string for modification
rb_str_modify_expand(v_dir, MAX_WPATH);
length = MultiByteToWideChar(CP_UTF8, 0, StringValueCStr(v_dir), -1, NULL, 0);
dir = (wchar_t*)ruby_xmalloc(MAX_WPATH * sizeof(wchar_t));
if (!MultiByteToWideChar(CP_UTF8, 0, StringValueCStr(v_dir), -1, dir, length)){
ruby_xfree(dir);
rb_raise_syserr("MultibyteToWideChar", GetLastError());
}
while (wcsstr(dir, L"/"))
dir[wcscspn(dir, L"/")] = L'\\';
// Check for tilde in first character
if ( (ptr = wcschr(dir, L'~')) && ((int)(ptr - dir) == 0) ){
if (ptr[1] && ptr[1] != L'\\'){
dir = find_user(++ptr);
}
else{
dir = expand_tilde();
#ifdef HAVE_PATHCCH_H
hr = PathCchAppendEx(dir, MAX_WPATH, ++ptr, 1);
if(hr != S_OK){
ruby_xfree(dir);
rb_raise_syserr("PathCchAppendEx", hr);
}
#else
if (!PathAppendW(dir, ++ptr)){
ruby_xfree(dir);
rb_raise_syserr("PathAppend", GetLastError());
}
#endif
}
}
if (!wcslen(path))
path = dir;
if (PathIsRelativeW(path)){
#ifdef HAVE_PATHCCH_H
hr = PathCchAppendEx(dir, MAX_WPATH, path, 1);
if(hr != S_OK){
ruby_xfree(dir);
rb_raise_syserr("PathCchAppendEx", hr);
}
#else
if(!PathAppendW(dir, path)){
ruby_xfree(dir);
rb_raise_syserr("PathAppend", GetLastError());
}
#endif
// Remove leading slashes from relative paths
if (dir[0] == L'\\')
++dir;
path = dir;
}
}
else{
if (!wcslen(path)){
char* pwd;
wchar_t* wpwd;
// First call, get the length
length = GetCurrentDirectoryW(0, NULL);
wpwd = (wchar_t*)ruby_xmalloc(length * sizeof(wchar_t));
length = GetCurrentDirectoryW(length, wpwd);
if(!length){
ruby_xfree(wpwd);
rb_raise_syserr("GetCurrentDirectory", GetLastError());
}
// Convert backslashes into forward slashes
while(wcsstr(wpwd, L"\\"))
wpwd[wcscspn(wpwd, L"\\")] = L'/';
// Convert string back to multibyte string before returning Ruby object
length = WideCharToMultiByte(CP_UTF8, 0, wpwd, -1, NULL, 0, NULL, NULL);
pwd = (char*)ruby_xmalloc(length);
length = WideCharToMultiByte(CP_UTF8, 0, wpwd, -1, pwd, length, NULL, NULL);
if (!length){
ruby_xfree(pwd);
rb_raise_syserr("WideCharToMultiByte", GetLastError());
}
return rb_str_new2(pwd);
}
}
// Strip all trailing backslashes
#ifdef HAVE_PATHCCH_H
while (PathCchRemoveBackslash(path, wcslen(path)+1) == S_OK);
#else
while (!*PathRemoveBackslashW(path));
#endif
// First call, get the length
length = GetFullPathNameW(path, 0, buffer, NULL);
buffer = (wchar_t*)ruby_xmalloc(length * sizeof(wchar_t));
// Now get the path
length = GetFullPathNameW(path, length, buffer, NULL);
if (!length){
ruby_xfree(buffer);
rb_raise_syserr("GetFullPathName", GetLastError());
}
// Convert backslashes into forward slashes
while(wcsstr(buffer, L"\\"))
buffer[wcscspn(buffer, L"\\")] = L'/';
length = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, NULL, 0, NULL, NULL);
final_path = (char*)ruby_xmalloc(length);
length = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, final_path, length, NULL, NULL);
ruby_xfree(buffer);
if (!length){
ruby_xfree(final_path);
rb_raise_syserr("WideCharToMultiByte", GetLastError());
}
v_path = rb_str_new(final_path, length - 1); // Don't count null terminator
if (rb_enc_to_index(path_encoding) != rb_utf8_encindex()){
ec = rb_econv_open("UTF-8", rb_enc_name(path_encoding), replaceflags);
v_path = rb_econv_str_convert(ec, v_path, ECONV_PARTIAL_INPUT);
rb_econv_close(ec);
}
rb_enc_associate(v_path, path_encoding);
if (OBJ_TAINTED(v_path_orig) || rb_equal(v_path, v_path_orig) == Qfalse)
OBJ_TAINT(v_path);
return v_path;
}
|