static void _doc_process_tag(doc_ptr doc, doc_tag_ptr tag) { if ( tag->type == DOC_TAG_CLOSE_COLOR || tag->type == DOC_TAG_CLOSE_STYLE || tag->type == DOC_TAG_CLOSE_INDENT ) { doc_pop_style(doc); } else if (tag->type == DOC_TAG_COLOR) { assert(tag->arg); if (tag->arg_size == 1) { if (tag->arg[0] == '*') doc_pop_style(doc); else { doc_style_t style = *doc_current_style(doc); /* copy */ switch (tag->arg[0]) { case 'd': style.color = TERM_DARK; break; case 'w': style.color = TERM_WHITE; break; case 's': style.color = TERM_SLATE; break; case 'o': style.color = TERM_ORANGE; break; case 'r': style.color = TERM_RED; break; case 'g': style.color = TERM_GREEN; break; case 'b': style.color = TERM_BLUE; break; case 'u': style.color = TERM_UMBER; break; case 'D': style.color = TERM_L_DARK; break; case 'W': style.color = TERM_L_WHITE; break; case 'v': style.color = TERM_VIOLET; break; case 'y': style.color = TERM_YELLOW; break; case 'R': style.color = TERM_L_RED; break; case 'G': style.color = TERM_L_GREEN; break; case 'B': style.color = TERM_L_BLUE; break; case 'U': style.color = TERM_L_UMBER; break; } doc_push_style(doc, &style); } } else { string_ptr arg = string_copy_sn(tag->arg, tag->arg_size); doc_style_t style = *doc_current_style(doc); /* copy */ doc_style_f f = _get_doc_style_f(doc, string_buffer(arg)); if (f) f(&style); {/* We don't copy the named style, just its color. */ /* Also, we damn well better push a style or we'll be upset when the good little user pops! */ doc_style_t copy = *doc_current_style(doc); copy.color = style.color; doc_push_style(doc, ©); } string_free(arg); } } else if (tag->type == DOC_TAG_INDENT) { doc_style_t style = *doc_current_style(doc); style.left = doc->cursor.x; doc_push_style(doc, &style); } else { string_ptr arg = string_copy_sn(tag->arg, tag->arg_size); switch (tag->type) { case DOC_TAG_STYLE: if (tag->arg_size == 1 && tag->arg[0] == '*') doc_pop_style(doc); else {/* Better silently add one if name doesn't exist ... */ doc_style_t copy = *doc_current_style(doc); doc_style_f f = _get_doc_style_f(doc, string_buffer(arg)); if (f) f(©); doc_push_style(doc, ©); } break; case DOC_TAG_VAR: _doc_process_var(doc, string_buffer(arg)); break; case DOC_TAG_TAB: { int pos = atoi(string_buffer(arg)) + doc_current_style(doc)->left; if (pos > doc->cursor.x) doc_insert_space(doc, pos - doc->cursor.x); else doc_rollback(doc, doc_pos_create(pos, doc->cursor.y)); break; } case DOC_TAG_TOPIC: { doc_bookmark_ptr mark = malloc(sizeof(doc_bookmark_t)); mark->name = arg; /* steal ownership */ arg = NULL; mark->pos = doc->cursor; vec_add(doc->bookmarks, mark); break; } case DOC_TAG_LINK: { doc_link_ptr link = malloc(sizeof(doc_link_t)); int split = string_chr(arg, 0, '#'); int ch = 'a' + int_map_count(doc->links); if (split >= 0) { substring_t left = string_left(arg, split); substring_t right = string_right(arg, string_length(arg) - split - 1); link->file = substring_copy(&left); link->topic = substring_copy(&right); } else { link->file = arg; /* steal ownership */ arg = NULL; link->topic = NULL; } link->location.start = doc->cursor; int_map_add(doc->links, ch, link); { /* TODO: This is flawed. Here's a real world example: "(see below <link:birth.txt#PrimaryStats>)." Can you see the problem? We might line break after "[a]" right before ").". Instead, "[a])." should be treated as the current word. To fix this, we'll need a parser with a token queue that we can push onto, but this raises storage issues. */ string_ptr s = string_alloc_format("<style:link>[%c]</style>", ch); doc_insert(doc, string_buffer(s)); string_free(s); link->location.stop = doc->cursor; } break; } } string_free(arg); } }
/* Idea borrowed from Vanilla 3.5, but recoded from scratch ... */ void do_cmd_list_monsters(void) { int i, ct_types, ct_total = 0; int_map_ptr info = int_map_alloc(free); /* Collect */ for (i = 0; i < max_m_idx; i++) { const monster_type *m_ptr = &m_list[i]; _mon_list_info_ptr info_ptr; if (!m_ptr->ap_r_idx) continue; if (!m_ptr->ml) continue; info_ptr = int_map_find(info, m_ptr->ap_r_idx); if (!info_ptr) { info_ptr = malloc(sizeof(_mon_list_info_t)); info_ptr->r_idx = m_ptr->ap_r_idx; info_ptr->ct_total = 0; info_ptr->ct_awake = 0; info_ptr->ct_los = 0; int_map_add(info, m_ptr->ap_r_idx, info_ptr); } assert(info_ptr); info_ptr->ct_total++; ct_total++; if (!MON_CSLEEP(m_ptr)) info_ptr->ct_awake++; if (projectable(py, px, m_ptr->fy, m_ptr->fx)) info_ptr->ct_los++; } ct_types = int_map_count(info); if (ct_types) { int_map_iter_ptr iter; int *order; int cx, cy, row = 1, col; /* Sort */ C_MAKE(order, ct_types, int); i = 0; for (iter = int_map_iter_alloc(info); int_map_iter_is_valid(iter); int_map_iter_next(iter)) { _mon_list_info_ptr info_ptr = int_map_iter_current(iter); order[i++] = info_ptr->r_idx; } int_map_iter_free(iter); ang_sort_comp = _compare_r_level; ang_sort_swap = _swap_int; ang_sort(order, NULL, ct_types); /* Display */ Term_get_size(&cx, &cy); col = cx - 52; screen_save(); c_prt(TERM_WHITE, format("You see %d monster%s", ct_total, ct_total != 1 ? "s" : ""), 0, col); for (i = 0; i < ct_types; i++) { int r_idx = order[i]; const monster_race *r_ptr = &r_info[r_idx]; byte attr = TERM_WHITE; _mon_list_info_ptr info_ptr = int_map_find(info, r_idx); char buf[100]; assert(info_ptr); Term_erase(col - 1, row, 53); if (row >= cy - 2) { c_prt(TERM_YELLOW, "...", row++, col+2); break; } if (r_ptr->flags1 & RF1_UNIQUE) attr = TERM_VIOLET; else if (r_ptr->level > base_level) attr = TERM_RED; else if (!info_ptr->ct_awake) attr = TERM_L_UMBER; if (info_ptr->ct_total == 1) sprintf(buf, "%s", r_name + r_ptr->name); else if (!info_ptr->ct_awake) sprintf(buf, "%s (%d sleeping)", r_name + r_ptr->name, info_ptr->ct_total); else if (info_ptr->ct_awake == info_ptr->ct_total) sprintf(buf, "%s (%d awake)", r_name + r_ptr->name, info_ptr->ct_total); else sprintf(buf, "%s (%d awake, %d sleeping)", r_name + r_ptr->name, info_ptr->ct_awake, info_ptr->ct_total - info_ptr->ct_awake); Term_queue_bigchar(col, row, r_ptr->x_attr, r_ptr->x_char, 0, 0); c_put_str(attr, format(" %-50.50s", buf), row++, col+1); } Term_erase(col - 1, row, 53); c_prt(TERM_YELLOW, "Hit any key.", row, col+2); inkey(); prt("", 0, 0); screen_load(); C_KILL(order, ct_types, int); } else msg_print("You see no visible monsters."); int_map_free(info); }
/** Self-test start function. * * Run all self-tests. * * @returns EOK on success. * @returns The first error occurred. * */ int self_test(void) { printf("Running networking self-tests\n"); printf("\nChar map test"); char_map_t cm; TEST(char_map_update(&cm, "ucho", 0, 3), EINVAL); TEST(char_map_initialize(&cm), EOK); TEST(char_map_exclude(&cm, "bla", 0), CHAR_MAP_NULL); TEST(char_map_find(&cm, "bla", 0), CHAR_MAP_NULL); TEST(char_map_add(&cm, "bla", 0, 1), EOK); TEST(char_map_find(&cm, "bla", 0), 1); TEST(char_map_add(&cm, "bla", 0, 10), EEXISTS); TEST(char_map_update(&cm, "bla", 0, 2), EOK); TEST(char_map_find(&cm, "bla", 0), 2); TEST(char_map_update(&cm, "ucho", 0, 2), EOK); TEST(char_map_exclude(&cm, "bla", 0), 2); TEST(char_map_exclude(&cm, "bla", 0), CHAR_MAP_NULL); TEST(char_map_find(&cm, "ucho", 0), 2); TEST(char_map_update(&cm, "ucho", 0, 3), EOK); TEST(char_map_find(&cm, "ucho", 0), 3); TEST(char_map_add(&cm, "blabla", 0, 5), EOK); TEST(char_map_find(&cm, "blabla", 0), 5); TEST(char_map_add(&cm, "bla", 0, 6), EOK); TEST(char_map_find(&cm, "bla", 0), 6); TEST(char_map_exclude(&cm, "bla", 0), 6); TEST(char_map_find(&cm, "bla", 0), CHAR_MAP_NULL); TEST(char_map_find(&cm, "blabla", 0), 5); TEST(char_map_add(&cm, "auto", 0, 7), EOK); TEST(char_map_find(&cm, "auto", 0), 7); TEST(char_map_add(&cm, "kara", 0, 8), EOK); TEST(char_map_find(&cm, "kara", 0), 8); TEST(char_map_add(&cm, "nic", 0, 9), EOK); TEST(char_map_find(&cm, "nic", 0), 9); TEST(char_map_find(&cm, "blabla", 0), 5); TEST(char_map_add(&cm, "micnicnic", 5, 9), EOK); TEST(char_map_find(&cm, "micni", 0), 9); TEST(char_map_find(&cm, "micnicn", 5), 9); TEST(char_map_add(&cm, "\x10\x0\x2\x2", 4, 15), EOK); TEST(char_map_find(&cm, "\x10\x0\x2\x2", 4), 15); TEST((char_map_destroy(&cm), EOK), EOK); TEST(char_map_update(&cm, "ucho", 0, 3), EINVAL); printf("\nCRC computation test"); uint32_t value; TEST(value = ~compute_crc32(~0, "123456789", 8 * 9), 0xcbf43926); TEST(value = ~compute_crc32(~0, "1", 8), 0x83dcefb7); TEST(value = ~compute_crc32(~0, "12", 8 * 2), 0x4f5344cd); TEST(value = ~compute_crc32(~0, "123", 8 * 3), 0x884863d2); TEST(value = ~compute_crc32(~0, "1234", 8 * 4), 0x9be3e0a3); TEST(value = ~compute_crc32(~0, "12345678", 8 * 8), 0x9ae0daaf); TEST(value = ~compute_crc32(~0, "ahoj pane", 8 * 9), 0x5fc3d706); printf("\nDynamic fifo test"); dyn_fifo_t fifo; TEST(dyn_fifo_push(&fifo, 1, 0), EINVAL); TEST(dyn_fifo_initialize(&fifo, 1), EOK); TEST(dyn_fifo_push(&fifo, 1, 0), EOK); TEST(dyn_fifo_pop(&fifo), 1); TEST(dyn_fifo_pop(&fifo), ENOENT); TEST(dyn_fifo_push(&fifo, 2, 1), EOK); TEST(dyn_fifo_push(&fifo, 3, 1), ENOMEM); TEST(dyn_fifo_push(&fifo, 3, 0), EOK); TEST(dyn_fifo_pop(&fifo), 2); TEST(dyn_fifo_pop(&fifo), 3); TEST(dyn_fifo_push(&fifo, 4, 2), EOK); TEST(dyn_fifo_push(&fifo, 5, 2), EOK); TEST(dyn_fifo_push(&fifo, 6, 2), ENOMEM); TEST(dyn_fifo_push(&fifo, 6, 5), EOK); TEST(dyn_fifo_push(&fifo, 7, 5), EOK); TEST(dyn_fifo_pop(&fifo), 4); TEST(dyn_fifo_pop(&fifo), 5); TEST(dyn_fifo_push(&fifo, 8, 5), EOK); TEST(dyn_fifo_push(&fifo, 9, 5), EOK); TEST(dyn_fifo_push(&fifo, 10, 6), EOK); TEST(dyn_fifo_push(&fifo, 11, 6), EOK); TEST(dyn_fifo_pop(&fifo), 6); TEST(dyn_fifo_pop(&fifo), 7); TEST(dyn_fifo_push(&fifo, 12, 6), EOK); TEST(dyn_fifo_push(&fifo, 13, 6), EOK); TEST(dyn_fifo_push(&fifo, 14, 6), ENOMEM); TEST(dyn_fifo_push(&fifo, 14, 8), EOK); TEST(dyn_fifo_pop(&fifo), 8); TEST(dyn_fifo_pop(&fifo), 9); TEST(dyn_fifo_pop(&fifo), 10); TEST(dyn_fifo_pop(&fifo), 11); TEST(dyn_fifo_pop(&fifo), 12); TEST(dyn_fifo_pop(&fifo), 13); TEST(dyn_fifo_pop(&fifo), 14); TEST(dyn_fifo_destroy(&fifo), EOK); TEST(dyn_fifo_push(&fifo, 1, 0), EINVAL); printf("\nGeneric char map test"); int *x; int *y; int *z; int *u; int *v; int *w; XMALLOC(x, int); XMALLOC(y, int); XMALLOC(z, int); XMALLOC(u, int); XMALLOC(v, int); XMALLOC(w, int); int_char_map_t icm; icm.magic = 0; TEST(int_char_map_add(&icm, "ucho", 0, z), EINVAL); TEST(int_char_map_initialize(&icm), EOK); TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK); TEST(int_char_map_find(&icm, "bla", 0), NULL); TEST(int_char_map_add(&icm, "bla", 0, x), EOK); TEST(int_char_map_find(&icm, "bla", 0), x); TEST(int_char_map_add(&icm, "bla", 0, y), EEXISTS); TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK); TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK); TEST(int_char_map_add(&icm, "blabla", 0, v), EOK); TEST(int_char_map_find(&icm, "blabla", 0), v); TEST(int_char_map_add(&icm, "bla", 0, w), EOK); TEST(int_char_map_find(&icm, "bla", 0), w); TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK); TEST(int_char_map_find(&icm, "bla", 0), NULL); TEST(int_char_map_find(&icm, "blabla", 0), v); TEST(int_char_map_add(&icm, "auto", 0, u), EOK); TEST(int_char_map_find(&icm, "auto", 0), u); TEST((int_char_map_destroy(&icm), EOK), EOK); TEST(int_char_map_add(&icm, "ucho", 0, z), EINVAL); printf("\nGeneric field test"); XMALLOC(x, int); XMALLOC(y, int); XMALLOC(z, int); XMALLOC(u, int); XMALLOC(v, int); XMALLOC(w, int); int_field_t gf; gf.magic = 0; TEST(int_field_add(&gf, x), EINVAL); TEST(int_field_count(&gf), -1); TEST(int_field_initialize(&gf), EOK); TEST(int_field_count(&gf), 0); TEST(int_field_get_index(&gf, 1), NULL); TEST(int_field_add(&gf, x), 0); TEST(int_field_get_index(&gf, 0), x); TEST((int_field_exclude_index(&gf, 0), EOK), EOK); TEST(int_field_get_index(&gf, 0), NULL); TEST(int_field_add(&gf, y), 1); TEST(int_field_get_index(&gf, 1), y); TEST(int_field_add(&gf, z), 2); TEST(int_field_get_index(&gf, 2), z); TEST(int_field_get_index(&gf, 1), y); TEST(int_field_count(&gf), 3); TEST(int_field_add(&gf, u), 3); TEST(int_field_get_index(&gf, 3), u); TEST(int_field_add(&gf, v), 4); TEST(int_field_get_index(&gf, 4), v); TEST(int_field_add(&gf, w), 5); TEST(int_field_get_index(&gf, 5), w); TEST(int_field_count(&gf), 6); TEST((int_field_exclude_index(&gf, 1), EOK), EOK); TEST(int_field_get_index(&gf, 1), NULL); TEST(int_field_get_index(&gf, 3), u); TEST((int_field_exclude_index(&gf, 7), EOK), EOK); TEST(int_field_get_index(&gf, 3), u); TEST(int_field_get_index(&gf, 5), w); TEST((int_field_exclude_index(&gf, 4), EOK), EOK); TEST(int_field_get_index(&gf, 4), NULL); TEST((int_field_destroy(&gf), EOK), EOK); TEST(int_field_count(&gf), -1); printf("\nInt map test"); XMALLOC(x, int); XMALLOC(y, int); XMALLOC(z, int); XMALLOC(u, int); XMALLOC(v, int); XMALLOC(w, int); int_map_t im; im.magic = 0; TEST(int_map_add(&im, 1, x), EINVAL); TEST(int_map_count(&im), -1); TEST(int_map_initialize(&im), EOK); TEST(int_map_count(&im), 0); TEST(int_map_find(&im, 1), NULL); TEST(int_map_add(&im, 1, x), 0); TEST(int_map_find(&im, 1), x); TEST((int_map_exclude(&im, 1), EOK), EOK); TEST(int_map_find(&im, 1), NULL); TEST(int_map_add(&im, 1, y), 1); TEST(int_map_find(&im, 1), y); TEST(int_map_add(&im, 4, z), 2); TEST(int_map_get_index(&im, 2), z); TEST(int_map_find(&im, 4), z); TEST(int_map_find(&im, 1), y); TEST(int_map_count(&im), 3); TEST(int_map_add(&im, 2, u), 3); TEST(int_map_find(&im, 2), u); TEST(int_map_add(&im, 3, v), 4); TEST(int_map_find(&im, 3), v); TEST(int_map_get_index(&im, 4), v); TEST(int_map_add(&im, 6, w), 5); TEST(int_map_find(&im, 6), w); TEST(int_map_count(&im), 6); TEST((int_map_exclude(&im, 1), EOK), EOK); TEST(int_map_find(&im, 1), NULL); TEST(int_map_find(&im, 2), u); TEST((int_map_exclude(&im, 7), EOK), EOK); TEST(int_map_find(&im, 2), u); TEST(int_map_find(&im, 6), w); TEST((int_map_exclude_index(&im, 4), EOK), EOK); TEST(int_map_get_index(&im, 4), NULL); TEST(int_map_find(&im, 3), NULL); TEST((int_map_destroy(&im), EOK), EOK); TEST(int_map_count(&im), -1); printf("\nMeasured strings test"); measured_string_ref string = measured_string_create_bulk("I am a measured string!", 0); printf("\n%x, %s at %x of %d\n", string, string->value, string->value, string->length); return EOK; }