<commit_before>#ifndef _JOURNAL_H
#define _JOURNAL_H

#include <map>
#include <list>
#include <string>
#include <ctime>
#include <iostream>

#include "amount.h"
#include "datetime.h"
#include "value.h"
#include "error.h"
#include "debug.h"
#include "util.h"

namespace ledger {

// These flags persist with the object
#define TRANSACTION_NORMAL     0x0000
#define TRANSACTION_VIRTUAL    0x0001
#define TRANSACTION_BALANCE    0x0002
#define TRANSACTION_AUTO       0x0004
#define TRANSACTION_BULK_ALLOC 0x0008

class entry_t;
class account_t;

class transaction_t
{
 public:
  enum state_t { UNCLEARED, CLEARED, PENDING };

  entry_t *	   entry;
  std::time_t	   _date;
  std::time_t	   _date_eff;
  account_t *	   account;
  amount_t	   amount;
  amount_t *	   cost;
  state_t	   state;
  unsigned short   flags;
  std::string	   note;
  istream_pos_type beg_pos;
  unsigned long    beg_line;
  istream_pos_type end_pos;
  unsigned long    end_line;
  mutable void *   data;

  static bool    use_effective_date;

  transaction_t(account_t * _account = NULL)
    : entry(NULL), _date(0), _date_eff(0), account(_account),
      cost(NULL), state(UNCLEARED), flags(TRANSACTION_NORMAL),
      data(NULL), beg_pos(0), beg_line(0), end_pos(0), end_line(0) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_t");
  }

  transaction_t(account_t *	   _account,
		const amount_t&    _amount,
		unsigned int	   _flags = TRANSACTION_NORMAL,
		const std::string& _note  = "")
    : entry(NULL), _date(0), _date_eff(0), account(_account),
      amount(_amount), cost(NULL), state(UNCLEARED), flags(_flags),
      note(_note), data(NULL), beg_pos(0), beg_line(0), end_pos(0),
      end_line(0) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_t");
  }

  transaction_t(const transaction_t& xact)
    : entry(xact.entry), _date(0), _date_eff(0),
      account(xact.account), amount(xact.amount),
      cost(xact.cost ? new amount_t(*xact.cost) : NULL),
      state(xact.state), flags(xact.flags), note(xact.note),
      data(NULL), beg_pos(0), beg_line(0), end_pos(0), end_line(0) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_t");
  }

  ~transaction_t() {
    DEBUG_PRINT("ledger.memory.dtors", "dtor transaction_t");
    if (cost)
      delete cost;
  }

  std::time_t actual_date() const;
  std::time_t effective_date() const;
  std::time_t date() const {
    if (use_effective_date)
      return effective_date();
    else
      return actual_date();
  }

  bool operator==(const transaction_t& xact) {
    return this == &xact;
  }
  bool operator!=(const transaction_t& xact) {
    return ! (*this == xact);
  }

  bool valid() const;
};

class journal_t;

typedef std::list<transaction_t *> transactions_list;

class entry_base_t
{
 public:
  journal_t *       journal;
  unsigned long     src_idx;
  istream_pos_type  beg_pos;
  unsigned long     beg_line;
  istream_pos_type  end_pos;
  unsigned long     end_line;
  transactions_list transactions;

  entry_base_t() : journal(NULL) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor entry_base_t");
  }
  entry_base_t(const entry_base_t& e) : journal(NULL) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor entry_base_t");
    for (transactions_list::const_iterator i = e.transactions.begin();
	 i != e.transactions.end();
	 i++)
      transactions.push_back(new transaction_t(**i));
  }
  virtual ~entry_base_t() {
    DEBUG_PRINT("ledger.memory.dtors", "dtor entry_base_t");
    for (transactions_list::iterator i = transactions.begin();
	 i != transactions.end();
	 i++)
      if (! ((*i)->flags & TRANSACTION_BULK_ALLOC))
	delete *i;
      else
	(*i)->~transaction_t();
  }

  bool operator==(const entry_base_t& entry) {
    return this == &entry;
  }
  bool operator!=(const entry_base_t& entry) {
    return ! (*this == entry);
  }

  virtual void add_transaction(transaction_t * xact);
  virtual bool remove_transaction(transaction_t * xact);

  virtual bool finalize();
  virtual bool valid() const = 0;
};

