OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "bin/file.h" |
| 6 |
| 7 #include "bin/builtin.h" |
| 8 #include "bin/dartutils.h" |
| 9 #include "bin/embedded_dart_io.h" |
| 10 #include "bin/io_buffer.h" |
| 11 #include "bin/utils.h" |
| 12 |
| 13 #include "include/dart_api.h" |
| 14 #include "include/dart_tools_api.h" |
| 15 |
| 16 namespace dart { |
| 17 namespace bin { |
| 18 |
| 19 // Are we capturing output from stdout for the VM service? |
| 20 static bool capture_stdout = false; |
| 21 |
| 22 // Are we capturing output from stderr for the VM service? |
| 23 static bool capture_stderr = false; |
| 24 |
| 25 void SetCaptureStdout(bool value) { |
| 26 capture_stdout = value; |
| 27 } |
| 28 |
| 29 |
| 30 void SetCaptureStderr(bool value) { |
| 31 capture_stderr = value; |
| 32 } |
| 33 |
| 34 |
| 35 bool ShouldCaptureStdout() { |
| 36 return capture_stdout; |
| 37 } |
| 38 |
| 39 |
| 40 bool ShouldCaptureStderr() { |
| 41 return capture_stderr; |
| 42 } |
| 43 |
| 44 |
| 45 bool File::ReadFully(void* buffer, int64_t num_bytes) { |
| 46 int64_t remaining = num_bytes; |
| 47 char* current_buffer = reinterpret_cast<char*>(buffer); |
| 48 while (remaining > 0) { |
| 49 int64_t bytes_read = Read(current_buffer, remaining); |
| 50 if (bytes_read <= 0) { |
| 51 return false; |
| 52 } |
| 53 remaining -= bytes_read; // Reduce the number of remaining bytes. |
| 54 current_buffer += bytes_read; // Move the buffer forward. |
| 55 } |
| 56 return true; |
| 57 } |
| 58 |
| 59 |
| 60 bool File::WriteFully(const void* buffer, int64_t num_bytes) { |
| 61 int64_t remaining = num_bytes; |
| 62 const char* current_buffer = reinterpret_cast<const char*>(buffer); |
| 63 while (remaining > 0) { |
| 64 int64_t bytes_written = Write(current_buffer, remaining); |
| 65 if (bytes_written < 0) { |
| 66 return false; |
| 67 } |
| 68 remaining -= bytes_written; // Reduce the number of remaining bytes. |
| 69 current_buffer += bytes_written; // Move the buffer forward. |
| 70 } |
| 71 if (capture_stdout || capture_stderr) { |
| 72 intptr_t fd = GetFD(); |
| 73 if ((fd == STDOUT_FILENO) && capture_stdout) { |
| 74 Dart_ServiceSendDataEvent("Stdout", "WriteEvent", |
| 75 reinterpret_cast<const uint8_t*>(buffer), |
| 76 num_bytes); |
| 77 } else if ((fd == STDERR_FILENO) && capture_stderr) { |
| 78 Dart_ServiceSendDataEvent("Stderr", "WriteEvent", |
| 79 reinterpret_cast<const uint8_t*>(buffer), |
| 80 num_bytes); |
| 81 } |
| 82 } |
| 83 return true; |
| 84 } |
| 85 |
| 86 |
| 87 File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) { |
| 88 ASSERT((mode == File::kDartRead) || |
| 89 (mode == File::kDartWrite) || |
| 90 (mode == File::kDartAppend) || |
| 91 (mode == File::kDartWriteOnly) || |
| 92 (mode == File::kDartWriteOnlyAppend)); |
| 93 if (mode == File::kDartWrite) { |
| 94 return File::kWriteTruncate; |
| 95 } |
| 96 if (mode == File::kDartAppend) { |
| 97 return File::kWrite; |
| 98 } |
| 99 if (mode == File::kDartWriteOnly) { |
| 100 return File::kWriteOnlyTruncate; |
| 101 } |
| 102 if (mode == File::kDartWriteOnlyAppend) { |
| 103 return File::kWriteOnly; |
| 104 } |
| 105 return File::kRead; |
| 106 } |
| 107 |
| 108 } // namespace bin |
| 109 } // namespace dart |
OLD | NEW |