aboutsummaryrefslogtreecommitdiff
path: root/tools/iDSK/src/Main.cpp
blob: 1bbe6d4b5152be3efcf49171da7cb46774526150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm> // pour contourner un bug de std::vector ...

#include "getopt_pp.h" /* Command line handling */

using namespace std;

#include "MyType.h"
#include "GestDsk.h"
#include "Outils.h"
#include "Main.h"
#include "endianPPC.h"
#include "ViewFile.h"

int main(int argc, char **argv)
{
	bool IsDskLoc, IsDskSet,
		ModeListDsk, ModeImportFile,
		ModeRemoveFile,
		ModeDisaFile, ModeListBasic,
		ModeListDams, ModeListHex,
		ModeGetFile, ModeNewDsk, Force_Overwrite,
		Read_only, System_file, Split_lines, ModeListAscii, NoOptionSet;

	ModeListDsk = ModeImportFile = ModeListAscii =
		ModeRemoveFile = ModeDisaFile =
			ModeListBasic = ModeListDams = ModeListHex = ModeNewDsk =
				ModeGetFile = IsDskLoc = IsDskSet = Force_Overwrite = Read_only = System_file =  false;
	NoOptionSet = true;			

	string DskFile, AmsdosFile;
	vector<string> AmsdosFileList;

	int exeAdress = 0, loadAdress = 0, AmsdosType = 1, UserNumber = 0;

	DSK MyDsk;

	IsDsk = IsDskValid = false;
	IsDskSaved = true;

	// Récupération des arguments avec getopt_pp
	{
		using namespace GetOpt;
		GetOpt_pp opts(argc, argv);

		opts >> GlobalOption(DskFile);
		if (DskFile != "")
			IsDskSet = true;

		opts >> OptionPresent('l', "list", ModeListDsk)

			>> OptionPresent('i', "import", ModeImportFile) >> Option('i', "import", AmsdosFileList)

			>> OptionPresent('r', "remove", ModeRemoveFile) >> Option('r', "remove", AmsdosFileList)

			>> OptionPresent('n', "new", ModeNewDsk)

			>> OptionPresent('z', "disassemble", ModeDisaFile) >> Option('z', "disassemble", AmsdosFileList)

			>> OptionPresent('a', "ascii", ModeListAscii) >> Option('a', "ascii", AmsdosFileList)

			>> OptionPresent('b', "basic", ModeListBasic) >> Option('b', "basic", AmsdosFileList)

			>> OptionPresent('d', "dams", ModeListDams) >> Option('d', "dams", AmsdosFileList)

			>> OptionPresent('h', "hex", ModeListHex) >> Option('h', "hex", AmsdosFileList)

			>> std::hex >> Option('e', "exec", exeAdress) >> Option('c', "load", loadAdress) >> std::dec >> Option('t', "type", AmsdosType)

			>> OptionPresent('g', "get", ModeGetFile) >> Option('g', "get", AmsdosFileList)

			>> OptionPresent('f', "force", Force_Overwrite) >> OptionPresent('o', "write-protect", Read_only) >> OptionPresent('s', "system", System_file) >> OptionPresent('p', "split-lines", Split_lines) >> Option('u', "user", UserNumber);

		if (opts.options_remain())
		{
			cout << "Unhandled option ! Check the syntax." << endl;
			exit(EXIT_FAILURE);
		}

	} //namespace getopt

	if (!IsDskSet)
	{
		cerr << "You did not select a DSK file to work with !" << endl;
		help();
	}
	else
		cerr << "DSK : " << DskFile << endl;

	if (ModeListBasic || ModeListHex || ModeListDams || ModeDisaFile || ModeListAscii)
	{
		NoOptionSet = false;
		if (!MyDsk.ReadDsk(DskFile))
		{
			cerr << "Error reading file (" << DskFile << ")." << endl;
			exit(EXIT_FAILURE);
		}
		if (!MyDsk.CheckDsk())
		{
			cerr << "Fichier image non supporté (" << DskFile << ")." << endl;
			exit(EXIT_FAILURE);
		}
		int Indice;
		for (vector<string>::iterator iter = AmsdosFileList.begin(); iter != AmsdosFileList.end(); iter++)
		{
			char *amsdosF = GetNomAmsdos(iter->c_str());
			cerr << "Amsdos file : " << amsdosF << endl;
			if ((Indice = MyDsk.FileIsIn(amsdosF)) < 0)
			{
				cerr << "Error: File " << amsdosF << " not found." << endl;
				exit(EXIT_FAILURE);
			}
			MyDsk.OnViewFic(Indice);

			if (ModeListBasic)
				cout << ViewBasic(Split_lines) << endl;
			else if (ModeListDams)
				cout << "Not yet coded ! Please try a newer version of iDSK ! Sorry !" << endl;
			else if (ModeListHex)
			{
				MyDsk.Hexdecimal();
				cout << Listing << endl;
			}
			else if (ModeDisaFile)
				cout << ViewDesass() << endl;
			else if (ModeListAscii)
				cout << ViewAscii() << endl;
		}
	}

	if (ModeNewDsk)
	{
		NoOptionSet = false;
		MyDsk.FormatDsk(9, 42);
		if (!MyDsk.WriteDsk(DskFile))
		{
			cerr << "Error writing file " << DskFile << endl;
			exit(EXIT_FAILURE);
		}
	}

	

	if (ModeImportFile)
	{ // Ajouter fichiers sur dsk
		NoOptionSet = false;
		if (!MyDsk.ReadDsk(DskFile))
		{
			cerr << "Error reading file (" << DskFile << ")." << endl;
			exit(EXIT_FAILURE);
		}

		if (!MyDsk.CheckDsk())
		{
			cerr << "Unsupported dsk file (" << DskFile << ")." << endl;
			exit(EXIT_FAILURE);
		}

		for (vector<string>::iterator iter = AmsdosFileList.begin(); iter != AmsdosFileList.end(); iter++)
		{
			string amsdosfile = GetNomAmsdos(iter->c_str());
			int Indice;
			// Ensure Indice is valid (the file is in the disk already) before atempting to remove it
			if ((Indice = MyDsk.FileIsIn(amsdosfile)) != -1)
			{
				if (!Force_Overwrite)
				{
					cerr << "(" << amsdosfile << ") File exists, replace ? (Y/N) (try -f switch for autoreplace...):";
					string answer;
					cin >> answer;
					if (toupper(answer[0]) == 'Y')
						MyDsk.RemoveFile(Indice);
					else
					{
						cerr << "Import cancelled, dsk unchanged." << endl;
						cout << MyDsk.ReadDskDir();
						exit(EXIT_SUCCESS);
					}
				}
				else
					MyDsk.RemoveFile(Indice);
			}
			cerr << "Amsdos file : " << *iter << endl;

			MyDsk.PutFileInDsk(*iter, AmsdosType, loadAdress, exeAdress, UserNumber, System_file, Read_only);
		}
		if (MyDsk.WriteDsk(DskFile))
			cout << MyDsk.ReadDskDir();
		else
			cerr << "Error writing file : " << DskFile << endl;
	}

	if (ModeRemoveFile)
	{
		NoOptionSet = false;
		if (!MyDsk.ReadDsk((char *)DskFile.c_str()))
		{
			cerr << "Error reading file (" << DskFile << ")." << endl;
			exit(EXIT_FAILURE);
		}
		if (!MyDsk.CheckDsk())
		{
			cerr << "unsupported DSK file (" << DskFile << ")." << endl;
			exit(EXIT_FAILURE);
		}
		int Indice;
		for (vector<string>::iterator iter = AmsdosFileList.begin(); iter != AmsdosFileList.end(); iter++)
		{
			char *amsdosF = GetNomAmsdos(iter->c_str());
			cerr << "Amsdos file : " << amsdosF << endl;
			if ((Indice = MyDsk.FileIsIn(amsdosF)) < 0)
			{
				cerr << "Error : file " << amsdosF << " not found." << endl;
				exit(EXIT_FAILURE);
			}
			MyDsk.RemoveFile(Indice);
			if (MyDsk.WriteDsk((char *)DskFile.c_str()))
				cout << MyDsk.ReadDskDir();
			else
				cerr << "Error writing file " << (*iter) << endl;
		}
	}

	if (ModeGetFile)
	{
		NoOptionSet = false;
		if (!MyDsk.ReadDsk((char *)DskFile.c_str()))
		{
			cerr << "Error reading dskfile (" << DskFile << ")." << endl;
			exit(EXIT_FAILURE);
		}
		if (!MyDsk.CheckDsk())
		{
			cerr << "Unsupported dsk (" << DskFile << ")." << endl;
			exit(EXIT_FAILURE);
		}
		int Indice;

		for (vector<string>::iterator iter = AmsdosFileList.begin(); iter != AmsdosFileList.end(); iter++)
		{
			char *amsdosF = GetNomAmsdos(iter->c_str());
			cerr << "Fichier Amsdos : " << amsdosF << endl;
			if ((Indice = MyDsk.FileIsIn(amsdosF)) < 0)
			{
				cerr << "Error : file " << amsdosF << " not found." << endl;
				exit(EXIT_FAILURE);
			}
			if (!MyDsk.GetFileInDsk((char *)(*iter).c_str(), Indice))
			{
				cerr << "System error : unable to copy (" << AmsdosFile << ")." << endl;
				exit(EXIT_FAILURE);
			}
		}
	}
	if (ModeListDsk || NoOptionSet)
	{ // lire Dsk
		if (!MyDsk.ReadDsk(DskFile))
		{
			cerr << "Error reading file (" << DskFile << ")." << endl;
			exit(EXIT_FAILURE);
		}
		if (!MyDsk.CheckDsk())
		{
			cerr << "Unsupported dsk file (" << DskFile << ")." << endl;
			exit(EXIT_FAILURE);
		}
		cout << MyDsk.ReadDskDir();
	}
	
	cerr << "------------------------------------" << endl;

	return (EXIT_SUCCESS);
}

