Merge pull request #110 from tesselode/fixes
[wdl/wdl-ol.git] / WDL / swell / mac_resgen.php
blob36eb0da7583a0c6d61533628acf6cefaea3f17b8
1 #!/usr/bin/php
2 <?php
4 if (!function_exists('file_put_contents')) {
5 function file_put_contents($filename, $data, $lockflag) {
6 $f = @fopen($filename, 'w');
7 if (!$f) {
8 return false;
9 } else {
10 $bytes = fwrite($f, $data);
11 fclose($f);
12 return $bytes;
17 function convertquotes($in)
19 $ret = "";
20 $qs= 0;
21 for ($x=0;$x<strlen($in); $x++)
23 if ($in[$x] == '"')
25 if (!$qs)
27 $qs=1;
29 else
31 $qs=0;
32 if ($x < strlen($in) && $in[$x+1] == '"')
34 $ret .= "\\";
35 continue;
39 $ret .= $in[$x];
41 return $ret;
44 function utf_fgets($fp, &$state)
46 if ($state==0)
48 $state = -1;
49 $s = fread($fp,2);
50 if (ord($s[0])==0xff && ord($s[1]) == 0xfe) $state=1; // UTF-16LE
51 else if (ord($s[0])==0xfe && ord($s[1]) == 0xff) $state=2; // UTF-16BE
52 else if (ord($s[0])==0xef && ord($s[1]) == 0xbb)
54 $s2 = fread($fp,1);
55 if (ord($s2[0]) != 0xbf) return $s . $s2 . fgets($fp,4096);
58 if ($state < 0) return fgets($fp,4096);
60 $ret = "";
61 while ($s = fread($fp,2))
63 if ($state == 1) $s = ord($s[0]) | (ord($s[1])<<8);
64 else $s = ord($s[1]) | (ord($s[0])<<8);
65 if ($s < 128)
67 $ret .= chr($s);
68 if ($s == ord("\n")) break;
70 else if ($s <= 0x7ff) $ret .= chr(0xc0 | (($s>>6)&0x1f)) . chr(0x80 | ($s&0x3f));
71 else $ret .= chr(0xe0 | (($s>>12)&0xf)) . chr(0x80 | (($s)>>6)&0x3f) . chr(0x80 | ($s&0x3f));
74 return $ret;
77 function swell_rc2cpp_dialog($fp) // returns array with ["data"] and optionally ["error"]
79 fseek($fp,0,SEEK_SET);
80 $utf_state=0;
81 $errstr="";
82 $retstr = "";
84 $retstr .= '#ifndef SWELL_DLG_SCALE_AUTOGEN' . "\n";
85 $retstr .= '#ifdef __APPLE__' . "\n";
86 $retstr .= ' #define SWELL_DLG_SCALE_AUTOGEN 1.7' . "\n";
87 $retstr .= '#else' . "\n";
88 $retstr .= ' #define SWELL_DLG_SCALE_AUTOGEN 1.8' . "\n";
89 $retstr .= '#endif' . "\n";
90 $retstr .= '#endif' . "\n";
91 $retstr .= '#ifndef SWELL_DLG_FLAGS_AUTOGEN' . "\n";
92 $retstr .= '#define SWELL_DLG_FLAGS_AUTOGEN SWELL_DLG_WS_FLIPPED|SWELL_DLG_WS_NOAUTOSIZE' . "\n";
93 $retstr .= "#endif\n";
94 $retstr .= "\n";
96 $dlg_state=0; // 1 = before BEGIN, 2=after BEGIN
98 $dlg_name="";
99 $dlg_size_w=0;
100 $dlg_size_h=0;
101 $dlg_title = "";
102 $dlg_styles = "SWELL_DLG_FLAGS_AUTOGEN";
103 $dlg_contents="";
105 $next_line="";
106 for (;;)
108 if ($next_line != "") { $x=$next_line; $next_line =""; }
109 else if (!($x=utf_fgets($fp,$utf_state))) break;
110 $x = convertquotes($x);
112 $y=trim($x);
113 if ($dlg_state>=2)
115 if (preg_match("/^LTEXT(.*), *WS_EX_RIGHT$/",$y,$match)) $y = "RTEXT" . $match[1];
116 $dlg_contents .= $y . "\n";
117 if ($y == "END")
119 if ($dlg_state==2) $dlg_styles.="|SWELL_DLG_WS_OPAQUE";
120 $retstr .= "#ifndef SET_$dlg_name" . "_SCALE\n";
121 $retstr .= "#define SET_$dlg_name" . "_SCALE SWELL_DLG_SCALE_AUTOGEN\n";
122 $retstr .= "#endif\n";
123 $retstr .= "#ifndef SET_$dlg_name" . "_STYLE\n";
124 $retstr .= "#define SET_$dlg_name" . "_STYLE $dlg_styles\n";
125 $retstr .= "#endif\n";
126 $retstr .= "SWELL_DEFINE_DIALOG_RESOURCE_BEGIN($dlg_name,SET_$dlg_name" . "_STYLE,\"$dlg_title\",$dlg_size_w,$dlg_size_h,SET_$dlg_name" . "_SCALE)\n";
127 $dlg_contents=str_replace("NOT WS_VISIBLE","SWELL_NOT_WS_VISIBLE",$dlg_contents);
128 $dlg_contents=str_replace("NOT\nWS_VISIBLE","SWELL_NOT_WS_VISIBLE",$dlg_contents);
129 $dlg_contents=str_replace("NOT \nWS_VISIBLE","SWELL_NOT_WS_VISIBLE",$dlg_contents);
130 $retstr .= $dlg_contents;
131 $retstr .= "SWELL_DEFINE_DIALOG_RESOURCE_END($dlg_name)\n\n\n";
132 $dlg_state=0;
134 else if (strlen($y)>1) $dlg_state=3;
136 else
138 $parms = explode(" ", $y);
139 if (count($parms) > 0)
141 if ($dlg_state == 0)
143 // if (substr($parms[0],0,8) == "IDD_PREF")
144 if (count($parms)>4 && ($parms[1] == 'DIALOGEX'||$parms[1] == 'DIALOG'))
146 $dlg_name=$parms[0];
147 $rdidx = 2;
148 if ($parms[$rdidx] == 'DISCARDABLE') $rdidx++;
149 while ($parms[$rdidx] == "" && $rdidx < count($parms)) $rdidx++;
150 $rdidx += 2;
151 $dlg_size_w = str_replace(",","",$parms[$rdidx++]);
152 $dlg_size_h = str_replace(",","",$parms[$rdidx++]);
153 if (count($parms) >= $rdidx && $dlg_size_w != "" && $dlg_size_h != "")
155 $dlg_title="";
156 $dlg_styles="SWELL_DLG_FLAGS_AUTOGEN";
157 $dlg_contents="";
158 $dlg_state=1;
160 else $errstr .= "WARNING: corrupted $dlg_name resource\n";
163 else if ($dlg_state == 1)
165 if ($parms[0] == "BEGIN")
167 $dlg_state=2;
168 $dlg_contents = $y ."\n";
170 else
172 if ($parms[0] == "CAPTION")
174 $dlg_title = str_replace("\"","",trim(substr($y,8)));
176 else if ($parms[0] == "STYLE" || $parms[0] == "EXSTYLE")
178 $rep=0;
179 for (;;)
181 $next_line = utf_fgets($fp,$utf_state);
182 if (!($next_line )) { $next_line=""; break; }
183 if (substr($next_line,0,1)==" " || substr($next_line,0,1)=="\t")
185 $y .= " " . trim(convertquotes($next_line));
186 $rep++;
187 $next_line="";
189 else break;
191 if ($rep) $parms = explode(" ", $y);
192 $opmode=0;
193 $rdidx=1;
194 while ($rdidx < count($parms))
196 if ($parms[$rdidx] == '|') { $opmode=0; }
197 else if ($parms[$rdidx] == 'NOT') { $opmode=1; }
198 else if ($parms[$rdidx] == 'WS_CHILD')
200 if (!$opmode) $dlg_styles .= "|SWELL_DLG_WS_CHILD";
202 else if ($parms[$rdidx] == 'WS_THICKFRAME')
204 if (!$opmode) $dlg_styles .= "|SWELL_DLG_WS_RESIZABLE";
206 else if ($parms[$rdidx] == 'WS_EX_ACCEPTFILES')
208 if (!$opmode) $dlg_styles .= "|SWELL_DLG_WS_DROPTARGET";
210 else $opmode=0;
211 $rdidx++;
219 if ($dlg_state != 0)
220 $errstr .= "WARNING: there may have been a truncated dialog resource ($dlg_name)\n";
222 $retstr .= "\n//EOF\n\n";
223 $rv = array();
224 $rv["data"] = $retstr;
225 $rv["error"] = $errstr;
226 return $rv;
229 function swell_rc2cpp_menu($fp) // returns array with ["data"] and optionally ["error"]
231 $retstr="";
232 $errstr="";
234 fseek($fp,0,SEEK_SET);
235 $utf_state=0;
237 $menu_symbol="";
238 $menu_depth=0;
239 while (($x=utf_fgets($fp,$utf_state)))
241 $x = convertquotes($x);
243 $y=trim($x);
244 if ($menu_symbol == "")
246 $parms = explode(" ", $y);
247 $tok = "MENU";
248 if (count($parms) >= 2 && $parms[1] == $tok)
250 $menu_symbol = $parms[0];
251 $menu_depth=0;
252 $retstr .= "SWELL_DEFINE_MENU_RESOURCE_BEGIN($menu_symbol)\n";
255 else
257 if ($y == "END")
259 $menu_depth-=1;
260 if ($menu_depth == 0)
262 $retstr .= "SWELL_DEFINE_MENU_RESOURCE_END($menu_symbol)\n\n\n";
264 if ($menu_depth < 1) $menu_symbol="";
266 if ($menu_depth>0)
268 if (substr($y,-strlen(", HELP")) == ", HELP")
270 $x=substr(rtrim($x),0,-strlen(", HELP")) . "\n";
272 $retstr .= $x;
274 if ($y == "BEGIN") $menu_depth+=1;
278 $retstr .= "\n//EOF\n\n";
279 $rv = array();
280 $rv["data"] = $retstr;
281 $rv["error"] = $errstr;
283 return $rv;
286 if (count($argv)<2) die("usage: mac_resgen.php [--force] file.rc ...\n");
288 $x=1;
289 $forcemode = 0;
290 if ($argv[$x] == "--force") { $forcemode=1; $x++; }
292 $lp = dirname(__FILE__);
293 $proc=0;
294 $skipped=0;
295 $err=0;
296 $quiet=0;
297 for (; $x < count($argv); $x ++)
299 $srcfn = $argv[$x];
300 if ($srcfn == "--quiet")
302 $quiet = 1;
303 continue;
305 if (!stristr($srcfn,".rc") || !($fp = @fopen($srcfn,"r")))
307 $err++;
308 echo "$srcfn: not valid or not found!\n";
309 continue;
311 $ofnmenu = $srcfn . "_mac_menu";
312 $ofndlg = $srcfn . "_mac_dlg";
314 $res = swell_rc2cpp_dialog($fp);
315 $res2 = swell_rc2cpp_menu($fp);
316 fclose($fp);
317 if ($res["error"] != "" || $res2["error"] != "")
319 $err++;
320 echo "$srcfn: error";
321 if ($res["error"] != "") echo " dialog: " . $res["error"];
322 if ($res2["error"] != "") echo " menu: " . $res2["error"];
323 echo "\n";
324 continue;
326 $f="";
327 if ($forcemode || !file_exists($ofndlg) || file_get_contents($ofndlg) != $res["data"])
329 $f .= "dlg updated";
330 if (!file_put_contents($ofndlg,$res["data"],LOCK_EX)) { echo "error writing $ofndlg\n"; $err++; }
332 if ($forcemode || !file_exists($ofnmenu) || file_get_contents($ofnmenu) != $res2["data"])
334 if ($f != "") $f .= ", ";
335 $f .= "menu updated";
336 if (!file_put_contents($ofnmenu,$res2["data"],LOCK_EX)) { echo "error writing $ofnmenu\n"; $err++; }
339 if ($f != "")
341 $proc++;
343 else
345 $skipped++;
346 $f = "skipped";
349 if (!$quiet) echo "$srcfn: $f\n";
351 if (!$quiet || $proc || $err) echo "processed $proc, skipped $skipped, error $err\n";