summaryrefslogtreecommitdiff
path: root/csv.cc
blob: e61e04a61e99a9db2e66a4c0904e3d2b8ffe9447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "csv.h"

namespace ledger {

namespace {
  inline void write_escaped_string(std::ostream& out, const string& xact)
  {
    out << "\"";
    foreach (char ch, xact)
      if (ch == '"') {
	out << "\\";
	out << "\"";
      } else {
	out << ch;
      }
    out << "\"";
  }
}

void format_csv_xacts::operator()(xact_t& xact)
{
  if (! xact.has_xdata() ||
      ! xact.xdata().has_flags(XACT_EXT_DISPLAYED)) {
    {
      format_t fmt("%D");
      std::ostringstream str;
#if 0
      fmt.format(str, details_t(xact));
#endif
      write_escaped_string(out, str.str());
    }
    out << ',';

    {
      format_t fmt("%P");
      std::ostringstream str;
#if 0
      fmt.format(str, details_t(xact));
#endif
      write_escaped_string(out, str.str());
    }
    out << ',';

    {
      format_t fmt("%A");
      std::ostringstream str;
#if 0
      fmt.format(str, details_t(xact));
#endif
      write_escaped_string(out, str.str());
    }
    out << ',';

    {
      format_t fmt("%t");
      std::ostringstream str;
#if 0
      fmt.format(str, details_t(xact));
#endif
      write_escaped_string(out, str.str());
    }
    out << ',';

    {
      format_t fmt("%T");
      std::ostringstream str;
#if 0
      fmt.format(str, details_t(xact));
#endif
      write_escaped_string(out, str.str());
    }
    out << ',';

    switch (xact.state) {
    case xact_t::CLEARED:
      write_escaped_string(out, "*");
      break;
    case xact_t::PENDING:
      write_escaped_string(out, "!");
      break;
    default: {
      xact_t::state_t state;
      if (xact.entry->get_state(&state))
	switch (state) {
	case xact_t::CLEARED:
	  write_escaped_string(out, "*");
	  break;
	case xact_t::PENDING:
	  write_escaped_string(out, "!");
	  break;
	default:
	  write_escaped_string(out, "");
	  break;
	}
    }
    }
    out << ',';

    if (xact.entry->code)
      write_escaped_string(out, *xact.entry->code);
    out << ',';

    {
      format_t fmt("%N");
      std::ostringstream str;
#if 0
      fmt.format(str, details_t(xact));
#endif
      write_escaped_string(out, str.str());
    }
    out << '\n';

    xact.xdata().add_flags(XACT_EXT_DISPLAYED);
  }
}

} // namespace ledger