Browse Source

Creates the directory for the asset's extraction on Android

Quentin01 8 years ago
parent
commit
cf68e03b21
1 changed files with 32 additions and 0 deletions
  1. 32 0
      port/AndroidJNI/AndroidJNIIOSystem.cpp

+ 32 - 0
port/AndroidJNI/AndroidJNIIOSystem.cpp

@@ -46,7 +46,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <android/api-level.h>
 #if __ANDROID__ and __ANDROID_API__ > 9 and defined(AI_CONFIG_ANDROID_JNI_ASSIMP_MANAGER_SUPPORT)
 
+#include <libgen.h>
 #include <stdlib.h>
+#include <sys/stat.h>
 #include <android/log.h>
 #include <android/asset_manager.h>
 #include <android/asset_manager_jni.h>
@@ -100,6 +102,27 @@ void AndroidJNIIOSystem::AndroidActivityInit(ANativeActivity* activity)
 	mApkAssetManager = activity->assetManager;
 }
 
+// ------------------------------------------------------------------------------------------------
+// Create the directory for the extracted resource
+static int mkpath(std::string path, mode_t mode)
+{
+    if (mkdir(path.c_str(), mode) == -1) {
+        switch(errno) {
+            case ENOENT:
+                if (mkpath(path.substr(0, path.find_last_of('/')), mode) == -1)
+                    return -1;
+                else
+                    return mkdir(path.c_str(), mode);
+            case EEXIST:
+                return 0;
+            default:
+                return -1;
+        }
+    }
+
+    return 0;
+}
+
 // ------------------------------------------------------------------------------------------------
 // Extracts android asset
 bool AndroidJNIIOSystem::AndroidExtractAsset(std::string name)
@@ -131,6 +154,15 @@ bool AndroidJNIIOSystem::AndroidExtractAsset(std::string name)
 		// Close
 		AAsset_close(asset);
 
+		// Prepare directory for output buffer
+		std::string directoryNewPath = newPath;
+		directoryNewPath = dirname(&directoryNewPath[0]);
+
+		if (mkpath(directoryNewPath, S_IRUSR | S_IWUSR | S_IXUSR) == -1) {
+			__android_log_print(ANDROID_LOG_ERROR, "assimp",
+					"Can not create the directory for the output file");
+		}
+
 		// Prepare output buffer
 		std::ofstream assetExtracted(newPath.c_str(),
 				std::ios::out | std::ios::binary);