From b2ad30b12ce4ac55d65ad0541264dbd72848508a Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Wed, 26 Jul 2023 22:27:04 +0800 Subject: [PATCH] added CSVOutput --- inc/hgl/util/csv/CSVOutput.h | 126 +++++++++++++++++++++++++++ inc/hgl/util/csv/CSVOutputStream.h | 63 ++++++++++++++ inc/hgl/util/sort/Sort.h | 133 ++++++++++++++--------------- src/CMakeLists.txt | 5 +- src/csv/CSVOutputStream.cpp | 29 +++++++ 5 files changed, 288 insertions(+), 68 deletions(-) create mode 100644 inc/hgl/util/csv/CSVOutput.h create mode 100644 inc/hgl/util/csv/CSVOutputStream.h create mode 100644 src/csv/CSVOutputStream.cpp diff --git a/inc/hgl/util/csv/CSVOutput.h b/inc/hgl/util/csv/CSVOutput.h new file mode 100644 index 0000000..4e2424c --- /dev/null +++ b/inc/hgl/util/csv/CSVOutput.h @@ -0,0 +1,126 @@ +#pragma once +#include +#include + +namespace hgl +{ + namespace util + { + /** + * CSV输出控制 + */ + template class CSVOutput + { + io::TextOutputStream *tos; + + uint FieldCount; + + T FieldsTerminatedChar; + T EnclosedChar; + + T NullStringField[2]; + + protected: + + uint write_count; + + void NextField() + { + --write_count; + + if(!write_count) + { + write_count=FieldCount; + tos->WriteLineEnd(); + } + else + tos->WriteChars(&FieldsTerminatedChar,1); + } + + public: + + CSVOutput( io::TextOutputStream *os, //文本输出流 + const uint field_count, //字段数量 + const T fields_terminated_char=T('\t'), //字段分隔符 + const T enclosed_char=T('"')) //字符串包裹字符 + { + tos=os; + + FieldCount =field_count; + + FieldsTerminatedChar=fields_terminated_char; + + EnclosedChar =enclosed_char; + + NullStringField[0] =enclosed_char; + NullStringField[1] =enclosed_char; + + write_count =FieldCount; + } + + ~CSVOutput()=default; + + void WriteString(const T *str,const int len) + { + if(!str||!*str||len<=0) + { + tos->WriteChars(NullStringField,2); + } + else + { + tos->WriteChars(&EnclosedChar,1); + tos->WriteChars(str,len); + tos->WriteChars(&EnclosedChar,1); + } + NextField(); + } + + void WriteString(const String &str) + { + WriteString(str.c_str(),str.Length()); + } + + void WriteStringList(const StringList &sl) + { + for(const String &str:sl) + WriteString(str); + } + + void WriteStringList(const std::initializer_list> &sl) + { + for(const String &str:sl) + WriteString(str); + } + + void WriteStringList(const std::initializer_list &sl) + { + for(const T *str:sl) + WriteString(str); + } + + template + void WriteInteger(const I &value) + { + tos->WriteString(String::numberOf(value)); + + NextField(); + } + + template + void WriteFloat(const F &value,const uint frac) + { + tos->WriteString(String::floatOf(value,frac)); + + NextField(); + } + };//class CSVOutput + + using UTF8CSVOutput=CSVOutput; + using UTF16CSVOutput=CSVOutput; + using UTF32CSVOutput=CSVOutput; + + inline CSVOutput * CreateUTF8CSVOutput (io::TextOutputStream * tos,const uint fc,const u8char ftc=U8_TEXT ('\t'),const u8char ec=U8_TEXT ('"')){return(new UTF8CSVOutput (tos,fc,ftc,ec));} + inline CSVOutput * CreateUTF16CSVOutput (io::TextOutputStream * tos,const uint fc,const u16char ftc=U16_TEXT('\t'),const u16char ec=U16_TEXT('"')){return(new UTF16CSVOutput(tos,fc,ftc,ec));} + inline CSVOutput * CreateUTF32CSVOutput (io::TextOutputStream * tos,const uint fc,const u32char ftc=U32_TEXT('\t'),const u32char ec=U32_TEXT('"')){return(new UTF16CSVOutput(tos,fc,ftc,ec));} + }//namespace util +}//namespace hgl diff --git a/inc/hgl/util/csv/CSVOutputStream.h b/inc/hgl/util/csv/CSVOutputStream.h new file mode 100644 index 0000000..49ecb21 --- /dev/null +++ b/inc/hgl/util/csv/CSVOutputStream.h @@ -0,0 +1,63 @@ +#pragma once + +#include + +namespace hgl +{ + namespace io + { + class OutputStream; + class TextOutputStream; + }//namespace io + + namespace util + { + template class CSVOutputStream + { + io::OutputStream *os; + + io::TextOutputStream *tos; + + CSVOutput *csv; + + public: + + CSVOutputStream(io::OutputStream *_os,io::TextOutputStream *_tos,CSVOutput *_csv) + { + os=_os; + tos=_tos; + csv=_csv; + } + + ~CSVOutputStream() + { + delete csv; + delete tos; + delete os; + } + + void WriteString(const T *str,const int len){csv->WriteString(str,len);} + void WriteString(const String &str){csv->WriteString(str);} + + void WriteStringList(const StringList &sl){return csv->WriteStringList(sl);} + void WriteStringList(const std::initializer_list> &sl){return csv->WriteStringList(sl);} + void WriteStringList(const std::initializer_list &sl){return csv->WriteStringList(sl);} + + template + void WriteInteger(const I &value){csv->WriteInteger(value);} + + template + void WriteFloat(const F &value,const uint frac){csv->WriteFloat(value,frac);} + };//template class CSVOutputStream + + template inline CSVOutputStream *CreateCSVOutputToStream(io::OutputStream *os,io::TextOutputStream *tos,const uint field_count,const T fields_terminated_char=T('\t'),const T enclosed_char=T('"')) + { + CSVOutput *csv=new CSVOutput(tos,field_count,fields_terminated_char,enclosed_char); + + return(new CSVOutputStream(os,tos,csv)); + } + + CSVOutputStream *CreateCSVOutputToUTF8File(const OSString &filename,const uint field_count,const u8char fields_terminated_char=U8_TEXT('\t'),const u8char enclosed_char=U8_TEXT('"')); + CSVOutputStream *CreateCSVOutputToUTF16LEFile(const OSString &filename,const uint field_count,const u16char fields_terminated_char=U16_TEXT('\t'),const u16char enclosed_char=U16_TEXT('"')); + }//namespace util +}//namespace hgl diff --git a/inc/hgl/util/sort/Sort.h b/inc/hgl/util/sort/Sort.h index a2d7e29..8e586bb 100644 --- a/inc/hgl/util/sort/Sort.h +++ b/inc/hgl/util/sort/Sort.h @@ -1,75 +1,75 @@ 锘#ifndef HGL_ALGORITHM_SORT_INCLUDE #define HGL_ALGORITHM_SORT_INCLUDE -#include +#include #include -template class SortBase -{ -protected: - - T *buffer; //鏁版嵁 - int number; //鏁版嵁涓暟 - - Comparator *comp; //姣旇緝鍑芥暟绫 - -public: - - /** - * 鏈被鏋勯犲嚱鏁 - * @param buf 鏁版嵁缂撳啿鍖 - * @param n 鏁版嵁涓暟 - * @param c 鏁版嵁澶у皬姣旇緝绫 - */ - SortBase(T *buf,int n,Comparator *c) - { - buffer =buf; - number =n; - comp =c; - } - - virtual ~SortBase()=default; - - int GetCount()const - { - return number; - } - - int compare(const T &a,const T &b) - { - return comp->compare(a,b); - } - - virtual int compare_by_index(int a,int b) - { - return comp->compare(buffer[a],buffer[b]); - } - - virtual void exchange(T &a,T &b) - { - comp->exchange(a,b); - } - - virtual void exchane_by_index(int a,int b) //浜ゆ崲涓や釜鏁版嵁 - { - comp->exchange(buffer[a],buffer[b]); - } - - virtual void cpy(T *dst,T *src) - { - comp->cpy(dst,src); - } - - virtual void cpy_by_index(int dst,int src) - { - comp->cpy(buffer+dst,buffer+src); - } - - virtual bool sort()=0; //鎺掑簭 -};//struct SortBase - namespace hgl { + template class SortBase + { + protected: + + T *buffer; //鏁版嵁 + int number; //鏁版嵁涓暟 + + Comparator *comp; //姣旇緝鍑芥暟绫 + + public: + + /** + * 鏈被鏋勯犲嚱鏁 + * @param buf 鏁版嵁缂撳啿鍖 + * @param n 鏁版嵁涓暟 + * @param c 鏁版嵁澶у皬姣旇緝绫 + */ + SortBase(T *buf,int n,Comparator *c) + { + buffer =buf; + number =n; + comp =c; + } + + virtual ~SortBase()=default; + + int GetCount()const + { + return number; + } + + int compare(const T &a,const T &b) + { + return comp->compare(a,b); + } + + virtual int compare_by_index(int a,int b) + { + return comp->compare(buffer[a],buffer[b]); + } + + virtual void exchange(T &a,T &b) + { + comp->exchange(a,b); + } + + virtual void exchane_by_index(int a,int b) //浜ゆ崲涓や釜鏁版嵁 + { + comp->exchange(buffer[a],buffer[b]); + } + + virtual void cpy(T *dst,T *src) + { + comp->cpy(dst,src); + } + + virtual void cpy_by_index(int dst,int src) + { + comp->cpy(buffer+dst,buffer+src); + } + + virtual bool sort()=0; //鎺掑簭 + };//struct SortBase + //鍫嗘帓搴 template class HeapSort:public SortBase { @@ -141,13 +141,12 @@ namespace hgl } template - bool Sort(List &list,Comparator *comp=Comparator()) + bool Sort(DataArray &list,Comparator *comp=Comparator()) { return Sort(list.GetData(), list.GetCount(), comp); } - /* //浠呭疄鐜版ā鎷熻櫄鎷熸垚鍛樺嚱鏁板嵆鍙紝鏃犻渶鏁翠釜绫婚噸杞 template<> int Comparator::compare(const BagCell &it1,const BagCell &it2) const diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8dd5386..0ce299c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -81,7 +81,10 @@ SOURCE_GROUP("Crypt" FILES ${CRYPT_HEADER_FILES} ${CRYPT_SOURCE_FILES}) #################################################################################################### SET(CSV_SOURCE ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVFieldSplite.h - csv/CSVFieldSplite.cpp) + ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVOutput.h + ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVOutputStream.h + csv/CSVFieldSplite.cpp + csv/CSVOutputStream.cpp) SOURCE_GROUP("CSV" FILES ${CSV_SOURCE}) diff --git a/src/csv/CSVOutputStream.cpp b/src/csv/CSVOutputStream.cpp new file mode 100644 index 0000000..faa96f8 --- /dev/null +++ b/src/csv/CSVOutputStream.cpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include + +namespace hgl +{ + namespace util + { + CSVOutputStream *CreateCSVOutputToUTF8File(const OSString &filename,const uint field_count,const u8char fields_terminated_char,const u8char enclosed_char) + { + io::FileOutputStream *fos=io::CreateFileOutputStream(filename); + + if(!fos)return(nullptr); + + return CreateCSVOutputToStream(fos,new io::UTF8TextOutputStream(fos),field_count,fields_terminated_char,enclosed_char); + } + + CSVOutputStream *CreateCSVOutputToUTF16LEFile(const OSString &filename,const uint field_count,const u16char fields_terminated_char,const u16char enclosed_char) + { + io::FileOutputStream *fos=io::CreateFileOutputStream(filename); + + if(!fos)return(nullptr); + + return CreateCSVOutputToStream(fos,new io::UTF16LETextOutputStream(fos),field_count,fields_terminated_char,enclosed_char); + } + }//namespace util +}//namespace hgl