| 1 |
#include <TChar.h> |
|---|
| 2 |
#include <string.h> |
|---|
| 3 |
#include <windows.h> |
|---|
| 4 |
#include "mm_jsapi.h" |
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
/** |
|---|
| 8 |
* convert URI to file path and return it. |
|---|
| 9 |
* caller must free it. |
|---|
| 10 |
*/ |
|---|
| 11 |
static unsigned short *uri2path(const unsigned short *uri) |
|---|
| 12 |
{ |
|---|
| 13 |
size_t len; |
|---|
| 14 |
unsigned short *path; |
|---|
| 15 |
size_t i; |
|---|
| 16 |
|
|---|
| 17 |
if (_tcsncmp(uri, TEXT("file:///"), 8) != 0) return NULL; |
|---|
| 18 |
|
|---|
| 19 |
len = _tcslen(uri); |
|---|
| 20 |
path = malloc(len * sizeof(TCHAR)); |
|---|
| 21 |
_stprintf_s(path, len, TEXT("\\\\?\\%s"), uri + 8); |
|---|
| 22 |
for (i = 0; i < len; ++i) { |
|---|
| 23 |
if (path[i] == '|') { |
|---|
| 24 |
path[i] = ':'; |
|---|
| 25 |
} else if (path[i] == '/') { |
|---|
| 26 |
path[i] = '\\'; |
|---|
| 27 |
} |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
return path; |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
/** |
|---|
| 34 |
* copy |
|---|
| 35 |
*/ |
|---|
| 36 |
static JSBool copy_inner(const unsigned short *fileURI, const unsigned short *copyURI) |
|---|
| 37 |
{ |
|---|
| 38 |
unsigned short *filepath, *copypath; |
|---|
| 39 |
JSBool rv; |
|---|
| 40 |
|
|---|
| 41 |
rv = JS_FALSE; |
|---|
| 42 |
filepath = uri2path(fileURI); |
|---|
| 43 |
copypath = uri2path(copyURI); |
|---|
| 44 |
|
|---|
| 45 |
if (filepath && copypath) { |
|---|
| 46 |
if (CopyFile(filepath, copypath, FALSE)) { |
|---|
| 47 |
rv = JS_TRUE; |
|---|
| 48 |
} |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
free(filepath); |
|---|
| 52 |
free(copypath); |
|---|
| 53 |
return rv; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
/** |
|---|
| 57 |
* decode encoded string using decode URI js function and return it. |
|---|
| 58 |
* caller must free return value. |
|---|
| 59 |
*/ |
|---|
| 60 |
static unsigned short *decodeURI(JSContext *cx, JSObject *obj, unsigned short *encoded) |
|---|
| 61 |
{ |
|---|
| 62 |
size_t js_len; |
|---|
| 63 |
unsigned short *js, *decoded; |
|---|
| 64 |
jsval decoded_jsval; |
|---|
| 65 |
unsigned int len; |
|---|
| 66 |
|
|---|
| 67 |
decoded = NULL; |
|---|
| 68 |
|
|---|
| 69 |
// size in words. |
|---|
| 70 |
js_len = _tcslen(encoded) + 100; |
|---|
| 71 |
|
|---|
| 72 |
js = malloc(js_len * sizeof(TCHAR)); |
|---|
| 73 |
_stprintf_s(js, js_len, TEXT("decodeURI(\"%s\")"), encoded); |
|---|
| 74 |
|
|---|
| 75 |
if (JS_ExecuteScript(cx, obj, js, 0, &decoded_jsval) != JS_FALSE) { |
|---|
| 76 |
decoded = JS_ValueToString(cx, decoded_jsval, &len); |
|---|
| 77 |
} |
|---|
| 78 |
free(js); |
|---|
| 79 |
|
|---|
| 80 |
return decoded; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
/** |
|---|
| 84 |
* return fileURI argument. |
|---|
| 85 |
* caller must free return value. |
|---|
| 86 |
*/ |
|---|
| 87 |
static unsigned short *get_fileURI(JSContext *cx, JSObject *obj, jsval *argv) |
|---|
| 88 |
{ |
|---|
| 89 |
unsigned int len; |
|---|
| 90 |
unsigned short *uri, *encoded_uri; |
|---|
| 91 |
|
|---|
| 92 |
uri = NULL; |
|---|
| 93 |
encoded_uri = JS_ValueToString(cx, argv[0], &len); |
|---|
| 94 |
if (encoded_uri) { |
|---|
| 95 |
uri = decodeURI(cx, obj, encoded_uri); |
|---|
| 96 |
} |
|---|
| 97 |
return uri; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
/** |
|---|
| 101 |
* return copyURI argument. |
|---|
| 102 |
* caller must free return value. |
|---|
| 103 |
*/ |
|---|
| 104 |
static unsigned short *get_copyURI(JSContext *cx, JSObject *obj, jsval *argv) |
|---|
| 105 |
{ |
|---|
| 106 |
unsigned int len; |
|---|
| 107 |
unsigned short *uri, *encoded_uri; |
|---|
| 108 |
|
|---|
| 109 |
uri = NULL; |
|---|
| 110 |
encoded_uri = JS_ValueToString(cx, argv[1], &len); |
|---|
| 111 |
if (encoded_uri) { |
|---|
| 112 |
uri = decodeURI(cx, obj, encoded_uri); |
|---|
| 113 |
} |
|---|
| 114 |
return uri; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
/** |
|---|
| 118 |
* Copy file. |
|---|
| 119 |
* TrueFLfile.copy(fileURI, copyURI) |
|---|
| 120 |
*/ |
|---|
| 121 |
JSBool copy(JSContext *cx, JSObject *obj, unsigned int argc, jsval *argv, jsval *rval) |
|---|
| 122 |
{ |
|---|
| 123 |
unsigned short *src, *dest; |
|---|
| 124 |
|
|---|
| 125 |
if (argc != 2) |
|---|
| 126 |
return JS_FALSE; |
|---|
| 127 |
|
|---|
| 128 |
src = get_fileURI(cx, obj, argv); |
|---|
| 129 |
dest = get_copyURI(cx, obj, argv); |
|---|
| 130 |
|
|---|
| 131 |
if (src && dest) { |
|---|
| 132 |
*rval = JS_BooleanToValue(copy_inner(src, dest)); |
|---|
| 133 |
} else { |
|---|
| 134 |
*rval = JS_BooleanToValue(JS_FALSE); |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
return JS_TRUE; |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
// MM_STATE is a macro that expands to some definitions that are |
|---|
| 142 |
// needed in order interact with Dreamweaver. This macro must be |
|---|
| 143 |
// defined exactly once in your library |
|---|
| 144 |
MM_STATE |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
// Flash calls MM_Init when your library is loaded |
|---|
| 148 |
void |
|---|
| 149 |
MM_Init() |
|---|
| 150 |
{ |
|---|
| 151 |
// file copy function |
|---|
| 152 |
JS_DefineFunction(_T("copy"), copy, 2); |
|---|
| 153 |
} |
|---|