FACT++  1.0
int Readline::Execute ( const std::string &  fname,
const std::map< std::string, std::string > &  args = std::map<std::string,std::string>() 
)

Executes commands read from an ascii file as they were typed in the console. Empty lines and lines beginning with # are ignored.

Parameters
fnameFilename of file to read
argsArguments to be passed to the script. A search and replace will be done for ${arg}
Returns
-1 if the file couldn't be read and the number of commands for which Process() was callled otherwise

Definition at line 1446 of file Readline.cc.

References buffer, fCommandLog, fLabel, fScript, fScriptDepth, fSection, fStopScript, IsStopped(), ProcessLine(), SetSection(), Stop(), and Tools::Trim().

Referenced by GetUpdatePrompt(), and Process().

1447 {
1448  // this could do the same:
1449  // rl_instream = fopen(str.c_str(), "r");
1450 
1451  if (IsStopped())
1452  return 0;
1453 
1454  string name = Tools::Trim(fname);
1455  fScript = name;
1456 
1457  fSection = -3;
1458  SetSection(-3);
1459  fLabel = -1;
1460 
1461  const size_t p = name.find_last_of(':');
1462  if (p!=string::npos)
1463  {
1464  fLabel = atoi(name.substr(p+1).c_str());
1465  name = name.substr(0, p);
1466  }
1467 
1468  ifstream fin(name.c_str());
1469  if (!fin)
1470  {
1471  fSection = -4;
1472  SetSection(-4);
1473  return -1;
1474  }
1475 
1476  if (fScriptDepth++==0)
1477  fStopScript = false;
1478 
1479  fCommandLog << "# " << Time() << " - " << name << " (START[" << fScriptDepth<< "]";
1480  if (fLabel>=0)
1481  fCommandLog << ':' << fLabel;
1482  fCommandLog << ")" << endl;
1483 
1484  fSection = -1;
1485  SetSection(-1);
1486 
1487  int rc = 0;
1488 
1489  string buffer;
1490  while (getline(fin, buffer, '\n') && !fStopScript)
1491  {
1492  buffer = Tools::Trim(buffer);
1493  if (buffer.empty())
1494  continue;
1495 
1496  rc++;
1497 
1498  if (buffer=="quit" || buffer==".q")
1499  {
1500  Stop();
1501  break;
1502  }
1503 
1504  // find and replace arguments
1505  for (auto it=args.begin(); it!=args.end(); it++)
1506  {
1507  const string find = "${"+it->first+"}";
1508  for (size_t pos=0; (pos=buffer.find(find, pos))!=string::npos; pos+=find.length())
1509  buffer.replace(pos, find.size(), it->second);
1510  }
1511 
1512  // process line
1513  ProcessLine(buffer);
1514  }
1515 
1516  fCommandLog << "# " << Time() << " - " << name << " (FINISHED[" << fScriptDepth<< "])" << endl;
1517 
1518  if (--fScriptDepth==0)
1519  fStopScript = false;
1520 
1521  fLabel = -1;
1522  fSection = -4;
1523  SetSection(-4);
1524 
1525  return rc;
1526 }
static int fScriptDepth
Definition: Readline.h:32
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
std::ofstream fCommandLog
Maximum number of lines in the history file.
Definition: Readline.h:25
static void Stop()
Definition: Readline.cc:1534
virtual void SetSection(int)
Definition: Readline.h:68
int fSection
Definition: Readline.h:30
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
static std::string fScript
Definition: Readline.h:48
void ProcessLine(const std::string &str)
Definition: Readline.cc:1370
std::string Trim(const std::string &str)
Definition: tools.cc:68
bool IsStopped() const
Definition: Readline.cc:1548
int fLabel
Definition: Readline.h:31
static bool fStopScript
Definition: Readline.h:33

+ Here is the call graph for this function:

+ Here is the caller graph for this function: