OSDN Git Service

a2a41dd8be0244e2907509b1d0436e5bcd26abb2
[nysol/mining.git] / include / kgCSVout.h
1 /* ////////// LICENSE INFO ////////////////////
2
3  * Copyright (C) 2013 by NYSOL CORPORATION
4  *
5  * Unless you have received this program directly from NYSOL pursuant
6  * to the terms of a commercial license agreement with NYSOL, then
7  * this program is licensed to you under the terms of the GNU Affero General
8  * Public License (AGPL) as published by the Free Software Foundation,
9  * either version 3 of the License, or (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF 
13  * NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
14  *
15  * Please refer to the AGPL (http://www.gnu.org/licenses/agpl-3.0.txt)
16  * for more details.
17
18  ////////// LICENSE INFO ////////////////////*/
19 // =============================================================================
20 // kgCSVout.h KGMODで用いられる入力CSVクラス
21 // =============================================================================
22 #pragma once
23 #include <iostream>
24 #include <kgConfig.h>
25 #include <kgError.h>
26 #include <kgEnv.h>
27 #include <kgCSV.h>
28 #include <kgArgFld.h>
29 #include <kgVal.h>
30 #include <boost/date_time/gregorian/gregorian.hpp>
31 #include <boost/date_time/posix_time/posix_time.hpp>
32 #define LIMIT_OUT 2
33 using namespace std;
34
35 namespace kglib  ////////////////////////////////////////////// start namespace
36 {
37 // 出力ファイルclass
38 // 入力バッファの構造体
39 // CSVの一行がメモリの連続領域にあることを保証したファイルバッファ
40 //
41 // バッファの全体像
42 //   _buf -----+------------+-----+-----+
43 //            0| ioSize_    |     |     |
44 //             +------------+     | _bufSize
45 //            1|            |     |     |
46 //             +------------+ queSize_  |
47 //            :|            |     |     |
48 //            :|            |     |     |
49 //            :|            |     |     |
50 //            :|            |     |     |
51 //             +------------+     |     |
52 //    ioCnt_-1 |            |     |     |
53 //             +------------+-----+     |
54 //             |            |           |
55 //             | _dupSize   |           |
56 //             |            |           |
57 //             +------------+-----+-----+
58 // _bufSize = _dupSize + _queSize
59 // _queSize = ioSize_ * writeCount_
60 // _dupSize :
61 //     書き出し緩衝領域
62 //     ファイルへの実writeは常に_bufからqueSize_バイトだけ出力される。
63 //     一方でkgWrite系関数ではqueSize以上をメモリに書き出す(超えた時にflushされる).
64 //     flush後に超過分を先頭(_buf)にコピーする. 
65 //     現在は_maxRecLenに等しい
66 // _writeSize  : FP.writeにて一回に書き込むサイズ
67 // _writeCnt : 何回writeするか。この回数でキューの大きさが決まる。
68 // _maxRecLen : 最大レコードサイズ : ユーザにより与えられる。
69 //             (kgConfig.h or 環境変数のKG_MaxRecLen)
70 // 制約事項 :
71 //   1) pipeのバッファサイズ = ioSize_ * n (nは1以上の整数)
72 //   2) _maxRecLen = ioSize_ * n (nは1以上の整数)
73 //   3) _dupSize <= _queSize
74 //   4) ioSize_ = readSize_(kgCSV.hより)
75 class kgCSVout 
76 {
77 private:
78         kgEnv*           env_;           // 環境変数クラスへのポインタ
79         int fd_;
80         kgstr_t   fname_; // ファイル名へのポインタ(標準入力はempty)
81         bool      opened_; // オープンしたかどうか
82
83         kgAutoPtr2<char> _buf_ap;    // 出力バッファbufをラッピングするsmart pointer
84         char*            _buf;       // 出力バッファ
85         size_t           _maxRecLen; // レコード最大長(環境変数からセットされる)
86         size_t           queSize_;       // 1つのqueueの大きさ(環境変数より)
87         size_t           ioSize_;      // write関数で一回に書き込むサイズ
88         size_t           ioCnt_;       // その回数
89
90         bool             _noFldName; // 1行目が項目名行でないフラグ
91         char             _dblFmt[256]; // double型出力フォーマット
92
93         char*            _curPnt;    // 現在の書き込み位置
94         char*            _border;    // buf配列の境界アドレス(ここを越えるとflash)
95         char*            _end;       // buf配列の最終アドレス(ここを越えるとerror)
96         size_t           _recNo;     // 現在書き込み中の行番号
97
98         char*            dupTop_;    // スラック領域へコピーする先頭アドレス
99         size_t           dupSize_;   // スラック領域へコピーするサイズ
100         size_t           bufSize_;   // スラック領域へコピーするサイズ
101
102 public:
103         // コンストラクタ
104         kgCSVout() : opened_(false),_recNo(0){}
105         kgCSVout(kgstr_t fileName, kgEnv *env=NULL, bool noFldName=false);
106
107         // デストラクタ 
108         ~kgCSVout(){ close();}
109         
110         // ファイルのオープンとクローズ
111         void open(kgstr_t fileName, kgEnv *env=NULL, bool noFldName=false, size_t cnt=4) throw(kgError);
112         void close(void) throw(kgError);
113
114         // 有効桁数セット
115         void    setPrecision(int precision);
116
117         // accessor
118         bool    noFldName(void) const {return _noFldName;}
119         bool    isOpend(void)           const {return opened_;}
120         size_t  recNo(void)                     const {return _recNo;}
121
122         // bufferの強制出力
123         void flush();
124
125         // --------------------
126         // データ型別の出力関数群
127         // (第2引数が有るのは区切り文字出力あり true:改行、false:コンマ)
128         // --------------------
129         void writeStrNdq(const char *str, bool eol);
130         void writeStrNdq(const char *str);
131         void writeStr(const char *str, bool eol);
132         void writeStr(const char *str);
133         void writeDbl(double val, bool eol);
134         void writeDbl(double val);
135         void writeInt(int val, bool eol);
136         void writeInt(int val);
137         void writeLong(long val, bool eol);
138         void writeLong(long val);
139         void writeSizeT(size_t val, bool eol);
140         void writeSizeT(size_t val);
141         void writeBool(bool val, const bool eol);
142         void writeBool(bool val);
143         void writeDate(const boost::gregorian::date& val, const bool eol);
144         void writeDate(const boost::gregorian::date& val);
145         void writeTime(const boost::posix_time::ptime& val, const bool eol);
146         void writeTime(const boost::posix_time::ptime& val);
147         void writeVal(const kgVal& val, bool eol);
148         void writeVal(const kgVal& val);
149         
150         // --------------------
151         // 一行出力
152         // --------------------
153         // csv一行を出力する(項目の解釈(DQなど)なし).
154         void writeRec(char* rec);
155         // csv一行+数値(mrandで使用)
156         void writeRec(char* rec,int val);
157
158         // --------------------
159         //  項目値の出力関数群
160         // --------------------
161         // csv項目を出力する.
162         // 使用= 汎用
163         void writeFld(size_t size, char** flds, bool eol=true);
164
165         // flgが-1の項目はfldの文字列を出力し,それ以外は対応するvalを出力
166         // 使用= sum, argv
167         void writeFld(char** fld, const vector<int>* flg, vector<kgVal>& val);
168
169         // 使用= chgstr, chgnum
170         void writeFld(char** fld, const vector<int>* flg, char** val);
171
172         //joinでアウタージョイン(-Nの場合に使用)マスタにあってトラにない場合
173         // flgが-1のものはNULL出力し、それ以外はvalから項目番号割り出しを出力
174         // 使用= join, njoin, nrjoin, rjoin
175         void writeFld(const vector<int>* flg,const vector<int>* num,char** fld , const vector<int   >* addcnt);
176
177         // 使用= number, newnumber, rand
178         void writeFld(char** fld ,size_t size ,int val,bool alpha_flg);
179
180         // 使用= count,
181         void writeFld(char** fld ,size_t size ,size_t val);
182
183         // 使用= cat, cut
184         void writeFld(char** fld ,const vector<int>* cnt,bool eol=true);
185
186         // 使用= join, nrjoin, rjoin
187         void writeFld(char** fld,size_t size, char** addfld , const vector<int   >* addcnt);
188
189         // 使用= chgstr, chgnum, nrjoin
190         void writeFld(char** fld,size_t size, char** addfld ,int addsize);
191
192         // 使用= setstr, sed
193         void writeFld(char** fld,size_t size, vector<string> *newFld);
194
195         // int -> アルファベット出力
196         // 使用 = kgcm
197         void writeInt2alpha(int val);
198
199         // --------------------
200         //  項目名の出力関数群
201         // --------------------
202         //同じものがないことを確認してから出力する
203         void writeFldNameCHK(vector<kgstr_t>& outfld ) throw(kgError);
204
205         // csv項目名を出力する.
206         // 使用= 汎用
207         void writeFldName(kgCSV& csv) throw(kgError);
208
209         // csv項目名を出力する. (利用mod: kgsum)
210         //   ex1. CSV a,b,c : f=a:A,c : a,b,c
211         // ただし,second=tureの場合,fldに指定された項目のみattr名に変えて出力
212         //   ex1. CSV a,b,c : f=a:A,c : A,b,c
213         // 使用= 汎用
214         void writeFldName(kgArgFld& fld, bool second) throw(kgError);
215
216         // csv項目+fld項目を出力.or fld項目+csv項目 (利用mod: kgjoin or mwindow)
217         //   ex. CSV a,b,c  fld x,y : a,b,c,x,y
218         // ただし,second=tureの場合,fldに指定された項目はattr名に変えて出力
219         //   ex. CSV a,b,c  fld x:X,y:Y : a,b,c,X,Y
220         // 使用= 汎用
221         void writeFldName(kgCSV& csv,kgArgFld& fld,bool second,bool back=true) throw(kgError);
222
223         // csv項目+newFldを出力
224         // 使用= rand, cal, count, number
225         void writeFldName(kgCSV& csv, kgstr_t newFld) throw(kgError);
226
227         // csv項目+newFld(vector)を出力
228         // 使用= combi, setstr
229         void writeFldName(kgCSV& csv, vector<kgstr_t> newFld) throw(kgError);
230
231         // 新規項目の項目名を出力
232         // 使用= newrand, newstr, newnumber
233         void writeFldName(kgstr_t newFld) throw(kgError);
234
235         // 新規項目の項目名(複数)を出力
236         // 使用= cat、chkcsv、fldname, sep2, cm
237         void writeFldName(vector<kgstr_t> newFld) throw(kgError);
238
239         //区切り文字出力(コンマ、改行、改行[出力行数カウント有り])
240         void writeDlm(void){ *_curPnt++ = ','; }
241         void writeEolNC(void){ *_curPnt++ = '\n'; }
242         void writeEol(void){
243                 _recNo++;
244                 //if(_recNo>LIMIT_OUT){
245                 //      throw kgError("limit line over");
246                 //}
247                 *_curPnt++ = '\n';
248         }
249
250 };
251
252
253 } //////////////////////////////////////////////////////////////// end namespace
254