Annotation of parser3/src/include/pa_table.h, revision 1.75
1.14 paf 1: /** @file
1.15 paf 2: Parser: table class decl.
3:
1.74 moko 4: Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
1.73 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1 paf 6: */
7:
8: #ifndef PA_TABLE_H
9: #define PA_TABLE_H
1.45 paf 10:
1.75 ! moko 11: #define IDENT_PA_TABLE_H "$Id: pa_table.h,v 1.74 2024/11/04 03:53:25 moko Exp $"
1.1 paf 12:
13: #include "pa_types.h"
14: #include "pa_hash.h"
1.2 paf 15: #include "pa_string.h"
1.1 paf 16:
1.75 ! moko 17: class Temp_current;
! 18:
1.16 paf 19: /**
1.19 paf 20: VTable backend.
1.16 paf 21:
22: holds:
1.14 paf 23: - column names[if any]
24: - data rows
25: - current row pointer
26:
27: uses String for column names and data items
28:
1.28 paf 29: hence most of tables are "named", no need to uptimize nameless onces.
30: rows and strings stored are read-only. once stored they can be removed,
31: but not altered. that's handy for quick copying & co. see table:join
1.14 paf 32: */
1.52 paf 33: class Table: public Array<ArrayString*> {
1.1 paf 34: public:
1.52 paf 35: typedef ArrayString* columns_type;
1.2 paf 36:
1.72 moko 37: Table(columns_type acolumns, size_t initial_rows=3);
1.52 paf 38: Table(const Table& src, Action_options& options);
1.21 paf 39:
1.42 paf 40: /// gets column names
1.52 paf 41: columns_type columns() { return fcolumns; }
1.42 paf 42:
1.75 ! moko 43: /// moves @a current pointer, can be out of range when restoring current in modified table
1.52 paf 44: void set_current(size_t acurrent) {
1.75 ! moko 45: fcurrent=acurrent<count() ? acurrent : count()>0 ? count()-1 : 0;
! 46: }
1.5 paf 47:
1.50 paf 48: /// @return current pointer
1.52 paf 49: size_t current() const { return fcurrent; }
1.37 paf 50: void offset(bool absolute, int offset);
1.5 paf 51:
1.67 moko 52: /// @return checks all rows to find maximum cells number
1.68 moko 53: size_t max_cells() const;
1.67 moko 54:
55: /** @return column index from @a column_name. '<0' if no such column
1.52 paf 56: if no such - 'bark'
1.32 paf 57: */
58: int column_name2index(const String& column, bool bark) const;
1.30 paf 59:
1.72 moko 60: void column_names_init();
61:
1.26 paf 62: /// @return item from @a column
1.52 paf 63: const String* item(size_t column);
1.30 paf 64:
1.65 moko 65: /// sets @a column value
66: void put_item(size_t column, const String*);
67:
1.66 moko 68: /// removes current row
69: void remove_current();
70:
1.30 paf 71: /// @return item from @a column. '0' if no such column
1.52 paf 72: const String* item(const String& column) {
1.32 paf 73: int index=column_name2index(column, false);
1.30 paf 74: return index>=0?item(index):0;
1.24 paf 75: }
1.23 paf 76:
77: /// saves to text file
78: void save(bool nameless_save, const String& file_spec);
1.2 paf 79:
1.52 paf 80: template<typename I>
81: void table_for_each(void (*func)(Table& self, I* info), I* info, Action_options& o) {
82: if(!o.adjust(count()))
83: return;
84:
1.75 ! moko 85: Temp_current tc(*this);
1.52 paf 86: size_t row=o.offset;
87: if(o.reverse) { // reverse
1.62 misha 88: for(size_t i=0; i<o.limit; i++) {
89: set_current(row-i);
1.52 paf 90: func(*this, info);
91: }
92: } else { // forward
93: for(size_t to=row+o.limit; row<to; row++) {
94: set_current(row);
95: func(*this, info);
96: }
97: }
98: }
99:
100: template<typename I>
1.54 paf 101: bool table_first_that(bool (*func)(Table& self, I info), I info, Action_options& o) {
1.52 paf 102: if(!o.adjust(count()))
103: return false;
104:
1.75 ! moko 105: Temp_current tc(*this);
1.52 paf 106: size_t row=o.offset;
107: if(o.reverse) { // reverse
1.62 misha 108: for(size_t i=0; i<o.limit; i++) {
109: set_current(row-i);
1.52 paf 110:
111: if(func(*this, info))
112: return true;
113: }
114: } else { // forward
115: for(size_t to=row+o.limit; row<to; row++) {
116: set_current(row);
117:
118: if(func(*this, info))
119: return true;
120: }
121: }
122:
123: return false;
124: }
125:
1.49 paf 126: bool locate(int column, const String& value, Action_options& options);
127: bool locate(const String& column, const String& value, Action_options& options);
1.24 paf 128:
1.12 paf 129: private:
1.21 paf 130:
1.52 paf 131: /// current row
132: size_t fcurrent;
1.1 paf 133:
1.52 paf 134: /// columns
135: columns_type fcolumns;
1.5 paf 136:
1.52 paf 137: /// column name->number lookup table
1.63 misha 138: typedef HashString<int> name2number_hash_class;
1.52 paf 139: name2number_hash_class* name2number;
1.5 paf 140:
1.52 paf 141: /// is that @c index falid?
1.55 paf 142: bool valid(size_t index) const { return index<count(); }
1.2 paf 143:
1.1 paf 144: };
145:
1.75 ! moko 146: /// Auto-object that temporarily saves and restores current
! 147: class Temp_current {
! 148: Table& ftable;
! 149: size_t fcurrent;
! 150: public:
! 151: Temp_current(Table& atable) : ftable(atable), fcurrent(atable.current()){}
! 152: ~Temp_current(){
! 153: ftable.set_current(fcurrent);
! 154: }
! 155: };
! 156:
1.1 paf 157: #endif
E-mail: