| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // experimental - do not submit |
| 6 |
| 7 #include <errno.h> |
| 8 #include <dirent.h> |
| 9 #include <string.h> |
| 10 #include <sys/stat.h> |
| 11 |
| 12 #include "bin/dartutils.h" |
| 13 #include "include/dart_api.h" |
| 14 |
| 15 void PrintString(Dart_Handle strHandle) { |
| 16 const char *cstring; |
| 17 Dart_StringToCString(strHandle, &cstring); |
| 18 printf("%s\n", cstring); |
| 19 } |
| 20 |
| 21 void PrintHandle(Dart_Handle handle) { |
| 22 PrintString(Dart_ToString(handle)); |
| 23 } |
| 24 |
| 25 static void handleErrorNumber(Dart_NativeArguments args, int error_number); |
| 26 |
| 27 void FUNCTION_NAME(FilePath_List)(Dart_NativeArguments args) { |
| 28 Dart_EnterScope(); |
| 29 printf("FilePath_List\n"); |
| 30 ASSERT(Dart_GetNativeArgumentCount(args) == 1); |
| 31 |
| 32 const char *path = NULL; |
| 33 Dart_StringToCString(Dart_GetNativeArgument(args, 0), &path); |
| 34 |
| 35 DIR* dir_pointer = opendir(path); |
| 36 if (!dir_pointer) { |
| 37 handleErrorNumber(args, errno); |
| 38 } else { |
| 39 const int MAX_ENTRY = 1000; // TODO(mattsh) - use growable array or vector |
| 40 Dart_Handle names[MAX_ENTRY]; |
| 41 int name_index = 0; |
| 42 dirent entry; |
| 43 while (true) { |
| 44 if (name_index >= MAX_ENTRY) { |
| 45 break; |
| 46 } |
| 47 dirent* result; |
| 48 int error_number = readdir_r(dir_pointer, &entry, &result); |
| 49 if (error_number) { |
| 50 handleErrorNumber(args, error_number); |
| 51 break; |
| 52 } |
| 53 if (!result) { |
| 54 break; |
| 55 } |
| 56 if (!strcmp(entry.d_name, ".") || !strcmp(entry.d_name, "..")) { |
| 57 continue; |
| 58 } |
| 59 names[name_index++] = Dart_NewString(entry.d_name); |
| 60 } |
| 61 Dart_Handle name_list = Dart_NewList(name_index); |
| 62 for (int i = 0; i < name_index; i++) { |
| 63 Dart_ListSetAt(name_list, i, names[i]); |
| 64 } |
| 65 Dart_SetReturnValue(args, name_list); |
| 66 } |
| 67 Dart_ExitScope(); |
| 68 } |
| 69 |
| 70 void FUNCTION_NAME(FilePath_IsDirectory)(Dart_NativeArguments args) { |
| 71 Dart_EnterScope(); |
| 72 printf("FilePath_IsDirectory\n"); |
| 73 ASSERT(Dart_GetNativeArgumentCount(args) == 1); |
| 74 |
| 75 const char *path = NULL; |
| 76 Dart_StringToCString(Dart_GetNativeArgument(args, 0), &path); |
| 77 |
| 78 struct stat entry_info; |
| 79 int lstat_success = lstat(path, &entry_info); |
| 80 if (lstat_success != 0) { |
| 81 handleErrorNumber(args, errno); |
| 82 } else { |
| 83 bool isDirectory = false; |
| 84 if ((entry_info.st_mode & S_IFMT) == S_IFDIR) { |
| 85 isDirectory = true; |
| 86 } |
| 87 Dart_SetReturnValue(args, Dart_NewBoolean(isDirectory)); |
| 88 } |
| 89 Dart_ExitScope(); |
| 90 } |
| 91 |
| 92 void handleErrorNumber(Dart_NativeArguments args, int error_number) { |
| 93 const char *message; |
| 94 const int MAX_LEN = 100; |
| 95 char message_buffer[MAX_LEN]; |
| 96 switch (error_number) { |
| 97 case ENOENT: |
| 98 message = "No such file or directory"; |
| 99 break; |
| 100 case ENAMETOOLONG: |
| 101 message = "File name too long"; |
| 102 break; |
| 103 default: |
| 104 snprintf(message_buffer, MAX_LEN, "TODO - errno %d", errno); |
| 105 message = message_buffer; |
| 106 break; |
| 107 } |
| 108 Dart_SetReturnValue(args, Dart_NewString(message)); |
| 109 } |
| OLD | NEW |