// Vance Thrift and Biller File Utility 2 // // Douglas Thrift // // $Id: VTBFileUtil2.cxx,v 1.9 2003/08/18 22:25:29 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.0alpha"; Gui gui; INT_PTR CALLBACK usage(HWND dialog, UINT msg, WPARAM w, LPARAM l); INT_PTR CALLBACK version(HWND dialog, UINT msg, WPARAM w, LPARAM l); 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(); } args.push_back(arg); } while (line.good()); } 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(); } inline void usage(HWND parent) { DialogBox(gui.instance, MAKEINTRESOURCE(IDD_USAGE), parent, usage); } inline 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(); arguments(args, GetCommandLine()); program = args[0]; Mode mode = none; gui.instance = hInstance; gui.icon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_VTB_ICON)); gui.tips = 0; 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"; } 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(); } if (debug) { cout << "Press enter key to exit . . ."; cin.get(); } return 0; } void tooltip(HWND tool, const string& tip) { HWND tooltip = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, tool, NULL, gui.instance, NULL); RECT rect; SetWindowPos(tooltip, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); GetClientRect(tool, &rect); TOOLINFO toolinfo; LPTSTR ctip = new CHAR[tip.length()]; sprintf(ctip, "%s", tip.c_str()); if (debug) cerr << "ctip = " << ctip << "\n"; toolinfo.cbSize = sizeof(TOOLINFO); toolinfo.uFlags = TTF_SUBCLASS; toolinfo.hwnd = tool; toolinfo.hinst = gui.instance; toolinfo.uId = gui.tips++; toolinfo.lpszText = ctip; toolinfo.rect.left = rect.left; toolinfo.rect.top = rect.top; toolinfo.rect.right = rect.right; toolinfo.rect.bottom = rect.bottom; SendMessage(tooltip, TTM_ADDTOOL, 0, LPARAM(LPTOOLINFO(&toolinfo))); delete [] ctip; } 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]\n" << "\nOptions:\n" << " -help\t\tDisplay this information\n" << " -version\tDisplay version information\n" << " -disc\t\tStart in Disc Browse mode\n" << " -scan\t\tStart in Scan Utility mode\n"; SendMessage(GetDlgItem(dialog, IDC_USAGE_TEXT), WM_SETTEXT, 0, LPARAM(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 << ")\n\n" << "Copyright © 2003, Douglas Thrift.\n"; SendMessage(GetDlgItem(dialog, IDC_VERSION_TEXT), WM_SETTEXT, 0, LPARAM(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; }