1 |
/* |
2 |
* SaveROM - Save Mac ROM to file |
3 |
* |
4 |
* Copyright (C) 1998-2004 Christian Bauer |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
|
21 |
#include <AppKit.h> |
22 |
#include <InterfaceKit.h> |
23 |
#include <StorageKit.h> |
24 |
|
25 |
#include <stdio.h> |
26 |
#include <unistd.h> |
27 |
|
28 |
|
29 |
// Constants |
30 |
const char APP_SIGNATURE[] = "application/x-vnd.cebix-SaveROM"; |
31 |
const char ROM_FILE_NAME[] = "ROM"; |
32 |
|
33 |
// Global variables |
34 |
static uint8 buf[0x400000]; |
35 |
|
36 |
// Application object |
37 |
class SaveROM : public BApplication { |
38 |
public: |
39 |
SaveROM() : BApplication(APP_SIGNATURE) |
40 |
{ |
41 |
// Find application directory and cwd to it |
42 |
app_info the_info; |
43 |
GetAppInfo(&the_info); |
44 |
BEntry the_file(&the_info.ref); |
45 |
BEntry the_dir; |
46 |
the_file.GetParent(&the_dir); |
47 |
BPath the_path; |
48 |
the_dir.GetPath(&the_path); |
49 |
chdir(the_path.Path()); |
50 |
} |
51 |
virtual void ReadyToRun(void); |
52 |
}; |
53 |
|
54 |
|
55 |
/* |
56 |
* Create application object and start it |
57 |
*/ |
58 |
|
59 |
int main(int argc, char **argv) |
60 |
{ |
61 |
SaveROM *the_app = new SaveROM(); |
62 |
the_app->Run(); |
63 |
delete the_app; |
64 |
return 0; |
65 |
} |
66 |
|
67 |
|
68 |
/* |
69 |
* Display error alert |
70 |
*/ |
71 |
|
72 |
static void ErrorAlert(const char *text) |
73 |
{ |
74 |
BAlert *alert = new BAlert("SaveROM Error", text, "Quit", NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT); |
75 |
alert->Go(); |
76 |
} |
77 |
|
78 |
|
79 |
/* |
80 |
* Display OK alert |
81 |
*/ |
82 |
|
83 |
static void InfoAlert(const char *text) |
84 |
{ |
85 |
BAlert *alert = new BAlert("SaveROM Message", text, "Quit", NULL, NULL, B_WIDTH_AS_USUAL, B_INFO_ALERT); |
86 |
alert->Go(); |
87 |
} |
88 |
|
89 |
|
90 |
/* |
91 |
* Main program |
92 |
*/ |
93 |
|
94 |
void SaveROM::ReadyToRun(void) |
95 |
{ |
96 |
int fd = open("/dev/sheep", 0); |
97 |
if (fd < 0) { |
98 |
ErrorAlert("Cannot open '/dev/sheep'."); |
99 |
goto done; |
100 |
} |
101 |
|
102 |
if (read(fd, buf, 0x400000) != 0x400000) { |
103 |
ErrorAlert("Cannot read ROM."); |
104 |
close(fd); |
105 |
goto done; |
106 |
} |
107 |
|
108 |
FILE *f = fopen(ROM_FILE_NAME, "wb"); |
109 |
if (f == NULL) { |
110 |
ErrorAlert("Cannot open ROM file."); |
111 |
close(fd); |
112 |
goto done; |
113 |
} |
114 |
|
115 |
if (fwrite(buf, 1, 0x400000, f) != 0x400000) { |
116 |
ErrorAlert("Cannot write ROM."); |
117 |
fclose(f); |
118 |
close(fd); |
119 |
goto done; |
120 |
} |
121 |
|
122 |
InfoAlert("ROM saved."); |
123 |
|
124 |
fclose(f); |
125 |
close(fd); |
126 |
done: |
127 |
PostMessage(B_QUIT_REQUESTED); |
128 |
} |