added comment about possible memory leak
[CleanReadLine.git] / Clean System Files / readLine.c
index b72d31a..bea7de6 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);
@@ -56,6 +57,63 @@ void cleanUsingHistory(){
        using_history();
 }
 
+void cleanGetState(int *offset, int *num, int *flags){
+       *offset = history_get_history_state()->offset;
+       *num = history_get_history_state()->length;
+       *flags = history_get_history_state()->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;
+
+       CleanStringVariable(cleanLine, strlen(cs_line));
+       *line = (CleanString) cleanLine;
+       memcpy(CleanStringCharacters(cleanLine), cs_line, strlen(cs_line));
+       CleanStringLength(cleanLine) = strlen(cs_line);
+
+       CleanStringVariable(cleanTimestamp, strlen(cs_stamp));
+       *timestamp = (CleanString) cleanTimestamp;
+       memcpy(CleanStringCharacters(cleanTimestamp), cs_stamp, strlen(cs_stamp));
+       CleanStringLength(cleanTimestamp) = strlen(cs_stamp);
+}
+
+void cleanInitNewHistoryState(int offset, int flags, int num){
+       if(history_state != NULL){
+               //we should test if we can recursively free the object
+               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;
+}
+
+void cleanSetNewHistoryEntry(int i, CleanString line, CleanString timestamp){
+       char *cs_line = cleanStringToCString(line);
+       char *cs_timestamp = cleanStringToCString(timestamp);
+       history_state->entries[i]->line = cs_line;
+       history_state->entries[i]->timestamp = cs_timestamp;
+}
+
+void cleanCommitSetHistory(void){
+       history_set_history_state(history_state);
+}
+
 void cleanAddHistory(CleanString entry){
        char *cs_entry = cleanStringToCString(entry);
        add_history(cs_entry);
@@ -126,4 +184,3 @@ int cleanHistoryTruncateFile(CleanString path, int nlines)
        free(cs_path);
        return errno;
 }
-