 |
Changeset 3638
- Timestamp:
- 06/18/08 22:14:59
(4 months ago)
- Author:
- kris
- Message:
FileConduit? now based around char[], and exposes the open() method for use.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3559 |
r3638 |
|
| 23 | 23 | private import tango.io.DeviceConduit; |
|---|
| 24 | 24 | |
|---|
| | 25 | private import stdc = tango.stdc.stringz; |
|---|
| | 26 | |
|---|
| 25 | 27 | private import Utf = tango.text.convert.Utf; |
|---|
| 26 | 28 | |
|---|
| … | … | |
| 237 | 239 | |
|---|
| 238 | 240 | // the file we're working with |
|---|
| 239 | | private PathView path_; |
|---|
| | 241 | private char[] path_; |
|---|
| 240 | 242 | |
|---|
| 241 | 243 | // the style we're opened with |
|---|
| 242 | | private Style style_; |
|---|
| | 244 | private Style style_; |
|---|
| | 245 | |
|---|
| | 246 | /*********************************************************************** |
|---|
| | 247 | |
|---|
| | 248 | Create a FileConduit for use with open() |
|---|
| | 249 | |
|---|
| | 250 | ***********************************************************************/ |
|---|
| | 251 | |
|---|
| | 252 | this () |
|---|
| | 253 | { |
|---|
| | 254 | } |
|---|
| 243 | 255 | |
|---|
| 244 | 256 | /*********************************************************************** |
|---|
| … | … | |
| 248 | 260 | ***********************************************************************/ |
|---|
| 249 | 261 | |
|---|
| 250 | | this (char[] name, Style style = ReadExisting) |
|---|
| 251 | | { |
|---|
| 252 | | this (new FilePath(name), style); |
|---|
| | 262 | this (char[] path, Style style = ReadExisting) |
|---|
| | 263 | { |
|---|
| | 264 | open (path, style); |
|---|
| 253 | 265 | } |
|---|
| 254 | 266 | |
|---|
| … | … | |
| 257 | 269 | Create a FileConduit with the provided path and style. |
|---|
| 258 | 270 | |
|---|
| 259 | | ***********************************************************************/ |
|---|
| 260 | | |
|---|
| 261 | | this (PathView path, Style style = ReadExisting) |
|---|
| 262 | | { |
|---|
| 263 | | // remember who we are |
|---|
| 264 | | path_ = path; |
|---|
| 265 | | |
|---|
| 266 | | // open the file |
|---|
| 267 | | open (this.style_ = style); |
|---|
| | 271 | Deprecated: use char[] ctor instead |
|---|
| | 272 | |
|---|
| | 273 | ***********************************************************************/ |
|---|
| | 274 | |
|---|
| | 275 | deprecated this (PathView path, Style style = ReadExisting) |
|---|
| | 276 | { |
|---|
| | 277 | this (path.toString, style); |
|---|
| 268 | 278 | } |
|---|
| 269 | 279 | |
|---|
| … | … | |
| 272 | 282 | Return the PathView used by this file. |
|---|
| 273 | 283 | |
|---|
| 274 | | ***********************************************************************/ |
|---|
| 275 | | |
|---|
| 276 | | PathView path () |
|---|
| 277 | | { |
|---|
| 278 | | return path_; |
|---|
| | 284 | We're removing PathView here, in favour of toString() |
|---|
| | 285 | |
|---|
| | 286 | ***********************************************************************/ |
|---|
| | 287 | |
|---|
| | 288 | deprecated PathView path () |
|---|
| | 289 | { |
|---|
| | 290 | // return something useful in the interim |
|---|
| | 291 | return new FilePath (path_); |
|---|
| 279 | 292 | } |
|---|
| 280 | 293 | |
|---|
| … | … | |
| 298 | 311 | override char[] toString () |
|---|
| 299 | 312 | { |
|---|
| 300 | | return path_.toString; |
|---|
| | 313 | return path_; |
|---|
| 301 | 314 | } |
|---|
| 302 | 315 | |
|---|
| … | … | |
| 346 | 359 | ***************************************************************/ |
|---|
| 347 | 360 | |
|---|
| 348 | | protected void open (Style style) |
|---|
| | 361 | void open (char[] path, Style style = ReadExisting) |
|---|
| 349 | 362 | { |
|---|
| 350 | 363 | DWORD attr, |
|---|
| … | … | |
| 386 | 399 | FILE_FLAG_WRITE_THROUGH, |
|---|
| 387 | 400 | ]; |
|---|
| | 401 | |
|---|
| | 402 | // remember our settings |
|---|
| | 403 | assert(path); |
|---|
| | 404 | path_ = path; |
|---|
| | 405 | style_ = style; |
|---|
| 388 | 406 | |
|---|
| 389 | 407 | attr = Attr[style.cache]; |
|---|
| … | … | |
| 399 | 417 | else |
|---|
| 400 | 418 | { |
|---|
| 401 | | wchar[256] tmp = void; |
|---|
| 402 | | auto name = Utf.toString16 (path.cString, tmp); |
|---|
| 403 | | handle = CreateFileW (name.ptr, access, share, |
|---|
| | 419 | char[512] zero = void; |
|---|
| | 420 | wchar[512] convert = void; |
|---|
| | 421 | |
|---|
| | 422 | // zero terminate and convert to utf16 |
|---|
| | 423 | auto name = stdc.toStringz (path, zero); |
|---|
| | 424 | auto wide = Utf.toString16 (name[0..path.length+1], convert); |
|---|
| | 425 | |
|---|
| | 426 | // open the file |
|---|
| | 427 | handle = CreateFileW (wide.ptr, access, share, |
|---|
| 404 | 428 | null, create, |
|---|
| 405 | 429 | attr | FILE_ATTRIBUTE_NORMAL, |
|---|
| … | … | |
| 408 | 432 | |
|---|
| 409 | 433 | if (handle is INVALID_HANDLE_VALUE) |
|---|
| 410 | | error (); |
|---|
| | 434 | error; |
|---|
| 411 | 435 | |
|---|
| 412 | 436 | // move to end of file? |
|---|
| … | … | |
| 445 | 469 | // must have Generic_Write access |
|---|
| 446 | 470 | if (! SetEndOfFile (handle)) |
|---|
| 447 | | error (); |
|---|
| | 471 | error; |
|---|
| 448 | 472 | } |
|---|
| 449 | 473 | |
|---|
| … | … | |
| 463 | 487 | if (result is -1 && |
|---|
| 464 | 488 | GetLastError() != ERROR_SUCCESS) |
|---|
| 465 | | error (); |
|---|
| | 489 | error; |
|---|
| 466 | 490 | |
|---|
| 467 | 491 | return result + (cast(long) high << 32); |
|---|
| … | … | |
| 491 | 515 | ***************************************************************/ |
|---|
| 492 | 516 | |
|---|
| 493 | | protected void open (Style style) |
|---|
| | 517 | void open (char[] path, Style style = ReadExisting) |
|---|
| 494 | 518 | { |
|---|
| 495 | 519 | alias int[] Flags; |
|---|
| … | … | |
| 519 | 543 | ]; |
|---|
| 520 | 544 | |
|---|
| | 545 | // remember our settings |
|---|
| | 546 | assert(path); |
|---|
| | 547 | path_ = path; |
|---|
| | 548 | style_ = style; |
|---|
| | 549 | |
|---|
| | 550 | // zero terminate and convert to utf16 |
|---|
| | 551 | char[512] zero = void; |
|---|
| | 552 | auto name = stdc.toStringz (path, zero); |
|---|
| 521 | 553 | auto mode = Access[style.access] | Create[style.open]; |
|---|
| 522 | 554 | |
|---|
| 523 | 555 | // always open as a large file |
|---|
| 524 | | handle = posix.open (path.cString.ptr, mode | O_LARGEFILE, 0666); |
|---|
| | 556 | handle = posix.open (name, mode | O_LARGEFILE, 0666); |
|---|
| 525 | 557 | if (handle is -1) |
|---|
| 526 | | error (); |
|---|
| | 558 | error; |
|---|
| 527 | 559 | } |
|---|
| 528 | 560 | |
|---|
| … | … | |
| 539 | 571 | // set filesize to be current seek-position |
|---|
| 540 | 572 | if (ftruncate (handle, cast(int) position) is -1) |
|---|
| 541 | | error (); |
|---|
| | 573 | error; |
|---|
| 542 | 574 | } |
|---|
| 543 | 575 | |
|---|
| … | … | |
| 553 | 585 | long result = posix.lseek (handle, cast(int) offset, anchor); |
|---|
| 554 | 586 | if (result is -1) |
|---|
| 555 | | error (); |
|---|
| | 587 | error; |
|---|
| 556 | 588 | return result; |
|---|
| 557 | 589 | } |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic