Skip to content

Commit

Permalink
better session handling
Browse files Browse the repository at this point in the history
- destroy after repl
- if rw fails, open ro
  • Loading branch information
christopherlam committed Oct 7, 2023
1 parent c2fee6d commit 4bbc62b
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions gnucash/gnucash-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ may be specified to describe some saved options.\n"
#include "gnc-session.h"
#include "gnc-prefs-utils.h"
#include "libguile.h"
#include <iostream> // std::cout
#include <fstream> // std::ifstream

struct cli_struct
{
Expand Down Expand Up @@ -171,18 +169,39 @@ scm_run_cli (void *data, [[maybe_unused]] int argc, [[maybe_unused]] char **argv
std::cout << "Starting CLI... " << std::endl;
scm_c_eval_string ("(start-repl)");
}
if (gnc_current_session_exist())
{
std::cout << "Destroying session..." << std::endl;
gnc_clear_current_session ();
}
}

static void load_file (std::string m_file_to_load,
bool m_interactive_rw)
{
std::cout << "Loading " << (m_interactive_rw ? "" : "(readonly) ")
<< m_file_to_load << "...";
std::cout << "\n\nLoading " << m_file_to_load << (m_interactive_rw ? " (r/w)" : " (readonly)");
auto session = gnc_get_current_session();
qof_session_begin (session, m_file_to_load.c_str(),
m_interactive_rw ? SESSION_READ_ONLY : SESSION_NORMAL_OPEN);
qof_session_load (session, NULL);
std::cout << "done!" << std::endl;
auto mode = m_interactive_rw ? SESSION_READ_ONLY : SESSION_NORMAL_OPEN;
while (true)
{
qof_session_begin (session, m_file_to_load.c_str(), mode);
auto io_err = qof_session_get_error (session);
switch (io_err)
{
case ERR_BACKEND_NO_ERR:
qof_session_load (session, NULL);
std::cout << "... done!\n\n";
return;
case ERR_BACKEND_LOCKED:
case ERR_BACKEND_READONLY:
std::cout << " (forced readonly)";
mode = SESSION_READ_ONLY;
break;
default:
std::cout << "... unknown error. Abort." << std::endl;
exit (1);
}
}
}

int
Expand Down

0 comments on commit 4bbc62b

Please sign in to comment.