summaryrefslogtreecommitdiff
path: root/cd-tool.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cd-tool.cpp')
-rw-r--r--cd-tool.cpp162
1 files changed, 64 insertions, 98 deletions
diff --git a/cd-tool.cpp b/cd-tool.cpp
index 35e550f..487a05f 100644
--- a/cd-tool.cpp
+++ b/cd-tool.cpp
@@ -30,6 +30,8 @@
#include "cdreader.h"
#include "cdutils.h"
#include "generic.h"
+#include "Main.h"
+#include "cdabstract.h"
int lga = 0;
struct option long_options[] = {
@@ -40,10 +42,24 @@ struct option long_options[] = {
{0, 0, NULL, 0 }
};
+CODE_BEGINS
+public:
+Appli() : cdutil(0), iso_r(0), iso_w(0), file(0) {}
+virtual ~Appli() {
+ delete cdutil;
+ delete iso_r;
+ delete iso_w;
+ delete file;
+}
+private:
+
+cdutils * cdutil;
+Handle * iso_r, * iso_w, * file;
+
void showhelp(void) {
printm(M_BARE,
"Usage:\n"
-"cd-tool [-m <mode>] [-p <ppf file>] [-f] <isofile> <command> [command args]\n"
+"%s [-m <mode>] [-p <ppf file>] [-f] <isofile> <command> [command args]\n"
"Where mode can be 1 for MODE 1, 2 for MODE 2, 3 for MODE 2 FORM 1,\n"
"4 for MODE 2 FORM 2 and 5 for autodetect, which is the default behaviour.\n"
"\n"
@@ -55,12 +71,11 @@ void showhelp(void) {
" extract <file> <addr> <size> - extract some sectors to <file>\n"
" insert-file <file> <path> - insert the file to <path>\n"
" insert <file> <addr> - insert some sectors at <addr>\n"
-"\n");
+"\n", argv[0]);
}
-int Main(int argc, char ** argv) {
+virtual int startup() throw (GeneralException) {
int type = GUESS, c, size, force = 0, sector;
- Handle * iso_r, * iso_w = 0;
char * ppf = 0, * iso_name = 0, * arg1 = 0, * arg2 = 0, * f;
verbosity = M_WARNING;
@@ -71,7 +86,7 @@ int Main(int argc, char ** argv) {
case 'H':
case '?':
showhelp();
- exit(0);
+ throw Exit(0);
case 'm':
type = atoi(optarg);
break;
@@ -87,7 +102,7 @@ int Main(int argc, char ** argv) {
if (argc == optind) {
showhelp();
printm(M_ERROR, "Need an iso filename to work on.\n");
- exit(-1);
+ throw Exit(-1);
}
iso_name = argv[optind++];
@@ -95,120 +110,107 @@ int Main(int argc, char ** argv) {
if (argc == optind) {
showhelp();
printm(M_ERROR, "Need a command to execute.\n");
- exit(-1);
+ throw Exit(-1);
}
- iso_r = new Input(iso_name);
+ iso_r = open_iso(iso_name);
- get_iso_infos(iso_r);
+ cdutil = new cdutils(iso_r);
+
+ if (!cdutil->get_iso_infos())
+ throw Exit(-1);
if (!strcmp(argv[optind], "infos")) {
- show_iso_infos(iso_r);
+ cdutil->show_iso_infos();
} else if (!strcmp(argv[optind], "path")) {
- show_pt_infos(iso_r);
+ cdutil->show_pt_infos();
} else if (!strcmp(argv[optind], "printdir")) {
- struct DirEntry dir;
+ cdutils::DirEntry dir;
optind++;
if ((argc - 1) != optind) {
showhelp();
printm(M_ERROR, "printdir needs one argument.\n");
- delete iso_r;
- exit(-1);
+ throw Exit(-1);
}
arg1 = argv[optind];
- dir = find_path(iso_r, f = strdup(arg1));
+ dir = cdutil->find_path(f = strdup(arg1));
free(f);
if (!dir.R) {
printm(M_ERROR, "Path %s was not found on iso.\n", arg1);
- delete iso_r;
- exit(-1);
+ throw Exit(-1);
}
if (!(dir.Flags & 2)) {
printm(M_ERROR, "Path %s design a file and not a directory.\n", arg1);
- delete iso_r;
- exit(-1);
+ throw Exit(-1);
}
- show_head_entry();
- show_dir(iso_r, &dir);
+ cdutil->show_head_entry();
+ cdutil->show_dir(&dir);
} else if (!strcmp(argv[optind], "extract-file")) {
- struct DirEntry dir;
- Handle * file;
+ cdutils::DirEntry dir;
optind++;
if ((argc - 2) != optind) {
showhelp();
printm(M_ERROR, "extract-file needs two arguments.\n");
- delete iso_r;
- exit(-1);
+ throw Exit(-1);
}
arg1 = argv[optind++];
arg2 = argv[optind++];
file = new Output(arg1);
- dir = find_path(iso_r, f = strdup(arg2));
+ dir = cdutil->find_path(f = strdup(arg2));
free(f);
if (!dir.R) {
printm(M_ERROR, "Path %s was not found on iso.\n", arg2);
- delete iso_r;
- delete file;
- exit(-1);
+ throw Exit(-1);
}
printm(M_STATUS, "Reading path %s to file %s.\n", arg2, arg1);
- read_file(iso_r, file, type, dir.Sector, dir.Size);
+ cdutil->read_file(file, type, dir.Sector, dir.Size);
delete file;
} else if (!strcmp(argv[optind], "extract")) {
- Handle * file;
-
optind++;
if ((argc - 3) != optind) {
showhelp();
printm(M_ERROR, "extract needs three arguments.\n");
- delete iso_r;
- exit(-1);
+ throw Exit(-1);
}
arg1 = argv[optind++];
size = atoi(argv[optind++]);
sector = atoi(argv[optind++]);
file = new Output(arg1);
printm(M_STATUS, "Reading %i bytes from sector %i to file %s.\n", size, sector, arg1);
- read_file(iso_r, file, type, sector, size);
+ cdutil->read_file(file, type, sector, size);
delete file;
} else if (!strcmp(argv[optind], "insert-file")) {
- struct DirEntry dir, * d;
+ cdutils::DirEntry dir, * d;
unsigned char * buffer;
- Handle * file;
int old_type;
if (ppf) {
- if (open_ppf(ppf, iso_r, "Created by CD-Tool") < 0) {
+ if (cdutil->open_ppf(ppf, "Created by CD-Tool") < 0) {
printm(M_ERROR, "Failed to open file %s for writing.\n", ppf);
}
} else {
iso_w = new Output(iso_name, 0, 0);
+ cdutil->set_iso_w(iso_w);
}
optind++;
if ((argc - 2) != optind) {
showhelp();
printm(M_ERROR, "insert-file needs two arguments.\n");
- delete iso_r;
- if (ppf) {
- close_ppf();
- } else {
- delete iso_w;
- }
- exit(-1);
+ throw Exit(-1);
}
arg1 = argv[optind++];
arg2 = argv[optind++];
file = new Input(arg1);
- dir = find_path(iso_r, f = strdup(arg2));
+ dir = cdutil->find_path(f = strdup(arg2));
free(f);
if (!dir.R) {
printm(M_ERROR, "Path %s was not found on iso.\n", arg2);
- exit(-1);
+ throw Exit(-1);
}
- old_type = guess_type(iso_r, dir.Sector);
+ old_type = cdutil->guess_type(dir.Sector);
if (type == GUESS) {
type = old_type;
@@ -219,86 +221,50 @@ int Main(int argc, char ** argv) {
printm(M_WARNING, "New file too big: %i vs %i in " + sec_modes[type] + ".\n", dir.Size, file->GetSize());
} else {
printm(M_ERROR, "New file too big: %i vs %i in " + sec_modes[type] + ".\n", dir.Size, file->GetSize());
- delete iso_r;
- if (ppf) {
- close_ppf();
- } else {
- delete iso_w;
- }
- delete file;
- exit(-1);
+ throw Exit(-1);
}
}
- printm(M_STATUS, "Writing file %s at path %s.\n", arg1, arg2);
- write_file(iso_r, iso_w, file, type, dir.Sector);
+ printm(M_STATUS, "Writing file %s at path %s (sector %i).\n", arg1, arg2, dir.Sector);
+ cdutil->write_file(file, type, dir.Sector);
printm(M_STATUS, "Updating directory entry.\n");
- dir = find_parent(iso_r, f = strdup(arg2));
+ dir = cdutil->find_parent(f = strdup(arg2));
free(f);
if ((f = strrchr(arg2, '/'))) {
f++;
} else {
f = arg2;
}
- d = find_dir_entry(iso_r, &buffer, &dir, f);
+ d = cdutil->find_dir_entry(&buffer, &dir, f);
d->Size = file->GetSize();
- write_datas(iso_r, iso_w, buffer, GUESS, dir.Sector, dir.Size);
+ cdutil->write_datas(buffer, GUESS, dir.Sector, dir.Size);
free(buffer);
- delete file;
- if (ppf) {
- close_ppf();
- } else {
- delete iso_w;
- }
} else if (!strcmp(argv[optind], "insert")) {
- Handle * file;
-
if (ppf) {
- if (open_ppf(ppf, iso_r, "Created by CD-Tool") < 0) {
+ if (cdutil->open_ppf(ppf, "Created by CD-Tool") < 0) {
printm(M_ERROR, "Failed to open file %s for writing.\n", ppf);
+ throw Exit(-1);
}
} else {
iso_w = new Output(iso_name, 0, 0);
+ cdutil->set_iso_w(iso_w);
}
optind++;
if ((argc - 2) != optind) {
showhelp();
printm(M_ERROR, "insert needs two arguments.\n");
- if (ppf) {
- close_ppf();
- } else {
- delete iso_w;
- }
- delete iso_r;
- exit(-1);
+ throw Exit(-1);
}
arg1 = argv[optind++];
sector = atoi(argv[optind++]);
file = new Input(arg1);
printm(M_STATUS, "Writing file %s at sector %i.\n", arg1, sector);
- write_file(iso_r, iso_w, file, type, sector);
- if (ppf) {
- close_ppf();
- } else {
- delete iso_w;
- }
- delete file;
+ cdutil->write_file(file, type, sector);
} else {
showhelp();
printm(M_ERROR, "Command %s unknow.\n", argv[optind]);
- delete iso_r;
- exit(-1);
- }
-
- delete iso_r;
- exit(0);
-}
-
-int main(int argc, char ** argv) {
- try {
- Main(argc, argv);
- }
- catch (GeneralException e) {
- fprintf(stderr, "%s\n", e.GetMsg());
+ throw Exit(-1);
}
+ return 0;
}
+CODE_ENDS