2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2007-2008 - INRIA - Allan CORNET
4 * Copyright (C) 2010 - DIGITEO - Vincent COUVERT
5 * Copyright (C) 2011 - DIGITEO - Allan CORNET
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
15 /*------------------------------------------------------------------------*/
16 #include "HistoryManager.hxx"
17 #include "HistoryManager.h"
18 #include "getCommentDateSession.h"
20 /*------------------------------------------------------------------------*/
28 #include "expandPathVariable.h"
29 #include "InitializeHistoryManager.h"
30 #include "TerminateHistoryManager.h"
31 #include "freeArrayOfString.h"
33 #include "strdup_windows.h"
35 #include "CommandHistory_Wrap.h"
38 /*------------------------------------------------------------------------*/
40 /*------------------------------------------------------------------------*/
41 static HistoryManager *ScilabHistory = NULL;
43 /*------------------------------------------------------------------------*/
44 BOOL historyIsEnabled(void)
53 /*------------------------------------------------------------------------*/
54 BOOL InitializeHistoryManager(void)
58 ScilabHistory = new HistoryManager();
67 /*------------------------------------------------------------------------*/
68 BOOL TerminateHistoryManager(void)
80 /*------------------------------------------------------------------------*/
81 BOOL setSearchedTokenInScilabHistory(char *token)
85 return ScilabHistory->setToken(token);
90 /*------------------------------------------------------------------------*/
91 BOOL resetSearchedTokenInScilabHistory(void)
95 return ScilabHistory->resetToken();
100 /*------------------------------------------------------------------------*/
101 char *getSearchedTokenInScilabHistory(void)
107 token = ScilabHistory->getToken();
112 /*------------------------------------------------------------------------*/
113 BOOL appendLineToScilabHistory(char *line)
120 char *cleanedline = NULL;
122 if (ScilabHistory && ScilabHistory->getNumberOfLines() == 0)
124 char * commentbeginsession = getCommentDateSession(FALSE);
125 ScilabHistory->appendLine(commentbeginsession);
126 FREE(commentbeginsession);
127 CommandHistoryExpandAll();
130 /* remove space & carriage return at the end of line */
131 cleanedline = strdup(line);
133 /* remove carriage return at the end of line */
134 for (i = (int)strlen(cleanedline); i > 0; i--)
136 if (cleanedline[i] == '\n')
138 cleanedline[i] = '\0';
143 /* remove spaces at the end of line */
144 i = (int)strlen(cleanedline) - 1;
147 if (cleanedline[i] == ' ')
149 cleanedline[i] = '\0';
161 bOK = ScilabHistory->appendLine(cleanedline);
174 /*------------------------------------------------------------------------*/
175 BOOL appendLinesToScilabHistory(char **lines, int numberoflines)
179 return ScilabHistory->appendLines(lines, numberoflines);
184 /*------------------------------------------------------------------------*/
185 void displayScilabHistory(void)
189 ScilabHistory->displayHistory();
193 /*------------------------------------------------------------------------*/
194 BOOL writeScilabHistoryToFile(char *filename)
198 return ScilabHistory->writeToFile(filename);
203 /*------------------------------------------------------------------------*/
204 BOOL loadScilabHistoryFromFile(char *filename)
208 return ScilabHistory->loadFromFile(filename);
213 /*------------------------------------------------------------------------*/
214 BOOL isScilabHistoryTruncated(void)
219 bOK = ScilabHistory->isTruncated();
223 /*------------------------------------------------------------------------*/
224 BOOL setFilenameScilabHistory(char *filename)
226 char * expandedPath = NULL;
232 expandedPath = expandPathVariable(filename);
235 ScilabHistory->setFilename(expandedPath);
240 ScilabHistory->setFilename(filename);
249 /*------------------------------------------------------------------------*/
250 char *getFilenameScilabHistory(void)
252 char *filename = NULL;
256 filename = ScilabHistory->getFilename();
261 /*------------------------------------------------------------------------*/
262 BOOL setDefaultFilenameScilabHistory(void)
266 return ScilabHistory->setDefaultFilename();
271 /*------------------------------------------------------------------------*/
272 void resetScilabHistory(void)
276 ScilabHistory->reset();
280 /*------------------------------------------------------------------------*/
281 char **getAllLinesOfScilabHistory(void)
288 lines = ScilabHistory->getAllLines(&nbElements);
289 /* SWIG need array finish with NULL */
292 lines = (char **)REALLOC(lines, sizeof(char *) * (nbElements + 1));
293 lines[nbElements] = NULL;
299 /*------------------------------------------------------------------------*/
300 int getSizeAllLinesOfScilabHistory(void)
307 lines = ScilabHistory->getAllLines(&nbElements);
310 freeArrayOfString(lines, nbElements);
315 /*------------------------------------------------------------------------*/
316 char *getLastLineInScilabHistory(void)
322 line = ScilabHistory->getLastLine();
327 /*------------------------------------------------------------------------*/
328 char *getPreviousLineInScilabHistory(void)
334 line = ScilabHistory->getPreviousLine();
339 /*------------------------------------------------------------------------*/
340 char *getNextLineInScilabHistory(void)
346 line = ScilabHistory->getNextLine();
351 /*------------------------------------------------------------------------*/
352 int getNumberOfLinesInScilabHistory(void)
358 val = ScilabHistory->getNumberOfLines();
363 /*------------------------------------------------------------------------*/
364 void setSaveConsecutiveDuplicateLinesInScilabHistory(BOOL doit)
368 ScilabHistory->setSaveConsecutiveDuplicateLines(doit);
372 /*------------------------------------------------------------------------*/
373 BOOL getSaveConsecutiveDuplicateLinesInScilabHistory(void)
377 return ScilabHistory->getSaveConsecutiveDuplicateLines();
382 /*------------------------------------------------------------------------*/
383 void setAfterHowManyLinesScilabHistoryIsSaved(int num)
387 ScilabHistory->setAfterHowManyLinesHistoryIsSaved(num);
391 /*------------------------------------------------------------------------*/
392 int getAfterHowManyLinesScilabHistoryIsSaved(void)
398 val = ScilabHistory->getAfterHowManyLinesHistoryIsSaved();
403 /*------------------------------------------------------------------------*/
404 char *getNthLineInScilabHistory(int N)
408 return ScilabHistory->getNthLine(N);
413 /*------------------------------------------------------------------------*/
414 BOOL deleteNthLineScilabHistory(int N)
418 return ScilabHistory->deleteNthLine(N);
423 /*------------------------------------------------------------------------*/
424 int getSizeScilabHistory(void)
430 val = ScilabHistory->getNumberOfLines() - 1;
435 /*------------------------------------------------------------------------*/
436 BOOL setSizeMaxScilabHistory(int nbLinesMax)
441 bOK = ScilabHistory->setNumberOfLinesMax(nbLinesMax);
445 /*------------------------------------------------------------------------*/
446 int getSizeMaxScilabHistory(void)
451 val = ScilabHistory->getNumberOfLinesMax();
455 /*------------------------------------------------------------------------*/
456 HistoryManager::HistoryManager()
459 CommandsList.clear();
460 saveconsecutiveduplicatelines = FALSE;
461 afterhowmanylineshistoryissaved = 0;
462 numberoflinesbeforehistoryissaved = 0;
464 CommandHistoryInitialize();
467 /*------------------------------------------------------------------------*/
468 HistoryManager::~HistoryManager()
470 CommandsList.clear();
473 /*------------------------------------------------------------------------*/
474 BOOL HistoryManager::appendLine(char *cline)
480 if (!saveconsecutiveduplicatelines)
482 char *previousline = getLastLine();
484 if ((previousline) && (strcmp(previousline, cline) == 0))
490 CommandLine Line(cline);
492 CommandsList.push_back(Line);
493 numberoflinesbeforehistoryissaved++;
496 CommandHistoryAppendLine(cline);
506 CommandLine Line(cline);
508 CommandsList.push_back(Line);
510 numberoflinesbeforehistoryissaved++;
513 CommandHistoryAppendLine(cline);
517 if (afterhowmanylineshistoryissaved != 0)
519 if (afterhowmanylineshistoryissaved == numberoflinesbeforehistoryissaved)
521 my_file.setHistory(CommandsList);
522 my_file.writeToFile();
523 numberoflinesbeforehistoryissaved = 0;
528 numberoflinesbeforehistoryissaved = 0;
534 /*------------------------------------------------------------------------*/
535 BOOL HistoryManager::appendLines(char **lines, int nbrlines)
540 for (i = 0; i < nbrlines; i++)
542 if ((lines[i] == NULL) || (!appendLine(lines[i])))
550 /*------------------------------------------------------------------------*/
551 void HistoryManager::displayHistory(void)
555 list < CommandLine >::iterator it_commands;
556 for (it_commands = CommandsList.begin(); it_commands != CommandsList.end(); ++it_commands)
558 std::string line = (*it_commands).get();
561 sciprint("%d : %s\n", nbline, line.c_str());
567 /*------------------------------------------------------------------------*/
568 char *HistoryManager::getFilename(void)
570 char *filename = NULL;
572 if (!my_file.getFilename().empty())
574 filename = strdup(my_file.getFilename().c_str());
579 /*------------------------------------------------------------------------*/
580 void HistoryManager::setFilename(char *filename)
585 name.assign(filename);
586 my_file.setFilename(name);
590 /*------------------------------------------------------------------------*/
591 BOOL HistoryManager::setDefaultFilename(void)
593 return my_file.setDefaultFilename();
596 /*------------------------------------------------------------------------*/
597 BOOL HistoryManager::writeToFile(char *filename)
602 name.assign(filename);
604 my_file.setHistory(CommandsList);
605 return my_file.writeToFile(name);
610 /*------------------------------------------------------------------------*/
611 BOOL HistoryManager::loadFromFile(char *filename)
616 char *commentbeginsession = NULL;
619 name.assign(filename);
621 if (my_file.loadFromFile(name) == HISTORY_TRUNCATED)
626 CommandsList.clear();
627 CommandsList = my_file.getHistory();
629 if (CommandsList.size() > 0)
631 char *firstLine = getFirstLine();
635 if (!isBeginningSessionLine(firstLine))
644 /* add date & time @ begin session */
645 commentbeginsession = getCommentDateSession(FALSE);
646 appendLine(commentbeginsession);
647 FREE(commentbeginsession);
648 commentbeginsession = NULL;
650 CommandHistoryLoadFromFile();
657 /*--------------------------------------------------------------------------*/
658 void HistoryManager::reset(void)
660 char *commentbeginsession = NULL;
662 CommandsList.clear();
665 my_file.setDefaultFilename();
669 saveconsecutiveduplicatelines = FALSE;
670 afterhowmanylineshistoryissaved = 0;
671 numberoflinesbeforehistoryissaved = 0;
673 CommandHistoryReset();
675 /* Add date & time begin session */
676 commentbeginsession = getCommentDateSession(FALSE);
677 if (commentbeginsession)
679 appendLine(commentbeginsession);
680 FREE(commentbeginsession);
681 commentbeginsession = NULL;
686 /*--------------------------------------------------------------------------*/
687 char **HistoryManager::getAllLines(int *numberoflines)
693 if (CommandsList.empty())
699 list < CommandLine >::iterator it_commands;
702 lines = (char **)MALLOC((int)CommandsList.size() * (sizeof(char *)));
703 for (it_commands = CommandsList.begin(); it_commands != CommandsList.end(); ++it_commands)
705 string line = (*it_commands).get();
709 lines[i] = strdup(line.c_str());
718 /*--------------------------------------------------------------------------*/
719 char *HistoryManager::getFirstLine(void)
723 if (!CommandsList.empty())
726 list < CommandLine >::iterator it_commands = CommandsList.begin();
727 str = (*it_commands).get();
730 line = strdup(str.c_str());
736 /*--------------------------------------------------------------------------*/
737 char *HistoryManager::getLastLine(void)
741 if (!CommandsList.empty())
744 list < CommandLine >::iterator it_commands = CommandsList.end();
746 str = (*it_commands).get();
749 line = strdup(str.c_str());
755 /*--------------------------------------------------------------------------*/
756 int HistoryManager::getNumberOfLines(void)
758 return (int)CommandsList.size();
761 /*--------------------------------------------------------------------------*/
762 char *HistoryManager::getNthLine(int N)
768 N = getNumberOfLines() + N;
771 if ((N >= 0) && (N <= getNumberOfLines()))
775 list < CommandLine >::iterator it_commands;
776 for (it_commands = CommandsList.begin(); it_commands != CommandsList.end(); ++it_commands)
782 str = (*it_commands).get();
785 return strdup(str.c_str());
794 /*--------------------------------------------------------------------------*/
795 BOOL HistoryManager::deleteNthLine(int N)
797 if ((N >= 0) && (N <= getNumberOfLines()))
801 list<CommandLine>::iterator it_start, it_end;
802 for (it_start = CommandsList.begin(); it_start != CommandsList.end(); ++it_start)
808 if (isBeginningSessionLine(*it_start))
810 for (; it_end != CommandsList.end(); ++it_end)
812 if (isBeginningSessionLine(*it_end))
819 CommandsList.erase(it_start, it_end);
821 my_search.setHistory(CommandsList);
822 my_search.setToken(std::string());
824 CommandHistoryDeleteLine(N);
833 /*--------------------------------------------------------------------------*/
834 void HistoryManager::setSaveConsecutiveDuplicateLines(BOOL doit)
836 saveconsecutiveduplicatelines = doit;
839 /*--------------------------------------------------------------------------*/
840 BOOL HistoryManager::getSaveConsecutiveDuplicateLines(void)
842 return saveconsecutiveduplicatelines;
845 /*--------------------------------------------------------------------------*/
846 void HistoryManager::setAfterHowManyLinesHistoryIsSaved(int num)
850 afterhowmanylineshistoryissaved = num;
851 numberoflinesbeforehistoryissaved = 0;
855 /*--------------------------------------------------------------------------*/
856 int HistoryManager::getAfterHowManyLinesHistoryIsSaved(void)
858 return afterhowmanylineshistoryissaved;
861 /*--------------------------------------------------------------------------*/
862 char *HistoryManager::getPreviousLine(void)
864 char *returnedline = NULL;
866 if (my_search.getSize() > 0)
868 std::string line = my_search.getPreviousLine();
871 returnedline = strdup(line.c_str());
877 /*--------------------------------------------------------------------------*/
878 char *HistoryManager::getNextLine(void)
880 char *returnedline = NULL;
882 if (my_search.getSize() > 0)
884 std::string line = my_search.getNextLine();
885 returnedline = strdup(line.c_str());
890 /*--------------------------------------------------------------------------*/
891 BOOL HistoryManager::setToken(char *token)
898 my_search.setHistory(CommandsList);
899 return my_search.setToken(Token);
902 /*--------------------------------------------------------------------------*/
903 char *HistoryManager::getToken(void)
905 char *returnedtoken = NULL;
907 std::string token = my_search.getToken();
911 returnedtoken = strdup(token.c_str());
913 return returnedtoken;
916 /*--------------------------------------------------------------------------*/
917 BOOL HistoryManager::resetToken(void)
919 return my_search.reset();
922 BOOL HistoryManager::isBeginningSessionLine(CommandLine& line)
924 std::string str = line.get();
925 return isBeginningSessionLine(str.c_str());
928 /*--------------------------------------------------------------------------*/
929 BOOL HistoryManager::isBeginningSessionLine(const char *line)
933 size_t len_begin = strlen(SESSION_PRAGMA_BEGIN);
934 size_t len_end = strlen(SESSION_PRAGMA_END);
935 size_t len_line = strlen(line);
937 if (len_line > len_begin + len_end)
939 if ((strncmp(line, SESSION_PRAGMA_BEGIN, len_begin) == 0) &&
940 (strncmp(line + len_line - len_end, SESSION_PRAGMA_END, len_end) == 0))
951 /*--------------------------------------------------------------------------*/
952 void HistoryManager::fixHistorySession(void)
954 /* add date & time @ begin session */
955 char *commentbeginsession = getCommentDateSession(FALSE);
957 if (commentbeginsession)
959 CommandLine Line(commentbeginsession);
961 CommandsList.push_front(Line);
962 FREE(commentbeginsession);
963 commentbeginsession = NULL;
967 /*--------------------------------------------------------------------------*/
968 BOOL HistoryManager::isTruncated(void)
972 /*--------------------------------------------------------------------------*/
973 BOOL HistoryManager::setNumberOfLinesMax(int nbLinesMax)
975 return my_file.setDefaultMaxNbLines(nbLinesMax);
977 /*--------------------------------------------------------------------------*/
978 int HistoryManager::getNumberOfLinesMax(void)
980 return my_file.getDefaultMaxNbLines();
982 /*--------------------------------------------------------------------------*/