 |
Changeset 3835
- Timestamp:
- 08/03/08 13:02:39
(4 months ago)
- Author:
- kris
- Message:
updated TempFile? to use char[] instead of FilePath?
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3818 |
r3835 |
|
| 16 | 16 | import tango.io.DeviceConduit : DeviceConduit; |
|---|
| 17 | 17 | import tango.io.FileConduit : FileConduit; |
|---|
| 18 | | import tango.io.FilePath : FilePath/*, PathView*/; |
|---|
| | 18 | import tango.io.FilePath : FilePath; |
|---|
| 19 | 19 | import tango.stdc.stringz : toStringz, toString16z; |
|---|
| 20 | 20 | |
|---|
| … | … | |
| 24 | 24 | version( Win32 ) |
|---|
| 25 | 25 | { |
|---|
| 26 | | import tango.sys.Common : DWORD, LONG; |
|---|
| | 26 | import tango.sys.Common : DWORD, LONG, MAX_PATH, PCHAR, CP_UTF8; |
|---|
| 27 | 27 | |
|---|
| 28 | 28 | enum : DWORD { FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000 } |
|---|
| … | … | |
| 39 | 39 | GetTempPathA, SetFilePointer, GetLastError, ERROR_SUCCESS; |
|---|
| 40 | 40 | |
|---|
| 41 | | HANDLE CreateFile(FilePath fn, DWORD da, DWORD sm, |
|---|
| | 41 | HANDLE CreateFile(char[] fn, DWORD da, DWORD sm, |
|---|
| 42 | 42 | LPSECURITY_ATTRIBUTES sa, DWORD cd, DWORD faa, HANDLE tf) |
|---|
| 43 | 43 | { |
|---|
| 44 | | return CreateFileA(fn.cString.ptr, da, sm, sa, cd, faa, tf); |
|---|
| | 44 | return CreateFileA(toStringz(fn), da, sm, sa, cd, faa, tf); |
|---|
| 45 | 45 | } |
|---|
| 46 | 46 | |
|---|
| … | … | |
| 61 | 61 | { |
|---|
| 62 | 62 | import tango.sys.Common : |
|---|
| | 63 | MultiByteToWideChar, WideCharToMultiByte, |
|---|
| 63 | 64 | GetVersionExW, OSVERSIONINFO, |
|---|
| 64 | 65 | CreateFileW, GENERIC_READ, GENERIC_WRITE, |
|---|
| … | … | |
| 69 | 70 | GetTempPathW, SetFilePointer, GetLastError, ERROR_SUCCESS; |
|---|
| 70 | 71 | |
|---|
| 71 | | import tango.text.convert.Utf : toString, toString16; |
|---|
| 72 | | |
|---|
| 73 | | HANDLE CreateFile(FilePath fn, DWORD da, DWORD sm, |
|---|
| | 72 | HANDLE CreateFile(char[] fn, DWORD da, DWORD sm, |
|---|
| 74 | 73 | LPSECURITY_ATTRIBUTES sa, DWORD cd, DWORD faa, HANDLE tf) |
|---|
| 75 | 74 | { |
|---|
| 76 | | return CreateFileW(toString16(fn.cString).ptr, |
|---|
| 77 | | da, sm, sa, cd, faa, tf); |
|---|
| | 75 | // convert into output buffer |
|---|
| | 76 | wchar[MAX_PATH+1] tmp = void; |
|---|
| | 77 | assert (fn.length < tmp.length); |
|---|
| | 78 | auto i = MultiByteToWideChar (CP_UTF8, 0, cast(PCHAR) fn.ptr, |
|---|
| | 79 | fn.length, tmp.ptr, tmp.length); |
|---|
| | 80 | tmp[i] = 0; |
|---|
| | 81 | return CreateFileW(tmp.ptr, da, sm, sa, cd, faa, tf); |
|---|
| 78 | 82 | } |
|---|
| 79 | 83 | |
|---|
| … | … | |
| 88 | 92 | if( len == 0 ) |
|---|
| 89 | 93 | throw new Exception("could not obtain temporary path"); |
|---|
| 90 | | return Path.standard(toString(result[0..len])); |
|---|
| | 94 | |
|---|
| | 95 | auto dir = new char [len * 3]; |
|---|
| | 96 | auto i = WideCharToMultiByte (CP_UTF8, 0, result.ptr, len, |
|---|
| | 97 | cast(PCHAR) dir.ptr, dir.length, null, null); |
|---|
| | 98 | return Path.standard (dir[0..i]); |
|---|
| 91 | 99 | } |
|---|
| 92 | 100 | } |
|---|
| … | … | |
| 286 | 294 | |
|---|
| 287 | 295 | // Path to the temporary file |
|---|
| 288 | | private /*PathView*/ FilePath _path; |
|---|
| | 296 | private char[] _path; |
|---|
| 289 | 297 | |
|---|
| 290 | 298 | // Style we've opened with |
|---|
| … | … | |
| 303 | 311 | this(char[] prefix, Style style = Style.init) |
|---|
| 304 | 312 | { |
|---|
| 305 | | this(FilePath(prefix), style); |
|---|
| 306 | | } |
|---|
| 307 | | |
|---|
| 308 | | /// |
|---|
| 309 | | this(FilePath prefix, Style style = Style.init) |
|---|
| 310 | | { |
|---|
| 311 | | create(prefix.dup, style); |
|---|
| | 313 | create (prefix, style); |
|---|
| | 314 | } |
|---|
| | 315 | |
|---|
| | 316 | /// deprecated: please use char[] version instead |
|---|
| | 317 | deprecated this(FilePath prefix, Style style = Style.init) |
|---|
| | 318 | { |
|---|
| | 319 | this (prefix.toString.dup, style); |
|---|
| 312 | 320 | } |
|---|
| 313 | 321 | |
|---|
| … | … | |
| 319 | 327 | /************************************************************************** |
|---|
| 320 | 328 | * |
|---|
| 321 | | * Returns a PathView to the temporary file. Please note that depending |
|---|
| | 329 | * Returns the path of the temporary file. Please note that depending |
|---|
| 322 | 330 | * on your platform, the returned path may or may not actually exist if |
|---|
| 323 | 331 | * you specified a transient file. |
|---|
| 324 | 332 | * |
|---|
| 325 | 333 | **************************************************************************/ |
|---|
| 326 | | /*PathView*/ FilePath path() |
|---|
| | 334 | char[] path() |
|---|
| 327 | 335 | { |
|---|
| 328 | 336 | return _path; |
|---|
| … | … | |
| 341 | 349 | override char[] toString() |
|---|
| 342 | 350 | { |
|---|
| 343 | | if( path.toString.length > 0 ) |
|---|
| 344 | | return path.toString; |
|---|
| | 351 | if( path.length > 0 ) |
|---|
| | 352 | return path; |
|---|
| 345 | 353 | else |
|---|
| 346 | 354 | return "<TempFile>"; |
|---|
| … | … | |
| 379 | 387 | } |
|---|
| 380 | 388 | |
|---|
| 381 | | private void create(FilePath prefix, Style style) |
|---|
| | 389 | private void create(char[] prefix, Style style) |
|---|
| 382 | 390 | { |
|---|
| 383 | 391 | for( size_t i=0; i<style.attempts; ++i ) |
|---|
| 384 | 392 | { |
|---|
| 385 | | if( create_path(prefix.dup.append(randomName), style) ) |
|---|
| | 393 | if( create_path(Path.join(prefix, randomName), style) ) |
|---|
| 386 | 394 | return; |
|---|
| 387 | 395 | } |
|---|
| … | … | |
| 402 | 410 | * Returns the path to the temporary directory. |
|---|
| 403 | 411 | */ |
|---|
| 404 | | public static FilePath tempPath() |
|---|
| 405 | | { |
|---|
| 406 | | return FilePath(GetTempPath()).dup; |
|---|
| | 412 | public static char[] tempPath() |
|---|
| | 413 | { |
|---|
| | 414 | return GetTempPath; |
|---|
| 407 | 415 | } |
|---|
| 408 | 416 | |
|---|
| … | … | |
| 411 | 419 | * style. |
|---|
| 412 | 420 | */ |
|---|
| 413 | | private bool create_path(FilePath path, Style style) |
|---|
| | 421 | private bool create_path(char[] path, Style style) |
|---|
| 414 | 422 | { |
|---|
| 415 | 423 | // TODO: Check permissions directly and throw an exception; |
|---|
| … | … | |
| 480 | 488 | * Returns the path to the temporary directory. |
|---|
| 481 | 489 | */ |
|---|
| 482 | | public static FilePath tempPath() |
|---|
| | 490 | public static char[] tempPath() |
|---|
| 483 | 491 | { |
|---|
| 484 | 492 | // Check for TMPDIR; failing that, use /tmp |
|---|
| 485 | 493 | if( auto tmpdir = Environment.get("TMPDIR") ) |
|---|
| 486 | | return FilePath(tmpdir).dup; |
|---|
| | 494 | return tmpdir.dup; |
|---|
| 487 | 495 | else |
|---|
| 488 | | return FilePath("/tmp/").dup; |
|---|
| | 496 | return "/tmp/"; |
|---|
| 489 | 497 | } |
|---|
| 490 | 498 | |
|---|
| … | … | |
| 493 | 501 | * style. |
|---|
| 494 | 502 | */ |
|---|
| 495 | | private bool create_path(FilePath path, Style style) |
|---|
| | 503 | private bool create_path(char[] path, Style style) |
|---|
| 496 | 504 | { |
|---|
| 497 | 505 | // Check suitability |
|---|
| 498 | 506 | { |
|---|
| 499 | | auto parent = path.path; |
|---|
| | 507 | auto parent = Path.parse(path).path; |
|---|
| 500 | 508 | auto parentz = toStringz(parent); |
|---|
| 501 | 509 | |
|---|
| … | … | |
| 528 | 536 | | O_NOFOLLOW | O_RDWR; |
|---|
| 529 | 537 | |
|---|
| 530 | | auto pathz = path.cString.ptr; |
|---|
| | 538 | auto pathz = toStringz(path); |
|---|
| 531 | 539 | |
|---|
| 532 | 540 | handle = open(pathz, flags, 0600); |
|---|
| … | … | |
| 579 | 587 | * |
|---|
| 580 | 588 | **********************************************************************/ |
|---|
| 581 | | FilePath tempPath(); |
|---|
| | 589 | char[] tempPath(); |
|---|
| 582 | 590 | } |
|---|
| 583 | 591 | else |
|---|
Download in other formats:
|
 |