Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Question about chars
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: Sat Nov 22, 2008 1:10 pm   Post subject: Question about chars

Hey i'm making a small one line text editor.
in my code i have insert mode and overstrike mode.

so lets say i have some text: abcdefX Y

String length is 20 chars for example

how do i make it add characters to it after the X(end of text, but not the length of the whole string, just used as example)
it works perfectly when i try to edit the text itself or enter the characters on the X or before, but when i move to Y for example it doesn't display the characters i enter.

if you need to see the code i'll post it up.
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Sat Nov 22, 2008 2:47 pm   Post subject: RE:Question about chars

I am unsure of your question. Are you wondering how to insert text into the character array? Or is this a UI issue?

And code would be nice.
JR




PostPosted: Sat Nov 22, 2008 4:36 pm   Post subject: Re: Question about chars

this is not UI, just a simple console 1 line editor. lets say the Array is 50 chars, but only 30 are used i can insert chars into the 31st position, but if i move over to the 32nd -49th i cant insert anything.

here is my edit function where all the text is inserted etc.

se - the string
slen - string length
flen is the field length, i use a field to where i write the text.
*start is where the cursor starts in the field
*offset is the location of the text corresponding to the field
ctype - is for later use, not implemented yet.

code:
int dt_edit(char *se, int slen, int row, int col, int flen,
                        int *start, int *offset, int *insert, int ctype){
                                int done = 0;
                                int ch = 0;
                                int OldStart = *start;
                                char Oldstring[slen];
                                int i =0;
                                int ii;
                                int len = 0;
                                strcpy(Oldstring,se);





                                while(!done){
                                        dt_draw(se + *offset,row, col, flen);
                                        dt_cursor(row, col + *start);
                                        ch = dt_getkey();
                                        switch(ch){
           case ESC:
                   *start = OldStart;
                   strcpy(se,Oldstring);
                   done = 1;
                   break;
           case RIGHT:
                   if(*start != flen)
                           (*start)++;

                   else if(*start+*offset < slen)
                           (*offset)++;

                   break;

           case DEL:
                   for(i=*start+*offset;se[i];i++)
                           se[i]=se[i+1];
                   break;

           case LEFT:
                   if(*start > 0)
                           (*start)--;
                   else if(*offset > 0)
                           (*offset)--;
                   break;
           case BS:
                   len = strlen(se) - 1;
                   if(*offset+*start != *offset)
                   {
                           for(i=*start+*offset;se[i]-1;i++)
                           {
                                   se[i-1] = se[i];

                           }
                           *start = *start - 1;
                   }
                   else if(*offset+*start == *offset && strlen(se) !=0 && *offset >0)
                   {

                           (*offset)--;
                           for(i=*offset+1;se[i];i++)
                           {
                                   se[i-1] = se[i];
                           }

                           se[len] = 0;

                           if(*offset + *start !=0 && *offset >2){
                                   (*offset) -= 2;
                                   (*start)  +=2;
                           }
                           else if(*offset != 0)
                           { (*offset)--;
                           (*start)++;}
                   }



                   break;
           case HOME:
                   *offset = *start = 0;
                   break;
           case INS:
                   *insert = !(*insert);
                   /*if(*insert == 0){
                   *insert = 1;
                   }
                   else{
                   *insert = 0;
                   }*/
                   break;

           case ENTER:
           case TAB:
           case UP:
           case DOWN:
           case PGUP:
           case PGDN:
           case F1:
           case F2:
           case F3:
           case F4:
           case F5:
           case F6:
           case F7:
           case F8:
           case F9:
           case F10:
                   done = 1;
                   break;

           default:

                   len = strlen(se) +1;




                   if (*start == flen && strlen(se) != slen && (*start + *offset) < slen)
                   {
                           (*start)--;

                           (*offset)++;
                   }

                   if (ch>=32 && ch<=126){
                           if (*insert){
                                   if(strlen(se) < slen && strlen(se) !=0)
                                   {

                                           if (*start + *offset < slen)
                                           {                               
                                                   for(ii=strlen(se)-1;ii>=*start+*offset;ii--)
                                                   {
                                                           se[ii+1] = se[ii];

                                                   }
                                                   se[*start+*offset]=ch;
                                                   (*start)++;
                                                   se[len]=0;

                                           }

                                   }

                           }           


                           else
                           {
                                   if(*start == flen && strlen(se)!= slen)
                                   {
                                           (*start)--;
                                           (*offset)++;
                                   }
                                   if(strlen(se) < slen)
                                   {

                                           if(se[*offset+*start] == 0){
                                                   se[*offset+*start+1] = 0;
                                           }
                                           se[*offset+*start] = ch;
                                           (*start)++;
                                   }
                           }
                   }
                   break;
                                        }
                                }
                                return ch;
}
md




PostPosted: Sun Nov 23, 2008 2:33 pm   Post subject: RE:Question about chars

I'm still not sure I understand the question, if you are asking how to insert characters into a char* based string it's pretty simple

code:

WHILE characters to insert AND space in string DO
    FOR i = position to insert character to end of string DO
        string[i+1] = string[i]
    string[position to insert character] = character to insert


Then you either increase the position that you are inserting a character in for every character, or do it the easy way and reverse the string you are inserting.
DemonWasp




PostPosted: Tue Nov 25, 2008 11:43 pm   Post subject: RE:Question about chars

md, I think you mean:
code:

WHILE characters to insert AND space in string DO
    FOR i = (end of string) to (position to insert character) BACKWARDS DO
        string[i+1] = string[i]
    string[position to insert character] = character to insert
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: