Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 150 - (show annotations) (download)
Sun Mar 16 10:36:07 2008 UTC (16 years ago) by tmurakam
File size: 4750 byte(s)
ver 2.4 tag

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("CSV変換ルールが選択されていません", "エラー");
68 return false;
69 }
70
71 // 銀行IDなどを設定
72 org = rule.Ident;
73 bankId = rule.BankId;
74 branchId = dlg.BranchId;
75 accountId = dlg.AccountId;
76
77 if (rule.FirstLine == null)
78 {
79 // 1行目から再度読み込み直す
80 sr.Close();
81 sr = new StreamReader(path, System.Text.Encoding.Default);
82 }
83
84 // 読み込み準備
85 rule.Reset();
86
87 return true;
88 }
89
90 public void Close()
91 {
92 sr.Close();
93 }
94
95 // CSV 読み込み処理
96 public override List<Transaction> ReadCard()
97 {
98 List<Transaction> transactions = new List<Transaction>();
99 string line;
100
101 while ((line = sr.ReadLine()) != null)
102 {
103 // CSV カラム分割
104 string[] row = SplitCsv(line);
105 if (row.Length <= 1) continue; // ad hoc...
106
107 // パース
108 Transaction t = rule.parse(row);
109 transactions.Add(t);
110 }
111
112 // 順序逆転処理
113 if (!rule.IsAscent)
114 {
115 transactions.Reverse();
116 }
117
118 return transactions;
119 }
120
121 // CSV のフィールド分割
122 private string[] SplitCsv(string line)
123 {
124 ArrayList fields = new ArrayList();
125 Regex regCsv = new System.Text.RegularExpressions.Regex(
126 "\\s*(\"(?:[^\"]|\"\")*\"|[^,]*)\\s*,", RegexOptions.None);
127
128 Match m = regCsv.Match(line + ",");
129 int count = 0;
130 while (m.Success)
131 {
132 string field = m.Groups[1].Value;
133
134 // 前後の空白を削除
135 field = field.Trim();
136
137 // ダブルクォートを抜く
138 if (field.StartsWith("\"") && field.EndsWith("\""))
139 {
140 field = field.Substring(1, field.Length - 2);
141 }
142 // "" を " に変換
143 field = field.Replace("\"\"", "\"");
144
145 // もう一度前後の空白を削除
146 field = field.Trim();
147
148 fields.Add(field);
149 count++;
150 m = m.NextMatch();
151 }
152
153 return fields.ToArray(typeof(string)) as string[];
154 }
155 }
156 }

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