1 /* ////////// LICENSE INFO ////////////////////
3 * Copyright (C) 2013 by NYSOL CORPORATION
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.
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.
15 * Please refer to the AGPL (http://www.gnu.org/licenses/agpl-3.0.txt)
18 ////////// LICENSE INFO ////////////////////*/
19 // =============================================================================
20 // kgCSVout.h KGMODで用いられる入力CSVクラス
21 // =============================================================================
30 #include <boost/date_time/gregorian/gregorian.hpp>
31 #include <boost/date_time/posix_time/posix_time.hpp>
35 namespace kglib ////////////////////////////////////////////// start namespace
39 // CSVの一行がメモリの連続領域にあることを保証したファイルバッファ
42 // _buf -----+------------+-----+-----+
44 // +------------+ | _bufSize
46 // +------------+ queSize_ |
53 // +------------+-----+ |
57 // +------------+-----+-----+
58 // _bufSize = _dupSize + _queSize
59 // _queSize = ioSize_ * writeCount_
62 // ファイルへの実writeは常に_bufからqueSize_バイトだけ出力される。
63 // 一方でkgWrite系関数ではqueSize以上をメモリに書き出す(超えた時にflushされる).
64 // flush後に超過分を先頭(_buf)にコピーする.
66 // _writeSize : FP.writeにて一回に書き込むサイズ
67 // _writeCnt : 何回writeするか。この回数でキューの大きさが決まる。
68 // _maxRecLen : 最大レコードサイズ : ユーザにより与えられる。
69 // (kgConfig.h or 環境変数のKG_MaxRecLen)
71 // 1) pipeのバッファサイズ = ioSize_ * n (nは1以上の整数)
72 // 2) _maxRecLen = ioSize_ * n (nは1以上の整数)
73 // 3) _dupSize <= _queSize
74 // 4) ioSize_ = readSize_(kgCSV.hより)
78 kgEnv* env_; // 環境変数クラスへのポインタ
80 kgstr_t fname_; // ファイル名へのポインタ(標準入力はempty)
81 bool opened_; // オープンしたかどうか
83 kgAutoPtr2<char> _buf_ap; // 出力バッファbufをラッピングするsmart pointer
85 size_t _maxRecLen; // レコード最大長(環境変数からセットされる)
86 size_t queSize_; // 1つのqueueの大きさ(環境変数より)
87 size_t ioSize_; // write関数で一回に書き込むサイズ
88 size_t ioCnt_; // その回数
90 bool _noFldName; // 1行目が項目名行でないフラグ
91 char _dblFmt[256]; // double型出力フォーマット
93 char* _curPnt; // 現在の書き込み位置
94 char* _border; // buf配列の境界アドレス(ここを越えるとflash)
95 char* _end; // buf配列の最終アドレス(ここを越えるとerror)
96 size_t _recNo; // 現在書き込み中の行番号
98 char* dupTop_; // スラック領域へコピーする先頭アドレス
99 size_t dupSize_; // スラック領域へコピーするサイズ
100 size_t bufSize_; // スラック領域へコピーするサイズ
104 kgCSVout() : opened_(false),_recNo(0){}
105 kgCSVout(kgstr_t fileName, kgEnv *env=NULL, bool noFldName=false);
108 ~kgCSVout(){ close();}
111 void open(kgstr_t fileName, kgEnv *env=NULL, bool noFldName=false, size_t cnt=4) throw(kgError);
112 void close(void) throw(kgError);
115 void setPrecision(int precision);
118 bool noFldName(void) const {return _noFldName;}
119 bool isOpend(void) const {return opened_;}
120 size_t recNo(void) const {return _recNo;}
125 // --------------------
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);
150 // --------------------
152 // --------------------
153 // csv一行を出力する(項目の解釈(DQなど)なし).
154 void writeRec(char* rec);
155 // csv一行+数値(mrandで使用)
156 void writeRec(char* rec,int val);
158 // --------------------
160 // --------------------
163 void writeFld(size_t size, char** flds, bool eol=true);
165 // flgが-1の項目はfldの文字列を出力し,それ以外は対応するvalを出力
167 void writeFld(char** fld, const vector<int>* flg, vector<kgVal>& val);
169 // 使用= chgstr, chgnum
170 void writeFld(char** fld, const vector<int>* flg, char** val);
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);
177 // 使用= number, newnumber, rand
178 void writeFld(char** fld ,size_t size ,int val,bool alpha_flg);
181 void writeFld(char** fld ,size_t size ,size_t val);
184 void writeFld(char** fld ,const vector<int>* cnt,bool eol=true);
186 // 使用= join, nrjoin, rjoin
187 void writeFld(char** fld,size_t size, char** addfld , const vector<int >* addcnt);
189 // 使用= chgstr, chgnum, nrjoin
190 void writeFld(char** fld,size_t size, char** addfld ,int addsize);
193 void writeFld(char** fld,size_t size, vector<string> *newFld);
197 void writeInt2alpha(int val);
199 // --------------------
201 // --------------------
202 //同じものがないことを確認してから出力する
203 void writeFldNameCHK(vector<kgstr_t>& outfld ) throw(kgError);
207 void writeFldName(kgCSV& csv) throw(kgError);
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
214 void writeFldName(kgArgFld& fld, bool second) throw(kgError);
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
221 void writeFldName(kgCSV& csv,kgArgFld& fld,bool second,bool back=true) throw(kgError);
224 // 使用= rand, cal, count, number
225 void writeFldName(kgCSV& csv, kgstr_t newFld) throw(kgError);
227 // csv項目+newFld(vector)を出力
229 void writeFldName(kgCSV& csv, vector<kgstr_t> newFld) throw(kgError);
232 // 使用= newrand, newstr, newnumber
233 void writeFldName(kgstr_t newFld) throw(kgError);
236 // 使用= cat、chkcsv、fldname, sep2, cm
237 void writeFldName(vector<kgstr_t> newFld) throw(kgError);
239 //区切り文字出力(コンマ、改行、改行[出力行数カウント有り])
240 void writeDlm(void){ *_curPnt++ = ','; }
241 void writeEolNC(void){ *_curPnt++ = '\n'; }
244 //if(_recNo>LIMIT_OUT){
245 // throw kgError("limit line over");
253 } //////////////////////////////////////////////////////////////// end namespace