Skip to content

Instantly share code, notes, and snippets.

@alanjds
Last active August 29, 2015 14:06
Show Gist options
  • Save alanjds/2c128e4f03cb07870e4b to your computer and use it in GitHub Desktop.
Save alanjds/2c128e4f03cb07870e4b to your computer and use it in GitHub Desktop.
diff --git a/bootstrap/minimal/jni/main.c b/bootstrap/minimal/jni/main.c
index ca1a7de..252059e 100644
--- a/bootstrap/minimal/jni/main.c
+++ b/bootstrap/minimal/jni/main.c
@@ -1,5 +1,7 @@
#include <jni.h>
#include <errno.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <android/log.h>
#include <android_native_app_glue.h>
@@ -51,9 +51,27 @@ static PyObject *androidembed_log(PyObject *self, PyObject *args) {
Py_RETURN_NONE;
}
+static PyObject *androidembed_asset_extract(PyObject *self, PyObject *args) {
+ char *asset_fn = NULL;
+ char *dest_fn = NULL;
+ AAssetManager *am = g_state->activity->assetManager;
+
+ if (!PyArg_ParseTuple(args, "ss", &asset_fn, &dest_fn)) {
+ return NULL;
+ }
+
+ if (asset_extract(am, asset_fn, dest_fn) < 0) {
+ LOGW("Unable to extract '%s' to '%s'", asset_fn, dest_fn);
+ return;
+ }
+
+ Py_RETURN_NONE;
+}
+
static PyMethodDef AndroidEmbedMethods[] = {
{"log", androidembed_log, METH_VARARGS, "Log on android platform"},
{"poll", androidembed_poll, METH_VARARGS, "Poll the android events"},
+ {"asset_extract", androidembed_asset_extract, METH_VARARGS, "Extract assets"},
{NULL, NULL, 0, NULL}
};
@@ -79,7 +81,26 @@ PyMODINIT_FUNC initandroidembed(void) {
(void) Py_InitModule("androidembed", AndroidEmbedMethods);
}
+// From: http://stackoverflow.com/a/9210960/798575
+int mkpath(char* file_path, mode_t mode) {
+ assert(file_path && *file_path);
+ char* p;
+ for (p=strchr(file_path+1, '/'); p; p=strchr(p+1, '/')) {
+ *p='\0';
+ LOGI("Trying to create '%s'", file_path);
+ if (mkdir(file_path, mode)==-1) {
+ if (errno!=EEXIST) {
+ *p='/';
+ return -1;
+ }
+ }
+ *p='/';
+ }
+ return 0;
+}
+
int asset_extract(AAssetManager *am, char *src_file, char *dst_file) {
+ mkpath(dst_file, 0600);
FILE *fhd = fopen(dst_file, "wb");
if (fhd == NULL) {
LOGW("Unable to open descriptor for %s (errno=%d:%s)",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment