size_t read_line(int socket, STRING_BUFFER_PTR *string_buffer_ptr) { // If input is invalid then just error out. // if (NULL == string_buffer_ptr){ return 0; } // We are getting a new "line" so wack the old one if it exists. // if (*string_buffer_ptr){ string_buffer_reset(*string_buffer_ptr); } else{ *string_buffer_ptr = string_buffer_new(); } // Now we read in the data: // char c = '\0'; while (c != '\n') { ssize_t n = recv(socket, &c, 1, 0); if (n > 0) { if (c == '\r') { n = recv(socket, &c, 1, MSG_PEEK); if ((n > 0) && (c == '\n')) { recv(socket, &c, 1, 0); } else { c = '\n'; } } string_buffer_append_char(*string_buffer_ptr, c); } else { c = '\n'; } } return string_buffer_c_string_length(*string_buffer_ptr); }
zarray_t * str_split (const char *str, const char *delim) { assert (str != NULL); assert (delim != NULL); zarray_t *parts = zarray_create (sizeof(char*)); string_buffer_t *sb = string_buffer_create (); size_t delim_len = strlen (delim); size_t len = strlen (str); size_t pos = 0; while (pos < len) { if (str_starts_with (&str[pos], delim) && delim_len > 0) { pos += delim_len; // never add empty strings (repeated tokens) if (string_buffer_size (sb) > 0) { char *part = string_buffer_to_string (sb); zarray_add (parts, &part); } string_buffer_reset (sb); } else { string_buffer_append (sb, str[pos]); pos++; } } if (string_buffer_size(sb) > 0) { char *part = string_buffer_to_string (sb); zarray_add (parts, &part); } string_buffer_destroy (sb); return parts; }
/* * Read next token and return its type tk * - set lex->token to tk * - set lex->tk_pos, etc. * - if token is TK_STRING, TK_NUM_RATIONAL, TK_NUM_FLOAT, TK_BV_CONSTANT, TK_SYMBOL, TK_ERROR, * the token value is stored in lex->buffer (as a string). */ yices_token_t next_yices_token(lexer_t *lex) { yices_token_t tk; reader_t *rd; string_buffer_t *buffer; int c; rd = &lex->reader; c = reader_current_char(rd); buffer = lex->buffer; string_buffer_reset(buffer); // skip spaces and comments for (;;) { while (isspace(c)) c = reader_next_char(rd); if (c != ';') break; do { // read to end-of-line or eof c = reader_next_char(rd); } while (c != '\n' && c != EOF); } // record token position (start of token) lex->tk_pos = rd->pos; lex->tk_line = rd->line; lex->tk_column = rd->column; switch (c) { case '(': tk = TK_LP; goto next_then_return; case ')': tk = TK_RP; goto next_then_return; case EOF: tk = TK_EOS; goto done; case ':': c = reader_next_char(rd); if (c == ':') { tk = TK_COLON_COLON; goto next_then_return; } else { // store ':' in the buffer since that may be used for reporting errors string_buffer_append_char(buffer, ':'); string_buffer_close(buffer); tk = TK_ERROR; goto done; } case '"': tk = read_string(lex); goto done; case '+': case '-': string_buffer_append_char(buffer, c); c = reader_next_char(rd); if (isdigit(c)) { string_buffer_append_char(buffer, c); reader_next_char(rd); tk = read_number(lex); } else { tk = read_symbol(lex); } goto done; case '0': string_buffer_append_char(buffer, c); c = reader_next_char(rd); if (c == 'b') { tk = read_bv_constant(lex); } else if (c == 'x') { tk = read_hex_constant(lex); } else { tk = read_number(lex); } goto done; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': string_buffer_append_char(buffer, c); reader_next_char(rd); tk = read_number(lex); goto done; default: // symbol or keyword string_buffer_append_char(buffer, c); reader_next_char(rd); tk = read_symbol(lex); goto done; } /* * read next character and exit */ next_then_return: reader_next_char(rd); done: lex->token = tk; return tk; }
int main() { int32_t x, y; uint32_t a, b, n; char c; string_buffer_t *s; s = &buffer; init_string_buffer(s, 0); show_test("empty buffer", s); string_buffer_reset(s); for (c = 'a'; c <= 'z'; c++) { string_buffer_append_char(s, c); } show_test("alphabet", s); string_buffer_reset(s); for (c = 'a'; c <= 'z'; c++) { string_buffer_append_char(s, c); } string_buffer_append_string(s, "au898ue2bcc90219"); show_test("alphabet+au898ue2bcc90219", s); x = INT32_MIN; for (;;){ sprintf(aux, "signed number: %" PRId32, x); string_buffer_reset(s); string_buffer_append_int32(s, x); show_test(aux, s); y = x >> 1; if (y == x) break; x = y; } x = INT32_MAX; for (;;) { sprintf(aux, "signed number: %" PRId32, x); string_buffer_reset(s); string_buffer_append_int32(s, x); show_test(aux, s); y = x>>1; if (y == x) break; x = y; } a = UINT32_MAX; for (;;){ sprintf(aux, "unsigned number: %" PRIu32, a); string_buffer_reset(s); string_buffer_append_uint32(s, a); show_test(aux, s); b = a >> 1; if (b == a) break; a = b; } mpz_init(z0); mpz_init(z1); mpq_init(q0); mpz_set_str(z0, "111102222033330123456789", 10); string_buffer_reset(s); string_buffer_append_mpz(s, z0); show_test("mpz: 111102222033330123456789", s); mpz_set_str(z0, "-111102222033330123456789", 10); string_buffer_reset(s); string_buffer_append_mpz(s, z0); show_test("mpz: -111102222033330123456789", s); string_buffer_reset(s); string_buffer_append_mpz(s, z1); show_test("mpz: 0", s); mpq_set_str(q0, "-98765432109876543210", 10); string_buffer_reset(s); string_buffer_append_mpq(s, q0); show_test("mpq: -98765432109876543210", s); mpq_set_str(q0, "-98765432109876543210/38192839777", 10); string_buffer_reset(s); string_buffer_append_mpq(s, q0); show_test("mpq: -98765432109876543210/38192839777", s); init_rationals(); rational_t r0; q_init(&r0); string_buffer_reset(s); string_buffer_append_rational(s, &r0); show_test("rational: 0", s); q_set_int32(&r0, -12, 73); string_buffer_reset(s); string_buffer_append_rational(s, &r0); show_test("rational: -12/73", s); q_set_mpq(&r0, q0); string_buffer_reset(s); string_buffer_append_rational(s, &r0); show_test("rational: -98765432109876543210/38192839777", s); q_set_mpz(&r0, z0); string_buffer_reset(s); string_buffer_append_rational(s, &r0); show_test("rational: -111102222033330123456789", s); printf("\nBit Vectors\n"); init_bvconstants(); bv0 = bvconst_alloc(1); bvconst_clear(bv0, 1); for (n=1; n<= 32; n++) { string_buffer_reset(s); string_buffer_append_bvconst(s, bv0, n); sprintf(aux, "bv[%" PRIu32"]: 0b000...", n); show_test(aux, s); } for (n=1; n <= 32; n++) { bvconst_clear(bv0, 1); bvconst_set_bit(bv0, n-1); string_buffer_reset(s); string_buffer_append_bvconst(s, bv0, n); sprintf(aux, "bv[%" PRIu32"]: 0b100...", n); show_test(aux, s); } bvconst_free(bv0, 1); cleanup_bvconstants(); cleanup_rationals(); mpz_clear(z0); mpz_clear(z1); mpq_clear(q0); delete_string_buffer(s); return 0; }
/* * Read the next token and return its code tk * - set lex->token to tk * - set lex->tk_pos * - if the token is not '(' or ')', then its value is in lex->buffer * as a string */ smt2_token_t next_smt2_token(lexer_t *lex) { reader_t *rd; string_buffer_t *buffer; int c; smt2_token_t tk; rd = &lex->reader; c = reader_current_char(rd); buffer = lex->buffer; string_buffer_reset(buffer); // skip spaces and comments for (;;) { while (isspace(c)) c = reader_next_char(rd); if (c != ';') break; // comments: read everything until the end of the line or EOF do { c = reader_next_char(rd); } while (c != '\n' && c != EOF); } // record start of token lex->tk_pos = rd->pos; lex->tk_line = rd->line; lex->tk_column = rd->column; switch (c) { case '(': tk = SMT2_TK_LP; goto next_then_return; case ')': tk = SMT2_TK_RP; goto next_then_return; case EOF: tk = SMT2_TK_EOS; goto done; case '"': if (two_dot_five_variant) { tk = smt2_read_string_var(lex); } else { tk = smt2_read_string(lex); } goto done; case '#': string_buffer_append_char(buffer, c); c = reader_next_char(rd); if (c == 'b') { tk = smt2_read_binary(lex); } else if (c == 'x') { tk = smt2_read_hexa(lex); } else { tk = SMT2_TK_ERROR; string_buffer_close(buffer); } goto done; case '0': tk = smt2_read_number0(lex); goto done; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': tk = smt2_read_number(lex); goto done; case ':': tk = smt2_read_keyword(lex); goto done; case '|': tk = smt2_read_quoted_symbol(lex); goto done; default: if (issimple(c)) { tk = smt2_read_symbol(lex); goto done; } else { tk = SMT2_TK_ERROR; /* * copy the bad character in buffer for * better error reporting */ string_buffer_append_char(buffer, c); string_buffer_close(buffer); goto next_then_return; } } next_then_return: reader_next_char(rd); done: lex->token = tk; return tk; }