Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- edit_string_v1.c by Dragan Milicev
- Edit a string in a single line using the left arrow, right arrow,
- home, end, backspace, insert and enter keys.
- Ascii table:
- characters_0 0 - 31 special characters
- characters_1 32 - 47
- 0 - 9 48 - 57 digits
- characters_2 58 - 64
- A - Z 65 - 90 capital letters
- characters_3 91 - 96
- a - z 97 - 122 small letters
- characters_4 123 - 126
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <string.h>
- #include <windows.h> // tracking cursor all the time, for cursor position functions
- // gotoxy(x,y) , wherex() , wherey(), showcursor(), hidecursor()
- #define CTRL_Z 26 // keys Ctrl + Z
- #define CTRL_X 24 // keys Ctrl + X
- #define CTRL_C 3 // keys Ctrl + C
- #define CTRL_V 22 // keys Ctrl + V
- #define BACKSPACE 8 // key Backspace
- #define TAB 9 // key Tab
- #define ENTER 13 // key Enter
- #define ESC 27 // Escape key
- #define UP 72 // up arrow
- #define DOWN 80 // down arrow
- #define LEFT 75 // left arrow
- #define RIGHT 77 // right arrow
- #define PG_UP 73 // Page Up key
- #define PG_DOWN 81 // Page Down down key
- #define HOME 71 // Home key
- #define END 79 // End key
- #define DEL 83 // Delete key
- #define INSERT 82 // Insert key
- // ----------CURSOR CONTROL FUNCTIONS------------------------------------------
- // make console cursor invisible
- void hidecursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 20;
- info.bVisible = FALSE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // make console cursor invisible
- void showcursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 20;
- info.bVisible = TRUE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // make console cursor invisible
- void bigcursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 100;
- info.bVisible = TRUE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // place cursor at position ( x, y ) = ( row, column )
- void gotoxy( int x, int y )
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
- }
- // return x coordinate (column) of current cursor position
- // on failure return -1
- int wherex()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.X;
- }
- // return y coordinate (row) of current cursor position
- // on failure return -1
- int wherey()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.Y;
- }
- // END OF-----CURSOR CONTROL FUNCTIONS------------------------------------------
- // ----------TEXT FOREGROUND AND BACKGROUND COLOR CONTROL FUNCTIONS-------------
- void SetColorAndBackground(int ForgC, int BackC)
- {
- WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
- return;
- } //SetColorAndBackground()
- // END OF----TEXT FOREGROUND AND BACKGROUND COLOR CONTROL FUNCTIONS--------------
- // Edit a string in a single line using the left arrow, right arrow,
- // home, end, backspace, insert and enter keys.
- char *edit_string( int x, int y, int fc, int bc, char str[] )
- {
- int i, c_choice, len, memX, insert_flag=1;
- char *s; // to copy str to s
- s = malloc( strlen(str) + 257 ); // reserve the memory space for copy and edit str
- if( s == NULL ) // checking whether memory reallocation succeeded
- {
- fprintf(stderr, "\n\n malloc() for s error! \n\n");
- exit(EXIT_FAILURE);
- }
- strcpy(s,str); // copy str to s
- gotoxy(x,y);
- SetColorAndBackground(fc,bc); // set colors to foreground and background colors
- printf("%s", s);
- while( 1 ) { // an infinite loop that exits with Enter or Escape
- c_choice = _getch(); // read the keyboard
- switch( c_choice ) { // scroll items and switch choice
- case BACKSPACE : // if Backspace key is pressed
- if(wherex()>x)
- {
- memX = wherex();
- len = strlen(s);
- for(i=memX-x-1; i<len; i++) // moving the characters to the left
- s[i] = s[i+1];
- s[i] = '\0'; // finish the new string s
- gotoxy(x,y); // erase last print of string s
- SetColorAndBackground(7,0); // reset colors to system default colors
- for(i=0; i<len; i++)
- printf(" ");
- gotoxy(x,y); // print string s
- SetColorAndBackground(fc,bc); // set colors to foreground and background colors
- printf("%s", s);
- gotoxy(memX-1,y );
- }
- break;
- case ENTER : // if Enter key is pressed, exit from editing and return(s)
- SetColorAndBackground(7,0); // reset colors to system default colors
- if( strlen(str) < strlen(s) ) // check memory space to copy s to str
- {
- realloc(str, strlen(s)+1); // realloc the memory space to copy s to str
- if( str == NULL ) // Checking whether memory reallocation succeeded
- {
- fprintf(stderr, "\n\n realloc() for str error! \n\n");
- exit(EXIT_FAILURE);
- }
- }
- strcpy(str,s);
- free(s); // free memory space reserved for s
- showcursor();
- return(str);
- break;
- case ESC : // if Escape key is pressed, exit from editing and return(0)
- SetColorAndBackground(7,0); // reset colors to system default colors
- free(s); // free memory space reserved for s
- showcursor();
- return(str); // return original string str
- break;
- case 0 : // if pressed a function key
- case 224 : // if pressed a special function key,
- c_choice = _getch(); // let's empty that 0 or 224 and
- // analyze which function key is pressed
- if( c_choice == LEFT ) // if pressed left arrow
- {
- if(wherex()>x)
- gotoxy(wherex()-1,y );
- } // if( c_choice == LEFT )
- if( c_choice == RIGHT ) // if pressed right arrow
- {
- if(wherex()<x+strlen(s))
- gotoxy(wherex()+1,y );
- } // if( c_choice == RIGHT )
- if( c_choice == HOME ) // if Home key is pressed
- {
- gotoxy(x,y);
- } // if( c_choice == HOME )
- if( c_choice == END ) // if End key is pressed
- {
- gotoxy(x+strlen(s),y );
- } // if( c_choice == END )
- if( c_choice == INSERT ) // if Insert key is pressed
- {
- if( insert_flag )
- insert_flag = 0;
- else
- insert_flag = 1;
- if( insert_flag )
- showcursor();
- else
- bigcursor();
- } // if( c_choice == INSERT )
- if( c_choice == DEL ) // if Delete key is pressed
- {
- if(wherex()>=x)
- {
- memX = wherex();
- len = strlen(s);
- for(i=memX-x; i<len; i++) // moving the characters to the left
- s[i] = s[i+1];
- s[i] = '\0'; // finish the new string s
- gotoxy(x,y); // erase last print of string s
- SetColorAndBackground(7,0); // reset colors to system default colors
- for(i=0; i<len; i++)
- printf(" ");
- gotoxy(x,y); // print string s
- SetColorAndBackground(fc,bc); // set colors to foreground and background colors
- printf("%s", s);
- gotoxy(memX,y );
- }
- } // if( c_choice == DEL )
- break;
- default:
- if( c_choice >= 32 && c_choice <= 126 )
- {
- if ( insert_flag ) // to insert a character to string s
- {
- memX = wherex();
- len = strlen(s);
- for(i=len+2; i>=memX-x; i--) // moving the characters from end to the right
- s[i+1] = s[i];
- s[memX-x] = c_choice; // new character
- gotoxy(x,y); // erase last print of string s
- for(i=0; i<len; i++)
- printf(" ");
- gotoxy(x,y); // print the new string s
- printf("%s", s);
- gotoxy(memX+1,y );
- } // if ( insert_flag )
- else // to overwrite a character to string s
- {
- memX = wherex();
- len = strlen(s);
- s[memX-x] = c_choice; // new character
- s[len+1] = '\0';
- gotoxy(x,y); // erase last print of string s
- for(i=0; i<len; i++)
- printf(" ");
- gotoxy(x,y); // print the new string s
- printf("%s", s);
- gotoxy(memX+1,y );
- } // else // to overwrite a character to string s
- } // if( ... )
- break;
- } // switch( c_choice )
- } // while( 1 )
- } // edit_string()
- int main(void)
- {
- int x=10, y=5, fc=9, bc=11;
- char str[] = "This is string to be edited.";
- gotoxy(x-1,y-2);
- printf("|%s|", str);
- edit_string( x, y, fc, bc, str );
- gotoxy(x-1,y+2);
- printf("|%s| \n\n", str);
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment