Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/FeliCa2Money.net/CsvCard.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 229 - (show annotations) (download)
Mon Oct 19 16:04:50 2009 UTC (14 years, 7 months ago) by tmurakam
File size: 6159 byte(s)
取引ID付与手順を変更
CVS の Order に "Sort" を追加
1 /*
2 * FeliCa2Money
3 *
4 * Copyright (C) 2001-2008 Takuya Murakami
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 // CSV 取り込み処理
22
23 using System;
24 using System.IO;
25 using System.Collections;
26 using System.Collections.Generic;
27 using System.Text;
28 using System.Text.RegularExpressions;
29 using System.Windows.Forms;
30
31 namespace FeliCa2Money
32 {
33 class CsvCard : Card
34 {
35 private CsvRules rules = new CsvRules();
36 private StreamReader sr;
37 private CsvRule rule;
38
39 public bool LoadAllRules()
40 {
41 return rules.LoadAllRules();
42 }
43
44 public bool OpenFile(string path)
45 {
46 // TODO: とりあえず SJIS で開く (UTF-8 とかあるかも?)
47 sr = new StreamReader(path, System.Text.Encoding.Default);
48
49 string firstLine = sr.ReadLine();
50
51 // 合致するルールを探す
52 rule = rules.FindRule(firstLine);
53
54 CsvDialog dlg = new CsvDialog(rules);
55
56 // ルール/口座番号などを選択
57 dlg.SelectRule(rule);
58 if (dlg.ShowDialog() == DialogResult.Cancel)
59 {
60 return false;
61 }
62
63 // 選択されたルールを取り出す
64 rule = dlg.SelectedRule();
65 if (rule == null)
66 {
67 MessageBox.Show(Properties.Resources.NoCsvRuleSelected, Properties.Resources.Error);
68 return false;
69 }
70
71 // 銀行IDなどを設定
72 ident = rule.Ident;
73 bankId = rule.BankId;
74 branchId = dlg.BranchId;
75 accountId = dlg.AccountId;
76
77 // 1行目から再度読み込み直す
78 sr.Close();
79 sr = new StreamReader(path, System.Text.Encoding.Default);
80
81 // firstLine まで読み飛ばす
82 if (rule.FirstLine != null)
83 {
84 while ((firstLine = sr.ReadLine()) != null)
85 {
86 if (firstLine == rule.FirstLine) break;
87 }
88 }
89
90 return true;
91 }
92
93 public void Close()
94 {
95 sr.Close();
96 }
97
98 private static int compareByDate(Transaction x, Transaction y)
99 {
100 return x.date.CompareTo(y.date);
101 }
102
103 // CSV 読み込み処理
104 public override List<Transaction> ReadCard()
105 {
106 List<Transaction> transactions = new List<Transaction>();
107 string line;
108
109 while ((line = sr.ReadLine()) != null)
110 {
111 // CSV カラム分割
112 string[] row = SplitCsv(line);
113 if (row.Length <= 1) continue; // ad hoc...
114
115 // パース
116 Transaction t = rule.parse(row);
117 transactions.Add(t);
118 }
119
120 // ソート処理
121 switch (rule.SortOrder)
122 {
123 default:
124 case CsvRule.SortAscent:
125 break;
126
127 case CsvRule.SortDescent:
128 transactions.Reverse();
129 break;
130
131 case CsvRule.SortAuto:
132 transactions.Sort(compareByDate);
133 break;
134 }
135
136 // ID採番
137 int idSerial = 0;
138 DateTime prevDate = new DateTime(1900, 1, 1, 0, 0, 0);
139
140 foreach (Transaction t in transactions)
141 {
142 if (t.id == Transaction.UnassignedId)
143 {
144 if (t.date == prevDate)
145 {
146 idSerial++;
147 }
148 else
149 {
150 idSerial = 0;
151 prevDate = t.date;
152 }
153 t.id = idSerial;
154 }
155 }
156
157 return transactions;
158 }
159
160 // CSV のフィールド分割
161 private string[] SplitCsv(string line)
162 {
163 ArrayList fields = new ArrayList();
164 Regex regCsv;
165
166 if (rule.IsTSV)
167 {
168 regCsv = new Regex("([^\\t]*)\\t");
169 line = line + "\t";
170 }
171 else
172 {
173 regCsv = new Regex("\\s*(\"(?:[^\"]|\"\")*\"|[^,]*)\\s*,", RegexOptions.None);
174 line = line + ",";
175 }
176
177 Match m = regCsv.Match(line);
178 int count = 0;
179 while (m.Success)
180 {
181 string field = m.Groups[1].Value;
182
183 // 前後の空白を削除
184 field = field.Trim();
185
186 // ダブルクォートを抜く
187 if (field.StartsWith("\"") && field.EndsWith("\""))
188 {
189 field = field.Substring(1, field.Length - 2);
190 }
191 // "" を " に変換
192 field = field.Replace("\"\"", "\"");
193
194 // もう一度前後の空白を削除
195 field = field.Trim();
196
197 fields.Add(field);
198 count++;
199 m = m.NextMatch();
200 }
201
202 return fields.ToArray(typeof(string)) as string[];
203 }
204 }
205 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26