OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
List.cpp
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2// $RCSfile: List.cpp,v $
3// ------------------------------------------------------------------------
4// $Revision: 1.2 $
5// ------------------------------------------------------------------------
6// Copyright: see Copyright.readme
7// ------------------------------------------------------------------------
8//
9// Class: List
10// The class for OPAL LIST commands.
11//
12// ------------------------------------------------------------------------
13//
14// $Date: 2001/08/13 15:25:22 $
15// $Author: jowett $
16//
17// ------------------------------------------------------------------------
18
19#include "Tables/List.h"
26#include "Parser/Token.h"
29#include <fstream>
30#include <iomanip>
31#include <list>
32#include <vector>
33#include <iostream>
34
35// Class List
36// ------------------------------------------------------------------------
37
38// The attributes of class List.
39namespace {
40 enum {
41 TABLE, // The name of the table to be listed.
42 FNAME, // The name of the file to be written.
43 ALL, // If true, list all columns.
44 COLUMN, // The column names.
45 SIZE
46 };
47}
48
49
51 Action(SIZE, "LIST",
52 "The \"LIST\" statement lists a named table.") {
54 ("TABLE", "Name of table to be listed");
56 ("FILE", "Name of file to receive output", "LIST");
58 ("ALL", "Set true to list all columns");
60 ("COLUMN", "Column specifiers");
61
63}
64
65
66List::List(const std::string &name, List *parent):
67 Action(name, parent)
68{}
69
70
72{}
73
74
75List *List::clone(const std::string &name) {
76 return new List(name, this);
77}
78
79
81 std::string tableName = Attributes::getString(itsAttr[TABLE]);
82 Table *table = Table::find(tableName);
83
84 if(table) {
85 std::string fileName = Attributes::getString(itsAttr[FNAME]);
86
87 if(fileName == "TERM") {
88 list(std::cout, table);
89 } else {
90 std::ofstream os(fileName.c_str());
91 if(os.good()) {
92 list(os, table);
93 } else {
94 throw OpalException("List::execute()",
95 "Unable to open output stream \"" +
96 fileName + "\".");
97 }
98 }
99 } else {
100 throw ParseError("List::execute()",
101 "Table \"" + tableName + "\" not found.");
102 }
103}
104
105
106void List::list(std::ostream &os, Table *table) {
107 bool listAll = Attributes::getBool(itsAttr[ALL]);
108 Table::CellArray cells;
109
110 // Column specification desired ?
111 if(listAll) {
112 cells = table->getDefault();
113 } else {
114 // Parse the column specifications.
115 // These are returned as TokenList's, since the table is not yet known.
116 // to the table; it therefore returns the column expressions in the
117 // form of a TokenListArray.
118 std::vector<std::list<Token> > columns =
120
121 for(std::vector<std::list<Token> >::iterator i = columns.begin();
122 i != columns.end(); ++i) {
123 std::list<Token> tokenList = *i;
124 SimpleStatement col("COLUMN[]", tokenList);
125 col.start();
126 int width = 12, prec = 8;
129
130 if(col.delimiter(':')) {
131 if(col.integer(width)) {
132 if(col.delimiter(':')) {
133 if(! col.integer(prec)) {
134 throw ParseError("List::list()",
135 "Invalid <precision> for table column \"" +
136 getOpalName() + "\".");
137 }
138 }
139 } else {
140 throw ParseError("List::list()",
141 "Invalid <width> for table column \"" +
142 getOpalName() + "\".");
143 }
144 }
145
146 if(width < prec + 6) width = prec + 6;
147 cells.push_back(Table::Cell(expr, width, prec));
148 }
149 }
150
151 // Now list the table.
152 table->printTable(os, cells);
153}
@ SIZE
Definition: IndexMap.cpp:174
const std::string name
PtrToScalar< double > parseTableExpression(Statement &, const Table *)
Parse table expression (depends on a table's rows).
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:90
Attribute makeTokenListArray(const std::string &name, const std::string &help)
Make token list attribute.
Definition: Attributes.cpp:625
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:100
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:343
std::vector< std::list< Token > > getTokenListArray(const Attribute &attr)
Return token list array value.
Definition: Attributes.cpp:630
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:332
std::string::iterator iterator
Definition: MSLang.h:16
The base class for all OPAL actions.
Definition: Action.h:30
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:191
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:310
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
The base class for all OPAL tables.
Definition: Table.h:42
virtual void printTable(std::ostream &, const CellArray &) const =0
Print list for the table.
virtual CellArray getDefault() const =0
Return the default print columns.
std::vector< Cell > CellArray
An array of cell descriptors.
Definition: Table.h:63
static Table * find(const std::string &name)
Find named Table.
Definition: Table.cpp:41
Descriptor for printing a table cell.
Definition: Table.h:47
A simple input statement in token form.
bool integer(int &value)
Return signed integer.
Definition: Statement.cpp:79
bool delimiter(char c)
Test for delimiter.
Definition: Statement.cpp:101
void start()
Return to start.
Definition: Statement.cpp:180
Parse exception.
Definition: ParseError.h:32
The LIST command.
Definition: List.h:32
virtual List * clone(const std::string &name)
Make clone.
Definition: List.cpp:75
List()
Exemplar constructor.
Definition: List.cpp:50
virtual ~List()
Definition: List.cpp:71
void list(std::ostream &, Table *)
Definition: List.cpp:106
virtual void execute()
Execute the command.
Definition: List.cpp:80
The base class for all OPAL exceptions.
Definition: OpalException.h:28