candle 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.md +4 -0
  3. data/VERSION +1 -0
  4. data/bin/candle +21 -0
  5. data/lib/candle.rb +68 -0
  6. data/lib/candle/command.rb +27 -0
  7. data/lib/candle/generators/actions.rb +183 -0
  8. data/lib/candle/generators/blank.rb +127 -0
  9. data/lib/candle/generators/cli.rb +55 -0
  10. data/lib/candle/generators/help.rb +47 -0
  11. data/lib/candle/generators/jam.rb +80 -0
  12. data/lib/candle/generators/lua/scripts/AppDelegate.lua +36 -0
  13. data/lib/candle/generators/lua/scripts/tests/init.lua +6 -0
  14. data/lib/candle/generators/lua/scripts/tests/someTest.lua +12 -0
  15. data/lib/candle/generators/lua/wax/ProtocolLoader.h +12 -0
  16. data/lib/candle/generators/lua/wax/bin/hammer +157 -0
  17. data/lib/candle/generators/lua/wax/bin/update-xibs +131 -0
  18. data/lib/candle/generators/lua/wax/bin/waxsim +0 -0
  19. data/lib/candle/generators/lua/wax/lib/build-scripts/compile-stdlib.sh +14 -0
  20. data/lib/candle/generators/lua/wax/lib/build-scripts/copy-scripts.sh +58 -0
  21. data/lib/candle/generators/lua/wax/lib/build-scripts/luac.lua +80 -0
  22. data/lib/candle/generators/lua/wax/lib/extensions/CGAffine/wax_CGTransform.h +12 -0
  23. data/lib/candle/generators/lua/wax/lib/extensions/CGAffine/wax_CGTransform.m +85 -0
  24. data/lib/candle/generators/lua/wax/lib/extensions/CGContext/wax_CGContext.h +12 -0
  25. data/lib/candle/generators/lua/wax/lib/extensions/CGContext/wax_CGContext.m +251 -0
  26. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http.h +14 -0
  27. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http.m +240 -0
  28. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http_connection.h +54 -0
  29. data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http_connection.m +304 -0
  30. data/lib/candle/generators/lua/wax/lib/extensions/filesystem/wax_filesystem.h +9 -0
  31. data/lib/candle/generators/lua/wax/lib/extensions/filesystem/wax_filesystem.m +273 -0
  32. data/lib/candle/generators/lua/wax/lib/extensions/json/Rakefile +10 -0
  33. data/lib/candle/generators/lua/wax/lib/extensions/json/wax_json.c +304 -0
  34. data/lib/candle/generators/lua/wax/lib/extensions/json/wax_json.h +11 -0
  35. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl-1.0.9.tar.gz +0 -0
  36. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_common.h +85 -0
  37. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_gen.h +159 -0
  38. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_parse.h +193 -0
  39. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl.c +159 -0
  40. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_alloc.c +65 -0
  41. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_alloc.h +50 -0
  42. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_buf.c +119 -0
  43. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_buf.h +73 -0
  44. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_bytestack.h +85 -0
  45. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_common.h +85 -0
  46. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_encode.c +188 -0
  47. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_encode.h +50 -0
  48. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_gen.c +322 -0
  49. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_gen.h +159 -0
  50. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_lex.c +737 -0
  51. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_lex.h +133 -0
  52. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parse.h +193 -0
  53. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parser.c +448 -0
  54. data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parser.h +82 -0
  55. data/lib/candle/generators/lua/wax/lib/lua/lapi.c +1087 -0
  56. data/lib/candle/generators/lua/wax/lib/lua/lapi.h +16 -0
  57. data/lib/candle/generators/lua/wax/lib/lua/lauxlib.c +652 -0
  58. data/lib/candle/generators/lua/wax/lib/lua/lauxlib.h +174 -0
  59. data/lib/candle/generators/lua/wax/lib/lua/lbaselib.c +653 -0
  60. data/lib/candle/generators/lua/wax/lib/lua/lcode.c +839 -0
  61. data/lib/candle/generators/lua/wax/lib/lua/lcode.h +76 -0
  62. data/lib/candle/generators/lua/wax/lib/lua/ldblib.c +397 -0
  63. data/lib/candle/generators/lua/wax/lib/lua/ldebug.c +638 -0
  64. data/lib/candle/generators/lua/wax/lib/lua/ldebug.h +33 -0
  65. data/lib/candle/generators/lua/wax/lib/lua/ldo.c +518 -0
  66. data/lib/candle/generators/lua/wax/lib/lua/ldo.h +57 -0
  67. data/lib/candle/generators/lua/wax/lib/lua/ldump.c +164 -0
  68. data/lib/candle/generators/lua/wax/lib/lua/lfunc.c +174 -0
  69. data/lib/candle/generators/lua/wax/lib/lua/lfunc.h +34 -0
  70. data/lib/candle/generators/lua/wax/lib/lua/lgc.c +711 -0
  71. data/lib/candle/generators/lua/wax/lib/lua/lgc.h +110 -0
  72. data/lib/candle/generators/lua/wax/lib/lua/linit.c +38 -0
  73. data/lib/candle/generators/lua/wax/lib/lua/liolib.c +553 -0
  74. data/lib/candle/generators/lua/wax/lib/lua/llex.c +461 -0
  75. data/lib/candle/generators/lua/wax/lib/lua/llex.h +81 -0
  76. data/lib/candle/generators/lua/wax/lib/lua/llimits.h +128 -0
  77. data/lib/candle/generators/lua/wax/lib/lua/lmathlib.c +263 -0
  78. data/lib/candle/generators/lua/wax/lib/lua/lmem.c +86 -0
  79. data/lib/candle/generators/lua/wax/lib/lua/lmem.h +49 -0
  80. data/lib/candle/generators/lua/wax/lib/lua/loadlib.c +666 -0
  81. data/lib/candle/generators/lua/wax/lib/lua/lobject.c +214 -0
  82. data/lib/candle/generators/lua/wax/lib/lua/lobject.h +381 -0
  83. data/lib/candle/generators/lua/wax/lib/lua/lopcodes.c +102 -0
  84. data/lib/candle/generators/lua/wax/lib/lua/lopcodes.h +268 -0
  85. data/lib/candle/generators/lua/wax/lib/lua/loslib.c +243 -0
  86. data/lib/candle/generators/lua/wax/lib/lua/lparser.c +1339 -0
  87. data/lib/candle/generators/lua/wax/lib/lua/lparser.h +82 -0
  88. data/lib/candle/generators/lua/wax/lib/lua/lstate.c +214 -0
  89. data/lib/candle/generators/lua/wax/lib/lua/lstate.h +169 -0
  90. data/lib/candle/generators/lua/wax/lib/lua/lstring.c +111 -0
  91. data/lib/candle/generators/lua/wax/lib/lua/lstring.h +31 -0
  92. data/lib/candle/generators/lua/wax/lib/lua/lstrlib.c +869 -0
  93. data/lib/candle/generators/lua/wax/lib/lua/ltable.c +588 -0
  94. data/lib/candle/generators/lua/wax/lib/lua/ltable.h +40 -0
  95. data/lib/candle/generators/lua/wax/lib/lua/ltablib.c +287 -0
  96. data/lib/candle/generators/lua/wax/lib/lua/ltm.c +75 -0
  97. data/lib/candle/generators/lua/wax/lib/lua/ltm.h +54 -0
  98. data/lib/candle/generators/lua/wax/lib/lua/lua.h +388 -0
  99. data/lib/candle/generators/lua/wax/lib/lua/luaconf.h +753 -0
  100. data/lib/candle/generators/lua/wax/lib/lua/lualib.h +53 -0
  101. data/lib/candle/generators/lua/wax/lib/lua/lundump.c +227 -0
  102. data/lib/candle/generators/lua/wax/lib/lua/lundump.h +36 -0
  103. data/lib/candle/generators/lua/wax/lib/lua/lvm.c +763 -0
  104. data/lib/candle/generators/lua/wax/lib/lua/lvm.h +36 -0
  105. data/lib/candle/generators/lua/wax/lib/lua/lzio.c +82 -0
  106. data/lib/candle/generators/lua/wax/lib/lua/lzio.h +67 -0
  107. data/lib/candle/generators/lua/wax/lib/lua/print.c +227 -0
  108. data/lib/candle/generators/lua/wax/lib/project.rake +159 -0
  109. data/lib/candle/generators/lua/wax/lib/stdlib/enums.lua +396 -0
  110. data/lib/candle/generators/lua/wax/lib/stdlib/ext/http.lua +43 -0
  111. data/lib/candle/generators/lua/wax/lib/stdlib/ext/init.lua +4 -0
  112. data/lib/candle/generators/lua/wax/lib/stdlib/ext/number.lua +21 -0
  113. data/lib/candle/generators/lua/wax/lib/stdlib/ext/string.lua +97 -0
  114. data/lib/candle/generators/lua/wax/lib/stdlib/ext/table.lua +165 -0
  115. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/WaxServer.lua +49 -0
  116. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/autoload.lua +10 -0
  117. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/base64.lua +64 -0
  118. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/bit.lua +274 -0
  119. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/cache.lua +73 -0
  120. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/callback.lua +22 -0
  121. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/frame.lua +76 -0
  122. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/init.lua +78 -0
  123. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/pickView.lua +54 -0
  124. data/lib/candle/generators/lua/wax/lib/stdlib/helpers/time.lua +102 -0
  125. data/lib/candle/generators/lua/wax/lib/stdlib/init.lua +18 -0
  126. data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/init.lua +2 -0
  127. data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/luamock.lua +84 -0
  128. data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/luaspec.lua +377 -0
  129. data/lib/candle/generators/lua/wax/lib/stdlib/repl.lua +9 -0
  130. data/lib/candle/generators/lua/wax/lib/stdlib/structs.lua +11 -0
  131. data/lib/candle/generators/lua/wax/lib/stdlib/waxClass.lua +42 -0
  132. data/lib/candle/generators/lua/wax/lib/wax.h +16 -0
  133. data/lib/candle/generators/lua/wax/lib/wax.m +260 -0
  134. data/lib/candle/generators/lua/wax/lib/wax_class.h +18 -0
  135. data/lib/candle/generators/lua/wax/lib/wax_class.m +190 -0
  136. data/lib/candle/generators/lua/wax/lib/wax_gc.h +20 -0
  137. data/lib/candle/generators/lua/wax/lib/wax_gc.m +56 -0
  138. data/lib/candle/generators/lua/wax/lib/wax_helpers.h +102 -0
  139. data/lib/candle/generators/lua/wax/lib/wax_helpers.m +870 -0
  140. data/lib/candle/generators/lua/wax/lib/wax_instance.h +34 -0
  141. data/lib/candle/generators/lua/wax/lib/wax_instance.m +810 -0
  142. data/lib/candle/generators/lua/wax/lib/wax_server.h +47 -0
  143. data/lib/candle/generators/lua/wax/lib/wax_server.m +252 -0
  144. data/lib/candle/generators/lua/wax/lib/wax_stdlib.h +3 -0
  145. data/lib/candle/generators/lua/wax/lib/wax_struct.h +26 -0
  146. data/lib/candle/generators/lua/wax/lib/wax_struct.m +335 -0
  147. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.pbxproj +836 -0
  148. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  149. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.xcworkspace/xcuserdata/eiffel.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  150. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/WaxApplication.xcscheme +86 -0
  151. data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/xcschememanagement.plist +22 -0
  152. data/lib/candle/generators/templates/blank/WaxApplication/Default-568h@2x.png +0 -0
  153. data/lib/candle/generators/templates/blank/WaxApplication/Default.png +0 -0
  154. data/lib/candle/generators/templates/blank/WaxApplication/Default@2x.png +0 -0
  155. data/lib/candle/generators/templates/blank/WaxApplication/ProtocolLoader.h +12 -0
  156. data/lib/candle/generators/templates/blank/WaxApplication/WaxApplication-Info.plist.tt +38 -0
  157. data/lib/candle/generators/templates/blank/WaxApplication/WaxApplication-Prefix.pch.tt +14 -0
  158. data/lib/candle/generators/templates/blank/WaxApplication/en.lproj/InfoPlist.strings +2 -0
  159. data/lib/candle/generators/templates/blank/WaxApplication/main.m.tt +20 -0
  160. data/lib/candle/tasks.rb +22 -0
  161. data/lib/candle/utility.rb +30 -0
  162. data/lib/candle/version.rb +9 -0
  163. data/lib/candle/view.rb +48 -0
  164. metadata +582 -0
@@ -0,0 +1,9 @@
1
+ //
2
+ // Created by Corey Johnson on 10/5/09.
3
+ // Copyright 2009 Probably Interactive. All rights reserved.
4
+ //
5
+
6
+ #import <Foundation/Foundation.h>
7
+ #import "lua.h"
8
+
9
+ int luaopen_wax_filesystem(lua_State *L);
@@ -0,0 +1,273 @@
1
+ //
2
+ // Created by Corey Johnson on 10/5/09.
3
+ // Copyright 2009 Probably Interactive. All rights reserved.
4
+ //
5
+
6
+ #import "wax_filesystem.h"
7
+
8
+ #import "wax_instance.h"
9
+ #import "wax_helpers.h"
10
+
11
+ #import "lua.h"
12
+ #import "lauxlib.h"
13
+
14
+ #define TABLE_NAME "wax.filesystem"
15
+
16
+ // wax.filesystem.search(searchPath, [pattern]) => table
17
+ // searchPath: string # where to start search (must be a directory)
18
+ // pattern: string # only include files that match the pattern
19
+ static int search(lua_State *L) {
20
+ NSString *searchPath = [NSString stringWithUTF8String:luaL_checkstring(L, 1)];
21
+ const char *searchPattern = nil;
22
+
23
+ if (lua_gettop(L) > 1) {
24
+ searchPattern = luaL_checkstring(L, 2);
25
+ }
26
+
27
+ NSFileManager *fm = [NSFileManager defaultManager];
28
+ NSError *error = nil;
29
+
30
+ NSArray *filenames = [fm subpathsOfDirectoryAtPath:searchPath error:&error];
31
+ NSMutableArray *paths = [NSMutableArray array];
32
+
33
+ for (NSString *filename in filenames) {
34
+ [paths addObject:[searchPath stringByAppendingPathComponent:filename]];
35
+ }
36
+
37
+ if (error) {
38
+ luaL_error(L, "Could not read directory path at '%s'. Error: %s", [searchPath UTF8String], [[error localizedDescription] UTF8String]);
39
+ }
40
+
41
+ if (searchPattern) {
42
+ NSMutableArray *matchedPaths = [NSMutableArray array];
43
+
44
+ lua_getglobal(L, "string");
45
+
46
+ for (NSString *path in paths) {
47
+ lua_getfield(L, -1, "match");
48
+ lua_pushstring(L, [path UTF8String]);
49
+ lua_pushstring(L, searchPattern);
50
+ lua_call(L, 2, 1);
51
+
52
+ if (!lua_isnil(L, -1)) {
53
+ [matchedPaths addObject:path];
54
+ }
55
+ lua_pop(L, 1);
56
+ }
57
+
58
+ lua_pop(L, 1); // pop string table off the stack
59
+
60
+ paths = matchedPaths;
61
+ }
62
+
63
+ wax_fromObjc(L, "@", &paths);
64
+
65
+ return 1;
66
+ }
67
+
68
+
69
+ // wax.filesystem.contents(path) => NSData | table
70
+ // path: string #if filepath is a directory, the result will be a table. A file will return NSData
71
+ static int contents(lua_State *L) {
72
+ NSString *path = [NSString stringWithUTF8String:luaL_checkstring(L, 1)];
73
+
74
+ NSFileManager *fm = [NSFileManager defaultManager];
75
+ BOOL directory;
76
+ BOOL exists = [fm fileExistsAtPath:path isDirectory:&directory];
77
+
78
+ if (!exists) {
79
+ lua_pushnil(L);
80
+ }
81
+ else if (directory) {
82
+ NSError *error = nil;
83
+ NSArray *contents = [fm contentsOfDirectoryAtPath:path error:&error];
84
+
85
+ if (error) {
86
+ wax_log(LOG_DEBUG, @"Could not get contents for directory at '%@'\n%@", path, [error localizedDescription]);
87
+ }
88
+
89
+ wax_fromObjc(L, "@", &contents);
90
+ }
91
+ else {
92
+ NSData *contents = [fm contentsAtPath:path];
93
+ wax_fromObjc(L, "@", &contents);
94
+ }
95
+
96
+ return 1;
97
+ }
98
+
99
+ static int basename(lua_State *L) {
100
+ const char *path = luaL_checkstring(L, 1);
101
+ char *basename = strrchr(path, '/');
102
+ lua_pushstring(L, basename ? basename + 1 : path);
103
+ return 1;
104
+ }
105
+
106
+ static int isDir(lua_State *L) {
107
+ NSString *path = [NSString stringWithUTF8String:luaL_checkstring(L, 1)];
108
+
109
+ NSFileManager *fm = [NSFileManager defaultManager];
110
+ BOOL directory;
111
+ BOOL exists = [fm fileExistsAtPath:path isDirectory:&directory];
112
+
113
+ lua_pushboolean(L, exists && directory);
114
+
115
+ return 1;
116
+ }
117
+
118
+ static int isFile(lua_State *L) {
119
+ NSString *path = [NSString stringWithUTF8String:luaL_checkstring(L, 1)];
120
+
121
+ NSFileManager *fm = [NSFileManager defaultManager];
122
+ BOOL directory;
123
+ BOOL exists = [fm fileExistsAtPath:path isDirectory:&directory];
124
+
125
+ lua_pushboolean(L, exists && !directory);
126
+
127
+ return 1;
128
+ }
129
+
130
+ static int exists(lua_State *L) {
131
+ NSString *path = [NSString stringWithUTF8String:luaL_checkstring(L, 1)];
132
+
133
+ NSFileManager *fm = [NSFileManager defaultManager];
134
+ BOOL exists = [fm fileExistsAtPath:path isDirectory:nil];
135
+
136
+ lua_pushboolean(L, exists);
137
+
138
+ return 1;
139
+ }
140
+
141
+ // wax.filesystem.attributes(path) => table
142
+ // path: string
143
+ //
144
+ // # contains some simple mappings to some attributes (just look at the code to find out what is mapped)
145
+ static int attributes(lua_State *L) {
146
+ NSString *path = [NSString stringWithUTF8String:luaL_checkstring(L, 1)];
147
+
148
+ NSFileManager *fm = [NSFileManager defaultManager];
149
+ NSError *error = nil;
150
+ NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[fm attributesOfItemAtPath:path error:&error]];
151
+
152
+ if (error) {
153
+ wax_log(LOG_DEBUG, @"No attributes found for '%@'\n%@", path, [error localizedDescription]);
154
+ lua_pushnil(L);
155
+ return 1;
156
+ }
157
+
158
+ // Make some helper accessors
159
+ NSTimeInterval modifiedAt = [[attributes objectForKey:NSFileModificationDate] timeIntervalSince1970];
160
+ [attributes setObject:[NSNumber numberWithDouble:modifiedAt] forKey:@"modifiedAt"];
161
+
162
+ NSTimeInterval createdAt = [[attributes objectForKey:NSFileCreationDate] timeIntervalSince1970];
163
+ [attributes setObject:[NSNumber numberWithDouble:createdAt] forKey:@"createdAt"];
164
+
165
+ NSNumber *fileSize = [attributes objectForKey:NSFileSize];
166
+ [attributes setObject:fileSize forKey:@"size"];
167
+
168
+ wax_fromObjc(L, "@", &attributes);
169
+
170
+ return 1;
171
+ }
172
+
173
+ // wax.filesystem.createFile(path, content) => boolean
174
+ // path: string # filepath
175
+ // content: NSData | string # contents of file
176
+ static int createFile(lua_State *L) {
177
+ NSString *path = [NSString stringWithUTF8String:luaL_checkstring(L, 1)];
178
+ NSData *data = nil;
179
+
180
+ if (lua_isstring(L, 2)) {
181
+ size_t contentLength;
182
+ const char *content = luaL_checklstring(L, 2, &contentLength);
183
+ data = [NSData dataWithBytesNoCopy:(char *)content length:contentLength freeWhenDone:NO];
184
+ }
185
+ else { // Assume it is an NSData object
186
+ wax_instance_userdata *instanceUserdata = (wax_instance_userdata *)luaL_checkudata(L, 2, WAX_INSTANCE_METATABLE_NAME);
187
+ data = instanceUserdata->instance;
188
+ }
189
+
190
+ NSError *error = nil;
191
+ BOOL success = [data writeToFile:path atomically:NO];
192
+
193
+ if (!success) {
194
+ wax_log(LOG_DEBUG, @"Could not create file at '%@'\n%@", path, [error localizedDescription]);
195
+ }
196
+
197
+ lua_pushboolean(L, success);
198
+
199
+ return 1;
200
+ }
201
+
202
+ // wax.filesystem.createDir(path, createParentDirs) => boolean
203
+ // path: string # dir path
204
+ // createParentDirs: boolean # (optional) should intermediate parent dirs be created÷
205
+ static int createDir(lua_State *L) {
206
+ NSString *path = [NSString stringWithUTF8String:luaL_checkstring(L, 1)];
207
+ BOOL createParentDirs = NO;
208
+ if (lua_gettop(L) > 1) createParentDirs = lua_toboolean(L, 2);
209
+
210
+ NSError *error = nil;
211
+ NSFileManager *fm = [NSFileManager defaultManager];
212
+ BOOL success = [fm createDirectoryAtPath:path withIntermediateDirectories:createParentDirs attributes:nil error:&error];
213
+
214
+ if (!success) {
215
+ wax_log(LOG_DEBUG, @"Could not create dir at '%@'\n%@", path, [error localizedDescription]);
216
+ }
217
+
218
+ lua_pushboolean(L, success);
219
+
220
+ return 1;
221
+ }
222
+
223
+
224
+ // wax.filesystem.delete(path, [force]) => boolean
225
+ // path: string # deletes the path or directory (EVEN IF DIRECTORY HAS CONTENTS)
226
+ static int delete(lua_State *L) {
227
+ NSString *path = [NSString stringWithUTF8String:luaL_checkstring(L, 1)];
228
+
229
+ BOOL success = YES;
230
+ NSFileManager *fm = [NSFileManager defaultManager];
231
+ if ([fm fileExistsAtPath:path]) {
232
+ NSError *error = nil;
233
+ success = [fm removeItemAtPath:path error:&error];
234
+
235
+ if (!success) {
236
+ wax_log(LOG_DEBUG, @"Could not delete file at '%@'\n%@", path, [error localizedDescription]);
237
+ }
238
+ }
239
+ else {
240
+ wax_log(LOG_DEBUG, @"Trying to delete path that does not exist '%@'", path);
241
+ }
242
+
243
+ lua_pushboolean(L, success);
244
+
245
+ return 1;
246
+ }
247
+
248
+ static const struct luaL_Reg functions[] = {
249
+ {"search", search},
250
+ {"contents", contents},
251
+ {"basename", basename},
252
+ {"exists", exists},
253
+ {"isDir", isDir},
254
+ {"isFile", isFile},
255
+
256
+ {"attributes", attributes},
257
+
258
+ {"createDir", createDir},
259
+ {"createFile", createFile},
260
+ {"delete", delete},
261
+
262
+ {NULL, NULL}
263
+ };
264
+
265
+ int luaopen_wax_filesystem(lua_State *L) {
266
+ BEGIN_STACK_MODIFY(L);
267
+
268
+ luaL_register(L, TABLE_NAME, functions);
269
+
270
+ END_STACK_MODIFY(L, 0)
271
+
272
+ return 1;
273
+ }
@@ -0,0 +1,10 @@
1
+ task :default do
2
+ sh 'tar zxvf yajl-1.0.9.tar.gz -- yajl-1.0.9/src/*.{c,h}'
3
+ rm_rf 'yajl'
4
+ mv 'yajl-1.0.9/src', 'yajl'
5
+ sh %[sed -i '' -e 's,include <yajl/yajl_common.h>,include "yajl_common.h",g' yajl/api/yajl_{parse,gen}.h]
6
+ ln_sf 'api/yajl_parse.h', 'yajl/'
7
+ ln_sf 'api/yajl_gen.h', 'yajl/'
8
+ ln_sf 'api/yajl_common.h', 'yajl/'
9
+ rm_rf 'yajl-1.0.9'
10
+ end
@@ -0,0 +1,304 @@
1
+ #include "wax_json.h"
2
+
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+ #include <stdbool.h>
6
+ #include <assert.h>
7
+
8
+ #include "lua.h"
9
+ #include "lauxlib.h"
10
+
11
+ static int parse(lua_State *L);
12
+ static int generate(lua_State *L);
13
+
14
+ static const struct luaL_Reg metaFunctions[] = {
15
+ {NULL, NULL}
16
+ };
17
+
18
+ static const struct luaL_Reg functions[] = {
19
+ {"parse", parse},
20
+ {"generate", generate},
21
+ {NULL, NULL}
22
+ };
23
+
24
+ int luaopen_wax_json(lua_State *L) {
25
+ luaL_newmetatable(L, JSON_METATABLE_NAME);
26
+ luaL_register(L, NULL, metaFunctions);
27
+ luaL_register(L, JSON_METATABLE_NAME, functions);
28
+
29
+ lua_pop(L, 2); // Remove tables from stack,
30
+
31
+ return 0;
32
+ }
33
+
34
+ #include "yajl/yajl_parse.h"
35
+ #include "yajl/yajl_lex.h"
36
+ #include "yajl/yajl_parser.h"
37
+ #include "yajl/yajl_bytestack.h"
38
+ #include "yajl/yajl_gen.h"
39
+ #include <string.h>
40
+
41
+ static yajl_gen gen;
42
+ static yajl_handle hand;
43
+ static yajl_bytestack state;
44
+ enum {
45
+ state_begin,
46
+ state_array,
47
+ state_hash
48
+ };
49
+
50
+ void push_hash_or_array(lua_State *L) {
51
+ switch (yajl_bs_current(state)) {
52
+ case state_hash:
53
+ lua_rawset(L, -3);
54
+ break;
55
+ case state_array:
56
+ lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
57
+ break;
58
+ }
59
+ }
60
+
61
+ static int push_null(void *ctx) {
62
+ lua_State *L = (lua_State *) ctx;
63
+ lua_pushnil(L);
64
+ push_hash_or_array(L);
65
+ return 1;
66
+ }
67
+
68
+ static int push_boolean(void *ctx, int boolean) {
69
+ lua_State *L = (lua_State *) ctx;
70
+ lua_pushboolean(L, boolean ? true : false);
71
+ push_hash_or_array(L);
72
+ return 1;
73
+ }
74
+
75
+ static int push_integer(void *ctx, long num) {
76
+ lua_State *L = (lua_State *) ctx;
77
+ lua_pushinteger(L, num);
78
+ push_hash_or_array(L);
79
+ return 1;
80
+ }
81
+
82
+ static int push_double(void *ctx, double num) {
83
+ lua_State *L = (lua_State *) ctx;
84
+ lua_pushnumber(L, num);
85
+ push_hash_or_array(L);
86
+ return 1;
87
+ }
88
+
89
+ static int push_string(void *ctx, const unsigned char *string, unsigned int len) {
90
+ lua_State *L = (lua_State *) ctx;
91
+ lua_pushlstring(L, (const char *)string, len);
92
+ push_hash_or_array(L);
93
+ return 1;
94
+ }
95
+
96
+ static int push_start_map(void *ctx) {
97
+ lua_State *L = (lua_State *) ctx;
98
+ yajl_bs_push(state, state_hash);
99
+ lua_newtable(L);
100
+ return 1;
101
+ }
102
+
103
+ static int push_map_key(void *ctx, const unsigned char *string, unsigned int len) {
104
+ lua_State *L = (lua_State *) ctx;
105
+ lua_pushlstring(L, (const char *)string, len);
106
+ return 1;
107
+ }
108
+
109
+ static int push_end_map(void *ctx) {
110
+ lua_State *L = (lua_State *) ctx;
111
+ yajl_bs_pop(state);
112
+ push_hash_or_array(L);
113
+ return 1;
114
+ }
115
+
116
+ static int push_start_array(void *ctx) {
117
+ lua_State *L = (lua_State *) ctx;
118
+ yajl_bs_push(state, state_array);
119
+ lua_newtable(L);
120
+ return 1;
121
+ }
122
+
123
+ static int push_end_array(void *ctx) {
124
+ lua_State *L = (lua_State *) ctx;
125
+ yajl_bs_pop(state);
126
+ push_hash_or_array(L);
127
+ return 1;
128
+ }
129
+
130
+ static yajl_callbacks callbacks = {
131
+ push_null,
132
+ push_boolean,
133
+ push_integer,
134
+ push_double,
135
+ NULL,
136
+ push_string,
137
+ push_start_map,
138
+ push_map_key,
139
+ push_end_map,
140
+ push_start_array,
141
+ push_end_array
142
+ };
143
+
144
+ static int parse_string(lua_State *L, const unsigned char* input, unsigned int len) {
145
+ yajl_parser_config cfg = { .allowComments = 1, .checkUTF8 = 0 };
146
+ yajl_status stat;
147
+ unsigned char* error = NULL;
148
+ char buffer[1024];
149
+
150
+ hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) L);
151
+ yajl_bs_init(state, &(hand->alloc));
152
+ yajl_bs_push(state, state_begin);
153
+
154
+ stat = yajl_parse(hand, input, len);
155
+ if (stat == yajl_status_ok || stat == yajl_status_insufficient_data) {
156
+ stat = yajl_parse_complete(hand);
157
+ }
158
+
159
+ if (stat != yajl_status_ok) {
160
+ error = yajl_get_error(hand, 1, input, len);
161
+ strncpy(buffer, (const char *)error, 1024);
162
+ yajl_free_error(hand, error);
163
+ yajl_bs_free(state);
164
+ yajl_free(hand);
165
+ lua_pushstring(L, "ERROR: Could not parse JSON string");
166
+ } else {
167
+ yajl_bs_free(state);
168
+ yajl_free(hand);
169
+ }
170
+
171
+ return 1;
172
+ }
173
+
174
+ static int parse(lua_State *L) {
175
+ size_t len;
176
+ const char *input = lua_tolstring(L, -1, &len);
177
+
178
+ if (!input) {
179
+ lua_pushstring(L, "ERROR: Could not parse JSON string");
180
+ return 1;
181
+ }
182
+
183
+ // Nothing to parse!
184
+ if (len == 0) {
185
+ lua_pushnil(L);
186
+ return 1;
187
+ }
188
+
189
+ return parse_string(L, (const unsigned char *)input, len);
190
+ }
191
+
192
+ void json_parseString(lua_State *L, const char *input) {
193
+ parse_string(L, (const unsigned char *)input, strlen(input));
194
+ }
195
+
196
+ void generate_value(yajl_gen gen, lua_State *L) {
197
+ yajl_gen_status stat;
198
+ int type = lua_type(L, -1);
199
+
200
+ switch (type) {
201
+ case LUA_TNIL:
202
+ case LUA_TNONE:
203
+ stat = yajl_gen_null(gen);
204
+ break;
205
+
206
+ case LUA_TBOOLEAN:
207
+ stat = yajl_gen_bool(gen, lua_toboolean(L, -1));
208
+ break;
209
+
210
+ case LUA_TNUMBER:
211
+ case LUA_TSTRING: {
212
+ size_t len;
213
+ const char *str;
214
+
215
+ if (type == LUA_TNUMBER) {
216
+ lua_pushvalue(L, -1); // copy of the number
217
+ str = lua_tolstring(L, -1, &len); // converts value on stack to a string
218
+ lua_pop(L, 1); // pop off copy
219
+ stat = yajl_gen_number(gen, str, (unsigned int)len);
220
+ } else {
221
+ str = lua_tolstring(L, -1, &len);
222
+ stat = yajl_gen_string(gen, (const unsigned char*)str, (unsigned int)len);
223
+ }
224
+ break;
225
+ }
226
+
227
+ case LUA_TTABLE: {
228
+ bool dictionary = false;
229
+ bool empty = true;
230
+
231
+ lua_pushvalue(L, -1); // Push the table reference on the top
232
+ lua_pushnil(L); /* first key */
233
+ while (lua_next(L, -2)) {
234
+ empty = false;
235
+ if (lua_type(L, -2) != LUA_TNUMBER) {
236
+ dictionary = true;
237
+ lua_pop(L, 2); // pop key and value off the stack
238
+ break;
239
+ }
240
+ else {
241
+ lua_pop(L, 1);
242
+ }
243
+ }
244
+
245
+ if (empty)
246
+ dictionary = true;
247
+
248
+ if (dictionary)
249
+ yajl_gen_map_open(gen);
250
+ else
251
+ yajl_gen_array_open(gen);
252
+
253
+ lua_pushnil(L); /* first key */
254
+ while (lua_next(L, -2)) {
255
+ if (dictionary) {
256
+ size_t len;
257
+ const char *str;
258
+ if (lua_type(L, -2) == LUA_TNUMBER) {
259
+ lua_pushvalue(L, -2); // copy of the number
260
+ str = lua_tolstring(L, -1, &len); // converts value on stack to a string
261
+ lua_pop(L, 1); // pop off copy
262
+ } else {
263
+ str = lua_tolstring(L, -2, &len);
264
+ }
265
+ stat = yajl_gen_string(gen, (const unsigned char*)str, (unsigned int)len);
266
+ }
267
+ generate_value(gen, L);
268
+ lua_pop(L, 1); // Pop off the value
269
+ }
270
+
271
+ if (dictionary)
272
+ yajl_gen_map_close(gen);
273
+ else
274
+ yajl_gen_array_close(gen);
275
+
276
+ lua_pop(L, 1); // Pop the table reference off
277
+ break;
278
+ }
279
+
280
+ case LUA_TFUNCTION:
281
+ case LUA_TUSERDATA:
282
+ case LUA_TTHREAD:
283
+ case LUA_TLIGHTUSERDATA:
284
+ default:
285
+ stat = yajl_gen_null(gen);
286
+ break;
287
+ }
288
+ }
289
+
290
+ static int generate(lua_State *L) {
291
+ const unsigned char * buf;
292
+ unsigned int len;
293
+ yajl_gen_config conf = { .beautify = 0, .indentString = " " };
294
+ gen = yajl_gen_alloc(&conf, NULL);
295
+
296
+ generate_value(gen, L);
297
+
298
+ yajl_gen_get_buf(gen, &buf, &len);
299
+ lua_pushlstring(L, (const char *)buf, len);
300
+
301
+ yajl_gen_clear(gen);
302
+ yajl_gen_free(gen);
303
+ return 1;
304
+ }