1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright (C) 2013 Intel Corporation
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "history.h"
/* Very simple history storage for easy usage of tool */
#define HISTORY_DEPTH 40
#define LINE_SIZE 200
static char lines[HISTORY_DEPTH][LINE_SIZE];
static int last_line = 0;
static int history_size = 0;
/* TODO: Storing history not implemented yet */
void history_store(const char *filename)
{
}
/* Restoring history from file */
void history_restore(const char *filename)
{
char line[1000];
FILE *f = fopen(filename, "rt");
if (f == NULL)
return;
for (;;) {
if (fgets(line, 1000, f) != NULL) {
int l = strlen(line);
while (l > 0 && isspace(line[--l]))
line[l] = 0;
if (l > 0)
history_add_line(line);
} else
break;
}
fclose(f);
}
/* Add new line to history buffer */
void history_add_line(const char *line)
{
if (line == NULL || strlen(line) == 0)
return;
if (strcmp(line, lines[last_line]) == 0)
return;
last_line = (last_line + 1) % HISTORY_DEPTH;
strncpy(&lines[last_line][0], line, LINE_SIZE - 1);
if (history_size < HISTORY_DEPTH)
history_size++;
}
/*
* Get n-th line from history
* 0 - means latest
* -1 - means oldest
* return -1 if there is no such line
*/
int history_get_line(int n, char *buf, int buf_size)
{
if (n == -1)
n = history_size - 1;
if (n >= history_size || buf_size == 0 || n < 0)
return -1;
strncpy(buf,
&lines[(HISTORY_DEPTH + last_line - n) % HISTORY_DEPTH][0],
buf_size - 1);
buf[buf_size - 1] = 0;
return n;
}
|