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

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

Issue 8383037: Improve error handling in the process library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 2 months 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/file.h ('k') | runtime/bin/file_linux.cc » ('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"
(...skipping 27 matching lines...) Expand all
38 } 38 }
39 remaining -= bytes_read; // Reduce the number of remaining bytes. 39 remaining -= bytes_read; // Reduce the number of remaining bytes.
40 current_buffer += bytes_read; // Move the buffer forward. 40 current_buffer += bytes_read; // Move the buffer forward.
41 } 41 }
42 return true; 42 return true;
43 } 43 }
44 44
45 45
46 static File* GetFileHandle(Dart_Handle fileobj) { 46 static File* GetFileHandle(Dart_Handle fileobj) {
47 Dart_Result result = Dart_GetNativeInstanceField(fileobj, kFileFieldIndex); 47 Dart_Result result = Dart_GetNativeInstanceField(fileobj, kFileFieldIndex);
48 assert(Dart_IsValidResult(result)); 48 ASSERT(Dart_IsValidResult(result));
49 File* file = reinterpret_cast<File*>(Dart_GetResultAsCIntptr(result)); 49 File* file = reinterpret_cast<File*>(Dart_GetResultAsCIntptr(result));
50 return file; 50 return file;
51 } 51 }
52 52
53 53
54 void FUNCTION_NAME(File_OpenFile)(Dart_NativeArguments args) { 54 void FUNCTION_NAME(File_OpenFile)(Dart_NativeArguments args) {
55 Dart_EnterScope(); 55 Dart_EnterScope();
56 Dart_Handle fileobj = Dart_GetNativeArgument(args, 0); 56 Dart_Handle fileobj = Dart_GetNativeArgument(args, 0);
57 const char* filename = 57 const char* filename =
58 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); 58 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 Dart_ExitScope(); 143 Dart_ExitScope();
144 } 144 }
145 145
146 146
147 void FUNCTION_NAME(File_ReadList)(Dart_NativeArguments args) { 147 void FUNCTION_NAME(File_ReadList)(Dart_NativeArguments args) {
148 Dart_EnterScope(); 148 Dart_EnterScope();
149 intptr_t return_value = -1; 149 intptr_t return_value = -1;
150 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); 150 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0));
151 if (file != NULL) { 151 if (file != NULL) {
152 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); 152 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1);
153 assert(Dart_IsArray(buffer_obj)); 153 ASSERT(Dart_IsArray(buffer_obj));
154 int64_t offset = 154 int64_t offset =
155 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); 155 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2));
156 int64_t length = 156 int64_t length =
157 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); 157 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3));
158 Dart_Result result = Dart_GetLength(buffer_obj); 158 Dart_Result result = Dart_GetLength(buffer_obj);
159 assert(Dart_IsValidResult(result)); 159 ASSERT(Dart_IsValidResult(result));
160 assert((offset + length) <= Dart_GetResultAsCIntptr(result)); 160 ASSERT((offset + length) <= Dart_GetResultAsCIntptr(result));
161 uint8_t* buffer = new uint8_t[length]; 161 uint8_t* buffer = new uint8_t[length];
162 int total_bytes_read = 162 int total_bytes_read =
163 file->Read(reinterpret_cast<void*>(buffer), length); 163 file->Read(reinterpret_cast<void*>(buffer), length);
164 /* 164 /*
165 * Reading 0 indicates end of file. 165 * Reading 0 indicates end of file.
166 */ 166 */
167 if (total_bytes_read >= 0) { 167 if (total_bytes_read >= 0) {
168 result = 168 result =
169 Dart_ArraySet(buffer_obj, offset, buffer, total_bytes_read); 169 Dart_ArraySet(buffer_obj, offset, buffer, total_bytes_read);
170 ASSERT(Dart_IsValidResult(result)); 170 ASSERT(Dart_IsValidResult(result));
171 return_value = total_bytes_read; 171 return_value = total_bytes_read;
172 } 172 }
173 delete[] buffer; 173 delete[] buffer;
174 } 174 }
175 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); 175 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
176 Dart_ExitScope(); 176 Dart_ExitScope();
177 } 177 }
178 178
179 179
180 void FUNCTION_NAME(File_WriteList)(Dart_NativeArguments args) { 180 void FUNCTION_NAME(File_WriteList)(Dart_NativeArguments args) {
181 Dart_EnterScope(); 181 Dart_EnterScope();
182 intptr_t return_value = -1; 182 intptr_t return_value = -1;
183 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); 183 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0));
184 if (file != NULL) { 184 if (file != NULL) {
185 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); 185 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1);
186 assert(Dart_IsArray(buffer_obj)); 186 ASSERT(Dart_IsArray(buffer_obj));
187 int64_t offset = 187 int64_t offset =
188 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); 188 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2));
189 int64_t length = 189 int64_t length =
190 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); 190 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3));
191 Dart_Result result = Dart_GetLength(buffer_obj); 191 Dart_Result result = Dart_GetLength(buffer_obj);
192 assert(Dart_IsValidResult(result)); 192 ASSERT(Dart_IsValidResult(result));
193 assert((offset + length) <= Dart_GetResultAsCIntptr(result)); 193 ASSERT((offset + length) <= Dart_GetResultAsCIntptr(result));
194 uint8_t* buffer = new uint8_t[length]; 194 uint8_t* buffer = new uint8_t[length];
195 result = Dart_ArrayGet(buffer_obj, offset, buffer, length); 195 result = Dart_ArrayGet(buffer_obj, offset, buffer, length);
196 ASSERT(Dart_IsValidResult(result)); 196 ASSERT(Dart_IsValidResult(result));
197 int total_bytes_written = 197 int total_bytes_written =
198 file->Write(reinterpret_cast<void*>(buffer), length); 198 file->Write(reinterpret_cast<void*>(buffer), length);
199 if (total_bytes_written >= 0) { 199 if (total_bytes_written >= 0) {
200 return_value = total_bytes_written; 200 return_value = total_bytes_written;
201 } 201 }
202 delete[] buffer; 202 delete[] buffer;
203 } 203 }
(...skipping 30 matching lines...) Expand all
234 Dart_EnterScope(); 234 Dart_EnterScope();
235 intptr_t return_value = -1; 235 intptr_t return_value = -1;
236 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); 236 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0));
237 if (file != NULL) { 237 if (file != NULL) {
238 file->Flush(); 238 file->Flush();
239 return_value = 0; 239 return_value = 0;
240 } 240 }
241 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); 241 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
242 Dart_ExitScope(); 242 Dart_ExitScope();
243 } 243 }
OLDNEW
« no previous file with comments | « runtime/bin/file.h ('k') | runtime/bin/file_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698