Thread: Struggling with using TAB along with insert to move characters in a string.

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Struggling with using TAB along with insert to move characters in a string.

    Code:
    case TAB_KEY:
                for(j=0;j<TAB_SIZE;j++){
                if(!*ins){
                  if(ch =32){
    				          for (i = strlen(str); i >= ctr; i--){
    	                     str[i + 1] = str[i];
    				}
    	            str[ctr] = ch;
    				if (*curpos < flen - 1)
    				{
    					(*curpos)++;
    					ctr++;
                                   }
                       }
    }
    I am designing a single line text editor using C.

    For you guys to understand

    TAB_SIZE is defined in a define statement which is between either 2 or 8. *ins is a pointer flag for insert mode or not. ch is the ASCII character for space which is integer 32. *curpos is a pointer to the current cursor position. str is the string and flen is the field length. CTR is an integer (counter) just set to 0 initially.

    What this code will do will shift all characters to the right, if the cursor is set to 0 on the first character and put the number of TAB_SIZE spaces before the characters. This is good, if my TAB_SIZE is 4 and I hit TAB with insert on, characters will be shifted, 4 spaces inserted before the string.

    When I have my cursor in the middle of the string and hit TAB, this is where I am confused, I am not sure how I can split the string at the cursor position and shift the characters right with the number of TAB_SIZE spaces.

    I would appreciate any tips for anyone who is good at character manipulation in a Win32 console.

    Thanks in advance.
    Last edited by INFERNO2K; 10-13-2005 at 04:05 PM.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    You should use the current position index (which I guess, in your case, is (curpos - str)) to calculate where the character at the cursor will end up, that is, how many spaces to insert. Then you copy the stuff following the cursor position up the number of positions you need to move them by and fill the area left "vacant" by spaces.

    The following function assumes that the line buffer is large enough. In actual practice, you will want to make sure you don't overrun the end of the buffer. Also, the function memmove, which I'm using here, checks for overlap and copies in the right direction.
    Code:
    char *tab_insert(char *buf, char *curpos, int tabsize)
    {
        int colpos = curpos - buf;  /* get the column position */
        int numspaces = (curpos + 1 + tabsize) % tabsize; /* how many spaces we insert */
        int tail_len = strlen(curpos); /* how many characters follow the cursor? */
        /* copy everything following the cursor to the right by numspaces positions */
        memmove(&curpos[numspaces], curpos, tail_len + 1); /* make sure you copy the nul */
        while (numspaces > 0)
        {
            *curpos++ = 0x20;  /* put a space in place */
            --numspaces;
        }
        return curpos;
    }
    If you don't understand the pointer arithmetic going on in the first argument to the call to memmove(), it gets the address of the character in the buffer numspaces to the "right" of the current cursor pointer.

    I have not tested this code, but it should be correct, or at least close enough to use as a model. The fuction returns the new cursor position.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if(ch =32){
    This assigns the value of 32 to 'ch'. It isn't doing a comparison check. Since it's non-zero, it will always evaluate to true, executing the following code block.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Turning your compiler warnings on should advise you of these situations. You'll see this if you use gcc:
    Code:
    itsme@itsme:~/C$ cat truthstatement.c
    int main(void)
    {
      int a;
    
      if(a = 1)
        ;
    
      return 0;
    }
    itsme@itsme:~/C$ gcc -Wall truthstatement.c -o truthstatement
    truthstatement.c: In function `main':
    truthstatement.c:5: warning: suggest parentheses around assignment used as truth value
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    case TAB_KEY:
    Since the code is in a switch statement, you might want a break at the end.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM