From 2ac123662f92ff0f5f580c02176c9ab5b5a5104b Mon Sep 17 00:00:00 2001 From: "M. Shulhan" Date: Thu, 29 Sep 2016 00:32:49 +0700 Subject: [PATCH] Add class Rowset which define a list of row. --- Makefile | 3 ++ Rowset.cc | 70 ++++++++++++++++++++++++++++++++ Rowset.hh | 35 ++++++++++++++++ test/Makefile | 7 +++- test/Rowset.test.cc | 98 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 Rowset.cc create mode 100644 Rowset.hh create mode 100644 test/Rowset.test.cc diff --git a/Makefile b/Makefile index 2e9132c..8c95842 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,7 @@ TARGET_OBJS = \ $(LIBVOS_BLD_D)/FTPD_client.oo \ $(LIBVOS_BLD_D)/FTPD_user.oo \ $(LIBVOS_BLD_D)/FTPD.oo \ + $(LIBVOS_BLD_D)/Rowset.oo \ $(LIBVOS_BLD_D)/SSVReader.oo # @@ -121,6 +122,8 @@ $(LIBVOS_BLD_D)/Buffer.oo : $(LIBVOS_BLD_D)/Object.oo $(LIBVOS_BLD_D)/List.oo : $(LIBVOS_BLD_D)/BNode.oo +$(LIBVOS_BLD_D)/Rowset.oo : $(LIBVOS_BLD_D)/List.oo + $(LIBVOS_BLD_D)/SockAddr.oo \ $(LIBVOS_BLD_D)/List.oo \ $(LIBVOS_BLD_D)/FTPClient.oo \ diff --git a/Rowset.cc b/Rowset.cc new file mode 100644 index 0000000..0110af8 --- /dev/null +++ b/Rowset.cc @@ -0,0 +1,70 @@ +// +// Copyright 2009-2016 M. Shulhan (ms@kilabit.info). All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// + +#include "Rowset.hh" +#include "Buffer.hh" + +namespace vos { + +const char* Rowset::__cname = "Rowset"; + +// +// `Rowset()` will create new set of row. +// +Rowset::Rowset() : List('\n') +{ +} + +// +// `~Rowset()` will release all rows memory to the system. +// +Rowset::~Rowset() +{} + +// +// `chars()` will return string presentation of set, where each node is +// printed from top to bottom and separated by `_sep` character. +// The returned string is in JSON array format. +// +const char* Rowset::chars() +{ + if (_v) { + free(_v); + _v = NULL; + } + + if (!_head) { + return _v; + } + + Buffer b; + BNode* node = _head; + + if (_head == _tail) { + b.concat("[ ", _head->chars(), " ]", NULL); + goto out; + } + + b.append_raw("[ "); + while (node != _tail) { + b.append_raw(node->chars()); + b.appendc(_sep); + + node = node->_right; + } + b.append_raw(node->chars()); + b.append_raw(" ]"); + +out: + _v = b._v; + b._v = NULL; + + return _v; +} + + +} // namespace vos +// vi: ts=8 sw=8 tw=78: diff --git a/Rowset.hh b/Rowset.hh new file mode 100644 index 0000000..0bf9b05 --- /dev/null +++ b/Rowset.hh @@ -0,0 +1,35 @@ +// +// Copyright 2009-2016 M. Shulhan (ms@kilabit.info). All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// + +#ifndef _LIBVOS_ROWSET_HH +#define _LIBVOS_ROWSET_HH 1 + +#include "List.hh" + +namespace vos { + +// +// class Rowset define a list of row. +// Each row may contain different number of record. +// +class Rowset : public List { +public: + Rowset(); + virtual ~Rowset(); + + const char* chars(); + + // `__cname` contain canonical name of this object. + static const char* __cname; +private: + + Rowset(const Rowset&); + void operator=(const Rowset&); +}; + +} // namespace vos +#endif +// vi: ts=8 sw=8 tw=78: diff --git a/test/Makefile b/test/Makefile index d72b3ac..919172b 100644 --- a/test/Makefile +++ b/test/Makefile @@ -33,6 +33,7 @@ Dlogger_OBJS= $(Buffer_OBJS) \ SSVReader_OBJS= $(Buffer_OBJS) \ $(LIBVOS_BLD_D)/File.oo \ $(LIBVOS_BLD_D)/Record.oo \ + $(LIBVOS_BLD_D)/Rowset.oo \ $(LIBVOS_BLD_D)/SSVReader.oo @@ -46,6 +47,9 @@ DNSQuery_OBJS= $(SSVReader_OBJS) \ host_to_dnsquery_OBJS= $(SSVReader_OBJS) \ $(DNSQuery_OBJS) +Rowset_OBJS= $(List_OBJS) \ + $(LIBVOS_BLD_D)/Rowset.oo + Resolver_OBJS= \ $(List_OBJS) \ $(DNSQuery_OBJS) \ @@ -61,7 +65,8 @@ TARGET= $(BLD_D)/Object.test \ $(BLD_D)/SSVReader.test \ $(BLD_D)/SockAddr.test \ $(BLD_D)/host_to_dnsquery.test \ - $(BLD_D)/Resolver.test + $(BLD_D)/Resolver.test \ + $(BLD_D)/Rowset.test .PHONY: all clean diff --git a/test/Rowset.test.cc b/test/Rowset.test.cc new file mode 100644 index 0000000..2db2d89 --- /dev/null +++ b/test/Rowset.test.cc @@ -0,0 +1,98 @@ +// +// Copyright 2009-2016 M. Shulhan (ms@kilabit.info). All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// + +#include "test.hh" +#include "../Rowset.hh" + +#define EXP_ROW_0 + +#define EXP_0 SB ( \ + SB( \ + V_STR(STR_TEST_0) SEP_ITEM \ + V_STR(STR_TEST_1) SEP_ITEM \ + V_STR(STR_TEST_2) \ + ) \ + ) + +#define EXP_1 SB ( \ + SB( \ + V_STR(STR_TEST_0) SEP_ITEM \ + V_STR(STR_TEST_1) SEP_ITEM \ + V_STR(STR_TEST_2) \ + ) SEP_LINE \ + SB( \ + V_STR(STR_TEST_2) SEP_ITEM \ + V_STR(STR_TEST_1) SEP_ITEM \ + V_STR(STR_TEST_0) \ + ) \ + ) + +using vos::Buffer; +using vos::List; +using vos::Rowset; + +Rowset rowset; + +List* create_row_0() +{ + Buffer* b = NULL; + List* row = new List(); + + b = new Buffer(); + b->copy_raw(STR_TEST_0); + row->push_tail(b); + + b = new Buffer(); + b->copy_raw(STR_TEST_1); + row->push_tail(b); + + b = new Buffer(); + b->copy_raw(STR_TEST_2); + row->push_tail(b); + + return row; +} + +List* create_row_1() +{ + Buffer* b = NULL; + List* row = new List(); + + b = new Buffer(); + b->copy_raw(STR_TEST_2); + row->push_tail(b); + + b = new Buffer(); + b->copy_raw(STR_TEST_1); + row->push_tail(b); + + b = new Buffer(); + b->copy_raw(STR_TEST_0); + row->push_tail(b); + + return row; +} + +int main() +{ + List* row; + + row = create_row_0(); + rowset.push_tail(row); + + printf("exp: '%s'\n", EXP_0); + printf("got: '%s'\n", rowset.chars()); + + assert(strcmp(EXP_0, rowset.chars()) == 0); + + row = create_row_1(); + rowset.push_tail(row); + + printf("exp: '%s'\n", EXP_1); + printf("got: '%s'\n", rowset.chars()); + + assert(strcmp(EXP_1, rowset.chars()) == 0); +}