Chromium Code Reviews| Index: runtime/bin/directory_exp.cc |
| diff --git a/runtime/bin/directory_exp.cc b/runtime/bin/directory_exp.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..32c684f798fe9f2f7501894b9518b7a5af80516d |
| --- /dev/null |
| +++ b/runtime/bin/directory_exp.cc |
| @@ -0,0 +1,109 @@ |
| +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +// experimental - do not submit |
| + |
| +#include <errno.h> |
| +#include <dirent.h> |
| +#include <string.h> |
| +#include <sys/stat.h> |
| + |
| +#include "bin/dartutils.h" |
| +#include "include/dart_api.h" |
| + |
| +void PrintString(Dart_Handle strHandle) { |
| + const char *cstring; |
| + Dart_StringToCString(strHandle, &cstring); |
| + printf("%s\n", cstring); |
| +} |
| + |
| +void PrintHandle(Dart_Handle handle) { |
| + PrintString(Dart_ToString(handle)); |
| +} |
| + |
| +static void handleErrorNumber(Dart_NativeArguments args, int error_number); |
| + |
| +void FUNCTION_NAME(FilePath_List)(Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + printf("FilePath_List\n"); |
|
Ben Laurie (Google)
2011/11/29 18:59:28
printf?
mattsh
2011/11/29 21:29:46
Actually I'm going to remove the directory listing
|
| + ASSERT(Dart_GetNativeArgumentCount(args) == 1); |
| + |
| + const char *path = NULL; |
| + Dart_StringToCString(Dart_GetNativeArgument(args, 0), &path); |
| + |
| + DIR* dir_pointer = opendir(path); |
| + if (!dir_pointer) { |
| + handleErrorNumber(args, errno); |
| + } else { |
| + const int MAX_ENTRY = 1000; // TODO(mattsh) - use growable array or vector |
| + Dart_Handle names[MAX_ENTRY]; |
| + int name_index = 0; |
| + dirent entry; |
| + while (true) { |
| + if (name_index >= MAX_ENTRY) { |
| + break; |
| + } |
| + dirent* result; |
| + int error_number = readdir_r(dir_pointer, &entry, &result); |
| + if (error_number) { |
| + handleErrorNumber(args, error_number); |
| + break; |
| + } |
| + if (!result) { |
| + break; |
| + } |
| + if (!strcmp(entry.d_name, ".") || !strcmp(entry.d_name, "..")) { |
| + continue; |
| + } |
| + names[name_index++] = Dart_NewString(entry.d_name); |
| + } |
| + Dart_Handle name_list = Dart_NewList(name_index); |
| + for (int i = 0; i < name_index; i++) { |
| + Dart_ListSetAt(name_list, i, names[i]); |
| + } |
| + Dart_SetReturnValue(args, name_list); |
| + } |
| + Dart_ExitScope(); |
| +} |
| + |
| +void FUNCTION_NAME(FilePath_IsDirectory)(Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + printf("FilePath_IsDirectory\n"); |
|
Ben Laurie (Google)
2011/11/29 18:59:28
printf?
mattsh
2011/11/29 21:29:46
Done.
|
| + ASSERT(Dart_GetNativeArgumentCount(args) == 1); |
| + |
| + const char *path = NULL; |
| + Dart_StringToCString(Dart_GetNativeArgument(args, 0), &path); |
| + |
| + struct stat entry_info; |
| + int lstat_success = lstat(path, &entry_info); |
| + if (lstat_success != 0) { |
| + handleErrorNumber(args, errno); |
| + } else { |
| + bool isDirectory = false; |
| + if ((entry_info.st_mode & S_IFMT) == S_IFDIR) { |
| + isDirectory = true; |
| + } |
| + Dart_SetReturnValue(args, Dart_NewBoolean(isDirectory)); |
| + } |
| + Dart_ExitScope(); |
| +} |
| + |
| +void handleErrorNumber(Dart_NativeArguments args, int error_number) { |
| + const char *message; |
| + const int MAX_LEN = 100; |
| + char message_buffer[MAX_LEN]; |
| + switch (error_number) { |
| + case ENOENT: |
| + message = "No such file or directory"; |
| + break; |
| + case ENAMETOOLONG: |
| + message = "File name too long"; |
| + break; |
| + default: |
| + snprintf(message_buffer, MAX_LEN, "TODO - errno %d", errno); |
| + message = message_buffer; |
| + break; |
| + } |
| + Dart_SetReturnValue(args, Dart_NewString(message)); |
| +} |