JSFileSystem.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Core/ProcessUtils.h>
  23. #include "JSCore.h"
  24. #include "JSEventHelper.h"
  25. #include "JSVM.h"
  26. namespace Atomic
  27. {
  28. static int Atomic_SplitPath(duk_context* ctx)
  29. {
  30. String path = duk_require_string(ctx, 0);
  31. String pathName;
  32. String fileName;
  33. String ext;
  34. duk_push_object(ctx);
  35. SplitPath(path, pathName, fileName, ext);
  36. duk_push_string(ctx, pathName.CString());
  37. duk_put_prop_string(ctx, -2, "pathName");
  38. duk_push_string(ctx, fileName.CString());
  39. duk_put_prop_string(ctx, -2, "fileName");
  40. duk_push_string(ctx, ext.CString());
  41. duk_put_prop_string(ctx, -2, "ext");
  42. return 1;
  43. }
  44. static int Atomic_AddTrailingSlash(duk_context* ctx)
  45. {
  46. String path = duk_require_string(ctx, 0);
  47. duk_push_string(ctx, AddTrailingSlash(path).CString());
  48. return 1;
  49. }
  50. static int Atomic_GetExtenstion(duk_context* ctx)
  51. {
  52. String path = duk_require_string(ctx, 0);
  53. duk_push_string(ctx, GetExtension(path).CString());
  54. return 1;
  55. }
  56. static int Atomic_GetParentPath(duk_context* ctx)
  57. {
  58. String path = duk_require_string(ctx, 0);
  59. duk_push_string(ctx, GetParentPath(path).CString());
  60. return 1;
  61. }
  62. static int Atomic_GetPath(duk_context* ctx)
  63. {
  64. String path = duk_require_string(ctx, 0);
  65. duk_push_string(ctx, GetPath(path).CString());
  66. return 1;
  67. }
  68. static int FileSystem_ScanDir(duk_context* ctx)
  69. {
  70. duk_push_this(ctx);
  71. FileSystem* fs = js_to_class_instance<FileSystem>(ctx, -1, 0);
  72. if ( !duk_is_string(ctx, 0) || !duk_is_string(ctx, 1) ||
  73. !duk_is_number(ctx, 2) || !duk_is_boolean(ctx, 3))
  74. {
  75. duk_push_string(ctx, "FileSystem::ScanDir bad args");
  76. duk_throw(ctx);
  77. }
  78. const char* pathName = duk_to_string(ctx, 0);
  79. const char* filter = duk_to_string(ctx, 1);
  80. unsigned flags = duk_to_number(ctx, 2);
  81. bool recursive = duk_to_boolean(ctx, 3) ? true : false;
  82. Vector<String> result;
  83. fs->ScanDir(result, pathName, filter, flags, recursive);
  84. duk_push_array(ctx);
  85. for (unsigned i = 0; i < result.Size(); i++)
  86. {
  87. duk_push_string(ctx, result[i].CString());
  88. duk_put_prop_index(ctx, -2, i);
  89. }
  90. return 1;
  91. }
  92. void jsapi_init_filesystem(JSVM* vm)
  93. {
  94. duk_context* ctx = vm->GetJSContext();
  95. duk_get_global_string(ctx, "Atomic");
  96. duk_push_c_function(ctx, Atomic_SplitPath, 1);
  97. duk_put_prop_string(ctx, -2, "splitPath");
  98. duk_push_c_function(ctx, Atomic_AddTrailingSlash, 1);
  99. duk_put_prop_string(ctx, -2, "addTrailingSlash");
  100. duk_push_c_function(ctx, Atomic_GetPath, 1);
  101. duk_put_prop_string(ctx, -2, "getPath");
  102. duk_push_c_function(ctx, Atomic_GetParentPath, 1);
  103. duk_put_prop_string(ctx, -2, "getParentPath");
  104. duk_push_c_function(ctx, Atomic_GetExtenstion, 1);
  105. duk_put_prop_string(ctx, -2, "getExtension");
  106. duk_pop(ctx); // pop Atomic object
  107. js_class_get_prototype(ctx, "Atomic", "FileSystem");
  108. duk_push_c_function(ctx, FileSystem_ScanDir, 4);
  109. duk_put_prop_string(ctx, -2, "scanDir");
  110. duk_pop(ctx);
  111. }
  112. }