From 4a533f550e5fba308eef1995fe0aef78c1d8e42b Mon Sep 17 00:00:00 2001 From: dongjulim Date: Wed, 27 Mar 2024 15:41:09 +0900 Subject: [PATCH] add binary_io test --- CMakeLists.txt | 6 +- src/binary_handle.cpp | 126 +++++++++++++++++++++++++++++++++++++++ src/memory_pool_test.cpp | 9 +-- 3 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 src/binary_handle.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ac64312..75b9b49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,8 @@ set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_COMPILER gcc) +add_compile_options(-g) + set(LIBRARY_SET stdc++ Poco::Foundation Poco::Net Poco::Util Poco::XML Poco::JSON Poco::NetSSL ) @@ -32,7 +34,7 @@ add_executable(secure_smtp_test src/secure_smtp_test.cpp) add_executable(secure_http_test ssl_https/secure_http_test.cpp) add_executable(application_util_test src/application_util_test.cpp) add_executable(memory_pool_test src/memory_pool_test.cpp) -add_executable(poco_test src/poco_test.cpp) +add_executable(binary_handle src/binary_handle.cpp) target_link_libraries(http_test1 ${LIBRARY_SET}) target_link_libraries(filesystem_test ${LIBRARY_SET}) @@ -55,4 +57,4 @@ target_link_libraries(secure_smtp_test ${LIBRARY_SET}) target_link_libraries(secure_http_test ${LIBRARY_SET}) target_link_libraries(application_util_test ${LIBRARY_SET}) target_link_libraries(memory_pool_test ${LIBRARY_SET}) -target_link_libraries(poco_test ${LIBRARY_SET}) \ No newline at end of file +target_link_libraries(binary_handle ${LIBRARY_SET}) \ No newline at end of file diff --git a/src/binary_handle.cpp b/src/binary_handle.cpp new file mode 100644 index 0000000..87fa0dd --- /dev/null +++ b/src/binary_handle.cpp @@ -0,0 +1,126 @@ +// +// Created by centos on 24. 3. 27.. +// + +#include "Poco/BinaryReader.h" +#include "Poco/BinaryWriter.h" +#include +#include +#include + +#define NUMBER_OF_CHILD 3 + +char bufferA[100] ={0x2E,0x2E,0x2E,0x68,0x65,0x6C,0x6C,0x6F,0x20,0x70,0x6F,0x63,0x6F,0x2E,0x2E,0x2E,0x66,0x6C,0x61,0x67,0x3A,0x7B,0x54,0x52,0x79,0x48,0x34,0x43,0x6B,0x4D,0x45,0x7D}; + +typedef struct{ + bool boolean; + unsigned int unumber; + unsigned short short_unumber; +}ChildData; + +typedef struct { + int number; + long long_number; + short short_number; + char char10[10]; + char char100[100]; + int child_num; //number of child_data struct + void* child_data; //have to malloc +}ParentData; + +void WriteDataSequence(Poco::BinaryWriter writer, ParentData* pd){ + writer<number<long_number<short_number<char10<char100<child_num; + for(int i=0;ichild_num;i++){ + writer<<((ChildData*)pd->child_data+i)->boolean<<((ChildData*)pd->child_data+i)->unumber<<((ChildData*)pd->child_data+i)->short_unumber; + } +} + +ParentData *ParentDataSample(){ + ChildData c_data[NUMBER_OF_CHILD]; + for(int i=0;i(malloc(sizeof(ParentData))); + memset(p_data,0,sizeof(ParentData)); + p_data->number = 1; + p_data->long_number = 10000; + p_data->short_number = 123; + + strcpy(p_data->char10,"poco_test"); + strcpy(p_data->char100, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore"); + p_data->child_num=3; + p_data->child_data = malloc(sizeof(ChildData)*3); + memset(p_data->child_data,0,sizeof(ChildData)*3); + for(int i=0;ichild_data+i,&c_data[i],sizeof(ChildData)); + } + + return p_data; +} + +void LittleWriter(void *data,bool bom){ + std::ofstream ostr("binary_sample_little.dat",std::ios::binary); + Poco::BinaryWriter writer(ostr,Poco::BinaryWriter::LITTLE_ENDIAN_BYTE_ORDER); + if(bom){ + writer.writeBOM(); + } + WriteDataSequence(writer,(ParentData*)data); +} +void BigWriter(void *data,bool bom){ + std::ofstream ostr("binary_sample_big.dat",std::ios::binary); + Poco::BinaryWriter writer(ostr,Poco::BinaryWriter::BIG_ENDIAN_BYTE_ORDER); + if(bom){ + writer.writeBOM(); + } + WriteDataSequence(writer,(ParentData*)data); +} + + +void Reader(std::string str,Poco::BinaryReader::StreamByteOrder order){ + std::ifstream istr(str, std::ios::binary); + Poco::BinaryReader reader(istr,order); +// reader.readBOM(); + + ParentData pd; std::string tmp_str1,tmp_str2; + reader>>pd.number>>pd.long_number>>pd.short_number>>tmp_str1>>tmp_str2>>pd.child_num; + std::cout<>cd.boolean>>cd.unumber>>cd.short_unumber; + std::cout<number<<' '<long_number<<' '<short_number<<' '<char10<<' '<char100<<' '<child_num; + for(int i=0;ichild_num;i++){ + std::cout<child_data+i)->boolean<<' '<<((ChildData*)pd->child_data+i)->unumber<<' '<<((ChildData*)pd->child_data+i)->short_unumber; + } + std::cout< #include -#define MAX_ROW 50000 +#define MAX_ROW 5000 typedef struct{ int size; @@ -21,7 +21,7 @@ typedef struct{ void* col_data; }Record; -std::string generateRandomString(int length) { +std::string GenerateRandomString(int length) { std::string randomString; static const char alphanum[] = "0123456789" @@ -42,6 +42,7 @@ void* MpAlloc(Poco::MemoryPool *mp, int size){ // memset(pointer,0,size); return pointer; } + void MpFree(Poco::MemoryPool *mp,void* pointer){ mp->release(pointer); } @@ -119,7 +120,7 @@ void CompareTest(int selected_test){ for (int i = 0; i < 9; ++i) { // 랜덤한 길이의 문자열 생성 int length = rand() % 40 + 5; // 5~45자 - std::string randomString = generateRandomString(length); + std::string randomString = GenerateRandomString(length); sample_data[j].emplace_back(randomString, randomString.length()+1); } @@ -238,7 +239,7 @@ void OverflowErrorTest(){ void * overflow_pointer[10]; for(int i=0;i<10;i++){ overflow_pointer[i]=overflow_mp.get(); - strcpy((char*)overflow_pointer[i],generateRandomString(130).c_str()); //!!!!!!! + strcpy((char*)overflow_pointer[i],GenerateRandomString(130).c_str()); //!!!!!!! } for(int i=0;i<10;i++){ std::cout<<(char*)overflow_pointer[i]<