[BACK]Return to history.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / windows / asir32gui

Annotation of OpenXM_contrib2/windows/asir32gui/history.c, Revision 1.1.1.1

1.1       noro        1: #include <stdio.h>
                      2: #include <string.h>
                      3: #include <malloc.h>
                      4:
                      5: char *prev_hist(void), *next_hist(void), *search_hist(char *p);
                      6: void write_hist(char *fname), read_hist(char *fname);
                      7: void add_hist(char *p), init_hist(int max);
                      8: void reset_traverse(void);
                      9:
                     10: static char **hist_table;
                     11: static int hist_full, hist_current, hist_length;
                     12: static int hist_traverse, hist_traverse_index;
                     13: #define CURRENT (hist_table[hist_current])
                     14:
                     15: void init_hist(int max)
                     16: {
                     17:        hist_length = max;
                     18:        hist_table = (char **)calloc(hist_length,sizeof(char *));
                     19:        hist_full = 0;
                     20:        hist_current = -1;
                     21: }
                     22:
                     23: void reset_traverse(void)
                     24: {
                     25:        hist_traverse = 0;
                     26: }
                     27:
                     28: void add_hist(char *p)
                     29: {
                     30:        if ( hist_current >= 0 && CURRENT && !strcmp(p,CURRENT) )
                     31:                return;
                     32:        hist_current++;
                     33:        if ( hist_current == hist_length ) {
                     34:                hist_current = 0;
                     35:                hist_full = 1;
                     36:        }
                     37:        CURRENT = (char *)realloc(CURRENT,strlen(p)+1);
                     38:        strcpy(CURRENT,p);
                     39: }
                     40:
                     41: char *search_hist(char *p)
                     42: {
                     43:        int len,slen,index,i;
                     44:
                     45:        if ( *p == '!' ) {
                     46:                if ( hist_current < 0 )
                     47:                        return 0;
                     48:                else
                     49:                        return CURRENT;
                     50:        }
                     51:        slen = strlen(p);
                     52:        len = hist_full ? hist_length : hist_current+1;
                     53:        index = hist_current;
                     54:        for ( i = len; i; i--, index-- ) {
                     55:                if ( index < 0 )
                     56:                        index += hist_length;
                     57:                if ( !strncmp(p,hist_table[index],slen) )
                     58:                        return hist_table[index];
                     59:        }
                     60:        return 0;
                     61: }
                     62:
                     63: void read_hist(char *fname)
                     64: {
                     65:        int l;
                     66:        char buf[BUFSIZ];
                     67:        FILE *fp;
                     68:
                     69:        if ( !(fp = fopen(fname,"rb")) )
                     70:                return;
                     71:        while ( fgets(buf,BUFSIZ-1,fp) ) {
                     72:                l = strlen(buf);
                     73:                if ( buf[l-1] == '\n' )
                     74:                        buf[l-1] = 0;
                     75:                add_hist(buf);
                     76:        }
                     77:        fclose(fp);
                     78: }
                     79:
                     80: void write_hist(char *fname)
                     81: {
                     82:        int len,index,i;
                     83:        FILE *fp;
                     84:
                     85:        if ( !(fp = fopen(fname,"wb")) )
                     86:                return;
                     87:        len = hist_full ? hist_length : hist_current+1;
                     88:        index = hist_full ? hist_current+1 : 0;
                     89:        for ( i = 0; i < len; i++, index++ ) {
                     90:                if ( index >= hist_length )
                     91:                        index -= hist_length;
                     92:                fprintf(fp,"%s\n",hist_table[index]);
                     93:        }
                     94:        fclose(fp);
                     95: }
                     96:
                     97: char *next_hist(void) {
                     98:        if ( !hist_traverse || hist_traverse_index == hist_current )
                     99:                return 0;
                    100:        else {
                    101:                hist_traverse_index = (hist_traverse_index+1) % hist_length;
                    102:                return hist_table[hist_traverse_index];
                    103:        }
                    104: }
                    105:
                    106: char *prev_hist(void) {
                    107:        if ( !hist_traverse ) {
                    108:                if ( hist_current >= 0 ) {
                    109:                        hist_traverse = 1;
                    110:                        hist_traverse_index = hist_current;
                    111:                        return hist_table[hist_traverse_index];
                    112:                } else
                    113:                        return 0;
                    114:        } else {
                    115:                hist_traverse_index--;
                    116:                if ( hist_traverse_index < 0 )
                    117:                        if ( hist_full )
                    118:                                hist_traverse_index += hist_length;
                    119:                        else
                    120:                                hist_traverse_index = 0;
                    121:                return hist_table[hist_traverse_index];
                    122:        }
                    123: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>