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