// returns -1 on error int CLoggingDlg::GetMaskValue() { UpdateData(TRUE); // get values from controls if (m_Mask.IsEmpty()) return 0; int mask; int res = _stscanf(m_Mask, _T("%X"), &mask); // sscanf will return 3 and success on "3ugh", so manually scan it if (!_istxdigit(m_Mask.GetAt(0)) || (m_Mask.GetLength() > 1 && !_istxdigit(m_Mask.GetAt(1))) || (m_Mask.GetLength() > 2 && !_istxdigit(m_Mask.GetAt(2))) || (m_Mask.GetLength() > 3 && !_istxdigit(m_Mask.GetAt(3)))) { return -1; } if (res <= 0 || res == EOF) return -1; else return mask; }
/** * @param chValue - contains the character code value of the key. * @param uRepCnt - contains the repeat count, the number of times the keystroke is repeated when user holds down the key. * @param uFlags - contains the scan code, key-transition code, previous key state, and context code. */ void CNumEdit::OnChar(TCHAR chValue, UINT /*uRepCnt*/, UINT /*uFlags*/) { if ((m_eNumFormat == NF_HEX && _istxdigit(chValue)) || (m_eNumFormat == NF_DEC && _istdigit(chValue)) || chValue == VK_BACK) { const _ATL_MSG* pMsg = GetCurrentMessage(); DefWindowProc(WM_CHAR, toupper(chValue), pMsg->lParam); } }
//used to delete non-hex chars after the user pastes into the hexbox void FillWithDialog::deletenonhex(HWND hEd) { GetWindowText(hEd, pcFWText, FW_MAX); int ii = 0; for (int i = 0; pcFWText[i] != '\0'; i++) { if (_istxdigit(pcFWText[i])) { pcFWText[ii] = pcFWText[i]; ii++; } } pcFWText[ii] = '\0'; SetWindowText(hEd, pcFWText); }
BOOL IsValidValue(const TCHAR* psz, DWORD& dwValue) { if (*psz == 0 || !_istdigit(*psz)) return FALSE; DWORD dw = 0; if (psz[0] == '0' && (psz[1] == 'x' || psz[1] == 'X')) { if (psz[1] == 0) return FALSE; psz += 2; while (_istxdigit(*psz)) { dw *= 16; dw += _istdigit(*psz) ? *psz - '0' : 10 + (*psz|0x20) - 'a'; ++psz; } } else if (psz[0] == '0') { while (isodigit(*psz)) { dw *= 8; dw += *psz - '0'; ++psz; } } else { while (_istdigit(*psz)) { dw *= 10; dw += *psz - '0'; ++psz; } } if (*psz != 0) return FALSE; dwValue = dw; return TRUE; }
//hex box msg handler LRESULT CALLBACK FillWithDialog::HexProc(HWND hEdit, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_CHAR: // only enter chars if they are hex digits or backspace if (!_istxdigit(static_cast<TBYTE>(wParam)) && wParam != VK_BACK) { MessageBeep(MB_ICONEXCLAMATION); return 0; } break; case WM_PASTE: CallWindowProc(reinterpret_cast<WNDPROC>(oldproc), hEdit, iMsg, wParam, lParam);//paste as usual deletenonhex(hEdit);//but delete non-hex chars return 0; } return CallWindowProc(reinterpret_cast<WNDPROC>(oldproc), hEdit, iMsg, wParam, lParam); }
void CNumEdit::OnPaste() { LockWindowUpdate(TRUE); DefWindowProc(); int nStart, nEnd; GetSel(nStart, nEnd); TCHAR szValue[64]; GetWindowText(szValue, countof(szValue)); int i = 0, j = 0; for (;;) { TCHAR chValue = szValue[i++]; if (chValue == _T('\0')) { szValue[j] = _T('\0'); break; } switch (m_eNumFormat) { case NF_HEX: if (_istxdigit(chValue)) szValue[j++] = (TCHAR)_totupper(chValue); break; case NF_DEC: if (_istdigit(chValue)) szValue[j++] = chValue; break; } } if (nStart > j) nStart = j; if (nEnd > j) nEnd = j; SetWindowText(szValue); SetSel(nStart, nEnd); LockWindowUpdate(FALSE); }
// examines m_opstring, sets checkboxes and edit boxes from it, and returns // false if an error is found (doesn't finish reading string if error is found) BOOL COptionsDlg::CheckOpstring() { UpdateData(TRUE); // TRUE means read from controls // now parse string TCHAR param[MAX_PATH]; TCHAR value[MAX_PATH]; TCHAR *pos = m_opstring.GetBuffer(0); TCHAR *prev_pos; BOOL valid = TRUE; BOOL matched_any; BOOL match[num_options]; int i; for (i=0; i<num_options; i++) match[i] = FALSE; DLL_TYPE dll_type = CDynamoRIOApp::GetDllType(); while (TRUE) { if (!getword(m_opstring.GetBuffer(0), &pos, param)) break; matched_any = FALSE; for (i=0; i<num_options; i++) { if (_tcscmp(param, names[i]) == 0) { CButton *button = (CButton *) GetDlgItem(checkboxes[i]); assert(button != NULL); if (button->GetCheck() == 0) button->SetCheck(1); match[i] = TRUE; matched_any = TRUE; if (hasvalue[i] != NOVALUE) { prev_pos = pos; if (!getword(m_opstring.GetBuffer(0), &pos, value)) { match[i] = FALSE; valid = FALSE; *values[i] = _T(""); } else { *values[i] = value; if (i == INSTRLIBNAME) { if (!CheckLibraryExists(value, FALSE)) { match[i] = FALSE; valid = FALSE; } } if (hasvalue[i] == NUM_DECIMAL || hasvalue[i] == NUM_HEX) { if (value[0] == _T('-')) { // value is missing, don't claim next param as value! // (for string (non-numeric) values, go ahead and do so) pos = prev_pos; *values[i] = _T(""); match[i] = FALSE; valid = FALSE; break; } int j; int len = _tcslen(value); for (j=0; j<len; j++) { if ((hasvalue[i] == NUM_DECIMAL && !_istdigit(value[j])) || (hasvalue[i] == NUM_HEX && ((j == 0 && value[j] != _T('0')) || (j == 1 && value[j] != _T('x') && value[j] != _T('X')) || (j > 1 && !_istxdigit(value[j]))))) { match[i] = FALSE; valid = FALSE; break; } } } } } else { match[i] = TRUE; matched_any = TRUE; } if (match[i]) { // if made it this far, now check vs. dll type if ((dll_type == DLL_RELEASE && !ok_with_release[i]) || (dll_type != DLL_PROFILE && i == PROF_COUNTS)) { // leave it checked valid = FALSE; } } break; } } if (!matched_any) valid = FALSE; } for (i=0; i<num_options; i++) { if (!match[i]) { CButton *button = (CButton *) GetDlgItem(checkboxes[i]); assert(button != NULL); if (button->GetCheck() > 0) button->SetCheck(0); } } UpdateData(FALSE); // FALSE means set controls return valid; }
BOOL SMaskEdit::ProcessMask(TCHAR& nChar, int nEndPos) { ASSERT(nEndPos < m_strMask.GetLength()); if (nEndPos < 0 || nEndPos >= m_strMask.GetLength()) return FALSE; // check the key against the mask switch (m_strMask.GetAt(nEndPos)) { case '0': // digit only //completely changed this return _istdigit(nChar); case '9': // digit or space return _istdigit(nChar) || _istspace(nChar); case '#': // digit or space or '+' or '-' return _istdigit(nChar) || (_istspace(nChar) || nChar == _T('-') || nChar == _T('+')); case 'd': // decimal return _istdigit(nChar) || (_istspace(nChar) || nChar == _T('-') || nChar == _T('+') || nChar == _T('.') || nChar == _T(',')); case 'L': // alpha only return IsAlphaChar(nChar); case '?': // alpha or space return IsAlphaChar(nChar) || _istspace(nChar); case 'A': // alpha numeric only return _istalnum(nChar) || IsAlphaChar(nChar); case 'a': // alpha numeric or space return _istalnum(nChar) || IsAlphaChar(nChar) || _istspace(nChar); case '&': // all print character only return IsPrintChar(nChar); case 'H': // hex digit return _istxdigit(nChar); case 'X': // hex digit or space return _istxdigit(nChar) || _istspace(nChar); case '>': if (IsAlphaChar(nChar)) { nChar = ConvertUnicodeAlpha(nChar, TRUE); return TRUE; } return FALSE; case '<': if (IsAlphaChar(nChar)) { nChar = ConvertUnicodeAlpha(nChar, FALSE); return TRUE; } return FALSE; } return FALSE; }
//int _tmain( int argc, TCHAR* argv[] ) int main( void ) { STARTUPINFO si; PROCESS_INFORMATION pi; TCHAR* cmd; BOOL option; BOOL opt_m; BOOL installed; HMODULE ansi; int rc = 0; int argc; LPWSTR* argv = CommandLineToArgvW( GetCommandLineW(), &argc ); if (argc > 1) { if (lstrcmp( argv[1], TEXT("--help") ) == 0 || (argv[1][0] == '-' && (argv[1][1] == '?' || argv[1][1] == 'h')) || (argv[1][0] == '/' && argv[1][1] == '?')) { help(); return rc; } if (lstrcmp( argv[1], TEXT("--version") ) == 0) { _putts( TEXT("ANSICON (" BITS "-bit) version " PVERS " (" PDATE ").") ); return rc; } } option = (argc > 1 && argv[1][0] == '-'); if (option && (_totlower( argv[1][1] ) == 'i' || _totlower( argv[1][1] ) == 'u')) { process_autorun( argv[1][1] ); return rc; } get_original_attr(); opt_m = FALSE; if (option && argv[1][1] == 'm') { WORD attr = 7; if (_istxdigit( argv[1][2] )) { attr = _istdigit( argv[1][2] ) ? argv[1][2] - '0' : (argv[1][2] | 0x20) - 'a' + 10; if (_istxdigit( argv[1][3])) { attr <<= 4; attr |= _istdigit( argv[1][3] ) ? argv[1][3] - '0' : (argv[1][3] | 0x20) - 'a' + 10; } } SetConsoleTextAttribute( hConOut, attr ); opt_m = TRUE; ++argv; --argc; option = (argc > 1 && argv[1][0] == '-'); } installed = (GetEnvironmentVariable( TEXT("ANSICON"), NULL, 0 ) != 0); if (option && argv[1][1] == 'p') { // If it's already installed, there's no need to do anything. if (installed) ; else if (GetParentProcessInfo( &pi )) { pi.hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pi.dwProcessId ); pi.hThread = OpenThread( THREAD_ALL_ACCESS, FALSE, pi.dwThreadId ); SuspendThread( pi.hThread ); Inject( &pi ); ResumeThread( pi.hThread ); CloseHandle( pi.hThread ); CloseHandle( pi.hProcess ); } else { _putts( TEXT("ANSICON: could not obtain the parent process.") ); rc = 1; } } else { ansi = 0; if (!installed) ansi = LoadLibrary( TEXT("ANSI" BITS ".dll") ); if (option && (argv[1][1] == 't' || argv[1][1] == 'T')) { BOOL title = (argv[1][1] == 'T'); if (argc == 2) { argv[2] = L"-"; ++argc; } for (; argc > 2; ++argv, --argc) { if (title) _tprintf( TEXT("==> %s <==\n"), argv[2] ); display( argv[2], title ); if (title) _puttchar( '\n' ); } } else { // Retrieve the original command line, skipping our name and the option. cmd = skip_spaces( skip_arg( skip_spaces( GetCommandLine() ) ) ); if (opt_m) cmd = skip_spaces( skip_arg( cmd ) ); if (cmd[0] == '-' && (cmd[1] == 'e' || cmd[1] == 'E')) { _fputts( cmd + 3, stdout ); if (cmd[1] == 'e') _puttchar( '\n' ); } else if (!isatty( 0 ) && *cmd == '\0') { display( TEXT("-"), FALSE ); } else { if (*cmd == '\0') { cmd = _tgetenv( TEXT("ComSpec") ); if (cmd == NULL) cmd = TEXT("cmd"); } ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); if (CreateProcess( NULL, cmd, NULL,NULL, TRUE, 0, NULL,NULL, &si, &pi )) { SetConsoleCtrlHandler( (PHANDLER_ROUTINE)CtrlHandler, TRUE ); WaitForSingleObject( pi.hProcess, INFINITE ); } else { *skip_arg( cmd ) = '\0'; _tprintf( TEXT("ANSICON: '%s' could not be executed.\n"), cmd ); rc = 1; } } } if (ansi) FreeLibrary( ansi ); } set_original_attr(); return rc; }
int isxdigit(int c) { return _istxdigit(c); }
// ************************************************************************** // OnApply () // // Description: // Apply button event handler. // // Parameters: // none // // Returns: // void // ************************************************************************** void CKServerGetErrorStringDlg::OnApply () { // Initialize some variables needed for request: HRESULT hr = E_FAIL; DWORD dwError = -1; LPWSTR pszError = NULL; bool bIsHex = false; TCHAR cChar; // Look for invalid characters in error code. Loop over // all characters in error code string: for (int i = 0; i < m_strErrorCode.GetLength (); i++) { // Get current character: cChar = m_strErrorCode [i]; // All digits are valid. Other characters may be valid. if (!_istdigit (cChar)) { // If first non-digit character is "x" or "X", then // we are probably looking at a hexidecimal value. In // that case, accept the character, set the bIsHex flag, // and continue checking the string. if (!bIsHex && (cChar == _T('x') || cChar == _T('X'))) bIsHex = true; // If we have seen the "X", and this character is a hex // digit, then it is valid. else if (bIsHex && _istxdigit (cChar)) continue; // Else we have seen the "X", but current character is not // a valid hex digit. Show message box that tells user error // code is invalid the return. else { // "Invalid hexadecimal number." AfxMessageBox (IDS_INVALIDHEXNUMBER); return; } } } // If we make it here, then error code has a valid format. // Convert string to number. if (bIsHex) _stscanf (m_strErrorCode, _T("%X"), &dwError); else dwError = (DWORD) _ttol (m_strErrorCode); // Get the error string: hr = m_pIServer->GetErrorString (dwError, 0, &pszError); // If request succeeded, process the results: if (SUCCEEDED (hr) && pszError != NULL) { m_strErrorDescription = pszError; } // Else if request failed, inform user of problem: else { CString strMsg; // "The server could not resolve this error code." strMsg.Format (IDS_UNABLETORESOLVEERRORCODE); AfxMessageBox (strMsg); } // Free memory server allocated for error string: if (pszError) CoTaskMemFree (pszError); // Reformat the error code as a hex number: m_strErrorCode.Format (_T("0x%08X"), dwError); // Update the controls to show reformatted code and error string: UpdateData (FALSE); }