-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh2o.cpp
210 lines (198 loc) · 5.53 KB
/
h2o.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
H2O - Convert Harvest csv files into Odoo format
*/
/*
* history
* 22.10.2015 - properly replace decimal point to accept >=10 hours
* 27.10.2016 - add notes to task description
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
#define SIZE 1024
#define PCOUNT 19
void error_exit (string msg);
void error_exit (fstream& f, string msg);
void error_exit (fstream& f, fstream& g, string msg, int line);
const char* pmap[2*PCOUNT] = {
"__export__.account_analytic_account_29","Tic",
"__export__.account_analytic_account_30","Ayt",
"__export__.account_analytic_account_31","Cer",
"__export__.account_analytic_account_32","Sim",
"__export__.account_analytic_account_33","UvA",
"__export__.account_analytic_account_34","Nor",
"__export__.account_analytic_account_40","Par",
"__export__.account_analytic_account_44","PwC",
"__export__.account_analytic_account_55","Bar",
"__export__.account_analytic_account_56","XPO",
"__export__.account_analytic_account_76","ZES",
"__export__.account_analytic_account_73","Int",
"__export__.account_analytic_account_72","Cir",
"__export__.account_analytic_account_83","Gai",
"__export__.account_analytic_account_87","Web",
"__export__.account_analytic_account_88","Com",
"__export__.account_analytic_account_113","Qua",
"__export__.account_analytic_account_120","Sta",
"__export__.account_analytic_account_153","Fea"
};
int lookup_project (int code)
{
int i;
for (i = 0; i < PCOUNT; i++)
if (code == tolower(pmap[2*i+1][0])+256*tolower(pmap[2*i+1][1])
+256*256*tolower(pmap[2*i+1][2]))
return 2*i;
return -1;
}
int main (int argc, char **argv)
{
fstream f, g;
char buf[SIZE], outfname[SIZE];
int i = 0;
// check args
if (argc != 2)
error_exit ("Need input file");
// open input file
f.open (argv[1], fstream::in);
if (f.fail ())
error_exit ("Error opening input file");
// skip column header
f.getline (buf, SIZE);
if (f.fail ())
error_exit (f, "Read fail at column header");
// open output file
snprintf (outfname, sizeof (outfname), "%s%s", "out-", argv[1]);
g.open (outfname, fstream::out);
if (g.fail ())
error_exit (f, "Error opening output file");
// write output column header
g << "date,account_id/id,journal_id/id,name,unit_amount" << endl;
// parse lines
while (++i && f.getline (buf, SIZE) && !f.eof ())
{
istringstream linein (buf);
ostringstream lineout;
if (f.fail () || linein.fail ())
error_exit (f, g, "Read fail at line ", i);
// ignore leading whitespaces, skip empty lines
if ((linein >> ws).peek () == EOF)
continue;
// read date in yyyy/mm/dd format
{
int dd, mm, yyyy;
char c, date[11];
linein >> yyyy >> c >> mm >> c >> dd >> c;
if (linein.fail ())
error_exit (f, g, "Fail to read date at line ", i);
snprintf (date, sizeof (date), "%4d-%02d-%02d", yyyy, mm, dd);
lineout << date << ",";
if (lineout.fail ())
error_exit (f, g, "Fail to write date at line ", i);
}
// read client
{
char cl[SIZE];
linein.getline (cl, SIZE, ',');
if (linein.fail ())
error_exit (f, g, "Fail to read project name at line ", i);
int pid =
lookup_project (tolower(cl[0])+256*tolower(cl[1])+256*256*tolower(cl[2]));
if (pid == -1)
error_exit (f, g, "Project not fount at line ", i);
lineout << pmap[pid] << ",hr_timesheet.analytic_journal,";
if (lineout.fail ())
error_exit (f, g, "Fail to write internal project name at line ", i);
}
// skip project and project code
linein.ignore (SIZE, ',');
linein.ignore (SIZE, ',');
if (linein.fail ())
error_exit (f, g, "Fail to skip project or project code at line ", i);
// read task
{
char task[SIZE];
linein.getline (task, SIZE, ',');
if (linein.fail ())
error_exit (f, g, "Fail to read task at line ", i);
lineout << task;
if (lineout.fail ())
error_exit (f, g, "Fail to write task at line ", i);
}
// read notes
{
char c = 0, notes[SIZE] = {0};
int j = 0;
if (linein.peek () == 34)
{
linein.get ();
while ((c = linein.get ()) != 34)
{
if (j == SIZE)
error_exit (f, g, "Buffer size exceeded reading notes at line ", i);
if (c == EOF)
{
f.getline (buf, SIZE);
if (f.eof ()) {
error_exit (f, g, "Unexpected end of file reading notes at line ", i);
}
linein.clear ();
linein.str (buf);
notes[j++] = ';';
}
else if (c != ',')
notes[j++] = c;
}
}
linein.ignore (SIZE, ',');
if (linein.fail ())
error_exit (f, g, "Fail to read notes at line ", i);
if (notes[0])
lineout << ": " << notes;
lineout << ",";
}
// read hours as a decimal string
{
char hrs[SIZE];
linein.ignore (1);
linein.getline (hrs, SIZE, '\"');
if (linein.fail ())
error_exit (f, g, "Fail to read hours at line ", i);
// replace , with .
for (char *p = hrs;*p;p++)
if (*p == ',') *p = '.';
lineout << hrs << endl;
if (lineout.fail ())
error_exit (f, g, "Fail to write hours at line ", i);
}
// skip everything else
g << lineout.str();
if (g.fail ())
error_exit (f, g, "Fail to write output at line ", i);
}
f.close ();
g.close ();
cout << "Output written in " << outfname << endl;
return 0;
}
void error_exit (string msg)
{
cerr << msg.c_str() << endl;
exit (0);
}
void error_exit (fstream& f, string msg)
{
f.close ();
cerr << msg.c_str() << endl;
exit (0);
}
void error_exit (fstream& f, fstream& g, string msg, int line)
{
char buf[SIZE];
snprintf (buf, sizeof (buf), "%s%d", msg.c_str(), line);
f.close();
g.close();
cerr << buf << endl;
exit (0);
}