From 851ac869ee55686234ae246ab9b5d938aec0f225 Mon Sep 17 00:00:00 2001 From: andy Date: Sun, 28 Oct 2012 14:10:39 +1300 Subject: [PATCH] More work. --- funcs.c | 34 ++++++++++++++++++++++++++++++++-- history.c | 35 +++++++++++++++++++++++++---------- keyhandler.c | 13 ++++--------- pd_readline.c | 13 +++++++------ pd_readline.h | 10 ++++++---- range.c | 7 +++++++ 6 files changed, 81 insertions(+), 31 deletions(-) diff --git a/funcs.c b/funcs.c index 8ac14c6..36241e5 100644 --- a/funcs.c +++ b/funcs.c @@ -19,8 +19,38 @@ /* Also test for the top and bottom of the history file. */ +/* Display a buffer */ +void show(buf b) +{ + printf("%s", b.array); +} + + + +/* Enter a char into a buffer and display the buffer array. */ +buf set(buf b, int i) +{ + if ( (b.index < 80) ) + { + b.array[b.index] = i; + b.index += 1 ; + } + else + { + memset(b.array[0], 0, sizeof(b.array) ); + b.array[0] = i; + b.index += 1 ; + } + + show(b); + return b; + +} + + + /* Move up in history list. */ -buf up(int i) +buf up(buf b) { @@ -29,7 +59,7 @@ buf up(int i) /* Move down in history list. */ -buf down(int i) +buf down(buf b) { diff --git a/history.c b/history.c index 2370750..dd70ed5 100644 --- a/history.c +++ b/history.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include "pd_readline.h" /* Helper function, to let us see if a file */ @@ -40,10 +42,12 @@ char hist[20][80]; /* Read the file into the array of strings. */ -/* TO DO - look at reading file into a series of structs. */ - -void readhistory(char *fname) +buf readhistory(char *fname) { + + /* Create a history buffer. */ + buf h; + int retval = fexists(fname); int i; @@ -53,22 +57,33 @@ void readhistory(char *fname) /* append new commands to it. */ FILE *fptr; fptr = fopen(fname, "rw"); - + + char line[80] ; + for(i=0; i<20; i++) - { - chop(fgets(hist[i], 80, fptr) ); + { + fgets(line, 80, fptr); + chop(line) ; + memcpy(hist[i], line, 80) ; + puts(hist[i]); } } /* retval == 0 */ else puts("Error! File does not exist. \n"); + /* Read the most recent command into histbuf */ + /* and set the index to 19. */ + /* + h.index = 19; + h.array = hist[h.index] ; + */ + + return h; + } - -/* TO DO - a function that reads in the 2d history array, and */ -/* returns a struct with one line from the array, and the index */ -/* of the line. */ + diff --git a/keyhandler.c b/keyhandler.c index 9636491..0ba3c6a 100644 --- a/keyhandler.c +++ b/keyhandler.c @@ -65,17 +65,12 @@ char getche(void) { /* Alt keys are just esc then key (e.g. Alt-g is esc g ). */ /* Ctrl (then letter) keys are just Dec 1 to Dec 26 */ -/* TO DO: Use the helper function range */ -/* ( range(rstart, rend, val). ) */ -/* to handle entire ranges at once. */ - - void keyhandler(buf b) { - int i = getch(); + int a = getch(); - int t = type(i); + int t = type(a); switch(t) { @@ -83,13 +78,13 @@ void keyhandler(buf b) case (1): break; /* Ctrl a */ case (2): break; /* Ctrl b */ case (3): getch(); spec(); break; /* Ctrl c */ - case (4): printf("%c", i); break; /* Printable chars. */ + case (4): set(b, a); break; /* Printable chars. */ case (5): delch(b); break; case (6): break; default: break; } - + } diff --git a/pd_readline.c b/pd_readline.c index c92870a..8649fc1 100644 --- a/pd_readline.c +++ b/pd_readline.c @@ -29,19 +29,20 @@ int main(void) { - /* Create a buffer for the text. */ + /* Create a buffer for entered text. */ buf mybuf; - + mybuf.index = 0; + /* Read in the command history file. */ - readhistory("test.txt"); - + buf histbuf = readhistory("test.txt"); + while(1) { - keyhandler(mybuf); - + keyhandler(mybuf); + } return 0; diff --git a/pd_readline.h b/pd_readline.h index a79e7a6..ea5efbe 100644 --- a/pd_readline.h +++ b/pd_readline.h @@ -15,9 +15,11 @@ typedef struct { -/* Cursor movement funcs. */ -buf up(int i); -buf down(int i); +/* Buffer funcs. */ +buf set(buf b, int i); +void show(buf b); +buf up(buf b); +buf down(buf b); buf left(buf b); buf right(buf b); buf delch(buf b); @@ -32,7 +34,7 @@ void spec(void); /* Other funcs. */ -void readhistory(char *fname); +buf readhistory(char *fname); void keyhandler(buf b); diff --git a/range.c b/range.c index ddb7ce0..c4c6cfe 100644 --- a/range.c +++ b/range.c @@ -40,6 +40,13 @@ int g = range(50, 80, 91); printf("%d %d %d %d %d %d %d \n", a, b, c, d, e, f, g); +int i = 65; +char s[2]; + +snprintf(s, 2, "%c", i); + +printf("%s \n" , s ); + return 0; -- 2.40.0