// Vance Thrift and Biller File Utility 2 // // Douglas Thrift // // $Id: VTBFileUtil2.cxx,v 1.15 2003/09/12 04:26:54 douglas Exp $ #include "VTBFileUtil2.h" #include "Chooser.h" #include "DiscBrowse.h" #include "ScanUtility.h" #include #include bool debug = false; string program; string programName = "Vance Thrift and Biller File Utility 2"; string programVersion = "2.0"; Gui gui; static INT_PTR CALLBACK usage(HWND dialog, UINT msg, WPARAM w, LPARAM l); static INT_PTR CALLBACK version(HWND dialog, UINT msg, WPARAM w, LPARAM l); static inline void arguments(vector& args, const string& command) { istringstream line(command); string arg; do { if (line.peek() == '\"') { line.get(); getline(line, arg, '\"'); } else { line >> arg; line.get(); } if (arg != "") { args.push_back(arg); } } while (line.good()); } static inline void munge(void) { AllocConsole(); SetConsoleTitle(programName.c_str()); int hin = _open_osfhandle(long(GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT); int hout = _open_osfhandle(long(GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT); int herr = _open_osfhandle(long(GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT); FILE* fin = _fdopen(hin, "r"); FILE* fout = _fdopen(hout, "w"); FILE* ferr = _fdopen(herr, "w"); *stdin = *fin; *stdout = *fout; *stderr = *ferr; setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); cin.sync_with_stdio(); cout.sync_with_stdio(); cerr.sync_with_stdio(); } void usage(HWND parent) { DialogBox(gui.instance, MAKEINTRESOURCE(IDD_USAGE), parent, usage); } void version(HWND parent) { DialogBox(gui.instance, MAKEINTRESOURCE(IDD_VERSION), parent, version); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { vector args; InitCommonControls(); CoInitialize(NULL); arguments(args, GetCommandLine()); program = args[0]; Mode mode = none; gui.instance = hInstance; gui.icon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_VTB_ICON)); gui.show = nShowCmd; if (toAnsi(toWide("Z:\\DouglasThrift")) != "Z:\\DouglasThrift") { string message = "Could not find the Microsoft Layer for Unicode."; switch (MessageBox(NULL, message.c_str(), programName.c_str(), MB_ABORTRETRYIGNORE | MB_ICONERROR)) { case IDABORT: return 1; break; case IDRETRY: { string url = string("http://www.microsoft.com/downloads/detai") + "ls.aspx?FamilyID=73ba7bd7-ed06-4f0d-80a4-2a7eeaee17e2&D" + "isplayLang=en", message = string("Click OK when the Mi") + "crosoft Layer for Unicode is installed."; ShellExecute(NULL, NULL, url.c_str(), NULL, NULL, gui.show); MessageBox(NULL, message.c_str(), programName.c_str(), MB_OK | MB_ICONINFORMATION); STARTUPINFO start; PROCESS_INFORMATION info; start.cb = sizeof(start); start.lpReserved = NULL; start.lpDesktop = NULL; start.lpTitle = NULL; start.dwFlags = STARTF_FORCEONFEEDBACK | STARTF_USESHOWWINDOW; start.wShowWindow = gui.show; start.cbReserved2 = 0; start.lpReserved2 = NULL; CreateProcess(program.c_str(), GetCommandLine(), NULL, NULL, FALSE, 0, NULL, NULL, &start, &info); CloseHandle(info.hProcess); CloseHandle(info.hThread); } return 0; break; case IDIGNORE: break; } } for (unsigned index = 1; index < args.size(); index++) { if (args[index] == "-help") { usage(); return 0; } else if (args[index] == "-version") { version(); return 0; } else if (args[index] == "-disc") { mode = disc; } else if (args[index] == "-scan") { mode = scan; } else if (args[index] == "-D") { if (!debug) { debug = true; cerr.setf(ios_base::boolalpha); } } else { string message = "Unknown argument: " + args[index]; MessageBox(NULL, message.c_str(), programName.c_str(), MB_ICONWARNING); } } if (debug) { munge(); cerr << "mode = "; switch (mode) { case none: cerr << "none"; break; case disc: cerr << "disc"; break; case scan: cerr << "scan"; break; default: cerr << mode; break; } cerr << "\n"; } License license; license.check(); if (mode == none) { Chooser chooser; mode = chooser.choose(); if (debug) { cerr << "mode = "; switch (mode) { case none: cerr << "none"; break; case disc: cerr << "disc"; break; case scan: cerr << "scan"; break; default: cerr << mode; break; } cerr << "\n"; } } if (mode == disc) { DiscBrowse browser; browser.run(); } else if (mode == scan) { ScanUtility utility; utility.run(); } CoUninitialize(); if (debug) { char cmd[MAX_PATH]; char arg[MAX_PATH]; GetEnvironmentVariable("ComSpec", cmd, MAX_PATH); StringCchPrintf(arg, MAX_PATH, "%s /c pause", cmd); STARTUPINFO start; PROCESS_INFORMATION info; start.cb = sizeof(start); start.lpReserved = NULL; start.lpDesktop = NULL; start.lpTitle = NULL; start.dwFlags = STARTF_USESTDHANDLES; start.cbReserved2 = 0; start.lpReserved2 = NULL; start.hStdInput = GetStdHandle(STD_INPUT_HANDLE); start.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); start.hStdError = GetStdHandle(STD_ERROR_HANDLE); CreateProcess(cmd, arg, NULL, NULL, TRUE, 0, NULL, NULL, &start, &info); CloseHandle(info.hProcess); CloseHandle(info.hThread); } return 0; } INT_PTR CALLBACK usage(HWND dialog, UINT msg, WPARAM w, LPARAM l) { switch (msg) { case WM_INITDIALOG: center(dialog); SendMessage(dialog, WM_SETICON, ICON_BIG, LPARAM(gui.icon)); SendMessage(dialog, WM_SETTEXT, 0, LPARAM(programName.c_str())); { ostringstream usage; usage << "Usage: VTBFileUtil [-help | -version | -disc | -scan]\r" << "\n" << "\r\nOptions:\r\n" << " -help\t\tDisplay this information\r\n" << " -version\t\tDisplay version information\r\n" << " -disc\t\tStart in Disc Browse mode\r\n" << " -scan\t\tStart in Scan Utility mode\r\n"; SetDlgItemText(dialog, IDC_USAGE_TEXT, usage.str().c_str()); } break; case WM_COMMAND: switch (LOWORD(w)) { case IDOK: case IDCANCEL: EndDialog(dialog, w); return TRUE; default: break; } break; } return FALSE; } INT_PTR CALLBACK version(HWND dialog, UINT msg, WPARAM w, LPARAM l) { switch (msg) { case WM_INITDIALOG: center(dialog); SendMessage(dialog, WM_SETICON, ICON_BIG, LPARAM(gui.icon)); SendMessage(dialog, WM_SETTEXT, 0, LPARAM(programName.c_str())); { ostringstream version; version << programName << " (" << programVersion << ")\r\n\r\n" << "Copyright © 2003, Douglas Thrift.\n"; SetDlgItemText(dialog, IDC_VERSION_TEXT, version.str().c_str()); } break; case WM_COMMAND: switch (LOWORD(w)) { case IDOK: case IDCANCEL: EndDialog(dialog, w); return TRUE; default: break; } break; } return FALSE; }