/* ======================================================================= */ BOOL DumpCDAudioTracks(PWINDOWHANDLES H, HANDLE CDHandle, DWORD DiscTotal, DWORD TrackCount, PCDTRACK CDTrackData, PCHAR BasePath) { DWORD DiscCurrent = 0; DWORD TracksCompleted = 0; if(PrepareBasePath(H, BasePath) == FALSE) return FALSE; DWORD TrackCurrent; for(TrackCurrent = 0; TrackCurrent < TrackCount; ++TrackCurrent) if(EncodeTrack(H, TrackCurrent, BasePath, CDHandle, CDTrackData, &DiscCurrent, &DiscTotal, &TrackCount) == TRUE) ++TracksCompleted; SendMessage(H->PO, PBM_SETRANGE32, 0, 1); SendMessage(H->PO, PBM_SETPOS, 1, 0); SendMessage(H->PA, PBM_SETRANGE32, 0, 1); SendMessage(H->PA, PBM_SETPOS, 1, 0); SetLabel(H->WH, "%s", WindowName); Log(LOG_WRITE, "Completed %u of the %u available tracks", TracksCompleted, TrackCount); if(TracksCompleted == 0) { if(RemoveDirectory(BasePath) == FALSE) { DWORD ErrorCode = GetLastError(); switch(ErrorCode) { case ERROR_DIR_NOT_EMPTY: case ERROR_PATH_NOT_FOUND: { break; } default: { Log(LOG_WRITE, "Error code %u deleting base path", ErrorCode); break; } } } else Log(LOG_WRITE, "Deleted base path due to error"); MessageBoxF(H->WH, WindowName, MB_ICONSTOP, "Operation failed.\n\nNone of the %u tracks found were converted.", TrackCount); } else if(TracksCompleted != TrackCount) MessageBoxF(H->WH, WindowName, MB_ICONEXCLAMATION, "Operation completed successfully but with errors\n\nOnly %u of the %u tracks were converted", TracksCompleted, TrackCount); else MessageBoxF(H->WH, WindowName, MB_ICONINFORMATION, "The operation completed successfully\n\n%u of %u tracks were converted", TracksCompleted, TrackCount); return TRUE; }
/* ======================================================================= */ PCDTRACK GetCDROMTracks(PWINDOWHANDLES H, HANDLE CDHandle, LPDWORD DT, LPDWORD TrackCount) { PCDTRACK CDTrackData = NULL; PCDROM_TOC Table = NULL; for(;;) { SendMessage(H->PO, PBM_SETRANGE32, 0, 1); SendMessage(H->PO, PBM_SETPOS, 0, 0); Table = GetCDROMTrackData(H, CDHandle); if(Table == NULL) return NULL; *TrackCount = Table->LastTrack - (Table->FirstTrack - 1); if(*TrackCount > 0) { CDTrackData = new CDTRACK[*TrackCount]; *DT = 0; DWORD Track; for(Track=0; Track<*TrackCount; ++Track) { CDTrackData[Track].Address = AddressToSectors(Table->TrackData[Track].Address); CDTrackData[Track].Length = AddressToSectors(Table->TrackData[Track+1].Address) - CDTrackData[Track].Address; *DT += CDTrackData[Track].Length/SECTORS_AT_READ; } Log(LOG_WRITE, "Found %u (%u to %u) audio tracks on CD-ROM media", *TrackCount, Table->FirstTrack, Table->LastTrack); SetLabel(H->WT, "Found %u CD digital audio tracks!", *TrackCount); break; } SetLabel(H->WT, "Scanning for tracks failed!\nNo CD digital audio tracks detected!"); Log(LOG_WRITE, "There are no tracks on this CD media"); if(MessageBoxF(H->WH, WindowName, MB_ICONEXCLAMATION | MB_RETRYCANCEL, "This CD-ROM does not contain any tracks!\nPlease insert a CD-ROM with audio tracks.\n\nPress RETRY to try again.\nPress CANCEL to terminate.") == IDCANCEL) break; delete Table; } delete Table; SendMessage(H->PO, PBM_SETRANGE32, 0, 1); SendMessage(H->PO, PBM_SETPOS, 1, 0); SendMessage(H->PA, PBM_SETPOS, 4, 0); return CDTrackData; }
/* ======================================================================= */ VOID Exception(INT Line, INT Code, PCHAR Format, ...) { CHAR FormatBuffer[1025]; _vsnprintf(FormatBuffer, sizeof(FormatBuffer), Format, (PCHAR)(&Format + 1)); CHAR FinalBuffer[1025]; if(Line) _snprintf(FinalBuffer, sizeof(FinalBuffer), "Error %u-%x-%u: %s.", Code, Code, Line, FormatBuffer); else _snprintf(FinalBuffer, sizeof(FinalBuffer), "%s.", FormatBuffer); MessageBoxF(Handles.WH, WindowName, (Line ? MB_ICONEXCLAMATION : MB_ICONINFORMATION) | MB_APPLMODAL, "%s", FinalBuffer); if(Handles.WH != NULL) SendMessage(Handles.WH, WM_CLOSE, 1, 0); else DeInitialise(Line); }
/* ======================================================================= */ PCDROM_TOC GetCDROMTrackData(PWINDOWHANDLES H, HANDLE CDHandle) { PCDROM_TOC CDData = new CDROM_TOC; ULONG BytesWritten; BOOL Success; DWORD ErrorCode; for(;;) { SendMessage(H->PO, PBM_SETRANGE32, 0, 1); SendMessage(H->PO, PBM_SETPOS, 0, 0); SetLabel(H->WT, "Scanning for table of contents..."); Success = DeviceIoControl(CDHandle, IOCTL_CDROM_READ_TOC, NULL, 0, CDData, sizeof(CDROM_TOC), &BytesWritten, NULL); SendMessage(H->PO, PBM_SETRANGE32, 0, 1); SendMessage(H->PO, PBM_SETPOS, 1, 0); SendMessage(H->PA, PBM_SETPOS, 3, 0); if(Success == TRUE) { Log(LOG_WRITE, "Received %u bytes for table of contents", BytesWritten); SetLabel(H->WT, "Received table of contents"); break; } ErrorCode = GetLastError(); Log(LOG_WRITE, "Error code %u scanning for table of contents", ErrorCode); if(ErrorCode == ERROR_NOT_READY) { SetLabel(H->WT, "Scanning for table of contents failed!\nThere is no CD-ROM disc inserted into the drive!\nWaiting for user interaction..."); if(MessageBoxF(H->WH, WindowName, MB_ICONEXCLAMATION | MB_RETRYCANCEL, "Please insert a CD-ROM into the drive.\n\nClick RETRY to try again.\nClick CANCEL to terminate.") == IDRETRY) continue; } else SetLabel(H->WT, "Scanning for table of contents failed!\nError code %u occured!", ErrorCode); delete CDData; CDData = NULL; break; } return CDData; }
/* ======================================================================= */ LRESULT CALLBACK WinProc(HWND H, UINT M, WPARAM W, LPARAM L) { switch(M) { case WM_COMMAND: { switch(LOWORD(W)) { case 1: { SendMessage(H, WM_CLOSE, 0, 0); break; } default: { break; } } break; } case WM_CLOSE: { SuspendThread(ThreadHandle); if(W == 1 || MessageBoxF(H, WindowName, MB_ICONQUESTION | MB_YESNO, "Do you want to terminate the conversion process?") == IDYES) DestroyWindow(H); else ResumeThread(ThreadHandle); break; } case WM_DESTROY: { Handles.WH = NULL; PostQuitMessage(0); break; } default: { return DefWindowProc(H, M, W, L); } } return 0; }
bool CUtils::CopyFile(LPCTSTR pszSource,LPCTSTR pszDest) { // Compare the files. First set rc to the result of the comparison (true if the same) bool rc=false; struct _stat s1,s2; if(-1!=_tstat(pszSource,&s1) && -1!=_tstat(pszDest,&s2) && s1.st_size==s2.st_size){ // Files both exist and are of equal size FILE *f1=_tfopen(pszSource,_T("rb")); if(f1){ FILE *f2=_tfopen(pszDest,_T("rb")); if(f2){ int nSize1,nSize2; rc=true; do{ char buf1[4096],buf2[4096]; nSize1=fread(buf1,1,sizeof buf1,f1); nSize2=fread(buf2,1,sizeof buf2,f2); if(nSize1!=nSize2 || 0!=memcmp(buf1,buf2,nSize1)){ rc=false; break; } } while (nSize1>0); fclose(f2); } fclose(f1); } } if(rc){ // Files are identical TRACE(_T("Copy not necessary: '%s' to '%s'\n"),pszSource,pszDest); } else { rc=TRUE==::CopyFile(pszSource,pszDest,FALSE); if(rc){ TRACE(_T("Copied '%s' to '%s'\n"),pszSource,pszDest); } else { MessageBoxF(_T("Failed to copy '%s' to '%s' - %s"),pszSource,pszDest,GetLastErrorMessageString()); } } return rc; }
/* ======================================================================= */ BOOL PrepareBasePath(PWINDOWHANDLES H, PCHAR BasePath) { if(CreateDirectory(BasePath, NULL) == 0) { DWORD ErrorCode = GetLastError(); if(ErrorCode == ERROR_ALREADY_EXISTS) { switch(MessageBoxF(H->WH, WindowName, MB_ICONEXCLAMATION | MB_YESNOCANCEL, "The directory %s already exists which means you may have already dumped this CD-ROM already. Do you want to continue anyway?\n\nClick the YES button to delete the directory and all of its' contents.\nClick the NO button to leave the directory intact and only overwrite existing files.\nClick the CANCEL button to terminate the program and do nothing.", BasePath)) { case IDCANCEL: { return FALSE; } case IDNO: { break; } case IDYES: { INT ErrorCode = DelTree(H->WH, BasePath); if(ErrorCode != S_OK) Log(LOG_WRITE, "Error code %08x removing the base path", ErrorCode); else Log(LOG_WRITE, "The base path was successfully deleted"); if(CreateDirectory(BasePath, NULL) == FALSE) Log(LOG_WRITE, "Error code %08x re-creating the base path", GetLastError()); else Log(LOG_WRITE, "Re-created the base path"); break; } } } else { Log(LOG_WRITE, "Error %u creating the base path", ErrorCode); return FALSE; } } else Log(LOG_WRITE, "Created the base path successfully"); return TRUE; }
bool ecUtils::Launch(const ecFileName &strFileName, const ecFileName &strViewer) { bool rc=false; if(!strViewer.IsEmpty())//use custom editor { wxString strCmdline(strViewer); PTCHAR pszCmdLine=strCmdline.GetBuffer(strCmdline.GetLength()); GetShortPathName(pszCmdLine,pszCmdLine,strCmdline.GetLength()); strCmdline.ReleaseBuffer(); strCmdline+=_TCHAR(' '); strCmdline+=strFileName; PROCESS_INFORMATION pi; STARTUPINFO si; si.cb = sizeof(STARTUPINFO); si.lpReserved = NULL; si.lpReserved2 = NULL; si.cbReserved2 = 0; si.lpDesktop = NULL; si.dwFlags = 0; si.lpTitle=NULL; if(CreateProcess( NULL, // app name //strCmdline.GetBuffer(strCmdline.GetLength()), // command line strCmdline.GetBuffer(strCmdline.GetLength()), // command line NULL, // process security NULL, // thread security TRUE, // inherit handles 0, NULL, // environment NULL, // current dir &si, // startup info &pi)){ CloseHandle(pi.hProcess); CloseHandle(pi.hThread); rc=true; } else { MessageBoxF(wxT("Failed to invoke %s.\n"),strCmdline); } strCmdline.ReleaseBuffer(); } else {// Use association TCHAR szExe[MAX_PATH]; HINSTANCE h=FindExecutable(strFileName,wxT("."),szExe); if(int(h)<=32){ wxString str; switch(int(h)){ case 0: str=wxT("The system is out of memory or resources.");break; case 31: str=wxT("There is no association for the specified file type.");break; case ERROR_FILE_NOT_FOUND: str=wxT("The specified file was not found.");break; case ERROR_PATH_NOT_FOUND: str=wxT("The specified path was not found.");break; case ERROR_BAD_FORMAT: str=wxT("The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).");break; default: break; } MessageBoxF(wxT("Failed to open document %s.\r\n%s"),strFileName,str); } else { SHELLEXECUTEINFO sei = {sizeof(sei), 0, AfxGetMainWnd()->GetSafeHwnd(), wxT("open"), strFileName, NULL, NULL, SW_SHOWNORMAL, AfxGetInstanceHandle( )}; sei.hInstApp=0; HINSTANCE hInst=ShellExecute(AfxGetMainWnd()->GetSafeHwnd(),wxT("open"), strFileName, NULL, wxT("."), 0)/*ShellExecuteEx(&sei)*/; if(int(hInst)<=32/*sei.hInstApp==0*/) { wxString str; switch(int(hInst)) { case 0 : str=wxT("The operating system is out of memory or resources. ");break; case ERROR_FILE_NOT_FOUND : str=wxT("The specified file was not found. ");break; case ERROR_PATH_NOT_FOUND : str=wxT("The specified path was not found. ");break; case ERROR_BAD_FORMAT : str=wxT("The .EXE file is invalid (non-Win32 .EXE or error in .EXE image). ");break; case SE_ERR_ACCESSDENIED : str=wxT("The operating system denied access to the specified file. ");break; case SE_ERR_ASSOCINCOMPLETE : str=wxT("The filename association is incomplete or invalid. ");break; case SE_ERR_DDEBUSY : str=wxT("The DDE transaction could not be completed because other DDE transactions were being processed. ");break; case SE_ERR_DDEFAIL : str=wxT("The DDE transaction failed. ");break; case SE_ERR_DDETIMEOUT : str=wxT("The DDE transaction could not be completed because the request timed out. ");break; case SE_ERR_DLLNOTFOUND : str=wxT("The specified dynamic-link library was not found. ");break; //case SE_ERR_FNF : str=wxT("The specified file was not found. ");break; case SE_ERR_NOASSOC : str=wxT("There is no application associated with the given filename extension. ");break; case SE_ERR_OOM : str=wxT("There was not enough memory to complete the operation. ");break; //case SE_ERR_PNF : str=wxT("The specified path was not found. ");break; case SE_ERR_SHARE : str=wxT("A sharing violation occurred. ");break; default: str=wxT("An unexpected error occurred");break; } MessageBoxF(wxT("Failed to open document %s using %s.\r\n%s"),strFileName,szExe,str); } else { rc=true; } } } return rc; }
/* ======================================================================= */ UCHAR GetCDROMDrive(PWINDOWHANDLES H) { SetErrorMode(SEM_NOOPENFILEERRORBOX); SendMessage(H->PA, PBM_SETRANGE32, 0, 4); SendMessage(H->PO, PBM_SETRANGE32, 0, 1); SendMessage(H->PO, PBM_SETPOS, 0, 0); CHAR DrivePath[5]; UCHAR DriveLetter; UINT DriveType; UCHAR DriveFound = 0; for(DriveLetter='A'; DriveLetter<='Z'; ++DriveLetter) { SetLabel(H->WT, "Scanning for CD-ROM drive...\nCurrently testing drive %c:...", DriveLetter); _snprintf(DrivePath, sizeof(DrivePath), "%c:\\", DriveLetter); DriveType = GetDriveType(DrivePath); if(DriveType == DRIVE_CDROM) { if(DriveFound == 0) DriveFound = DriveLetter; else switch(MessageBoxF(H->WH, WindowName, MB_ICONQUESTION | MB_YESNOCANCEL, "Found another CD-ROM device at drive %c: as well as the one at %c:. Do you want to use the prior drive instead?\n\nPress the YES button to accept the new drive.\nPress the NO button to keep the new drive.\nPress the CANCEL button to terminate.", DriveFound, DriveLetter)) { case IDYES: { DriveFound = DriveLetter; break; } case IDNO: { break; } case IDCANCEL: { DriveFound = 0; DriveLetter = 'Z'; break; } } } SendMessage(H->PO, PBM_STEPIT, 0, 0); } if(DriveFound == 0) { SetLabel(H->WT, "Scanning completed...\nNo valid CD-ROM drive detected!"); Log(LOG_WRITE, "Could not detect a valid CD-ROM device"); } else { SetLabel(H->WT, "Scanning completed...\nFound drive %c: as a valid CD-ROM drive.", DriveFound); Log(LOG_WRITE, "Found drive %c as a CD-ROM device", DriveFound); } SendMessage(H->PO, PBM_SETRANGE32, 0, 1); SendMessage(H->PO, PBM_SETPOS, 1, 0); SendMessage(H->PA, PBM_SETPOS, 1, 0); return DriveFound; }