Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class Rowset which define a list of row.
- Loading branch information
Showing
5 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |