]> pd.if.org Git - pd_readline/blob - history.c
498cbe8e86af1a9267a7b4c9b89374fe1fa4df50
[pd_readline] / history.c
1
2
3 /*  history.c                                          */ 
4 /*  Command history.                                   */ 
5 /*  This code is released to the public domain.        */ 
6 /*  "Share and enjoy...."  ;)                          */  
7 /*  See the UNLICENSE file for details.                */ 
8
9 #include <string.h>   
10 #include <stdio.h> 
11 #include <stdlib.h> 
12 #include <malloc.h>
13 #include "pd_readline.h"  
14
15
16 /*  Helper function, to let us see if a file */ 
17 /*  exists in the current directory.  */ 
18 int fexists(char *fname)
19 {  
20    FILE *fptr;  
21    fptr = fopen(fname, "r") ;     
22    if ( !fptr )  return -1 ;  /* File does not exist in dir. */                  
23    fclose(fptr);  
24    return 0;    /* File DOES exist in dir.  */   
25
26
27
28 /* Helper function to chop newlines off the lines read in. */ 
29 /* Without this being done, an extra newline is inserted */ 
30 /* (which is usually not what is wanted). */ 
31 char *chop(char *s)
32 {
33   s[strcspn(s,"\n")] = '\0'; 
34   return s; 
35 }
36
37
38
39 /* Read the file into the array of strings.   */ 
40 hist readhistory(char *fname) 
41
42         
43    /*  Create a history buffer.  */ 
44    hist h;      
45         
46    int retval = fexists(fname); 
47         
48    int i; 
49    if (retval == 0) { 
50           /* File exists, so open it. */  
51           /* We open it in read-write mode so we can */ 
52           /* append new commands to it. */ 
53           FILE *fptr;  
54           fptr = fopen(fname, "rw"); 
55                 
56       char line[80] ; 
57                                            
58           for(i=0; i<20; i++)  
59           { 
60                 fgets(line, 80, fptr);                   
61                 chop(line) ;  
62                 
63                 /* TO DO: fix the "too few arguments" bug here.... */  
64                 memcpy(&h.array[i], line, 80) ;   
65                 puts(h.array[i]); 
66           }
67           
68    }  /* retval == 0  */           
69         
70         else puts("Error! File does not exist. \n");  
71                 
72         /*  Set the curindex to 19.  */     
73             h.curindex = 19;       
74                 
75         return h;
76         
77 }       
78
79                                            
80
81  
82
83
84