Annotation of parser3/src/include/pa_table.h, revision 1.77
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.77 ! moko 11: #define IDENT_PA_TABLE_H "$Id: pa_table.h,v 1.76 2024/11/17 14:04:28 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>
1.77 ! moko 81: void table_for_each(void (*func)(Table& self, I* info), I* info, Action_options& o);
1.52 paf 82:
83: template<typename I>
1.54 paf 84: bool table_first_that(bool (*func)(Table& self, I info), I info, Action_options& o) {
1.52 paf 85: if(!o.adjust(count()))
86: return false;
87:
1.76 moko 88: size_t saved_current=current();
1.52 paf 89: size_t row=o.offset;
90: if(o.reverse) { // reverse
1.62 misha 91: for(size_t i=0; i<o.limit; i++) {
92: set_current(row-i);
1.52 paf 93:
94: if(func(*this, info))
95: return true;
96: }
97: } else { // forward
98: for(size_t to=row+o.limit; row<to; row++) {
99: set_current(row);
100:
101: if(func(*this, info))
102: return true;
103: }
104: }
105:
1.76 moko 106: set_current(saved_current);
1.52 paf 107: return false;
108: }
109:
1.49 paf 110: bool locate(int column, const String& value, Action_options& options);
111: bool locate(const String& column, const String& value, Action_options& options);
1.24 paf 112:
1.12 paf 113: private:
1.21 paf 114:
1.52 paf 115: /// current row
116: size_t fcurrent;
1.1 paf 117:
1.52 paf 118: /// columns
119: columns_type fcolumns;
1.5 paf 120:
1.52 paf 121: /// column name->number lookup table
1.63 misha 122: typedef HashString<int> name2number_hash_class;
1.52 paf 123: name2number_hash_class* name2number;
1.5 paf 124:
1.52 paf 125: /// is that @c index falid?
1.55 paf 126: bool valid(size_t index) const { return index<count(); }
1.2 paf 127:
1.1 paf 128: };
129:
1.75 moko 130: /// Auto-object that temporarily saves and restores current
131: class Temp_current {
132: Table& ftable;
133: size_t fcurrent;
134: public:
135: Temp_current(Table& atable) : ftable(atable), fcurrent(atable.current()){}
136: ~Temp_current(){
137: ftable.set_current(fcurrent);
138: }
139: };
140:
1.77 ! moko 141: template<typename I> void Table::table_for_each(void (*func)(Table& self, I* info), I* info, Action_options& o) {
! 142: if(!o.adjust(count()))
! 143: return;
! 144:
! 145: Temp_current tc(*this);
! 146: size_t row=o.offset;
! 147: if(o.reverse) { // reverse
! 148: for(size_t i=0; i<o.limit; i++) {
! 149: set_current(row-i);
! 150: func(*this, info);
! 151: }
! 152: } else { // forward
! 153: for(size_t to=row+o.limit; row<to; row++) {
! 154: set_current(row);
! 155: func(*this, info);
! 156: }
! 157: }
! 158: }
! 159:
1.1 paf 160: #endif
E-mail: