1 | <?php |
---|
2 | /** |
---|
3 | * PAYGENT B2B MODULE |
---|
4 | * CSVWriter.php |
---|
5 | * |
---|
6 | * Copyright (C) 2007 by PAYGENT Co., Ltd. |
---|
7 | * All rights reserved. |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * CSVWriter CSV`®Åt@CðoÍ·éB |
---|
12 | * gpû@F<br /> |
---|
13 | * <pre><code> |
---|
14 | * // writerIuWFNgðìéBftHgÍShift_JISGR[h |
---|
15 | * CSVWriter writer = null; |
---|
16 | * try { |
---|
17 | * writer = new CSVWriter( |
---|
18 | * "c:\\temp\\test.txt", CSVWriter.ENCODING_SJIS); |
---|
19 | * writer.open(); |
---|
20 | * List list = new ArrayList(); |
---|
21 | * list.add("1"); |
---|
22 | * list.add("abc"); |
---|
23 | * list.add(""); |
---|
24 | * list.add(","); |
---|
25 | * list.add("ÉÙñ²"); |
---|
26 | * writer.writeOneLine(list); |
---|
27 | * list.remove(0); |
---|
28 | * list.add(0, "2"); |
---|
29 | * writer.writeOneLine(list); |
---|
30 | * } finally { |
---|
31 | * writer.close(); |
---|
32 | * } |
---|
33 | * </code></pre> |
---|
34 | * @version $Revision: 1.4 $ |
---|
35 | * @author $Author: t-mori $ |
---|
36 | */ |
---|
37 | |
---|
38 | /** t@CoÍpEncoding Shift_JIS */ |
---|
39 | define("CSVWriter__ENCODING_SJIS", "Shift_JIS"); |
---|
40 | /** t@CoÍpEncoding EUC-JP */ |
---|
41 | define("CSVWriter__ENCODING_EUC", "EUC_JP"); |
---|
42 | /** t@CoÍpEncoding MS932 */ |
---|
43 | define("CSVWriter__ENCODING_MS932", "SJIS-win"); //"Windows-31J"; |
---|
44 | /** t@CoÍÌüsR[h \r\n */ |
---|
45 | define("CSVWriter__WINDOWS_NEWLINE", "\r\n"); |
---|
46 | /** t@CoÍÌüsR[h \n */ |
---|
47 | define("CSVWriter__UNIX_NEWLINE", "\n"); |
---|
48 | /** t@CoÍÌüsR[h \r */ |
---|
49 | define("CSVWriter__MAC_NEWLINE", "\r"); |
---|
50 | |
---|
51 | class CSVWriter { |
---|
52 | |
---|
53 | var $csvFile; |
---|
54 | var $filePath; |
---|
55 | var $encoding; |
---|
56 | var $envelop; |
---|
57 | var $newLine = CSVWriter__WINDOWS_NEWLINE; |
---|
58 | |
---|
59 | /** |
---|
60 | * RXgN^BGR[hyÑÚf[^ÍݶÌwèðs¢Writerð쬷éB |
---|
61 | * @param filePath t@CpX |
---|
62 | * @param encoding t@CÌGR[h |
---|
63 | * @param envelop Úf[^Íݶ |
---|
64 | */ |
---|
65 | function CSVWriter($filePath, $encoding = CSVWriter__ENCODING_MS932, $envelop = CSVTokenizer__DEF_ITEM_ENVELOPE) { |
---|
66 | $this->filePath = $filePath; |
---|
67 | $this->encoding = $encoding; |
---|
68 | $this->envelop = $envelop; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * oÍt@CðJB |
---|
73 | * t@CoͪÂ\ÈóÔÉ·éB |
---|
74 | * @return boolean TRUE:¬÷AFALSEF¸s |
---|
75 | */ |
---|
76 | function open() { |
---|
77 | |
---|
78 | $this->csvFile = fopen($this->filePath, "w"); |
---|
79 | if ($this->csvFile == false) { |
---|
80 | $this->csvFile = null; |
---|
81 | trigger_error("cannot open file " . $this->filePath . " to write", E_USER_NOTICE); |
---|
82 | return false; |
---|
83 | } |
---|
84 | |
---|
85 | // `FbNGR[fBO |
---|
86 | if (mb_convert_encoding("GR[h", $this->encoding) === false){ |
---|
87 | trigger_error("Unsupported Encoding " . $this->encoding . ".", E_USER_NOTICE); |
---|
88 | return false; |
---|
89 | } |
---|
90 | return true; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * oÍt@Cð¶éB |
---|
95 | * Äxt@Cð쬷éêÍOpen©çs¤±ÆB |
---|
96 | */ |
---|
97 | function close() { |
---|
98 | if ($this->csvFile != null) { |
---|
99 | fclose($this->csvFile); |
---|
100 | $this->csvFile = null; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * üsR[hðÝè·éB¢ÝèÌêA\nÅoÍ·éB |
---|
106 | * @param newLine üsR[h̶ñ |
---|
107 | */ |
---|
108 | function setNewLine($newLine) { |
---|
109 | $this->newLine = $newLine; |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * t@Cðêsª«ÞBöÉüsR[hðÇÁ·éB |
---|
114 | * ListÌêAListÌgðCSV`®ÌêsÉÏ·µAoÍðs¤B |
---|
115 | * @param line êsªÌ¶ñ(String)½¢Ízñ(array) |
---|
116 | * @return «ß½çtrueB |
---|
117 | */ |
---|
118 | function writeOneLine($line) { |
---|
119 | |
---|
120 | if (is_string($line)) { |
---|
121 | if ($this->csvFile == null) { |
---|
122 | trigger_error("File not open.", E_USER_NOTICE); |
---|
123 | return false; |
---|
124 | } |
---|
125 | $encLine = $line; |
---|
126 | |
---|
127 | if (fwrite($this->csvFile, $line) === false) { |
---|
128 | trigger_error("File can not write.", E_USER_NOTICE); |
---|
129 | return false; |
---|
130 | } |
---|
131 | fwrite($this->csvFile, $this->newLine); |
---|
132 | flush($this->csvFile); |
---|
133 | return true; |
---|
134 | } |
---|
135 | else if (is_array($line)) { |
---|
136 | $strLine = ""; |
---|
137 | |
---|
138 | // List to CSVString |
---|
139 | $bFirstLine = true; |
---|
140 | foreach($line as $i => $data) { |
---|
141 | if ($bFirstLine) { |
---|
142 | $bFirstLine = false; |
---|
143 | } else { |
---|
144 | $strLine .= ","; |
---|
145 | } |
---|
146 | |
---|
147 | if ($this->envelop != CSVTokenizer__NO_ITEM_ENVELOPE) { |
---|
148 | $strLine .= $this->envelop; |
---|
149 | } |
---|
150 | $strLine .= $this->cnvKnmString($data); |
---|
151 | if ($this->envelop != CSVTokenizer__NO_ITEM_ENVELOPE) { |
---|
152 | $strLine .= $this->envelop; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | return $this->writeOneLine($strLine); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * ¶ñÉJ}ª¶Ý·éêÍ""ÅÍÞB |
---|
162 | * ¶ñÉ_uNH[e[Vª¶Ý·éêÍ_uNH[e[VÅGXP[vµA |
---|
163 | * _uNH[e[VÅÍÞB |
---|
164 | * @param str Ï·ÎÛ¶ñ |
---|
165 | * @return Ϸʶñ |
---|
166 | */ |
---|
167 | function cnvKnmString($str) { |
---|
168 | if ($str == null) { |
---|
169 | return null; |
---|
170 | } |
---|
171 | $flg = false; |
---|
172 | $buf = ""; |
---|
173 | for ($i = 0; $i < strlen($str); $i++) { |
---|
174 | if ($str{$i} == $this->envelop) { |
---|
175 | $buf .= $this->envelop; |
---|
176 | $flg = true; |
---|
177 | } |
---|
178 | if ($str{$i} == CSVTokenizer__DEF_SEPARATOR) { |
---|
179 | $flg = true; |
---|
180 | } |
---|
181 | $buf .= $str{$i}; |
---|
182 | } |
---|
183 | return $buf; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | ?> |
---|