]> pd.if.org Git - pd_readline/blob - history.c
23707502c626fb6d9a5a78686220405b05c4facf
[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
13
14 /*  Helper function, to let us see if a file */ 
15 /*  exists in the current directory.  */ 
16 int fexists(char *fname)
17 {  
18    FILE *fptr;  
19    fptr = fopen(fname, "r") ;     
20    if ( !fptr )  return -1 ;  /* File does not exist in dir. */                  
21    fclose(fptr);  
22    return 0;    /* File DOES exist in dir.  */   
23
24
25
26 /* Helper function to chop newlines off the lines read in. */ 
27 /* Without this being done, an extra newline is inserted */ 
28 /* (which is usually not what is wanted). */ 
29 char *chop(char *s)
30 {
31   s[strcspn(s,"\n")] = '\0'; 
32   return s; 
33 }
34
35
36 /* An array to store the command-history file in. */ 
37 /* Only 20 lines are read. */ 
38 char hist[20][80];  
39
40
41
42 /* Read the file into the array of strings.                */ 
43 /* TO DO - look at reading file into a series of structs.  */ 
44
45 void readhistory(char *fname) 
46
47    int retval = fexists(fname); 
48         
49    int i; 
50    if (retval == 0) { 
51           /* File exists, so open it. */  
52           /* We open it in read-write mode so we can */ 
53           /* append new commands to it. */ 
54           FILE *fptr;  
55           fptr = fopen(fname, "rw"); 
56                            
57           for(i=0; i<20; i++)  
58           {  
59                 chop(fgets(hist[i], 80, fptr) );  
60           }
61           
62    }  /* retval == 0  */           
63         
64         else puts("Error! File does not exist. \n");  
65         
66 }       
67
68
69 /* TO DO - a function that reads in the 2d history array, and    */ 
70 /* returns a struct with one line from the array, and the index  */
71 /* of the line.                                                  */ 
72
73  
74
75
76