void help(void)
{
	cout << endl;
	cout << "--------------------------------------------------------------------------------" << endl;
	cout << "################################################################################" << endl;
	cout << VERSION << " (by Demoniak, Sid, PulkoMandy), http://github.com/cpcsdk " << endl;
	cout << "################################################################################" << endl;
	cout << endl;
	cout << "Usage : " << endl;
	cout << "\t" << PROGNAME << " <DSKfile> [OPTIONS] [files to process]" << endl;
	cout << "OPTIONS :                              EXAMPLE" << endl;
	cout << "-l : List disk catalog                 iDSK floppy.dsk -l (default option is no option is set)" << endl;
	cout << "-g : export ('Get') file               iDSK floppy.dsk -g myprog.bas" << endl;
	cout << "-r : Remove file                       iDSK floppy.dsk -r myprog.bas" << endl;
	cout << "-n : create New dsk file               iDSK floppy2.dsk -n" << endl;
	cout << "-z : disassemble a binary file         iDSK floppy.dsk -z myprog.bin" << endl;
	cout << "-b : list a Basic file                 iDSK floppy.dsk -b myprog.bas" << endl
		 << "-p : split lines after 80 char             ... -p" << endl;
	cout << "-a : list a Ascii file                 iDSK floppy.dsk -a myprog.txt" << endl;
	cout << "-d : list a Dams file                  iDSK floppy.dsk -d myprog.dms" << endl;
	cout << "-h : list a binary file as Hexadecimal iDSK floppy.dsk -h myprog.bin" << endl;
	cout << "-i : Import file                       iDSK floppy.dsk -i myprog.bas" << endl
		 << " -t : fileType (0=ASCII/1=BINARY/2=raw)     ... -t 1" << endl;
	cout << " -e : hex Execute address of file           ... -e C000 -t 1" << endl;
	cout << " -c : hex loading address of file           ... -e C000 -c 4000 -t 1" << endl;
	cout << " -f : Force overwriting if file exists      ... -f" << endl
		 << " -o : insert a read-Only file               ... -o" << endl
		 << " -s : insert a System file                  ... -s" << endl
		 << " -u : insert file with User number          ... -u 3" << endl;
	cout << "--------------------------------------------------------------------------------" << endl;
	cout << "Please report bugs ! - Demoniak/Sid/PulkoMandy" << endl;
	exit(0);
}