| 131 | } |
| 132 | |
| 133 | CScriptArray *CScriptFileSystem::GetFiles() const |
| 134 | { |
| 135 | // Obtain a pointer to the engine |
| 136 | asIScriptContext *ctx = asGetActiveContext(); |
| 137 | asIScriptEngine *engine = ctx->GetEngine(); |
| 138 | |
| 139 | // TODO: This should only be done once |
| 140 | // TODO: This assumes that CScriptArray was already registered |
| 141 | asITypeInfo *arrayType = engine->GetTypeInfoByDecl("array<string>"); |
| 142 | |
| 143 | // Create the array object |
| 144 | CScriptArray *array = CScriptArray::Create(arrayType); |
| 145 | |
| 146 | #if defined(_WIN32) |
| 147 | // Windows uses UTF16 so it is necessary to convert the string |
| 148 | wchar_t bufUTF16[10000]; |
| 149 | string searchPattern = currentPath + "/*"; |
| 150 | MultiByteToWideChar(CP_UTF8, 0, searchPattern.c_str(), -1, bufUTF16, 10000); |
| 151 | |
| 152 | WIN32_FIND_DATAW ffd; |
| 153 | HANDLE hFind = FindFirstFileW(bufUTF16, &ffd); |
| 154 | if( INVALID_HANDLE_VALUE == hFind ) |
| 155 | return array; |
| 156 | |
| 157 | do |
| 158 | { |
| 159 | // Skip directories |
| 160 | if( (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) |
| 161 | continue; |
| 162 | |
| 163 | // Convert the file name back to UTF8 |
| 164 | char bufUTF8[10000]; |
| 165 | WideCharToMultiByte(CP_UTF8, 0, ffd.cFileName, -1, bufUTF8, 10000, 0, 0); |
| 166 | |
| 167 | // Add the file to the array |
| 168 | array->Resize(array->GetSize()+1); |
| 169 | ((string*)(array->At(array->GetSize()-1)))->assign(bufUTF8); |
| 170 | } |
| 171 | while( FindNextFileW(hFind, &ffd) != 0 ); |
| 172 | |
| 173 | FindClose(hFind); |
| 174 | #else |
| 175 | dirent *ent = 0; |
| 176 | DIR *dir = opendir(currentPath.c_str()); |
| 177 | while( (ent = readdir(dir)) != NULL ) |
| 178 | { |
| 179 | const string filename = ent->d_name; |
| 180 | |
| 181 | // Skip . and .. |
| 182 | if( filename[0] == '.' ) |
| 183 | continue; |
| 184 | |
| 185 | // Skip sub directories |
| 186 | const string fullname = currentPath + "/" + filename; |
| 187 | struct stat st; |
| 188 | if( stat(fullname.c_str(), &st) == -1 ) |
| 189 | continue; |
| 190 | if( (st.st_mode & S_IFDIR) != 0 ) |