| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 | 9 |
| 10 #include "include/dart_api.h" | 10 #include "include/dart_api.h" |
| 11 | 11 |
| 12 | 12 |
| 13 static const int kFileFieldIndex = 0; | |
| 14 | |
| 15 | |
| 16 bool File::ReadFully(void* buffer, int64_t num_bytes) { | 13 bool File::ReadFully(void* buffer, int64_t num_bytes) { |
| 17 int64_t remaining = num_bytes; | 14 int64_t remaining = num_bytes; |
| 18 char* current_buffer = reinterpret_cast<char*>(buffer); | 15 char* current_buffer = reinterpret_cast<char*>(buffer); |
| 19 while (remaining > 0) { | 16 while (remaining > 0) { |
| 20 int bytes_read = Read(current_buffer, remaining); | 17 int bytes_read = Read(current_buffer, remaining); |
| 21 if (bytes_read <= 0) { | 18 if (bytes_read <= 0) { |
| 22 return false; | 19 return false; |
| 23 } | 20 } |
| 24 remaining -= bytes_read; // Reduce the number of remaining bytes. | 21 remaining -= bytes_read; // Reduce the number of remaining bytes. |
| 25 current_buffer += bytes_read; // Move the buffer forward. | 22 current_buffer += bytes_read; // Move the buffer forward. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 37 return false; | 34 return false; |
| 38 } | 35 } |
| 39 remaining -= bytes_read; // Reduce the number of remaining bytes. | 36 remaining -= bytes_read; // Reduce the number of remaining bytes. |
| 40 current_buffer += bytes_read; // Move the buffer forward. | 37 current_buffer += bytes_read; // Move the buffer forward. |
| 41 } | 38 } |
| 42 return true; | 39 return true; |
| 43 } | 40 } |
| 44 | 41 |
| 45 | 42 |
| 46 static File* GetFileHandle(Dart_Handle fileobj) { | 43 static File* GetFileHandle(Dart_Handle fileobj) { |
| 47 intptr_t value = 0; | 44 intptr_t value = |
| 48 Dart_Handle result = Dart_GetNativeInstanceField(fileobj, kFileFieldIndex, | 45 DartUtils::GetIntegerInstanceField(fileobj, DartUtils::kIdFieldName); |
| 49 &value); | |
| 50 ASSERT(Dart_IsValid(result)); | |
| 51 File* file = reinterpret_cast<File*>(value); | 46 File* file = reinterpret_cast<File*>(value); |
| 52 return file; | 47 return file; |
| 53 } | 48 } |
| 54 | 49 |
| 55 | 50 |
| 56 void FUNCTION_NAME(File_OpenFile)(Dart_NativeArguments args) { | 51 void FUNCTION_NAME(File_OpenFile)(Dart_NativeArguments args) { |
| 57 Dart_EnterScope(); | 52 Dart_EnterScope(); |
| 58 Dart_Handle fileobj = Dart_GetNativeArgument(args, 0); | 53 Dart_Handle fileobj = Dart_GetNativeArgument(args, 0); |
| 59 const char* filename = | 54 const char* filename = |
| 60 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 55 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
| 61 bool writable = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 2)); | 56 bool writable = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 2)); |
| 62 File* file = File::OpenFile(filename, writable); | 57 File* file = File::OpenFile(filename, writable); |
| 63 Dart_SetNativeInstanceField(fileobj, | 58 DartUtils::SetIntegerInstanceField(fileobj, |
| 64 kFileFieldIndex, | 59 DartUtils::kIdFieldName, |
| 65 reinterpret_cast<intptr_t>(file)); | 60 reinterpret_cast<intptr_t>(file)); |
| 66 Dart_SetReturnValue(args, Dart_NewBoolean(file != NULL)); | 61 Dart_SetReturnValue(args, Dart_NewBoolean(file != NULL)); |
| 67 Dart_ExitScope(); | 62 Dart_ExitScope(); |
| 68 } | 63 } |
| 69 | 64 |
| 70 | 65 |
| 71 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { | 66 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { |
| 72 Dart_EnterScope(); | 67 Dart_EnterScope(); |
| 73 const char* filename = | 68 const char* filename = |
| 74 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 69 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
| 75 bool exists = File::FileExists(filename); | 70 bool exists = File::FileExists(filename); |
| 76 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); | 71 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); |
| 77 Dart_ExitScope(); | 72 Dart_ExitScope(); |
| 78 } | 73 } |
| 79 | 74 |
| 80 | 75 |
| 81 void FUNCTION_NAME(File_Close)(Dart_NativeArguments args) { | 76 void FUNCTION_NAME(File_Close)(Dart_NativeArguments args) { |
| 82 Dart_EnterScope(); | 77 Dart_EnterScope(); |
| 83 intptr_t return_value = -1; | 78 intptr_t return_value = -1; |
| 84 Dart_Handle fileobj = Dart_GetNativeArgument(args, 0); | 79 Dart_Handle fileobj = Dart_GetNativeArgument(args, 0); |
| 85 File* file = GetFileHandle(fileobj); | 80 File* file = GetFileHandle(fileobj); |
| 86 if (file != NULL) { | 81 if (file != NULL) { |
| 87 Dart_SetNativeInstanceField(fileobj, | 82 DartUtils::SetIntegerInstanceField(fileobj, DartUtils::kIdFieldName, 0); |
| 88 kFileFieldIndex, | |
| 89 NULL); | |
| 90 delete file; | 83 delete file; |
| 91 return_value = 0; | 84 return_value = 0; |
| 92 } | 85 } |
| 93 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 86 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 94 Dart_ExitScope(); | 87 Dart_ExitScope(); |
| 95 } | 88 } |
| 96 | 89 |
| 97 | 90 |
| 98 void FUNCTION_NAME(File_ReadByte)(Dart_NativeArguments args) { | 91 void FUNCTION_NAME(File_ReadByte)(Dart_NativeArguments args) { |
| 99 Dart_EnterScope(); | 92 Dart_EnterScope(); |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 Dart_EnterScope(); | 231 Dart_EnterScope(); |
| 239 intptr_t return_value = -1; | 232 intptr_t return_value = -1; |
| 240 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 233 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); |
| 241 if (file != NULL) { | 234 if (file != NULL) { |
| 242 file->Flush(); | 235 file->Flush(); |
| 243 return_value = 0; | 236 return_value = 0; |
| 244 } | 237 } |
| 245 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 238 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 246 Dart_ExitScope(); | 239 Dart_ExitScope(); |
| 247 } | 240 } |
| OLD | NEW |