??? 05/12/09 14:14 Read: times Msg Score: +1 +1 Good Answer/Helpful |
#165268 - Alternative parsing Responding to: ???'s previous message |
Do you mean null as in backslash-zero? This is the same as just writing "if (SBUF != 0)".
But once more: An X followed by a couple of hundred characters that are not X and not zero, will result in a very nice buffer overflow, since your state machine does not keep track of how many characters you have seen. Why not just do: ch = SBUF; switch (state) { case WAIT_START: switch (ch) { case 'P': state = GET_P: idx = 0; break; case 'D': state = GET_D; idx = 0; break; ... default: ; // continue waiting for a valid command character } break; case WAIT_P: if (ch == '.' || ch >= '0' && ch <= '9') { if (idx < BUF_SIZE-1) { buf[idx++] = ch; } else { // error somewhere - too much data. state = WAIT_START; } } else { // first character not part of number. buf[idx] = ' '; my_num = atof(buf); store_proportional(my_num); // wait for new start character state = WAIT_START; } break; ... default: // error - unknown state. state = WAIT_START; } |