int _tmain(int argc, _TCHAR* argv[]) { _cputs(buffer); _putch('\r'); _putch('\n'); _cputs(buffer1); //Displays buffer1 message a = _getche(); //Puts first input in variable a //Accounts for multiple numbered inputs while(a>='0' && a <= '9') { _asm sub a, 30h;// b = b * 10 + a; a = _getche(); } _putch('\r'); _putch('\n'); _cputs(buffer2); //Displays buffer2 message c = _getche(); //Puts second input in c //Accounts for multiple numbered inputs while(c>='0' && c <= '9') { _asm sub c, 30h;// d = d * 10 + c; c = _getche(); } _putch('\r'); _putch('\n'); //Calls the subtraction function sub(b,d); _putch('\r'); _putch('\n'); getche(); return 0; }
/* were just going to fake it here and get input from the keyboard */ char *get_tty_password(const char *opt_message) { char to[80]; char *pos=to,*end=to+sizeof(to)-1; int i=0; DBUG_ENTER("get_tty_password"); _cputs(opt_message ? opt_message : "Enter password: "******"\b \b"); pos--; continue; } } if (tmp == '\n' || tmp == '\r' || tmp == 3) break; if (iscntrl(tmp) || pos == end) continue; _cputs("*"); *(pos++) = tmp; } while (pos != to && isspace(pos[-1]) == ' ') pos--; /* Allow dummy space at end */ *pos=0; _cputs("\n"); DBUG_RETURN(my_strdup(to,MYF(MY_FAE))); }
//-------------------------------------------------------------- void clear_line() //clear to end of line { //80 spaces //.....1234567890123456789012345678901234567890 //.....0........1.........2.........3.........4 _cputs(" "); _cputs(" "); }
/* were just going to fake it here and get input from the keyboard */ void get_tty_password_buff(const char *opt_message, char *to, size_t length) { HANDLE consoleinput; DWORD oldstate; char *pos=to,*end=to+length-1; int i=0; consoleinput= CreateFile("CONIN$", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ , NULL, OPEN_EXISTING, 0, NULL); if (consoleinput == NULL || consoleinput == INVALID_HANDLE_VALUE) { /* This is a GUI application or service without console input, bail out. */ *to= 0; return; } _cputs(opt_message ? opt_message : "Enter password: "******"\b \b"); pos--; continue; } } if (tmp == '\n' || tmp == '\r') break; if (iscntrl(tmp) || pos == end) continue; _cputs("*"); *(pos++) = tmp; } /* Reset console mode after password input. */ SetConsoleMode(consoleinput, oldstate); CloseHandle(consoleinput); *pos=0; _cputs("\n"); }
static RD_BOOL l_ui_authenticate(struct rdp_inst * inst) { if (!AllocConsole()) _cputs("\n"); /* reusing debug console */ _cputs("Please enter NLA login credential.\n"); _cputs("User name:"); if (inst->settings->username[0] == 0) getsn(inst->settings->username, sizeof(inst->settings->username), 0); else _cputs(inst->settings->username); _putch('\n'); _cputs("Domain:"); if (inst->settings->domain[0] == 0) getsn(inst->settings->domain, sizeof(inst->settings->domain), 0); else _cputs(inst->settings->domain); _putch('\n'); _cputs("Password:"******"***"); _putch('\n'); return 1; }
static char *get_password(FILE *file, char *buffer, int length) #endif { char inChar; int CharsProcessed= 1; #ifdef _WIN32 DWORD Offset= 0; #else int Offset= 0; #endif memset(buffer, 0, length); do { #ifdef _WIN32 if (!ReadConsole(Hdl, &inChar, 1, &CharsProcessed, NULL) || !CharsProcessed) break; #else inChar= fgetc(file); #endif switch(inChar) { case '\b': /* backslash */ if (Offset) { /* cursor is always at the end */ Offset--; buffer[Offset]= 0; #ifdef _WIN32 _cputs("\b \b"); #endif } break; case '\n': case '\r': break; default: buffer[Offset]= inChar; if (Offset < length - 2) Offset++; #ifdef _WIN32 _cputs("*"); #endif break; } } while (CharsProcessed && inChar != '\n' && inChar != '\r'); return buffer; }
/********************************************************************* * _cprintf (MSVCRT.@) */ int CDECL _cprintf(const char* format, ...) { char buf[2048], *mem = buf; int written, resize = sizeof(buf), retval; __ms_va_list valist; __ms_va_start( valist, format ); /* There are two conventions for snprintf failing: * Return -1 if we truncated, or * Return the number of bytes that would have been written * The code below handles both cases */ while ((written = MSVCRT_vsnprintf( mem, resize, format, valist )) == -1 || written > resize) { resize = (written == -1 ? resize * 2 : written + 1); if (mem != buf) MSVCRT_free (mem); if (!(mem = MSVCRT_malloc(resize))) return MSVCRT_EOF; __ms_va_start( valist, format ); } __ms_va_end(valist); LOCK_CONSOLE; retval = _cputs( mem ); UNLOCK_CONSOLE; if (mem != buf) MSVCRT_free (mem); return retval; }
int main() { _gotoxy(10, 10); //Does not compile _textcolor(YELLOW); //Does not compile _textbackground(BLUE); //Does not compile _cputs("Hello"); }
void cput(const char *str) { #ifndef __WINDOWS__ write(STDOUT_FILENO,str, strlen(str)); #else _cputs(str); #endif }
void cputn(const char *str, int len) { #ifndef __WINDOWS__ write(STDOUT_FILENO,str, len); #else char buf[128]; memset(buf,0,sizeof(buf)); strncpy(buf,str,len); buf[len] = 0; _cputs(buf); #endif }
int Console::OutTxt(const char * format , ...) { va_list arglist; char buffer[1024]; int len; va_start(arglist, format); assert(format); len = _vsnprintf(buffer, sizeof(buffer)-1, format, arglist); assert(len >= 0); return _cputs(buffer) == 0 ? len : -1; }
//-------------------------------------------------------------- void draw_rectangle(int left, int top, int right, int bottom) { char temp[80]; int width = right - left + 1; int j; for(j=0; j<width; j++) //string of squares temp[j] = fill_char; temp[j] = 0; //null for(int y=top; y<=bottom; y++) //stack of strings { set_cursor_pos(left, y); _cputs(temp); } }
int SDL_main(int arc, char** argv) { bool quit = false; SDL_Event e; if (!mysty::Init::Initialize()) { _cputs("Failed to initialize mysty engine"); quit = true; } while (!quit) { while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: quit = true; break; case SDL_KEYDOWN: switch (e.key.keysym.sym) { case SDLK_ESCAPE: quit = true; break; } } } } return 0; }
void puts(const char *s) { _cputs(s); }
/* Function for accepting password input from users */ char* getpasswd(const char *prompt) { static char pwbuf[MAX_PASS_LEN + 1] = {0}; char *ptr; int c; #ifndef WIN32 FILE *fp; sigset_t sig, old_sig; struct termios ts, old_ts; if((fp = fopen(ctermid(NULL), "r+")) == NULL) return(NULL); setbuf(fp, NULL); /* Setup blocks for SIGINT and SIGTSTP and save the original signal * mask. */ sigemptyset(&sig); sigaddset(&sig, SIGINT); sigaddset(&sig, SIGTSTP); sigprocmask(SIG_BLOCK, &sig, &old_sig); /* Save current tty state for later restoration after we disable echo * of characters to the tty. */ tcgetattr(fileno(fp), &ts); old_ts = ts; ts.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); tcsetattr(fileno(fp), TCSAFLUSH, &ts); fputs(prompt, fp); #endif /* Read in the password. */ ptr = pwbuf; #ifdef WIN32 _cputs(prompt); while((c = _getch()) != '\r') { /* Handle a backspace without backing up too far. */ if(c == '\b') { if(ptr != pwbuf) *ptr--; continue; } /* Handle a Ctrl-U to clear the password entry and start over * (like it works under Unix). */ if(c == 0x15) { ptr = pwbuf; continue; } #else while((c = getc(fp)) != EOF && c != '\n') { #endif if(ptr < &pwbuf[MAX_PASS_LEN]) *ptr++ = c; } /* Null terminate the password. */ *ptr = 0; #ifndef WIN32 /* we can go ahead and echo out a newline. */ putc('\n', fp); /* Restore our tty state and signal handlers. */ tcsetattr(fileno(fp), TCSAFLUSH, &old_ts); sigprocmask(SIG_BLOCK, &old_sig, NULL); fclose(fp); #else /* In Windows, it would be a CR-LF */ _putch('\r'); _putch('\n'); #endif return(pwbuf); } /* Function for accepting password input from from a file */ char* getpasswd_file(const char *pw_file, const char *server_str) { FILE *pwfile_ptr; unsigned int numLines = 0, i = 0, found_dst; static char pwbuf[MAX_PASS_LEN + 1] = {0}; char conf_line_buf[MAX_LINE_LEN] = {0}; char tmp_char_buf[MAX_LINE_LEN] = {0}; char *lptr; if ((pwfile_ptr = fopen(pw_file, "r")) == NULL) { fprintf(stderr, "Could not open config file: %s\n", pw_file); exit(1); } while ((fgets(conf_line_buf, MAX_LINE_LEN, pwfile_ptr)) != NULL) { numLines++; conf_line_buf[MAX_LINE_LEN-1] = '\0'; lptr = conf_line_buf; memset(tmp_char_buf, 0x0, MAX_LINE_LEN); while (*lptr == ' ' || *lptr == '\t' || *lptr == '=') lptr++; /* Get past comments and empty lines. */ if (*lptr == '#' || *lptr == '\n' || *lptr == '\r' || *lptr == '\0' || *lptr == ';') continue; /* Look for a line like "<SPA destination IP>: <password>" - this allows * multiple keys to be placed within the same file, and the client will * reference the matching one for the SPA server we are contacting */ found_dst = 1; for (i=0; i < strlen(server_str); i++) if (*lptr++ != server_str[i]) found_dst = 0; if (! found_dst) continue; if (*lptr == ':') lptr++; else continue; /* Skip whitespace until we get to the password */ while (*lptr == ' ' || *lptr == '\t' || *lptr == '=') lptr++; i = 0; while (*lptr != '\0' && *lptr != '\n') { pwbuf[i] = *lptr; lptr++; i++; } pwbuf[i] = '\0'; } fclose(pwfile_ptr); if (pwbuf[0] == '\0') { fprintf(stderr, "Could not get password for IP: %s from: %s\n", server_str, pw_file); exit(1); } return pwbuf; }
int main (int argc, char *argv[]) { #if defined(_MT) || defined(_REENTRANT) int min_threads, max_threads ; int num_rounds ; int chperthread ; #endif unsigned seed=12345 ; int num_chunks=10000; long sleep_cnt; int matches; if (argc > 7) { sleep_cnt = atoi(argv[1]); min_size = atoi(argv[2]); max_size = atoi(argv[3]); chperthread = atoi(argv[4]); num_rounds = atoi(argv[5]); seed = atoi(argv[6]); max_threads = atoi(argv[7]); min_threads = max_threads; goto DoneWithInput; } #if defined(_MT) || defined(_REENTRANT) //#ifdef _MT printf( "\nMulti-threaded test driver \n") ; #else printf( "\nSingle-threaded test driver \n") ; #endif printf("C version (malloc and free)\n") ; printf("runtime (sec): ") ; /* 15 seconds */ if ((matches = scanf ("%ld", &sleep_cnt)) != 1) { printf("error scanning stdin for sleep_cnt - exiting\n"); return(1); } printf("chunk size (min,max): ") ; /* 8 40 */ if ((matches = scanf("%d %d", &min_size, &max_size )) != 2) { printf("error scanning stdin for chunk size (min,max) - exiting\n"); return(1); } #if defined(_MT) || defined(_REENTRANT) //#ifdef _MT printf("threads (min, max): ") ; /* same, 1 1, 2 2, etc. */ if ((matches = scanf("%d %d", &min_threads, &max_threads)) != 2) { printf("error scanning stdin for threads (min,max) - exiting\n"); return(1); } printf("chunks/thread: ") ; if ((matches = scanf("%d", &chperthread )) != 1) { /* 10K */ printf("error scanning stdin for chunks/thread - exiting\n"); return(1); } printf("no of rounds: ") ; if ((matches = scanf("%d", &num_rounds )) != 1) { /* 10 */ printf("error scanning stdin for no of rounds - exiting\n"); return(1); } num_chunks = max_threads*chperthread ; #else printf("no of chunks: ") ; if ((matches = scanf("%d", &num_chunks )) != 1) { printf("error scanning stdin for no of chunks - exiting\n"); return(1); } #endif printf("random seed: ") ; if ((matches = scanf("%d", &seed)) != 1) { printf("error scanning stdin for random seed - exiting\n"); return(1); } printf("\n"); DoneWithInput: if( num_chunks > MAX_BLOCKS ){ printf("Max %d chunks - exiting\n", MAX_BLOCKS ) ; return(1) ; } pthread_setconcurrency (max_threads); lran2_init(&rgen, seed) ; // Call allocator-specific initialization function mm_init(); numCPU=getNumProcessors(); #if defined(_MT) || defined(_REENTRANT) //#ifdef _MT runthreads(sleep_cnt, min_threads, max_threads, chperthread, num_rounds) ; #else runloops(sleep_cnt, num_chunks ) ; #endif #ifdef _DEBUG _cputs("Hit any key to exit...") ; (void)_getch() ; #endif return(0) ; } /* main */
/** * @brief Function for accepting password input from users * * The functions reads chars from a buffered stream and store them in a buffer of * chars. If a file descriptor is supplied then, the password is read from * the associated stream, otherwise a new buffered stream is created and a * prompt is displayed to the user. * * @param prompt String displayed on the terminal to prompt the user for a * password or an encryption key * @param fd File descriptor to use to read the pasword from. If fd is set * to FD_INVALID, then a new stream is opened. * * @return NULL if a problem occured or the user killed the terminal (Ctrl-C)\n * otherwise the password - empty password is accepted. */ char* getpasswd(const char *prompt, int fd) { char *ptr; #ifndef WIN32 sigset_t sig, old_sig; struct termios ts, old_ts; FILE *fp; /* If a valid file descriptor is supplied, we try to open a stream from it */ if (FD_IS_VALID(fd)) { fp = fdopen(fd, "r"); if (fp == NULL) { log_msg(LOG_VERBOSITY_ERROR, "getpasswd() - " "Unable to create a stream from the file descriptor : %s", strerror(errno)); exit(EXIT_FAILURE); } } /* Otherwise we are going to open a new stream */ else { if((fp = fopen(ctermid(NULL), "r+")) == NULL) return(NULL); setbuf(fp, NULL); /* Setup blocks for SIGINT and SIGTSTP and save the original signal * mask. */ sigemptyset(&sig); sigaddset(&sig, SIGINT); sigaddset(&sig, SIGTSTP); sigprocmask(SIG_BLOCK, &sig, &old_sig); /* * Save current tty state for later restoration after we : * - disable echo of characters to the tty * - disable signal generation * - disable cannonical mode (input read line by line mode) */ tcgetattr(fileno(fp), &ts); old_ts = ts; ts.c_lflag &= ~(ECHO | ICANON | ISIG); tcsetattr(fileno(fp), TCSAFLUSH, &ts); fputs(prompt, fp); } #else _cputs(prompt); #endif /* Read the password */ ptr = read_passwd_from_stream(fp); #ifndef WIN32 /* If we used a new buffered stream */ if (FD_IS_VALID(fd) == 0) { /* we can go ahead and echo out a newline. */ putc(PW_LF_CHAR, fp); /* Restore our tty state and signal handlers. */ tcsetattr(fileno(fp), TCSAFLUSH, &old_ts); sigprocmask(SIG_BLOCK, &old_sig, NULL); } fclose(fp); #else /* In Windows, it would be a CR-LF */ _putch(PW_CR_CHAR); _putch(PW_LF_CHAR); #endif return (ptr); }
int main (int argc, char *argv[]) { #if defined(USE_LFH) && defined(_WIN32) // Activate 'Low Fragmentation Heap'. ULONG info = 2; HeapSetInformation (GetProcessHeap(), HeapCompatibilityInformation, &info, sizeof(info)); #endif #if 0 // defined(__SVR4) { psinfo_t ps; int pid = getpid(); char fname[255]; sprintf (fname, "/proc/%d/psinfo", pid); // sprintf (fname, "/proc/self/ps"); FILE * f = fopen (fname, "rb"); printf ("opening %s\n", fname); if (f) { fread (&ps, sizeof(ps), 1, f); printf ("resident set size = %dK\n", ps.pr_rssize); fclose (f); } } #endif #if defined(_MT) || defined(_REENTRANT) int min_threads, max_threads ; int num_rounds ; int chperthread ; #endif unsigned seed=12345 ; int num_chunks=10000; long sleep_cnt; if (argc > 7) { sleep_cnt = atoi(argv[1]); min_size = atoi(argv[2]); max_size = atoi(argv[3]); chperthread = atoi(argv[4]); num_rounds = atoi(argv[5]); seed = atoi(argv[6]); max_threads = atoi(argv[7]); min_threads = max_threads; printf ("sleep = %ld, min = %d, max = %d, per thread = %d, num rounds = %d, seed = %d, max_threads = %d, min_threads = %d\n", sleep_cnt, min_size, max_size, chperthread, num_rounds, seed, max_threads, min_threads); goto DoneWithInput; } #if defined(_MT) || defined(_REENTRANT) //#ifdef _MT printf( "\nMulti-threaded test driver \n") ; #else printf( "\nSingle-threaded test driver \n") ; #endif #ifdef CPP printf("C++ version (new and delete)\n") ; #else printf("C version (malloc and free)\n") ; #endif printf("runtime (sec): ") ; scanf ("%ld", &sleep_cnt); printf("chunk size (min,max): ") ; scanf("%d %d", &min_size, &max_size ) ; #if defined(_MT) || defined(_REENTRANT) //#ifdef _MT printf("threads (min, max): ") ; scanf("%d %d", &min_threads, &max_threads) ; printf("chunks/thread: ") ; scanf("%d", &chperthread ) ; printf("no of rounds: ") ; scanf("%d", &num_rounds ) ; num_chunks = max_threads*chperthread ; #else printf("no of chunks: ") ; scanf("%d", &num_chunks ) ; #endif printf("random seed: ") ; scanf("%d", &seed) ; DoneWithInput: if( num_chunks > MAX_BLOCKS ){ printf("Max %d chunks - exiting\n", MAX_BLOCKS ) ; return(1) ; } #ifndef __WIN32__ #ifdef __SVR4 pthread_setconcurrency (max_threads); #endif #endif lran2_init(&rgen, seed) ; // init_space = CountReservedSpace() ; #if defined(_MT) || defined(_REENTRANT) //#ifdef _MT runthreads(sleep_cnt, min_threads, max_threads, chperthread, num_rounds) ; #else runloops(sleep_cnt, num_chunks ) ; #endif #ifdef _DEBUG _cputs("Hit any key to exit...") ; (void)_getch() ; #endif return 0; } /* main */
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { int status = 0; try { Consumer_Client client; status = client.init (argc, argv); if (status != 0) { ACE_ERROR_RETURN ((LM_ERROR, "Error: Client init failed.\n"), 1); } CosNotifyChannelAdmin::EventChannel_var ec = client.create_event_channel ("MyEventChannel", 0); #ifdef TEST_QOS_MAX_QUEUE_LENGTH ACE_OS::printf ("%s: setting max queue length to 1000\n", argv[0]); CosNotification::AdminProperties properties(1); properties.length(1); properties[0].name = CORBA::string_dup (CosNotification::MaxQueueLength); properties[0].value <<= 1000; ec->set_admin(properties); #endif //TEST_QOS_MAX_QUEUE_LENGTH CORBA::ORB_ptr orb = client.orb (); CORBA::Object_var object = orb->string_to_object (ior); MonitorTestInterface_var sig = MonitorTestInterface::_narrow (object.in ()); if (CORBA::is_nil (sig.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "Error: Narrow failed.\n"), 1); } CosNotifyChannelAdmin::ConsumerAdmin_var admin = create_consumeradmin (ec.in ()); if (CORBA::is_nil (admin.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "Error: nil ConsumerAdmin.\n"), 1); } create_consumers (admin.in (), &client); sig->running (MonitorTestInterface::Consumer); ACE_DEBUG ((LM_DEBUG, "\nConsumer waiting for events...\n")); FILE* ready_file = ACE_OS::fopen (ACE_TEXT_ALWAYS_CHAR(ready_output_file), "w"); if (ready_file == 0) { ACE_ERROR_RETURN ((LM_ERROR, "Cannot open ready file for writing\n"), 1); } ACE_OS::fprintf (ready_file, "ready\n"); ACE_OS::fclose (ready_file); client.ORB_run (); #ifdef PAUSE_ON_EXIT _cputs( "All events received. Still connected.\n"); _cputs( "Hit a key to exit consumer: " ); _getch(); #endif // PAUSE_ON_EXIT ACE_DEBUG ((LM_DEBUG, "Consumer done.\n")); consumer_1->disconnect (); ec->destroy (); sig->finished (MonitorTestInterface::Consumer); } catch (const CORBA::Exception& e) { e._tao_print_exception ("Consumer Error: "); status = 1; } return status; }
void moMidiDevice::midiCallback(HMIDIIN handle, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2) { LPMIDIHDR lpMIDIHeader; unsigned char * ptr; TCHAR buffer[80]; unsigned char bytes; moMidiDevice* pMidiDevice = NULL; pMidiDevice = (moMidiDevice*)dwInstance; /* Determine why Windows called me */ switch (uMsg) { /* Received some regular MIDI message */ case MIM_DATA: { /* Display the time stamp, and the bytes. (Note: I always display 3 bytes even for Midi messages that have less) */ sprintf(&buffer[0], "0x%08X 0x%02X 0x%02X 0x%02X\0", dwParam2, dwParam1 & 0x000000FF, (dwParam1>>8) & 0x000000FF, (dwParam1>>16) & 0x000000FF); _cputs(&buffer[0]); moMidiData mididata; mididata.m_Type = MOMIDI_ROTARY;//???? mididata.m_Channel = (dwParam1 & 0x000000FF) - 175;//channel 1 = 0xB0 mididata.m_CC = (dwParam1>>8) & 0x000000FF; mididata.m_Val = (dwParam1>>16) & 0x000000FF; if (pMidiDevice) pMidiDevice->NewData( mididata ); break; } /* Received all or part of some System Exclusive message */ case MIM_LONGDATA: { /* If this application is ready to close down, then don't midiInAddBuffer() again */ if (!(pMidiDevice->SysXFlag & 0x80)) { /* Assign address of MIDIHDR to a LPMIDIHDR variable. Makes it easier to access the field that contains the pointer to our block of MIDI events */ lpMIDIHeader = (LPMIDIHDR)dwParam1; /* Get address of the MIDI event that caused this call */ ptr = (unsigned char *)(lpMIDIHeader->lpData); /* Is this the first block of System Exclusive bytes? */ if (!pMidiDevice->SysXFlag) { /* Print out a noticeable heading as well as the timestamp of the first block. (But note that other, subsequent blocks will have their own time stamps). */ printf("*************** System Exclusive **************\r\n0x%08X ", dwParam2); /* Indicate we've begun handling a particular System Exclusive message */ pMidiDevice->SysXFlag |= 0x01; } /* Is this the last block (ie, the end of System Exclusive byte is here in the buffer)? */ if (*(ptr + (lpMIDIHeader->dwBytesRecorded - 1)) == 0xF7) { /* Indicate we're done handling this particular System Exclusive message */ pMidiDevice->SysXFlag &= (~0x01); } /* Display the bytes -- 16 per line */ bytes = 16; while((lpMIDIHeader->dwBytesRecorded--)) { if (!(--bytes)) { sprintf(&buffer[0], "0x%02X\r\n", *(ptr)++); bytes = 16; } else sprintf(&buffer[0], "0x%02X ", *(ptr)++); _cputs(&buffer[0]); } /* Was this the last block of System Exclusive bytes? */ if (!pMidiDevice->SysXFlag) { /* Print out a noticeable ending */ _cputs("\r\n******************************************\r\n"); } /* Queue the MIDIHDR for more input */ midiInAddBuffer(handle, lpMIDIHeader, sizeof(MIDIHDR)); } break; } /* Process these messages if you desire */ /* case MIM_OPEN: case MIM_CLOSE: case MIM_ERROR: case MIM_LONGERROR: case MIM_MOREDATA: break; */ } }
void main( void ) { int ch = '?'; while ( ( ch != '1' ) && ( ch != '2' ) && ( ch != 'B' ) && ( ch != 'b' ) ) { _cputs( "\nType '1' for binary code to Gray code conversion\nType '2' for Gray code to binary code conversion\nType 'B' to say good-bye\n" ); ch = _getche(); } bool bBinaryToGray = true; switch ( ch ) { case 'B': case 'b': return; break; case '1': bBinaryToGray = true; break; case '2': bBinaryToGray = false; break; } char inputBuffer[ MAX_NUMBER_OF_BIT + 10]; for ( ; ; ) { if ( bBinaryToGray ) printf( "\nEnter binary code up to %d bits(or type 'B' + [Enter] to say good-bye):\n\n", MAX_NUMBER_OF_BIT ); else printf( "\nEnter Gray code up to %d bits(or type 'B' + [Enter] to say good-bye):\n\n", MAX_NUMBER_OF_BIT ); memset( inputBuffer, 0, sizeof( inputBuffer ) ); fgets( inputBuffer, sizeof( inputBuffer ), stdin ); if ( ( inputBuffer[0] == 'B' ) || ( inputBuffer[0] == 'b' ) ) break; unsigned short nInputValue = 0; int nInputLength = 0; bool bInputValid = true; int i = 0; while ( ( bInputValid == true ) && ( i < MAX_NUMBER_OF_BIT ) && ( inputBuffer[i] != 0x0A ) && ( inputBuffer[i] != 0x0D ) ) { nInputValue = nInputValue << 1; nInputLength = nInputLength + 1; switch ( inputBuffer[i] ) { case '0': break; case '1': nInputValue = nInputValue | 1; break; default: bInputValid = false; break; } i = i + 1; } if ( nInputLength < 1 ) continue; if ( bInputValid == false ) { printf( "\nInvalid input.\n" ); continue; } char outputBuffer[ MAX_NUMBER_OF_BIT + 1]; if ( bBinaryToGray ) { unsigned short nConvertedValue = BinaryToGray( nInputValue ); ConvertIntToBinaryString( nConvertedValue, nInputLength, outputBuffer ); printf( "\nGray code: %s\n", outputBuffer ); } else { unsigned short nConvertedValue = GrayToBinary( nInputValue ); ConvertIntToBinaryString( nConvertedValue, nInputLength, outputBuffer ); printf( "\nBinary code: %s\n", outputBuffer ); } } }