Seek a specific chars in a string?

Talk about making games.

Seek a specific chars in a string?

Postby bat78 » Mon Nov 18, 2013 8:03 pm

Hi guys :)
Some stuff force me to think of this case..
As far as something like..

Code: Select all
void main () {
  char sign[256] = "Game 4 Editor";
  char str [6];
  int i;
  sscanf (sign,"%s %*s %d",str,&i);
  fprintf ("%s -> %d\n",str,i);
}


Won't work, so i'm completely desperate. How you can make like..
(figuratively):
Code: Select all
if (strcmp(string, "%s.txt") == 0 ) { if (strlen(string) >= 10) strcpy(string, "File too large") }


So only if the string ends with .txt the code is running. Maybe using fseek, but as i remember fseek, seek char by number and it does not compare strings.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Seek a specific chars in a string?

Postby DarkParadox » Mon Nov 18, 2013 10:44 pm

You can use simple direct comparison.
Code: Select all
char test[256];
strcpy(test,"run.txt");
if (test[strlen(test)-1] == 't' &&
    test[strlen(test)-2] == 'x' &&
    test[strlen(test)-3] == 't' &&
    test[strlen(test)-4] == '.')
{
    // Do something
}

Note that this code only works with char arrays, however, not char* pointers.
If you want to compare a char* with this method, you'll have to strcpy its data into an array.
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: Seek a specific chars in a string?

Postby bat78 » Mon Nov 18, 2013 10:47 pm

Thanks, i will try this right now. In that time could i ask something else.

Is there is any method of:
If a line of text file contain more then x chars, then the chars going on the next line

For a length of chars at line i can use strlen and for new lines i am using:
Code: Select all
sprintf(text, "%s%s%s", line[0], line[1], line[2]);


[Edit]
It works, however i am using the gE text filling (because there is no other way to type text?) i gotta convert it to an array.
Code: Select all
char array[256];
strcpy(array, text);


[Edit}
"textbox -> Draw Actor, line 2: READ attemted before allowe access area"
Hmm...i think in this case this cannot be:

strcpy(test,"run.txt");
if (test[strlen(test)-1] == 't' &&
test[strlen(test)-2] == 'x' &&
test[strlen(test)-3] == 't' &&
test[strlen(test)-4] == '.')
{
ExitGame();
}

on the first compare, it allows only under 0 like:
Code: Select all
strcpy(test, text);
if (test[strlen(test)-(1)] == 't' &&
    test[strlen(test)-2] == 'x' &&
    test[strlen(test)-3] == 't' &&
    test[strlen(test)-4] == '.')
{
    ExitGame();
}

But in this case it won't work.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Seek a specific chars in a string?

Postby DarkParadox » Mon Nov 18, 2013 11:38 pm

Oh man, I see I messed up. I tested the code with a strcpy from a pointer to an array just now and I'm getting the same error.
Here, instead, I threw together a function for you and made sure it worked with char* correctly this time.
Code: Select all
int endswith(char*source, char*check) {
   char*temp1;
   strncpy(temp1, source, strlen(source)-strlen(check));
   strcat(temp1, check);
   if(strcmp(source, temp1)==0) return 1;
   else return 0;
}

After putting it in Global Code and adding it, use it by typing something like this:
Code: Select all
if(endswith(mystring, ".txt"))
{
    // Do something
}


Now, to answer some other questions:
Yes, that's the only way to draw text normally, if you need another there's one or two hidden among the forums, but if I may do so I recommend my own (in ltCanvas Tools) over on my topic here: viewtopic.php?f=4&t=9093
If you want your text to look good though, there's no real option other than the built-in text actors.
Last edited by DarkParadox on Mon Nov 18, 2013 11:42 pm, edited 1 time in total.
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: Seek a specific chars in a string?

Postby bat78 » Mon Nov 18, 2013 11:41 pm

Let me try this!
I have some problems loading my project..dunno why gE acts like that. Constantly load my project without showing it up. Pff.
I gotta use my old version ehhh that sucks. xD

Edit:
Oh yea.. and after 5 min of loading. It stops working.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Seek a specific chars in a string?

Postby DarkParadox » Mon Nov 18, 2013 11:50 pm

Sorry, but I really don't know how to help you further than I already have with your stuff, I'm not sure where or what the errors are in whatever text loading code you've got going on, but as for inserting newlines at positions, you can do that with another function here which will insert a string at any position in another string:
Code: Select all
char * strInsert (char* txt, int pos, char* in) {
    char firsthalf[255], secondhalf[255];
    int i;
    strncpy(firsthalf, txt, pos);
   
    // And now loop through the rest and add it to
    // secondhalf
    for (i=0; i<(strlen(txt)-pos); i++) {
        secondhalf[i] = txt[i+pos];
    }
   
    // Append the character to firsthalf
    strcat(firsthalf, in);
   
    // Reappend secondhalf
    strcat(firsthalf, secondhalf);
                                   
    // And make our text the new variable.
    strcpy(txt, firsthalf);
    return txt;
}


So, after getting all the lines of a text file, you could do something like
Code: Select all
// Not sure if this works, wrote it on the spot
int i;
int linelength = 15;
for(i=0; i<strlen(mytext); i+=linelength)
{
    strcpy(mytext, strInsert(mytext, "\n", i));
}

This doesn't check for line lengths, though. It'll insert them indiscriminately. Just an example.
Last edited by DarkParadox on Tue Nov 19, 2013 1:58 am, edited 1 time in total.
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: Seek a specific chars in a string?

Postby bat78 » Mon Nov 18, 2013 11:57 pm

Yes, scoreup, i just friendly mentioned about the errors. It could be useful as info to someone :)
May i show you my project and what is it about DarkParadox?
_________________________________________________________________________________
The new code for comparing were crashing gE when used.
And.. i wrote easer bit of code with is working.. about the lines:
Code: Select all
void nLine(const char * fname, char * textArray)
{
    FILE * pFile = fopen(fname, "r");
    char * line = textArray;
    textArray = line;
    if (strlen(textArray) > 39) { line[39] = '\n';}
    fclose(pFile);
}

_________________________________________________________________________________
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Seek a specific chars in a string?

Postby skydereign » Tue Nov 19, 2013 10:45 am

For the record, since it wasn't mentioned yet, there is a function strchr that does what the topic title is asking for.
http://game-editor.com/docs/script_reference.htm wrote:strchr: Returns a pointer to the first occurrence of the low-order byte of ch in the string pointed to by str. If no match is found, a null pointer is returned.

char *strchr(const char *str, int ch);
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Seek a specific chars in a string?

Postby bat78 » Tue Nov 19, 2013 1:30 pm

Thanks skydreign. You reminds me of a function encyclopedia. :)
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest