setting history now works
authorMart Lubbers <mart@martlubbers.net>
Tue, 26 Jan 2016 18:56:19 +0000 (19:56 +0100)
committerMart Lubbers <mart@martlubbers.net>
Tue, 26 Jan 2016 18:56:19 +0000 (19:56 +0100)
Clean System Files/readLine.c
ReadLine.icl
test.icl

index 4836bcb..fa9315a 100644 (file)
@@ -7,6 +7,7 @@
 #include "Clean.h"
 
 static char *cs_answer = (char *)NULL;
+HISTORY_STATE *history_state = NULL;
 
 char *cleanStringToCString(CleanString s){
        unsigned long len = CleanStringLength(s);
@@ -62,10 +63,6 @@ void cleanGetState(int *offset, int *num, int *flags){
        *flags = history_get_history_state()->flags;
 }
 
-void cleanSetState(int offset, int num, int flags){
-       ;
-}
-
 void cleanGetHistoryItem(int num, CleanString *line, CleanString *timestamp){
        char *cs_line = history_get_history_state()->entries[num]->line;
        char *cs_stamp = history_get_history_state()->entries[num]->timestamp;
@@ -81,6 +78,47 @@ void cleanGetHistoryItem(int num, CleanString *line, CleanString *timestamp){
        CleanStringLength(cleanTimestamp) = strlen(cs_stamp);
 }
 
+void cleanInitNewHistoryState(int offset, int flags, int num){
+       printf("Initializing: %d, %d, %d\n", offset, flags, num);
+       if(history_state != NULL){
+               free(history_state);
+       }
+       history_state = (HISTORY_STATE *)malloc(sizeof(HISTORY_STATE));
+       if(history_state == NULL){
+               printf("Not enough memory...\n");
+               exit(1);
+       }
+       HIST_ENTRY **entries = (HIST_ENTRY **)malloc(sizeof(HIST_ENTRY*)*num);
+       if(entries == NULL){
+               printf("Not enough memory...\n");
+               exit(1);
+       }
+       for(int i = 0; i<num; i++){
+               entries[i] = (HIST_ENTRY *)malloc(sizeof(HIST_ENTRY));
+       }
+       history_state->offset = offset;
+       history_state->entries = entries;
+       history_state->length = num;
+       history_state->size = num;
+       history_state->flags = 0;
+       printf("Initialization done\n");
+}
+
+void cleanSetNewHistoryEntry(int i, CleanString line, CleanString timestamp){
+       char *cs_line = cleanStringToCString(line);
+       char *cs_timestamp = cleanStringToCString(timestamp);
+       printf("adding: %d, %s, %s\n", i, cs_line, cs_timestamp);
+       history_state->entries[i]->line = cs_line;
+       history_state->entries[i]->timestamp = cs_timestamp;
+       printf("done adding: %d, %s, %s\n", i, cs_line, cs_timestamp);
+}
+
+void cleanCommitSetHistory(void){
+       printf("commit\n");
+       history_set_history_state(history_state);
+       printf("committed\n");
+}
+
 void cleanAddHistory(CleanString entry){
        char *cs_entry = cleanStringToCString(entry);
        add_history(cs_entry);
index b377371..07b90ac 100644 (file)
@@ -45,7 +45,30 @@ historyGetHistoryState e
                
 
 historySetHistoryState :: !HistoryState !*env -> !*env
-historySetHistoryState hs e = abort "Not Implemented"
+historySetHistoryState {entries,offset,flags} e
+# e = initNewHistoryState offset flags (length entries) e
+# e = setItems entries 0 e
+= commit e
+       where
+               initNewHistoryState :: !Int !Int !Int !*env -> !*env
+               initNewHistoryState o f l e = code {
+                               ccall cleanInitNewHistoryState "III:V:A"
+                       }
+               setItems :: ![HistoryItem] !Int !*env -> !*env
+               setItems [] _ e = e
+               setItems [x:xs] i e
+               # e = setItem i x.line x.timestamp e
+               = setItems xs (i+1) e
+
+               setItem :: !Int !String !String !*env -> !*env
+               setItem i l t e = code {
+                               ccall cleanSetNewHistoryEntry "ISS:V:A"
+                       }
+               
+               commit :: !*env -> !*env
+               commit e = code {
+                               ccall cleanCommitSetHistory ":V:A"
+                       }
 
 //History List Management
 addHistory :: !String !*env -> !*env
index 43f867d..7612e6a 100644 (file)
--- a/test.icl
+++ b/test.icl
@@ -3,7 +3,12 @@ module test
 import StdEnv
 import ReadLine
 
-Start :: *World -> (Int, String, HistoryState, *World)
+testHistory :: HistoryState
+testHistory = {HistoryState | 
+       entries=[{HistoryItem | line="custom", timestamp=""}],
+       offset=1, flags=0}
+
+Start :: *World -> (Int, String, HistoryState, HistoryState, *World)
 Start w
 # (f, w) = stdio w
 #! w = setReadLineName "Test program" w
@@ -14,6 +19,8 @@ Start w
 #! (s, w) = readLine "uparrow should word with history: " False w
 #! (_, w) = writeHistory "readline.history" w
 #! (i, w) = historySearch "testentry" -1 w
+#! (oh, w) = historyGetHistoryState w
+#! w = historySetHistoryState testHistory w
 #! (h, w) = historyGetHistoryState w
 #! w = clearHistory w
-= (i, s, h, w)
+= (i, s, oh, h, w)