Develop and Download Open Source Software

Browse Subversion Repository

Contents of /tags/FeliCa2Money-2.7/CsvCard.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 194 - (show annotations) (download)
Sat Apr 19 11:18:51 2008 UTC (15 years, 11 months ago) by tmurakam
File size: 5158 byte(s)
ver 2.7

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 // 読み込み準備
91 rule.Reset();
92
93 return true;
94 }
95
96 public void Close()
97 {
98 sr.Close();
99 }
100
101 // CSV 読み込み処理
102 public override List<Transaction> ReadCard()
103 {
104 List<Transaction> transactions = new List<Transaction>();
105 string line;
106
107 while ((line = sr.ReadLine()) != null)
108 {
109 // CSV カラム分割
110 string[] row = SplitCsv(line);
111 if (row.Length <= 1) continue; // ad hoc...
112
113 // パース
114 Transaction t = rule.parse(row);
115 transactions.Add(t);
116 }
117
118 // 順序逆転処理
119 if (!rule.IsAscent)
120 {
121 transactions.Reverse();
122 }
123
124 return transactions;
125 }
126
127 // CSV のフィールド分割
128 private string[] SplitCsv(string line)
129 {
130 ArrayList fields = new ArrayList();
131 Regex regCsv;
132
133 if (rule.IsTSV)
134 {
135 regCsv = new Regex("([^\\t]*)\\t");
136 line = line + "\t";
137 }
138 else
139 {
140 regCsv = new Regex("\\s*(\"(?:[^\"]|\"\")*\"|[^,]*)\\s*,", RegexOptions.None);
141 line = line + ",";
142 }
143
144 Match m = regCsv.Match(line);
145 int count = 0;
146 while (m.Success)
147 {
148 string field = m.Groups[1].Value;
149
150 // 前後の空白を削除
151 field = field.Trim();
152
153 // ダブルクォートを抜く
154 if (field.StartsWith("\"") && field.EndsWith("\""))
155 {
156 field = field.Substring(1, field.Length - 2);
157 }
158 // "" を " に変換
159 field = field.Replace("\"\"", "\"");
160
161 // もう一度前後の空白を削除
162 field = field.Trim();
163
164 fields.Add(field);
165 count++;
166 m = m.NextMatch();
167 }
168
169 return fields.ToArray(typeof(string)) as string[];
170 }
171 }
172 }

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