Exemplo n.º 1
0
codelist_entry *process_dat(){
	char *orig_line = get_rest_of_orig_line();
	int len = strlen(orig_line);
	char *buf = calloc(len - 3, sizeof(char));
	//Take off the DAT and following space
	strncpy(buf, orig_line + 4, len - 3);
	trim(buf);
	printf("Dat: %s\n", buf);

	codelist_entry *entry = calloc(sizeof(codelist_entry), 1);
	if(is_quoted_string(buf)){
		get_data_from_quoted_string(buf, entry);
	}else{
		get_data_from_nums(buf, entry);
	}

	printf("Data: ");
	for(int i = 0; i < entry->data_size; i++){
		printf("0x%04x ", entry->data[i]);
	}
	printf("\n");
	for(int i = 0; i < entry->data_size; i++){
		unsigned short cur = entry->data[i];
		printf("%c%c", (cur >> 8), (cur & 0xFF));
	}
	printf("\n");

	return entry;
}
Exemplo n.º 2
0
void Node_Value_Parser::handle_append_command(Command_Value& cv, Moldable* bc)
{
    if (tm.current_token() != Mogu_Syntax::append) return;
    
    Node_Value tmp {};
    std::string str {};
    uint8_t flags {cv.get_flags()};
    int token {tm.current_token()};
    bool preposition_found {false};

    // This is used for checking unexpected input to see if it's actually
    // supposed to be represented as an integer used as a list index.
    bool check_if_list {false};

    tm.next();
    token = tm.current_token();

    // Cycle through the input, testing flag combinations and setting 
    // things where appropriate, until we're out of tokens.
    while (token != Mogu_Syntax::OUT_OF_RANGE_END)
    {
        // A string will either be a value or an identifier.
        if (!preposition_found && Mogu_Syntax::location == token)
        {
            tm.next();
            std::stringstream buf;
            // append location user foo bar to self
            while (token != Mogu_Syntax::preposition)
            {
                if (Mogu_Syntax::TOKEN_DELIM == token)
                    buf << tm.fetch_string();
                else buf << tm.current_token();
                buf << " ";
                tm.next();
                token = tm.current_token();
            }
            std::string in {buf.str()}; //user foo bar
            Node_Value_Parser p {};
            Command_Value c {*bc};
            Node_Value v {};
            p.set_user_id(user_id);
            p.set_group_id(group_id);
            p.give_input(in,v); //"widget baz bop"
            in = stripquotes(v.get_string()); // widget baz bop
            buf.str(std::string{});
            buf << Mogu_Syntax::append.str << " " << in << " ";
            while (token != Mogu_Syntax::OUT_OF_RANGE_END)
            {
                if (Mogu_Syntax::TOKEN_DELIM == token)
                    buf << tm.fetch_string();
                else buf << tm.current_token();
                buf << " ";
                tm.next();
                token = tm.current_token();
            }
            p.give_input(buf.str(),c,bc);
            cv = c;
            return;
        }
        if (Mogu_Syntax::TOKEN_DELIM == token)
        {
            str = tm.fetch_string();
            tmp.set_string(str);
            // If the only thing that has been set is the action, 
            // then the token delimiter will be a quoted string.
            if ((flags == (uint8_t)Command_Flags::action) && is_quoted_string(str))
            {
                flags = cv.set(Command_Flags::value, tmp);
            }
            // If the r_object has been set, but there's no identifier,
            // the next string encountered must be the identifier.
            else if (cv.test(Command_Flags::r_object) && 
                    !cv.test(Command_Flags::r_identifier))
            {
                flags = cv.set(Command_Flags::r_identifier, tmp);
            }
            // Same as above, but for standard objects.
            else if (cv.test(Command_Flags::object) && 
                    !cv.test(Command_Flags::identifier))
            {
                flags = cv.set(Command_Flags::identifier, tmp);
            }
            // In all other cases, this is unexpected input, but it might
            // be a list index. 'continue' not present here because we want to
            // keep moving through the decision tree.
            else check_if_list = true;
        }
        // Object tokens will either be the object or r_object, depending
        // on if it was reached before the preposition or not.
        else if (is_object_token(token))
        {
            if (!preposition_found && !cv.test(Command_Flags::r_object))
            {
                flags = cv.set(Command_Flags::r_object, token);
                if (token == Mogu_Syntax::user)
                {   // The only instance where we have
                    // "append user to ..." is when we are
                    // appending a user to a group.
                    tm.next();
                    if (tm.current_token()==Mogu_Syntax::preposition)
                    {
                        handle_user_to_group(cv,bc);
                        break;
                    }
                    else
                        tm.prev();
                }
            }
            else if (preposition_found && !cv.test(Command_Flags::object))
            {
                flags = cv.set(Command_Flags::object, token);
            }
            else
                check_if_list = true;
                // Do not continue.
        }
        else if (is_state_token(token))
        {
            tmp.set_int(token);
            if (!preposition_found && !cv.test(Command_Flags::r_arg))
            {
                flags = cv.set(Command_Flags::r_arg, tmp);
            }
            else if (preposition_found && !cv.test(Command_Flags::arg))
            {
                flags = cv.set(Command_Flags::arg, token);
            }
            else
                check_if_list = true;
        }
        else if (is_preposition_token(token))
        {
            preposition_found = true;
        }
        // This would be reached in the event of unexpected input.
        // If the check_if_list flag is turned on, we're going to give the 
        // input the benefit of the doubt and see whether or not the previous
        // token was a list token. If so, that's what this is. Otherwise, 
        // we're going to return without doing anything.
        else if (check_if_list)
        {
            tm.prev();
            if (Mogu_Syntax::list != tm.current_token())
                return;
            tm.next(); // Make sure to go back where we were in the input.
            tmp.set_int((int) token);
            if (!preposition_found && !cv.test(Command_Flags::r_arg)) 
            {
                cv.set(Command_Flags::r_arg, tmp);
            }
            else if (preposition_found && !cv.test(Command_Flags::arg))
            {
                cv.set(Command_Flags::arg, tmp);
            }
            // We have no clue what the hell this is. 
            else return;
        }

        tm.next();
        token = tm.current_token();
    } // end while loop

    // After this, there is the possibility that we'll have a reduceable value
    // with r_OBJ/r_ID/r_arg. If so, we'll go ahead and reduce that now:
    if (cv.object_is_reduceable(true))
    {
        cv.reduce_object(true, bc);
    }
}