merging cockos wdl
[wdl/wdl-ol.git] / WDL / swell / mac_resgen.php
blobd9564c9d87f44f1598723cbad8bc0010bcfb1687
1 #!/usr/bin/php
2 <?
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 swell_rc2cpp_dialog($fp) // returns array with ["data"] and optionally ["error"]
19 fseek($fp,0,SEEK_SET);
20 $errstr="";
21 $retstr = "";
23 $retstr .= '#ifndef SWELL_DLG_SCALE_AUTOGEN' . "\n";
24 $retstr .= '#define SWELL_DLG_SCALE_AUTOGEN 1.7' . "\n";
25 $retstr .= '#endif' . "\n";
26 $retstr .= '#ifndef SWELL_DLG_FLAGS_AUTOGEN' . "\n";
27 $retstr .= '#define SWELL_DLG_FLAGS_AUTOGEN SWELL_DLG_WS_FLIPPED|SWELL_DLG_WS_NOAUTOSIZE' . "\n";
28 $retstr .= "#endif\n";
29 $retstr .= "\n";
31 $dlg_state=0; // 1 = before BEGIN, 2=after BEGIN
33 $dlg_name="";
34 $dlg_size_w=0;
35 $dlg_size_h=0;
36 $dlg_title = "";
37 $dlg_styles = "SWELL_DLG_FLAGS_AUTOGEN";
38 $dlg_contents="";
40 $next_line="";
41 for (;;)
43 if ($next_line != "") { $x=$next_line; $next_line =""; }
44 else if (!($x=fgets($fp))) break;
46 $y=trim($x);
47 if ($dlg_state>=2)
49 $dlg_contents .= $y . "\n";
50 if ($y == "END")
52 if ($dlg_state==2) $dlg_styles.="|SWELL_DLG_WS_OPAQUE";
53 $retstr .= "#ifndef SET_$dlg_name" . "_SCALE\n";
54 $retstr .= "#define SET_$dlg_name" . "_SCALE SWELL_DLG_SCALE_AUTOGEN\n";
55 $retstr .= "#endif\n";
56 $retstr .= "#ifndef SET_$dlg_name" . "_STYLE\n";
57 $retstr .= "#define SET_$dlg_name" . "_STYLE $dlg_styles\n";
58 $retstr .= "#endif\n";
59 $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";
60 $dlg_contents=str_replace("NOT WS_VISIBLE","SWELL_NOT_WS_VISIBLE",$dlg_contents);
61 $dlg_contents=str_replace("NOT\nWS_VISIBLE","SWELL_NOT_WS_VISIBLE",$dlg_contents);
62 $dlg_contents=str_replace("NOT \nWS_VISIBLE","SWELL_NOT_WS_VISIBLE",$dlg_contents);
63 $retstr .= $dlg_contents;
64 $retstr .= "SWELL_DEFINE_DIALOG_RESOURCE_END($dlg_name)\n\n\n";
65 $dlg_state=0;
67 else if (strlen($y)>1) $dlg_state=3;
69 else
71 $parms = explode(" ", $y);
72 if (count($parms) > 0)
74 if ($dlg_state == 0)
76 // if (substr($parms[0],0,8) == "IDD_PREF")
77 if (count($parms)>4 && ($parms[1] == 'DIALOGEX'||$parms[1] == 'DIALOG'))
79 $dlg_name=$parms[0];
80 $rdidx = 2;
81 if ($parms[$rdidx] == 'DISCARDABLE') $rdidx++;
82 while ($parms[$rdidx] == "" && $rdidx < count($parms)) $rdidx++;
83 $rdidx += 2;
84 $dlg_size_w = str_replace(",","",$parms[$rdidx++]);
85 $dlg_size_h = str_replace(",","",$parms[$rdidx++]);
86 if (count($parms) >= $rdidx && $dlg_size_w != "" && $dlg_size_h != "")
88 $dlg_title="";
89 $dlg_styles="SWELL_DLG_FLAGS_AUTOGEN";
90 $dlg_contents="";
91 $dlg_state=1;
93 else $errstr .= "WARNING: corrupted $dlg_name resource\n";
96 else if ($dlg_state == 1)
98 if ($parms[0] == "BEGIN")
100 $dlg_state=2;
101 $dlg_contents = $y ."\n";
103 else
105 if ($parms[0] == "CAPTION")
107 $dlg_title = str_replace("\"","",trim(substr($y,8)));
109 else if ($parms[0] == "STYLE" || $parms[0] == "EXSTYLE")
111 $rep=0;
112 for (;;)
114 $next_line = fgets($fp,4096);
115 if (!($next_line )) { $next_line=""; break; }
116 if (substr($next_line,0,1)==" " || substr($next_line,0,1)=="\t")
118 $y .= " " . trim($next_line);
119 $rep++;
120 $next_line="";
122 else break;
124 if ($rep) $parms = explode(" ", $y);
125 $opmode=0;
126 $rdidx=1;
127 while ($rdidx < count($parms))
129 if ($parms[$rdidx] == '|') { $opmode=0; }
130 else if ($parms[$rdidx] == 'NOT') { $opmode=1; }
131 else if ($parms[$rdidx] == 'WS_CHILD')
133 if (!$opmode) $dlg_styles .= "|SWELL_DLG_WS_CHILD";
135 else if ($parms[$rdidx] == 'WS_THICKFRAME')
137 if (!$opmode) $dlg_styles .= "|SWELL_DLG_WS_RESIZABLE";
139 else if ($parms[$rdidx] == 'WS_EX_ACCEPTFILES')
141 if (!$opmode) $dlg_styles .= "|SWELL_DLG_WS_DROPTARGET";
143 else $opmode=0;
144 $rdidx++;
152 if ($dlg_state != 0)
153 $errstr .= "WARNING: there may have been a truncated dialog resource ($dlg_name)\n";
155 $retstr .= "\n//EOF\n\n";
156 $rv = array();
157 $rv["data"] = $retstr;
158 $rv["error"] = $errstr;
159 return $rv;
162 function swell_rc2cpp_menu($fp) // returns array with ["data"] and optionally ["error"]
164 $retstr="";
165 $errstr="";
167 fseek($fp,0,SEEK_SET);
169 $menu_symbol="";
170 $menu_depth=0;
171 while (($x=fgets($fp)))
173 $a=strpos($x, "\"\"");
174 if ($a)
176 $b=strpos($x, "\"");
177 $c=strpos($x, "\"", $a+1);
178 if ($b && $c && $b < $a && $c > $a)
180 $x=str_replace("\"\"", "\\\"", $x);
184 $y=trim($x);
185 if ($menu_symbol == "")
187 $parms = explode(" ", $y);
188 $tok = "MENU";
189 if (count($parms) >= 2 && $parms[1] == $tok)
191 $menu_symbol = $parms[0];
192 $menu_depth=0;
193 $retstr .= "SWELL_DEFINE_MENU_RESOURCE_BEGIN($menu_symbol)\n";
196 else
198 if ($y == "END")
200 $menu_depth-=1;
201 if ($menu_depth == 0)
203 $retstr .= "SWELL_DEFINE_MENU_RESOURCE_END($menu_symbol)\n\n\n";
205 if ($menu_depth < 1) $menu_symbol="";
207 if ($menu_depth>0)
209 if (substr($y,-strlen(", HELP")) == ", HELP")
211 $x=substr(rtrim($x),0,-strlen(", HELP")) . "\n";
213 $retstr .= $x;
215 if ($y == "BEGIN") $menu_depth+=1;
219 $retstr .= "\n//EOF\n\n";
220 $rv = array();
221 $rv["data"] = $retstr;
222 $rv["error"] = $errstr;
224 return $rv;
227 if (count($argv)<2) die("usage: mac_resgen.php [--force] file.rc ...\n");
229 $x=1;
230 $forcemode = 0;
231 if ($argv[$x] == "--force") { $forcemode=1; $x++; }
233 $lp = dirname(__FILE__);
234 $proc=0;
235 $skipped=0;
236 $err=0;
237 for (; $x < count($argv); $x ++)
239 $srcfn = $argv[$x];
240 if (!stristr($srcfn,".rc") || !($fp = @fopen($srcfn,"r")))
242 $err++;
243 echo "$srcfn: not valid or not found!\n";
244 continue;
246 echo "$srcfn: ";
247 $ofnmenu = $srcfn . "_mac_menu";
248 $ofndlg = $srcfn . "_mac_dlg";
250 $res = swell_rc2cpp_dialog($fp);
251 $res2 = swell_rc2cpp_menu($fp);
252 fclose($fp);
253 if ($res["error"] != "" || $res2["error"] != "")
255 $err++;
256 echo "error";
257 if ($res["error"] != "") echo " dialog: " . $res["error"];
258 if ($res2["error"] != "") echo " menu: " . $res2["error"];
259 echo "\n";
260 continue;
262 $f="";
263 if ($forcemode || !file_exists($ofndlg) || file_get_contents($ofndlg) != $res["data"])
265 $f .= "dlg updated";
266 if (!file_put_contents($ofndlg,$res["data"],LOCK_EX)) { echo "error writing $ofndlg\n"; $err++; }
268 if ($forcemode || !file_exists($ofnmenu) || file_get_contents($ofnmenu) != $res2["data"])
270 if ($f != "") $f .= ", ";
271 $f .= "menu updated";
272 if (!file_put_contents($ofnmenu,$res2["data"],LOCK_EX)) { echo "error writing $ofnmenu\n"; $err++; }
276 if ($f) echo "$f\n";
277 else echo "skipped\n";
278 if ($f != "") $proc++;
279 else $skipped++;
281 echo "processed $proc, skipped $skipped, error $err\n";