Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4VUIshell Class Referenceabstract

#include <G4VUIshell.hh>

+ Inheritance diagram for G4VUIshell:

Public Member Functions

 G4VUIshell (const G4String &prompt="> ")
 
virtual ~G4VUIshell ()
 
void SetNColumn (G4int ncol)
 
void SetPrompt (const G4String &prompt)
 
void SetCurrentDirectory (const G4String &ccd)
 
virtual void SetLsColor (TermColorIndex, TermColorIndex)
 
virtual void ShowCurrentDirectory () const
 
virtual void ListCommand (const G4String &input, const G4String &candidate="") const
 
virtual G4String GetCommandLineString (const char *msg=nullptr)=0
 
virtual void ResetTerminal ()
 

Protected Member Functions

virtual void MakePrompt (const char *msg=nullptr)
 
G4UIcommandTreeGetCommandTree (const G4String &dir) const
 
G4String GetAbsCommandDirPath (const G4String &apath) const
 
G4String GetCommandPathTail (const G4String &apath) const
 

Protected Attributes

G4String promptSetting
 
G4String promptString
 
G4int nColumn
 
G4bool lsColorFlag
 
TermColorIndex directoryColor
 
TermColorIndex commandColor
 
G4String currentCommandDir
 

Detailed Description

Definition at line 67 of file G4VUIshell.hh.

Constructor & Destructor Documentation

◆ G4VUIshell()

G4VUIshell::G4VUIshell ( const G4String & prompt = "> ")

Definition at line 44 of file G4VUIshell.cc.

45 : promptSetting(prompt),
46 promptString(""),
47 nColumn(80),
48 lsColorFlag(false),
52///////////////////////////////////////////////////////////////////
53{}
@ BLACK
Definition G4VUIshell.hh:55
TermColorIndex directoryColor
G4int nColumn
G4String currentCommandDir
TermColorIndex commandColor
G4String promptString
G4bool lsColorFlag
G4String promptSetting

◆ ~G4VUIshell()

G4VUIshell::~G4VUIshell ( )
virtualdefault

Member Function Documentation

◆ GetAbsCommandDirPath()

G4String G4VUIshell::GetAbsCommandDirPath ( const G4String & apath) const
protected

Definition at line 140 of file G4VUIshell.cc.

142{
143 if (apath.empty()) return apath; // null string
144
145 // if "apath" does not start with "/",
146 // then it is treared as relative path
147 G4String bpath = apath;
148 if (apath[(std::size_t)0] != '/') bpath = currentCommandDir + apath;
149
150 // parsing...
151 G4String absPath = "/";
152 for (std::size_t indx = 1; indx <= bpath.length() - 1;) {
153 std::size_t jslash = bpath.find('/', indx); // search index begin with "/"
154 if (indx == jslash) { // skip first '///'
155 ++indx;
156 continue;
157 }
158 if (jslash != G4String::npos) {
159 if (bpath.substr(indx, jslash - indx) == "..") { // directory up
160 if (absPath == "/") {
161 indx = jslash + 1;
162 continue;
163 }
164 if (absPath.length() >= 2) {
165 absPath.erase(absPath.length() - 1); // remove last "/"
166 auto jpre = absPath.rfind('/');
167 if (jpre != G4String::npos) absPath.erase(jpre + 1);
168 }
169 }
170 else if (bpath.substr(indx, jslash - indx) == ".") { // nothing to do
171 }
172 else { // add
173 if (jslash != indx || bpath[(G4int)indx] != '/') // truncate "////"
174 absPath += bpath.substr(indx, jslash - indx + 1);
175 // better to be check directory existence. (it costs!)
176 }
177 indx = jslash + 1;
178 }
179 else { // directory ONLY (ignore non-"/" terminated string)
180 break;
181 }
182 }
183
184 return absPath;
185}
int G4int
Definition G4Types.hh:85

Referenced by G4UItcsh::CompleteCommand(), and G4UItcsh::ListMatchedCommand().

◆ GetCommandLineString()

virtual G4String G4VUIshell::GetCommandLineString ( const char * msg = nullptr)
pure virtual

Implemented in G4UIcsh, and G4UItcsh.

◆ GetCommandPathTail()

G4String G4VUIshell::GetCommandPathTail ( const G4String & apath) const
protected

Definition at line 188 of file G4VUIshell.cc.