class entry_t : public entry_base_t
{
 public:
  std::time_t _date;
  std::time_t _date_eff;
  std::string code;
  std::string payee;<commit_msg>Added a global variable `entry_balance' which contains the balance of the last attempted entry finalization.<commit_after>#ifndef _JOURNAL_H
#define _JOURNAL_H

#include <map>
#include <list>
#include <string>
#include <ctime>
#include <iostream>

#include "amount.h"
#include "datetime.h"
#include "value.h"
#include "error.h"
#include "debug.h"
#include "util.h"

namespace ledger {

// These flags persist with the object
#define TRANSACTION_NORMAL     0x0000
#define TRANSACTION_VIRTUAL    0x0001
#define TRANSACTION_BALANCE    0x0002
#define TRANSACTION_AUTO       0x0004
#define TRANSACTION_BULK_ALLOC 0x0008

class entry_t;
class account_t;

class transaction_t
{
 public:
  enum state_t { UNCLEARED, CLEARED, PENDING };

  entry_t *	   entry;
  std::time_t	   _date;
  std::time_t	   _date_eff;
  account_t *	   account;
  amount_t	   amount;
  amount_t *	   cost;
  state_t	   state;
  unsigned short   flags;
  std::string	   note;
  istream_pos_type beg_pos;
  unsigned long    beg_line;
  istream_pos_type end_pos;
  unsigned long    end_line;
  mutable void *   data;

  static bool    use_effective_date;

  transaction_t(account_t * _account = NULL)
    : entry(NULL), _date(0), _date_eff(0), account(_account),
      cost(NULL), state(UNCLEARED), flags(TRANSACTION_NORMAL),
      data(NULL), beg_pos(0), beg_line(0), end_pos(0), end_line(0) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_t");
  }

  transaction_t(account_t *	   _account,
		const amount_t&    _amount,
		unsigned int	   _flags = TRANSACTION_NORMAL,
		const std::string& _note  = "")
    : entry(NULL), _date(0), _date_eff(0), account(_account),
      amount(_amount), cost(NULL), state(UNCLEARED), flags(_flags),
      note(_note), data(NULL), beg_pos(0), beg_line(0), end_pos(0),
      end_line(0) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_t");
  }

  transaction_t(const transaction_t& xact)
    : entry(xact.entry), _date(0), _date_eff(0),
      account(xact.account), amount(xact.amount),
      cost(xact.cost ? new amount_t(*xact.cost) : NULL),
      state(xact.state), flags(xact.flags), note(xact.note),
      data(NULL), beg_pos(0), beg_line(0), end_pos(0), end_line(0) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_t");
  }

  ~transaction_t() {
    DEBUG_PRINT("ledger.memory.dtors", "dtor transaction_t");
    if (cost)
      delete cost;
  }

  std::time_t actual_date() const;
  std::time_t effective_date() const;
  std::time_t date() const {
    if (use_effective_date)
      return effective_date();
    else
      return actual_date();
  }

  bool operator==(const transaction_t& xact) {
    return this == &xact;
  }
  bool operator!=(const transaction_t& xact) {
    return ! (*this == xact);
  }

  bool valid() const;
};

class journal_t;

typedef std::list<transaction_t *> transactions_list;

class entry_base_t
{
 public:
  journal_t *       journal;
  unsigned long     src_idx;
  istream_pos_type  beg_pos;
  unsigned long     beg_line;
  istream_pos_type  end_pos;
  unsigned long     end_line;
  transactions_list transactions;

  entry_base_t() : journal(NULL) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor entry_base_t");
  }
  entry_base_t(const entry_base_t& e) : journal(NULL) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor entry_base_t");
    for (transactions_list::const_iterator i = e.transactions.begin();
	 i != e.transactions.end();
	 i++)
      transactions.push_back(new transaction_t(**i));
  }
  virtual ~entry_base_t() {
    DEBUG_PRINT("ledger.memory.dtors", "dtor entry_base_t");
    for (transactions_list::iterator i = transactions.begin();
	 i != transactions.end();
	 i++)
      if (! ((*i)->flags & TRANSACTION_BULK_ALLOC))
	delete *i;
      else
	(*i)->~transaction_t();
  }

  bool operator==(const entry_base_t& entry) {
    return this == &entry;
  }
  bool operator!=(const entry_base_t& entry) {
    return ! (*this == entry);
  }

  virtual void add_transaction(transaction_t * xact);
  virtual bool remove_transaction(transaction_t * xact);

  virtual bool finalize();
  virtual bool valid() const = 0;
};

extern value_t entry_balance;

class entry_t : public entry_base_t
{
 public:
  std::time_t _date;
  std::time_t _date_eff;
  std::string code;
  std::string payee;
{'fraction_non_alphanumeric': 0.12875536480686695, 'fraction_numerical': 0.010491177873152123, 'mean_word_length': 4.005369928400954, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '848742', 'n_tokens_mistral': 3085, 'n_tokens_neox': 2996, 'n_words': 646}