~timchen119/+junk/bluez5-trusty

« back to all changes in this revision

Viewing changes to android/client/history.c

  • Committer: tim
  • Date: 2015-09-04 08:03:03 UTC
  • Revision ID: tim@tim-inspiron-5442-20150904080303-75u7z8bdsl17xz8q
* init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Intel Corporation
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 *
 
16
 */
 
17
 
 
18
#include <stdlib.h>
 
19
#include <string.h>
 
20
#include <stdio.h>
 
21
#include <ctype.h>
 
22
 
 
23
#include "history.h"
 
24
 
 
25
/* Very simple history storage for easy usage of tool */
 
26
 
 
27
#define HISTORY_DEPTH 40
 
28
#define LINE_SIZE 200
 
29
static char lines[HISTORY_DEPTH][LINE_SIZE];
 
30
static int last_line = 0;
 
31
static int history_size = 0;
 
32
 
 
33
/* TODO: Storing history not implemented yet */
 
34
void history_store(const char *filename)
 
35
{
 
36
}
 
37
 
 
38
/* Restoring history from file */
 
39
void history_restore(const char *filename)
 
40
{
 
41
        char line[1000];
 
42
        FILE *f = fopen(filename, "rt");
 
43
 
 
44
        if (f == NULL)
 
45
                return;
 
46
 
 
47
        for (;;) {
 
48
                if (fgets(line, 1000, f) != NULL) {
 
49
                        int l = strlen(line);
 
50
 
 
51
                        while (l > 0 && isspace(line[--l]))
 
52
                                line[l] = 0;
 
53
 
 
54
                        if (l > 0)
 
55
                                history_add_line(line);
 
56
                } else
 
57
                        break;
 
58
        }
 
59
 
 
60
        fclose(f);
 
61
}
 
62
 
 
63
/* Add new line to history buffer */
 
64
void history_add_line(const char *line)
 
65
{
 
66
        if (line == NULL || strlen(line) == 0)
 
67
                return;
 
68
 
 
69
        if (strcmp(line, lines[last_line]) == 0)
 
70
                return;
 
71
 
 
72
        last_line = (last_line + 1) % HISTORY_DEPTH;
 
73
        strncpy(&lines[last_line][0], line, LINE_SIZE - 1);
 
74
        if (history_size < HISTORY_DEPTH)
 
75
                history_size++;
 
76
}
 
77
 
 
78
/*
 
79
 * Get n-th line from history
 
80
 * 0 - means latest
 
81
 * -1 - means oldest
 
82
 * return -1 if there is no such line
 
83
 */
 
84
int history_get_line(int n, char *buf, int buf_size)
 
85
{
 
86
        if (n == -1)
 
87
                n = history_size - 1;
 
88
 
 
89
        if (n >= history_size || buf_size == 0 || n < 0)
 
90
                return -1;
 
91
 
 
92
        strncpy(buf,
 
93
                &lines[(HISTORY_DEPTH + last_line - n) % HISTORY_DEPTH][0],
 
94
                buf_size - 1);
 
95
        buf[buf_size - 1] = 0;
 
96
 
 
97
        return n;
 
98
}