190{ // xxx/xxx/zzz -> zzz, trancate /// -> /
191 if (apath.empty()) return apath;
192
193 auto lstr = (G4int)apath.length();
194
195 // for trancating "/"
196 G4bool Qsla = false;
197 if (apath[lstr - 1] == '/') Qsla = true;
198
199 // searching last '/' from tail
200 G4int indx = -1;
201 for (G4int i = lstr - 1; i >= 0; --i) {
202 if (Qsla && apath[i] != '/') Qsla = false; // break "/" flag!!
203 if (apath[i] == '/' && ! Qsla) {
204 indx = i;
205 break;
206 }
207 }
208
209 if (indx == -1) return apath; // not found
210
211 if (indx == 0 && lstr == 1) { // "/"
212 G4String nullStr;
213 return nullStr;
214 }
215 // G4String newPath= apath(indx+1,lstr-indx-1);
216 G4String newPath = apath;
217 newPath = newPath.substr(indx + 1, lstr - indx - 1);
218 return newPath;
219}
bool G4bool
Definition G4Types.hh:86

Referenced by G4UItcsh::CompleteCommand().

◆ GetCommandTree()

G4UIcommandTree * G4VUIshell::GetCommandTree ( const G4String & dir) const
protected

Definition at line 113 of file G4VUIshell.cc.

115{
117
118 G4UIcommandTree* cmdTree = UI->GetTree(); // root tree
119
120 G4String absPath = GetAbsCommandDirPath(G4StrUtil::strip_copy(input));
121
122 // parsing absolute path ...
123 if (absPath.length() == 0) return nullptr;
124 if (absPath[G4int(absPath.length() - 1)] != '/') return nullptr; // error??
125 if (absPath == "/") return cmdTree;
126
127 for (std::size_t indx = 1; indx < absPath.length() - 1;) {
128 std::size_t jslash = absPath.find('/', indx); // search index begin with "/"
129 if (jslash != G4String::npos) {
130 if (cmdTree != nullptr) cmdTree = cmdTree->GetTree(G4String(absPath.substr(0, jslash + 1)));
131 }
132 indx = jslash + 1;
133 }
134
135 if (cmdTree == nullptr) return nullptr;
136 return cmdTree;
137}
G4UIcommandTree * GetTree(G4int i)
G4UIcommandTree * GetTree() const
static G4UImanager * GetUIpointer()
G4String GetAbsCommandDirPath(const G4String &apath) const

Referenced by G4UItcsh::CompleteCommand().

◆ ListCommand()

void G4VUIshell::ListCommand ( const G4String & input,
const G4String & candidate = "" ) const
virtual

Definition at line 225 of file G4VUIshell.cc.

227{
228 // specified directpry
229 G4String input = G4StrUtil::strip_copy(dir);
230
231 // command tree of "user specified directory"
233 G4String vcmd;
234
235 auto len = (G4int)input.length();
236 if (! input.empty()) {
237 G4int indx = -1;
238 for (G4int i = len - 1; i >= 0; --i) { // search last '/'
239 if (input[i] == '/') {
240 indx = i;
241 break;
242 }
243 }
244 // get abs. path
245 if (indx != -1) vpath = GetAbsCommandDirPath(input.substr(0, indx + 1));
246 if (indx != 0 || len != 1) vcmd = input.substr(indx + 1, len - indx - 1); // care for "/"
247 }
248
249 // check "vcmd" is directory?
250 G4String inputpath = vpath + vcmd;
251 if (! vcmd.empty()) {
252 G4String tmpstr = inputpath + "/";
253 if (GetCommandTree(tmpstr) != nullptr) {
254 vpath = tmpstr;
255 vcmd = "";
256 }
257 }
258
259 // check "vpath" directory exists?
260 G4UIcommandTree* atree = GetCommandTree(vpath);
261 if (atree == nullptr) {
262 G4cout << "<" << input << ">: No such directory" << G4endl;
263 return;
264 }
265
266 // list matched directories/commands
267 G4String stream;
268 G4bool isMatch = false;
269
270 G4int Ndir = atree->GetTreeEntry();
271 G4int Ncmd = atree->GetCommandEntry();
272 if (Ndir == 0 && Ncmd == 0) return; // no contents
273
274 // directory ...
275 for (G4int idir = 1; idir <= Ndir; idir++) {
276 if (idir == 1 && lsColorFlag) stream += TermColorString[directoryColor];
277 G4String fpdir = atree->GetTree(idir)->GetPathName();
278 // matching test
279 if (candidate.empty()) { // list all
280 if (vcmd.empty() || fpdir == inputpath) {
281 stream += GetCommandPathTail(fpdir);
282 stream += " ";
283 isMatch = true;
284 }
285 }
286 else { // list only matched with candidate
287 if (fpdir.find(candidate, 0) == 0) {
288 stream += GetCommandPathTail(fpdir);
289 stream += " ";
290 }
291 }
292 }
293
294 // command ...
295 for (G4int icmd = 1; icmd <= Ncmd; icmd++) {
296 if (icmd == 1 && lsColorFlag) stream += TermColorString[commandColor];
297 G4String fpcmd = atree->GetPathName() + atree->GetCommand(icmd)->GetCommandName();
298 // matching test
299 if (candidate.empty()) { // list all
300 if (vcmd.empty() || fpcmd == inputpath) {
301 stream += GetCommandPathTail(fpcmd);
302 stream += "* ";
303 isMatch = true;
304 }
305 }
306 else { // list only matched with candidate
307 if (fpcmd.find(candidate, 0) == 0) {
308 stream += GetCommandPathTail(fpcmd);
309 stream += "* ";
310 }
311 }
312 }
313
314 // waring : not matched
315 if (! isMatch && candidate.empty())
316 G4cout << "<" << input << ">: No such directory or command" << std::flush;
317
318 // display
319 G4UIArrayString arrayString(stream);
320 arrayString.Show(nColumn);
321}
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
G4int GetCommandEntry() const
G4UIcommand * GetCommand(G4int i)
const G4String & GetPathName() const
G4int GetTreeEntry() const
const G4String & GetCommandName() const
G4UIcommandTree * GetCommandTree(const G4String &dir) const
G4String GetCommandPathTail(const G4String &apath) const

