Skip to content

Commit

Permalink
Add class Rowset which define a list of row.
Browse files Browse the repository at this point in the history
  • Loading branch information
shuLhan committed Sep 28, 2016
1 parent fb4238d commit 2ac1236
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -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

#
Expand Down Expand Up @@ -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 \
Expand Down
70 changes: 70 additions & 0 deletions 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:
35 changes: 35 additions & 0 deletions 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:
7 changes: 6 additions & 1 deletion test/Makefile
Expand Up @@ -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


Expand All @@ -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) \
Expand All @@ -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

Expand Down
98 changes: 98 additions & 0 deletions 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);
}

0 comments on commit 2ac1236

Please sign in to comment.