| 1 |
/* |
| 2 |
* MoneyImport : Convert Japan Net Bank csv file to MS Money OFX file. |
| 3 |
* |
| 4 |
* Copyright (c) 2001-2003 Takuya Murakami. All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
| 21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
* |
| 29 |
* $Id$ |
| 30 |
*/ |
| 31 |
//--------------------------------------------------------------------------- |
| 32 |
|
| 33 |
#include <vcl.h> |
| 34 |
#pragma hdrstop |
| 35 |
#include <stdio.h> |
| 36 |
|
| 37 |
#define DEFINE_TRNNAME |
| 38 |
#include "Transaction.h" |
| 39 |
|
| 40 |
struct trntable trntable_income[] = { |
| 41 |
{"����", T_INT}, |
| 42 |
{"�U�� ", T_DIRECTDEP}, |
| 43 |
{NULL, T_DEP} |
| 44 |
}; |
| 45 |
|
| 46 |
struct trntable trntable_outgo[] = { |
| 47 |
{"�`�s�l", T_ATM}, |
| 48 |
{NULL, T_DEBIT} |
| 49 |
}; |
| 50 |
|
| 51 |
void Transaction::SetTransactionType(const char *desc, int type) |
| 52 |
{ |
| 53 |
struct trntable *tab; |
| 54 |
|
| 55 |
switch (type) { |
| 56 |
case T_INCOME: |
| 57 |
tab = trntable_income; |
| 58 |
break; |
| 59 |
|
| 60 |
case T_OUTGO: |
| 61 |
tab = trntable_outgo; |
| 62 |
break; |
| 63 |
|
| 64 |
default: |
| 65 |
/* ### */ |
| 66 |
break; |
| 67 |
} |
| 68 |
|
| 69 |
while (tab->key) { |
| 70 |
if (strstr(desc, tab->key) != 0) { |
| 71 |
this->type = tab->type; |
| 72 |
return; |
| 73 |
} |
| 74 |
tab++; |
| 75 |
} |
| 76 |
this->type = tab->type; |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
const char *Transaction::GetTrnTypeStr(void) |
| 81 |
{ |
| 82 |
return trnname[type]; |
| 83 |
} |
| 84 |
|
| 85 |
// CSV ���J���}���������������� |
| 86 |
static int SplitLine(char *line, char **rows) |
| 87 |
{ |
| 88 |
int quoted = 0; |
| 89 |
int n = 0; |
| 90 |
char *p; |
| 91 |
|
| 92 |
rows[0] = line; |
| 93 |
for (p = line; *p; p++) { |
| 94 |
if (*p == '"') { |
| 95 |
quoted = !quoted; |
| 96 |
*p = '\0'; |
| 97 |
|
| 98 |
if (quoted) { |
| 99 |
rows[n] = p + 1; |
| 100 |
} |
| 101 |
} |
| 102 |
else if (*p == ',' && !quoted) { |
| 103 |
*p = '\0'; |
| 104 |
n++; |
| 105 |
rows[n] = p + 1; |
| 106 |
} |
| 107 |
} |
| 108 |
return n+1; |
| 109 |
} |
| 110 |
|
| 111 |
TransactionList::~TransactionList() |
| 112 |
{ |
| 113 |
Transaction *next; |
| 114 |
|
| 115 |
while (head) { |
| 116 |
next = head->next; |
| 117 |
delete head; |
| 118 |
|
| 119 |
head = next; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// |
| 124 |
// CSV �t�@�C������������ |
| 125 |
// |
| 126 |
int TransactionList::ReadCsv(FILE *fp) |
| 127 |
{ |
| 128 |
char buf[300]; |
| 129 |
char *rows[30]; |
| 130 |
|
| 131 |
Transaction *t; |
| 132 |
int err; |
| 133 |
|
| 134 |
// IDENT �������� |
| 135 |
if (fgets(buf, sizeof(buf), fp) == NULL) { |
| 136 |
return -1; // fatal error; |
| 137 |
} |
| 138 |
if (strncmp(buf, Ident(), strlen(Ident())) != 0) { |
| 139 |
return -1; // IDENT �s���v |
| 140 |
} |
| 141 |
|
| 142 |
while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 143 |
|
| 144 |
// ���s���������� |
| 145 |
buf[ strlen(buf) - 1] = '\0'; |
| 146 |
|
| 147 |
int n = SplitLine(buf, rows); |
| 148 |
|
| 149 |
t = GenerateTransaction(n, rows, &err); |
| 150 |
if (!t) { |
| 151 |
if (err) return -1; // fatal error |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
if (!tail) { |
| 156 |
head = tail = t; |
| 157 |
} else { |
| 158 |
tail->next = t; |
| 159 |
tail = t; |
| 160 |
} |
| 161 |
t->next = NULL; |
| 162 |
} |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
// |
| 167 |
// �g�����U�N�V���� ID ���� |
| 168 |
// |
| 169 |
int TransactionList::GenerateTransactionId(int key) |
| 170 |
{ |
| 171 |
if (key != prev_key) { |
| 172 |
serial = 0; |
| 173 |
prev_key = key; |
| 174 |
} else { |
| 175 |
serial++; |
| 176 |
} |
| 177 |
return serial; |
| 178 |
} |
| 179 |
|
| 180 |
// |
| 181 |
// ���[�e�B���e�B���� |
| 182 |
// |
| 183 |
|
| 184 |
// SJIS->UTF8 |
| 185 |
AnsiString utf8(char *sjis) |
| 186 |
{ |
| 187 |
wchar_t wbuf[150]; |
| 188 |
char buf[300]; |
| 189 |
AnsiString utf8; |
| 190 |
|
| 191 |
MultiByteToWideChar(CP_OEMCP, 0, sjis, -1, |
| 192 |
wbuf, sizeof(buf) / 2); |
| 193 |
WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, |
| 194 |
buf, sizeof(buf), NULL, NULL); |
| 195 |
|
| 196 |
utf8 = buf; |
| 197 |
return utf8; |
| 198 |
} |
| 199 |
|
| 200 |
|