Referenced by G4UItcsh::ListMatchedCommand().

◆ MakePrompt()

void G4VUIshell::MakePrompt ( const char * msg = nullptr)
protectedvirtual

Reimplemented in G4UItcsh.

Definition at line 60 of file G4VUIshell.cc.

62{
63 if (promptSetting.length() <= 1) {
65 return;
66 }
67
68 promptString = "";
69 G4int i;
70 for (i = 0; i < (G4int)promptSetting.length() - 1; ++i) {
71 if (promptSetting[i] == '%') {
72 switch (promptSetting[i + 1]) {
73 case 's': // current application status
74 {
75 G4String stateStr;
76 if (msg != nullptr) {
77 stateStr = msg;
78 }
79 else {
81 stateStr = statM->GetStateString(statM->GetCurrentState());
82 }
83 promptString.append(stateStr);
84 i++;
85 } break;
86 case '/': // current working directory
88 i++;
89 break;
90 default:
92 break;
93 }
94 }
95 else {
97 }
98 }
99
100 // append last chaacter
101 if (i == G4int(promptSetting.length()) - 1) promptString += promptSetting[i];
102}
const G4ApplicationState & GetCurrentState() const
G4String GetStateString(const G4ApplicationState &aState) const
static G4StateManager * GetStateManager()

Referenced by G4UIcsh::GetCommandLineString().

◆ ResetTerminal()

void G4VUIshell::ResetTerminal ( )
virtual

Reimplemented in G4UItcsh.

Definition at line 105 of file G4VUIshell.cc.

107{}

◆ SetCurrentDirectory()

void G4VUIshell::SetCurrentDirectory ( const G4String & ccd)
inline

Definition at line 123 of file G4VUIshell.hh.

123{ currentCommandDir = dir; }

◆ SetLsColor()

void G4VUIshell::SetLsColor ( TermColorIndex ,
TermColorIndex  )
inlinevirtual

Reimplemented in G4UItcsh.

Definition at line 125 of file G4VUIshell.hh.

125{}

Referenced by G4UIExecutive::SetLsColor().

◆ SetNColumn()

void G4VUIshell::SetNColumn ( G4int ncol)
inline

Definition at line 119 of file G4VUIshell.hh.

119{ nColumn = ncol; }

◆ SetPrompt()

void G4VUIshell::SetPrompt ( const G4String & prompt)
inline

Definition at line 121 of file G4VUIshell.hh.

121{ promptSetting = prompt; }

Referenced by G4UIExecutive::SetPrompt(), and G4UIterminal::SetPrompt().

◆ ShowCurrentDirectory()

void G4VUIshell::ShowCurrentDirectory ( ) const
inlinevirtual

Definition at line 127 of file G4VUIshell.hh.

Member Data Documentation

◆ commandColor

TermColorIndex G4VUIshell::commandColor
protected

Definition at line 110 of file G4VUIshell.hh.

Referenced by G4UItcsh::SetLsColor().

◆ currentCommandDir

◆ directoryColor

TermColorIndex G4VUIshell::directoryColor
protected

Definition at line 109 of file G4VUIshell.hh.

Referenced by G4UItcsh::SetLsColor().

◆ lsColorFlag

G4bool G4VUIshell::lsColorFlag
protected

Definition at line 108 of file G4VUIshell.hh.

Referenced by G4UItcsh::SetLsColor().

◆ nColumn

G4int G4VUIshell::nColumn
protected

Definition at line 104 of file G4VUIshell.hh.

Referenced by SetNColumn().

◆ promptSetting

G4String G4VUIshell::promptSetting
protected

Definition at line 102 of file G4VUIshell.hh.

Referenced by G4UItcsh::MakePrompt(), MakePrompt(), and SetPrompt().

◆ promptString


The documentation for this class was generated from the following files: