Import Geant4 10.7.0 source tree
This commit is contained in:
+116
-104
@@ -23,198 +23,213 @@
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
// G4UIbatch
|
||||
//
|
||||
// ====================================================================
|
||||
// G4UIbatch.cc
|
||||
//
|
||||
// ====================================================================
|
||||
// Author: M.Asai, 2000
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
#include "G4UIbatch.hh"
|
||||
#include "G4UImanager.hh"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// --------------------------------------------------------------------
|
||||
static void Tokenize(const G4String& str, std::vector<G4String>& tokens)
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
{
|
||||
const char* delimiter= " ";
|
||||
const char* delimiter = " ";
|
||||
|
||||
G4String::size_type pos0= str.find_first_not_of(delimiter);
|
||||
G4String::size_type pos = str.find_first_of(delimiter, pos0);
|
||||
G4String::size_type pos0 = str.find_first_not_of(delimiter);
|
||||
G4String::size_type pos = str.find_first_of(delimiter, pos0);
|
||||
|
||||
while (pos != G4String::npos || pos0 != G4String::npos) {
|
||||
if (str[pos0] == '\"') {
|
||||
pos = str.find_first_of("\"", pos0+1);
|
||||
if(pos != G4String::npos) pos++;
|
||||
while(pos != G4String::npos || pos0 != G4String::npos)
|
||||
{
|
||||
if(str[pos0] == '\"')
|
||||
{
|
||||
pos = str.find_first_of("\"", pos0 + 1);
|
||||
if(pos != G4String::npos)
|
||||
pos++;
|
||||
}
|
||||
if (str[pos0] == '\'') {
|
||||
pos = str.find_first_of("\'", pos0+1);
|
||||
if(pos != G4String::npos) pos++;
|
||||
if(str[pos0] == '\'')
|
||||
{
|
||||
pos = str.find_first_of("\'", pos0 + 1);
|
||||
if(pos != G4String::npos)
|
||||
pos++;
|
||||
}
|
||||
|
||||
tokens.push_back(str.substr(pos0, pos-pos0));
|
||||
tokens.push_back(str.substr(pos0, pos - pos0));
|
||||
pos0 = str.find_first_not_of(delimiter, pos);
|
||||
pos = str.find_first_of(delimiter, pos0);
|
||||
pos = str.find_first_of(delimiter, pos0);
|
||||
}
|
||||
}
|
||||
|
||||
// ====================================================================
|
||||
//
|
||||
// class description
|
||||
//
|
||||
// ====================================================================
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// --------------------------------------------------------------------
|
||||
G4UIbatch::G4UIbatch(const char* fileName, G4UIsession* prevSession)
|
||||
: G4UIsession(1),
|
||||
previousSession(prevSession), isOpened(false)
|
||||
////////////////////////////////////////////////////////////////////
|
||||
: G4UIsession(1)
|
||||
, previousSession(prevSession)
|
||||
{
|
||||
macroStream.open(fileName, std::ios::in);
|
||||
if(macroStream.fail()) {
|
||||
G4cerr << "ERROR: Can not open a macro file <"
|
||||
<< fileName << ">. Set macro path with \"/control/macroPath\" if needed."
|
||||
if(macroStream.fail())
|
||||
{
|
||||
G4cerr << "ERROR: Can not open a macro file <" << fileName
|
||||
<< ">. Set macro path with \"/control/macroPath\" if needed."
|
||||
<< G4endl;
|
||||
lastRC = fParameterUnreadable;
|
||||
} else {
|
||||
isOpened= true;
|
||||
}
|
||||
else
|
||||
{
|
||||
isOpened = true;
|
||||
}
|
||||
|
||||
G4UImanager::GetUIpointer()-> SetSession(this);
|
||||
G4UImanager::GetUIpointer()->SetSession(this);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////
|
||||
// --------------------------------------------------------------------
|
||||
G4UIbatch::~G4UIbatch()
|
||||
///////////////////////
|
||||
{
|
||||
if(isOpened) macroStream.close();
|
||||
if(isOpened)
|
||||
macroStream.close();
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////
|
||||
// --------------------------------------------------------------------
|
||||
G4String G4UIbatch::ReadCommand()
|
||||
/////////////////////////////////
|
||||
{
|
||||
enum { BUFSIZE= 4096 };
|
||||
static G4ThreadLocal char *linebuf = 0 ; if (!linebuf) linebuf = new char [BUFSIZE];
|
||||
enum
|
||||
{
|
||||
BUFSIZE = 4096
|
||||
};
|
||||
static G4ThreadLocal char* linebuf = 0;
|
||||
if(!linebuf)
|
||||
linebuf = new char[BUFSIZE];
|
||||
const char ctrM = 0x0d;
|
||||
|
||||
G4String cmdtotal= "";
|
||||
G4bool qcontinued= false;
|
||||
while(macroStream.good()) {
|
||||
G4String cmdtotal = "";
|
||||
G4bool qcontinued = false;
|
||||
while(macroStream.good())
|
||||
{
|
||||
macroStream.getline(linebuf, BUFSIZE);
|
||||
|
||||
G4String cmdline(linebuf);
|
||||
|
||||
// TAB-> ' ' conversion
|
||||
str_size nb=0;
|
||||
while ((nb= cmdline.find('\t',nb)) != G4String::npos) {
|
||||
str_size nb = 0;
|
||||
while((nb = cmdline.find('\t', nb)) != G4String::npos)
|
||||
{
|
||||
cmdline.replace(nb, 1, " ");
|
||||
}
|
||||
|
||||
// strip
|
||||
cmdline= cmdline.strip(G4String::both);
|
||||
cmdline= cmdline.strip(G4String::trailing, ctrM);
|
||||
cmdline = cmdline.strip(G4String::both);
|
||||
cmdline = cmdline.strip(G4String::trailing, ctrM);
|
||||
|
||||
// skip null line if single line
|
||||
if(!qcontinued && cmdline.size()==0) continue;
|
||||
if(!qcontinued && cmdline.size() == 0)
|
||||
continue;
|
||||
|
||||
// '#' is treated as echoing something
|
||||
if(cmdline[(size_t)0]=='#') return cmdline;
|
||||
if(cmdline[(std::size_t) 0] == '#')
|
||||
return cmdline;
|
||||
|
||||
// tokenize...
|
||||
std::vector<G4String> tokens;
|
||||
Tokenize(cmdline, tokens);
|
||||
qcontinued= false;
|
||||
for (G4int i=0; i< G4int(tokens.size()); i++) {
|
||||
qcontinued = false;
|
||||
for(G4int i = 0; i < G4int(tokens.size()); ++i)
|
||||
{
|
||||
// string after '#" is ignored
|
||||
if(tokens[i][(size_t)0] == '#' ) break;
|
||||
if(tokens[i][(std::size_t) 0] == '#')
|
||||
break;
|
||||
// '\' or '_' is treated as continued line.
|
||||
if(tokens[i] == '\\' || tokens[i] == '_' ) {
|
||||
qcontinued= true;
|
||||
if(tokens[i] == '\\' || tokens[i] == '_')
|
||||
{
|
||||
qcontinued = true;
|
||||
// check nothing after line continuation character
|
||||
if( i != G4int(tokens.size())-1) {
|
||||
G4Exception("G4UIbatch::ReadCommand","UI0003",
|
||||
JustWarning,
|
||||
"unexpected character after line continuation character");
|
||||
if(i != G4int(tokens.size()) - 1)
|
||||
{
|
||||
G4Exception("G4UIbatch::ReadCommand", "UI0003", JustWarning,
|
||||
"unexpected character after line continuation character");
|
||||
}
|
||||
break; // stop parsing
|
||||
break; // stop parsing
|
||||
}
|
||||
cmdtotal+= tokens[i];
|
||||
cmdtotal+= " ";
|
||||
cmdtotal += tokens[i];
|
||||
cmdtotal += " ";
|
||||
}
|
||||
|
||||
if(qcontinued) continue; // read the next line
|
||||
if(qcontinued)
|
||||
continue; // read the next line
|
||||
|
||||
if(cmdtotal.size() != 0) break;
|
||||
if(macroStream.eof()) break;
|
||||
if(cmdtotal.size() != 0)
|
||||
break;
|
||||
if(macroStream.eof())
|
||||
break;
|
||||
}
|
||||
|
||||
// strip again
|
||||
cmdtotal= cmdtotal.strip(G4String::both);
|
||||
cmdtotal = cmdtotal.strip(G4String::both);
|
||||
|
||||
// finally,
|
||||
if(macroStream.eof() && cmdtotal.size()==0) {
|
||||
if(macroStream.eof() && cmdtotal.size() == 0)
|
||||
{
|
||||
return "exit";
|
||||
}
|
||||
|
||||
return cmdtotal;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
// --------------------------------------------------------------------
|
||||
G4int G4UIbatch::ExecCommand(const G4String& command)
|
||||
/////////////////////////////////////////////////////
|
||||
{
|
||||
G4UImanager* UI= G4UImanager::GetUIpointer();
|
||||
G4int rc= UI-> ApplyCommand(command);
|
||||
G4UImanager* UI = G4UImanager::GetUIpointer();
|
||||
G4int rc = UI->ApplyCommand(command);
|
||||
|
||||
switch(rc) {
|
||||
case fCommandSucceeded:
|
||||
break;
|
||||
case fCommandNotFound:
|
||||
G4cerr << "***** COMMAND NOT FOUND <"
|
||||
<< command << "> *****" << G4endl;
|
||||
break;
|
||||
case fIllegalApplicationState:
|
||||
G4cerr << "***** Illegal application state <"
|
||||
<< command << "> *****" << G4endl;
|
||||
break;
|
||||
default:
|
||||
G4int pn= rc%100;
|
||||
G4cerr << "***** Illegal parameter (" << pn << ") <"
|
||||
<< command << "> *****" << G4endl;
|
||||
switch(rc)
|
||||
{
|
||||
case fCommandSucceeded:
|
||||
break;
|
||||
case fCommandNotFound:
|
||||
G4cerr << "***** COMMAND NOT FOUND <" << command << "> *****" << G4endl;
|
||||
break;
|
||||
case fIllegalApplicationState:
|
||||
G4cerr << "***** Illegal application state <" << command << "> *****"
|
||||
<< G4endl;
|
||||
break;
|
||||
default:
|
||||
G4int pn = rc % 100;
|
||||
G4cerr << "***** Illegal parameter (" << pn << ") <" << command
|
||||
<< "> *****" << G4endl;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////
|
||||
G4UIsession * G4UIbatch::SessionStart()
|
||||
///////////////////////////////////////
|
||||
// --------------------------------------------------------------------
|
||||
G4UIsession* G4UIbatch::SessionStart()
|
||||
{
|
||||
if(!isOpened) return previousSession;
|
||||
if(!isOpened)
|
||||
return previousSession;
|
||||
|
||||
while(1) {
|
||||
while(1)
|
||||
{
|
||||
G4String newCommand = ReadCommand();
|
||||
|
||||
if(newCommand == "exit") {
|
||||
if(newCommand == "exit")
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// just echo something
|
||||
if( newCommand[(size_t)0] == '#') {
|
||||
if(G4UImanager::GetUIpointer()-> GetVerboseLevel()==2) {
|
||||
if(newCommand[(std::size_t) 0] == '#')
|
||||
{
|
||||
if(G4UImanager::GetUIpointer()->GetVerboseLevel() == 2)
|
||||
{
|
||||
G4cout << newCommand << G4endl;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// execute command
|
||||
G4int rc= ExecCommand(newCommand);
|
||||
if(rc != fCommandSucceeded) {
|
||||
G4int rc = ExecCommand(newCommand);
|
||||
if(rc != fCommandSucceeded)
|
||||
{
|
||||
G4cerr << G4endl << "***** Batch is interrupted!! *****" << G4endl;
|
||||
lastRC = rc;
|
||||
break;
|
||||
@@ -224,10 +239,8 @@ G4UIsession * G4UIbatch::SessionStart()
|
||||
return previousSession;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////
|
||||
// --------------------------------------------------------------------
|
||||
void G4UIbatch::PauseSessionStart(const G4String& Prompt)
|
||||
/////////////////////////////////////////////////////////
|
||||
{
|
||||
G4cout << "Pause session <" << Prompt << "> start." << G4endl;
|
||||
|
||||
@@ -235,4 +248,3 @@ void G4UIbatch::PauseSessionStart(const G4String& Prompt)
|
||||
|
||||
G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user