Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(910)

Side by Side Diff: runtime/bin/file.cc

Issue 8399033: First round of changes to the file interface. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/builtin_in.cc ('k') | runtime/bin/file.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 29 matching lines...) Expand all
129 122
130 123
131 void FUNCTION_NAME(File_WriteString)(Dart_NativeArguments args) { 124 void FUNCTION_NAME(File_WriteString)(Dart_NativeArguments args) {
132 Dart_EnterScope(); 125 Dart_EnterScope();
133 intptr_t return_value = -1; 126 intptr_t return_value = -1;
134 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); 127 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0));
135 if (file != NULL) { 128 if (file != NULL) {
136 const char* str = 129 const char* str =
137 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); 130 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
138 int bytes_written = file->Write(reinterpret_cast<const void*>(str), 131 int bytes_written = file->Write(reinterpret_cast<const void*>(str),
139 strlen(str)); 132 strlen(str));
140 if (bytes_written >= 0) { 133 if (bytes_written >= 0) {
141 return_value = bytes_written; 134 return_value = bytes_written;
142 } 135 }
143 } 136 }
144 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); 137 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
145 Dart_ExitScope(); 138 Dart_ExitScope();
146 } 139 }
147 140
148 141
149 void FUNCTION_NAME(File_ReadList)(Dart_NativeArguments args) { 142 void FUNCTION_NAME(File_ReadList)(Dart_NativeArguments args) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 }
OLDNEW
« no previous file with comments | « runtime/bin/builtin_in.cc ('k') | runtime/bin/file.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698