17#include <boost/tokenizer.hpp>
24using namespace Kernel;
33 "The filename of the output TBL file.");
36 "The name of the workspace containing the data you want to "
37 "save to a TBL file.");
45 size_t rowCount = ws->rowCount();
46 for (
size_t i = 0; i < rowCount; ++i) {
48 if (row.
cell<
int>(ws->columnCount() - 2) != 0) {
65 throw std::runtime_error(
"Please provide an input workspace to be saved.");
68 std::ofstream file(filename.c_str());
74 std::vector<std::string> columnHeadings = ws->getColumnNames();
76 for (
size_t rowIndex = 0; rowIndex < ws->rowCount(); ++rowIndex) {
78 for (
size_t columnIndex = 0; columnIndex < columnHeadings.size(); columnIndex++) {
79 if (ws->getColumn(columnIndex)->type() !=
"str") {
81 int error = remove(filename.c_str());
83 throw std::runtime_error(columnHeadings[columnIndex] +
" column must be of type \"str\". The TBL "
84 "file will not be saved");
86 throw std::runtime_error(
"Saving TBL unsuccessful, please check the " + columnHeadings[columnIndex] +
87 " column is of type \"str\".");
89 if (columnIndex == columnHeadings.size() - 1)
90 writeVal<std::string>(row.
String(columnIndex), file,
false,
true);
92 writeVal<std::string>(row.
String(columnIndex), file);
101 auto it = columnHeadings.begin();
102 for (; it != columnHeadings.end() - 1; ++it) {
103 auto const &heading = *it;
104 writeVal<std::string>(heading, file);
106 auto const &lastHeading = *it;
107 writeVal<std::string>(lastHeading, file,
false,
true);
118template <
class T>
void SaveTBL::writeVal(
const T &val, std::ofstream &file,
bool endsep,
bool endline) {
119 std::string valStr = boost::lexical_cast<std::string>(val);
120 size_t comPos = valStr.find(
',');
121 if (comPos != std::string::npos) {
122 file <<
'"' << val <<
'"';
124 file << boost::lexical_cast<T>(val);
#define DECLARE_ALGORITHM(classname)
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
@ Save
to specify a file to write to, the file may or may not exist
TableRow represents a row in a TableWorkspace.
std::string & String(size_t col)
Returns a reference to the element in position col if its type is std::string.
T & cell(size_t col)
Templated method to access the element col in the row.
A property class for workspaces.
void writeColumnNames(std::ofstream &file, std::vector< std::string > const &columnHeadings)
const char m_sep
the separator
void writeVal(const T &val, std::ofstream &file, bool endsep=true, bool endline=false)
Writes a value to the file.
std::map< int, std::vector< size_t > > m_stichgroups
Map the separator options to their string equivalents.
std::vector< size_t > m_nogroup
void findGroups(const API::ITableWorkspace_sptr &ws)
Finds the stitch groups that need to be on the same line.
void init() override
Overwrites Algorithm method.
SaveTBL()
Default constructor.
void exec() override
Overwrites Algorithm method.
Records the filename and the description of failure.
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
@ Input
An input workspace.