iDevGames Forums
converting text input to a c char - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: converting text input to a c char (/thread-366.html)



converting text input to a c char - NelsonMandella - Feb 2, 2010 03:08 PM

How would I convert.....

Code:
NSString *characters = [theEvent characters];
    if ([characters length])
    {
        unichar character = [characters characterAtIndex:0];
    }

......to a c character or an array of c chars?



Also, where would I find constants for key codes such as return, escape, etc... ?


converting text input to a c char - OneSadCookie - Feb 2, 2010 03:40 PM

Text input is a huge minefield. Perhaps you should start by explaining what kind of thing you're making here (FPS? RTS? Typing Tutor? High-score name entry field?).

If the character you've received is ASCII, (char)character is a C char. That may or may not be useful to you.


converting text input to a c char - NelsonMandella - Feb 2, 2010 03:54 PM

OneSadCookie Wrote:Text input is a huge minefield. Perhaps you should start by explaining what kind of thing you're making here (FPS? RTS? Typing Tutor? High-score name entry field?).

If the character you've received is ASCII, (char)character is a C char. That may or may not be useful to you.

Sure. I'm working on a text parser for an in-game console, similar in feel to the unix terminal. Basically you type in a line of text, and when you hit enter, the it gets parsed and executed by the console.

So here's roughly how I figure my console will read in text,
Code:
NSString *characters = [theEvent characters];
    if ([characters length])
    {
        unichar character = [characters characterAtIndex:0];
        
        if (is_console_open())
        {
            char *str = [ characters UTF8String];
            if  (*str == ENTER_KEYCONSTNANT)// this works if  constant
                                                                                  // is a single-digit int
                console_execute_input();
            else
                console_type_char( str);
            
            return;
        }
        
        switch (character)
        {
                      // game stuff here
                }


My console keeps a c char array representing a single line of input and appends the str character to that line whenever a key is down and the console is open.

I've searched over a lot of apple docs and can't find anything on the delete or return character key constants other than the actual integer values


converting text input to a c char - OneSadCookie - Feb 2, 2010 04:24 PM

Return and backspace are ASCII ('\n' and '\b' respectively). You can find a list of the nonstandard ones in NSEvent.h ( /System/Library/Frameworks/AppKit.framework/Headers/NSEvent.h ).

It sounds like you want characters, and you want to ignore nonascii characters, so you'll want to do something like:

Code:
NSString *s = [event characters];
size_t i, n = [s length];
for (i = 0; i < n; ++i)
{
    unichar u = [s characterAtIndex:i];
    if (u > 127)
    {
        // ignore or beep for nonascii characters
    }
    else if (isprint(u))
    {
        // append to console (char)u
    }
    else if (u == '\n')
    {
        // execute a command
    }
    else if (u == '\b')
    {
        // backspace
    }
    else if (u == '\t')
    {
        // command completion
    }
    else
    {
        // the character's pretty wacky, maybe beep
    }
}



converting text input to a c char - NelsonMandella - Feb 3, 2010 01:52 PM

Thanks, I managed to get input worked out, but I've run into another minor issue.

Here's what my parser would look like trying to parse the string "MOVE PLAYER", residing in the input_line variable.

Code:
char input_line[1000];   // a string of chars representing input


BOOL read_word( char* word, int which_word )
{
       //parses line_input into individual words, if which_word == 1
      // then the parser puts the first word in the input_line into word_str
    word = nil;
    
    int first_word = get_word_start( which_word );
    
    int word_len = get_word_length( which_word );
    
        // not a valid word
    if (first_word == -1 || word_len == -1)
        return FALSE;
        
    strncpy( word, &line_input[first_word], word_len );
    
    return TRUE;
}


void parse()
{
       char word[255];
      
if (read_word(word, 1))
    if ( strcmp(word, "PLAYER") == 0)
          if (read_word( word, 2))
                 if ( strcmp(word, "MOVE") == 0)
                       //-- you get the idea
}

the problem is that if the first word read into"word" (which in the example is PLAYER) is longer than the second word(MOVE), the second word ends up being, in this case, "moveer" where the "er" are characters left over from "player" read in from the first call to read_word().

So, is there a way to restore word[] to it's totally null/empty state using only its pointer from within read_word()?


converting text input to a c char - PowerMacX - Feb 3, 2010 05:42 PM

Try:
Code:
    ...
    strncpy( word, &line_input[first_word], word_len );
    word[word_len] = '\0'; // add this line
    return TRUE;



converting text input to a c char - NelsonMandella - Feb 4, 2010 10:58 AM

Got it working, thanks.