Load the history from the specified file. If the file does not exist * zero is returned and no operation is performed. * * If the file exists and the operation succeeded 0 is returned, otherwise * on error -1 is returned. */
| 614 | * If the file exists and the operation succeeded 0 is returned, otherwise |
| 615 | * on error -1 is returned. */ |
| 616 | int linenoiseHistoryLoad(const char* filename) { |
| 617 | FILE* fp = fopen(filename, "r"); |
| 618 | char buf[LINENOISE_MAX_LINE + 1]; |
| 619 | buf[LINENOISE_MAX_LINE] = '\0'; |
| 620 | |
| 621 | if (fp == NULL) |
| 622 | return -1; |
| 623 | |
| 624 | std::string result; |
| 625 | while (fgets(buf, LINENOISE_MAX_LINE, fp) != NULL) { |
| 626 | char* p; |
| 627 | |
| 628 | // strip the newline first |
| 629 | p = strchr(buf, '\r'); |
| 630 | if (!p) { |
| 631 | p = strchr(buf, '\n'); |
| 632 | } |
| 633 | if (p) { |
| 634 | *p = '\0'; |
| 635 | } |
| 636 | if (result.empty() && buf[0] == ':') { |
| 637 | // if the first character is a colon this is a colon command |
| 638 | // add the full line to the history |
| 639 | linenoiseHistoryAdd(buf); |
| 640 | continue; |
| 641 | } else if (result.empty() && buf[0] == '\0') { |
| 642 | continue; |
| 643 | } |
| 644 | // else we are parsing a Cypher statement |
| 645 | result += buf; |
| 646 | if (cypherComplete(buf)) { |
| 647 | // this line contains a full Cypher statement - add it to the history |
| 648 | linenoiseHistoryAdd(result.c_str()); |
| 649 | result = std::string(); |
| 650 | continue; |
| 651 | } |
| 652 | // the result does not contain a full Cypher statement - add a newline deliminator and move |
| 653 | // on to the next line |
| 654 | result += "\r\n"; |
| 655 | } |
| 656 | fclose(fp); |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | #ifdef _WIN32 |
| 662 | HANDLE hOut; |
no test coverage detected