示例#1
0
文件: history.c 项目: att/ast
//
// Given the current command and line number, and number of lines back or foward, compute the new
// command and line number.
//
Histloc_t hist_locate(History_t *hp, int command, int line, int lines) {
    Histloc_t next;

    line += lines;
    if (!hp) {
        command = -1;
        goto done;
    }
    if (lines > 0) {
        int count;
        while (command <= hp->histind) {
            count = hist_copy(NULL, 0, command, -1);
            if (count > line) goto done;
            line -= count;
            command++;
        }
    } else {
        int least = (int)hp->histind - hp->histsize;
        while (1) {
            if (line >= 0) goto done;
            if (--command < least) break;
            line += hist_copy(NULL, 0, command, -1);
        }
        command = -1;
    }
done:
    next.hist_line = line;
    next.hist_command = command;
    next.hist_char = 0;
    return next;
}
示例#2
0
文件: history.c 项目: att/ast
//
// Return word number <word> from command number <command>.
//
char *hist_word(char *string, int size, int word) {
    int c;
    char *s1 = string;
    unsigned char *cp = (unsigned char *)s1;
    int flag = 0;
    History_t *hp = hist_ptr;

    if (!hp) return NULL;
    hist_copy(string, size, (int)hp->histind - 1, -1);
    for (; (c = *cp); cp++) {
        c = isspace(c);
        if (c && flag) {
            *cp = 0;
            if (--word == 0) break;
            flag = 0;
        } else if (c == 0 && flag == 0) {
            s1 = (char *)cp;
            flag++;
        }
    }
    *cp = 0;
    // We can't use strcpy() because the two buffers may overlap.
    if (s1 != string) memmove(string, s1, strlen(s1) + 1);
    return string;
}
示例#3
0
/**
 * Add an entry with text `text` to the history list, with type `type`
 * ("HIST_xxx" in player-history.h), and artifact number `id` (0 for
 * everything else).
 *
 * Return true on success.
 */
bool history_add_full(struct player *p,
                      bitflag *type,
                      int aidx,
                      int dlev,
                      int clev,
                      int turnno,
                      const char *text)
{
    struct player_history *h = &p->hist;

    /* Allocate or expand the history list if needed */
    if (!h->entries)
        history_init(h);
    else if (h->next == h->length)
        history_realloc(h);

    /* Add entry */
    hist_copy(h->entries[h->next].type, type);
    h->entries[h->next].dlev = dlev;
    h->entries[h->next].clev = clev;
    h->entries[h->next].a_idx = aidx;
    h->entries[h->next].turn = turnno;
    my_strcpy(h->entries[h->next].event,
              text,
              sizeof(h->entries[h->next].event));

    h->next++;

    return true;
}
示例#4
0
/**
 * Add an entry with text `event` to the history list, with type `type`
 * ("HIST_xxx" in player-history.h), and artifact number `id` (0 for
 * everything else).
 *
 * Return true on success.
 */
bool history_add_full(bitflag *type, struct artifact *artifact, s16b dlev,
		s16b clev, s32b turnno, const char *text)
{
	/* Allocate or expand the history list as needed */
	if (!history_list)
		history_init(HISTORY_BIRTH_SIZE);
	else if ((history_ctr == history_size) &&
			 !history_set_num(history_size + 10))
		return false;

	/* History list exists and is not full.  Add an entry at the current
	 * counter location. */
	hist_copy(history_list[history_ctr].type, type);
	history_list[history_ctr].dlev = dlev;
	history_list[history_ctr].clev = clev;
	history_list[history_ctr].a_idx = artifact ? artifact->aidx : 0;
	history_list[history_ctr].turn = turnno;
	my_strcpy(history_list[history_ctr].event,
	          text, sizeof(history_list[history_ctr].event));

	history_ctr++;

	return true;
}