added CSVOutput
This commit is contained in:
parent
3103321585
commit
b2ad30b12c
126
inc/hgl/util/csv/CSVOutput.h
Normal file
126
inc/hgl/util/csv/CSVOutput.h
Normal file
@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
#include<initializer_list>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
/**
|
||||
* CSV输出控制
|
||||
*/
|
||||
template<typename T> 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<T> &str)
|
||||
{
|
||||
WriteString(str.c_str(),str.Length());
|
||||
}
|
||||
|
||||
void WriteStringList(const StringList<T> &sl)
|
||||
{
|
||||
for(const String<T> &str:sl)
|
||||
WriteString(str);
|
||||
}
|
||||
|
||||
void WriteStringList(const std::initializer_list<String<T>> &sl)
|
||||
{
|
||||
for(const String<T> &str:sl)
|
||||
WriteString(str);
|
||||
}
|
||||
|
||||
void WriteStringList(const std::initializer_list<const T *> &sl)
|
||||
{
|
||||
for(const T *str:sl)
|
||||
WriteString(str);
|
||||
}
|
||||
|
||||
template<typename I>
|
||||
void WriteInteger(const I &value)
|
||||
{
|
||||
tos->WriteString(String<T>::numberOf(value));
|
||||
|
||||
NextField();
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void WriteFloat(const F &value,const uint frac)
|
||||
{
|
||||
tos->WriteString(String<T>::floatOf(value,frac));
|
||||
|
||||
NextField();
|
||||
}
|
||||
};//class CSVOutput
|
||||
|
||||
using UTF8CSVOutput=CSVOutput<u8char>;
|
||||
using UTF16CSVOutput=CSVOutput<u16char>;
|
||||
using UTF32CSVOutput=CSVOutput<u32char>;
|
||||
|
||||
inline CSVOutput<u8char> * 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<u16char> * 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<u16char> * 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
|
63
inc/hgl/util/csv/CSVOutputStream.h
Normal file
63
inc/hgl/util/csv/CSVOutputStream.h
Normal file
@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/util/csv/CSVOutput.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
class OutputStream;
|
||||
class TextOutputStream;
|
||||
}//namespace io
|
||||
|
||||
namespace util
|
||||
{
|
||||
template<typename T> class CSVOutputStream
|
||||
{
|
||||
io::OutputStream *os;
|
||||
|
||||
io::TextOutputStream *tos;
|
||||
|
||||
CSVOutput<T> *csv;
|
||||
|
||||
public:
|
||||
|
||||
CSVOutputStream(io::OutputStream *_os,io::TextOutputStream *_tos,CSVOutput<T> *_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<T> &str){csv->WriteString(str);}
|
||||
|
||||
void WriteStringList(const StringList<T> &sl){return csv->WriteStringList(sl);}
|
||||
void WriteStringList(const std::initializer_list<String<T>> &sl){return csv->WriteStringList(sl);}
|
||||
void WriteStringList(const std::initializer_list<const T *> &sl){return csv->WriteStringList(sl);}
|
||||
|
||||
template<typename I>
|
||||
void WriteInteger(const I &value){csv->WriteInteger(value);}
|
||||
|
||||
template<typename F>
|
||||
void WriteFloat(const F &value,const uint frac){csv->WriteFloat(value,frac);}
|
||||
};//template<typename T> class CSVOutputStream
|
||||
|
||||
template<typename T> inline CSVOutputStream<T> *CreateCSVOutputToStream(io::OutputStream *os,io::TextOutputStream *tos,const uint field_count,const T fields_terminated_char=T('\t'),const T enclosed_char=T('"'))
|
||||
{
|
||||
CSVOutput<T> *csv=new CSVOutput<T>(tos,field_count,fields_terminated_char,enclosed_char);
|
||||
|
||||
return(new CSVOutputStream<T>(os,tos,csv));
|
||||
}
|
||||
|
||||
CSVOutputStream<u8char> *CreateCSVOutputToUTF8File(const OSString &filename,const uint field_count,const u8char fields_terminated_char=U8_TEXT('\t'),const u8char enclosed_char=U8_TEXT('"'));
|
||||
CSVOutputStream<u16char> *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
|
@ -1,19 +1,21 @@
|
||||
#ifndef HGL_ALGORITHM_SORT_INCLUDE
|
||||
#define HGL_ALGORITHM_SORT_INCLUDE
|
||||
|
||||
#include<hgl/TypeFunc.h>
|
||||
#include<hgl/CompOperator.h>
|
||||
#include<string.h>
|
||||
|
||||
template<typename T> class SortBase
|
||||
namespace hgl
|
||||
{
|
||||
protected:
|
||||
template<typename T> class SortBase
|
||||
{
|
||||
protected:
|
||||
|
||||
T *buffer; //数据
|
||||
int number; //数据个数
|
||||
|
||||
Comparator<T> *comp; //比较函数类
|
||||
|
||||
public:
|
||||
public:
|
||||
|
||||
/**
|
||||
* 本类构造函数
|
||||
@ -66,10 +68,8 @@ public:
|
||||
}
|
||||
|
||||
virtual bool sort()=0; //排序
|
||||
};//struct SortBase
|
||||
};//struct SortBase
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
//堆排序
|
||||
template<typename T> class HeapSort:public SortBase<T>
|
||||
{
|
||||
@ -141,13 +141,12 @@ namespace hgl
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Sort(List<T> &list,Comparator<T> *comp=Comparator<T>())
|
||||
bool Sort(DataArray<T> &list,Comparator<T> *comp=Comparator<T>())
|
||||
{
|
||||
return Sort(list.GetData(),
|
||||
list.GetCount(),
|
||||
comp);
|
||||
}
|
||||
|
||||
/*
|
||||
//仅实现模拟虚拟成员函数即可,无需整个类重载
|
||||
template<> int Comparator<BagCell>::compare(const BagCell &it1,const BagCell &it2) const
|
||||
|
@ -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})
|
||||
|
||||
|
29
src/csv/CSVOutputStream.cpp
Normal file
29
src/csv/CSVOutputStream.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/util/csv/CSVOutputStream.h>
|
||||
#include<hgl/io/FileOutputStream.h>
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
CSVOutputStream<char> *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<u8char>(fos,new io::UTF8TextOutputStream(fos),field_count,fields_terminated_char,enclosed_char);
|
||||
}
|
||||
|
||||
CSVOutputStream<u16char> *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<u16char>(fos,new io::UTF16LETextOutputStream(fos),field_count,fields_terminated_char,enclosed_char);
|
||||
}
|
||||
}//namespace util
|
||||
}//namespace hgl
|
Loading…
x
Reference in New Issue
Block a user