Blame android/client/history.c

Packit 34410b
/*
Packit 34410b
 * Copyright (C) 2013 Intel Corporation
Packit 34410b
 *
Packit 34410b
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 34410b
 * you may not use this file except in compliance with the License.
Packit 34410b
 * You may obtain a copy of the License at
Packit 34410b
 *
Packit 34410b
 *      http://www.apache.org/licenses/LICENSE-2.0
Packit 34410b
 *
Packit 34410b
 * Unless required by applicable law or agreed to in writing, software
Packit 34410b
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 34410b
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 34410b
 * See the License for the specific language governing permissions and
Packit 34410b
 * limitations under the License.
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#include <stdlib.h>
Packit 34410b
#include <string.h>
Packit 34410b
#include <stdio.h>
Packit 34410b
#include <ctype.h>
Packit 34410b
Packit 34410b
#include "history.h"
Packit 34410b
Packit 34410b
/* Very simple history storage for easy usage of tool */
Packit 34410b
Packit 34410b
#define HISTORY_DEPTH 40
Packit 34410b
#define LINE_SIZE 200
Packit 34410b
static char lines[HISTORY_DEPTH][LINE_SIZE];
Packit 34410b
static int last_line = 0;
Packit 34410b
static int history_size = 0;
Packit 34410b
Packit 34410b
/* TODO: Storing history not implemented yet */
Packit 34410b
void history_store(const char *filename)
Packit 34410b
{
Packit 34410b
}
Packit 34410b
Packit 34410b
/* Restoring history from file */
Packit 34410b
void history_restore(const char *filename)
Packit 34410b
{
Packit 34410b
	char line[1000];
Packit 34410b
	FILE *f = fopen(filename, "rt");
Packit 34410b
Packit 34410b
	if (f == NULL)
Packit 34410b
		return;
Packit 34410b
Packit 34410b
	for (;;) {
Packit 34410b
		if (fgets(line, 1000, f) != NULL) {
Packit 34410b
			int l = strlen(line);
Packit 34410b
Packit 34410b
			while (l > 0 && isspace(line[--l]))
Packit 34410b
				line[l] = 0;
Packit 34410b
Packit 34410b
			if (l > 0)
Packit 34410b
				history_add_line(line);
Packit 34410b
		} else
Packit 34410b
			break;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	fclose(f);
Packit 34410b
}
Packit 34410b
Packit 34410b
/* Add new line to history buffer */
Packit 34410b
void history_add_line(const char *line)
Packit 34410b
{
Packit 34410b
	if (line == NULL || strlen(line) == 0)
Packit 34410b
		return;
Packit 34410b
Packit 34410b
	if (strcmp(line, lines[last_line]) == 0)
Packit 34410b
		return;
Packit 34410b
Packit 34410b
	last_line = (last_line + 1) % HISTORY_DEPTH;
Packit 34410b
	strncpy(&lines[last_line][0], line, LINE_SIZE - 1);
Packit 34410b
	if (history_size < HISTORY_DEPTH)
Packit 34410b
		history_size++;
Packit 34410b
}
Packit 34410b
Packit 34410b
/*
Packit 34410b
 * Get n-th line from history
Packit 34410b
 * 0 - means latest
Packit 34410b
 * -1 - means oldest
Packit 34410b
 * return -1 if there is no such line
Packit 34410b
 */
Packit 34410b
int history_get_line(int n, char *buf, int buf_size)
Packit 34410b
{
Packit 34410b
	if (n == -1)
Packit 34410b
		n = history_size - 1;
Packit 34410b
Packit 34410b
	if (n >= history_size || buf_size == 0 || n < 0)
Packit 34410b
		return -1;
Packit 34410b
Packit 34410b
	strncpy(buf,
Packit 34410b
		&lines[(HISTORY_DEPTH + last_line - n) % HISTORY_DEPTH][0],
Packit 34410b
		buf_size - 1);
Packit 34410b
	buf[buf_size - 1] = 0;
Packit 34410b
Packit 34410b
	return n;
Packit 34410b
}