TextEditor.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2011 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/Util/units/TextEditor.h
10
11#ifndef __TextEditor_h
12#define __TextEditor_h
13
14#include "TextInterface.h"
15#include "strutil.h"
16
18template<class T>
19class TextEditor : public Reference::Able
20{
21
22 public:
23
24 TextEditor ()
25 {
26 prefix_name = true;
27 output_identifier = true;
28 }
29
31 void add_commands (const std::string& str)
32 { standard_separation (commands, str); }
33
34 void add_script (const std::string& filename)
35 { loadlines (filename, commands); }
36
38 void add_extensions (const std::string& str)
39 { standard_separation (extensions_to_add, str); }
40
42 void remove_extensions (const std::string& str)
43 { standard_separation (extensions_to_remove, str); }
44
46 bool will_modify () const;
47
49 std::string process (T*);
50
52 virtual void add_extension (T*, const std::string& name) { }
53
55 virtual void remove_extension (T*, const std::string& name) { }
56
58 virtual std::string get_identifier (const T*) { return std::string(); }
59
62
65
67 std::string delimiter;
68
69 protected:
70
72 std::vector<std::string> commands;
73
75 std::vector<std::string> extensions_to_add;
76
78 std::vector<std::string> extensions_to_remove;
79
80};
81
82template <typename T>
84{
85 if (extensions_to_add.size() > 0 &&
86 extensions_to_add[0] != "help")
87 return true;
88
89 if (extensions_to_remove.size() > 0)
90 return true;
91
92 for (unsigned j = 0; j < commands.size(); j++)
93 if (commands[j].find('=') != std::string::npos &&
94 commands[j].find("help") == std::string::npos)
95 return true;
96
97 return false;
98}
99
100template <typename T>
101std::string TextEditor<T>::process (T* instance)
102{
103 for (unsigned i = 0; i < extensions_to_add.size(); i++)
104 add_extension (instance, extensions_to_add[i]);
105
106 for (unsigned i = 0; i < extensions_to_remove.size(); i++)
108
109 std::string result;
110
111 if (will_modify() && commands.size() == 0)
112 return result;
113
114 Reference::To<TextInterface::Parser> interface = instance->get_interface();
115
116 if (commands.size() == 0)
117 return interface->help (true);
118
119 // so that a space precedes each parameter processed
120 interface->set_indentation (" ");
121 interface->set_prefix_name (prefix_name);
122
123 if (delimiter.length())
124 interface->set_delimiter (delimiter);
125
127 result = get_identifier (instance);
128
129 for (unsigned j = 0; j < commands.size(); j++)
130 result += interface->process (commands[j]);
131
132 return result;
133}
134
135#endif
Manages Reference::To references to the instance.
Definition ReferenceAble.h:35
Template class manages Reference::Able objects.
Definition ReferenceTo.h:25
std::vector< std::string > extensions_to_remove
extensions to be removed
Definition TextEditor.h:78
void add_extensions(const std::string &str)
Add a semi-colon or comma-separated list of extensions to install.
Definition TextEditor.h:38
std::vector< std::string > extensions_to_add
extensions to be added
Definition TextEditor.h:75
bool will_modify() const
Return true if the process method will modify the archive.
Definition TextEditor.h:83
void add_commands(const std::string &str)
Add a semi-colon or comma-separated list of commands to execute.
Definition TextEditor.h:31
std::string delimiter
Optionally set the delimiter used to separate elements of a container.
Definition TextEditor.h:67
std::vector< std::string > commands
commands to be executed
Definition TextEditor.h:72
void remove_extensions(const std::string &str)
Add a semi-colon or comma-separated list of extensions to remove.
Definition TextEditor.h:42
std::string process(T *)
Edit the Archive according to the current state.
Definition TextEditor.h:101
virtual std::string get_identifier(const T *)
Get the identifier for this instance.
Definition TextEditor.h:58
bool prefix_name
Prefix parameter value queries with parameter name=.
Definition TextEditor.h:61
virtual void remove_extension(T *, const std::string &name)
Remove the named extension from the instance.
Definition TextEditor.h:55
bool output_identifier
Print the name of each file processed.
Definition TextEditor.h:64
virtual void add_extension(T *, const std::string &name)
Add the named extension to the instance.
Definition TextEditor.h:52

Generated using doxygen 1.14.0