|
Introduction
This page presents the detailed C/C++ program/script for operation of eValid in
Interactive Mode.
This example follows the outline given in
the example interactive mode structure.
C/C++ Code
Here is the C/C++ code that implements the interactive mode structure.
// Interactive Mode Example.cpp
//
// Notes:
// - An ascii file called "commands.txt" must exist
// in the same path as the sample program and must
// contain a list of eValid script commands to execute.
// - This sample program must have permission to
// create and write to the file "C:\interactive_mode.evs".
// - This sample program performs no error checking.
//
#include "stdafx.h"
#include <windows.h>
#include <fstream>
#include <string>
#define EVALID_PATH "evalid.exe"
#define SCRIPT_FILE "c:\\interactive_mode.evs"
#define COMMAND_FILE "commands.txt"
using namespace std;
void runEvalid()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
string commandLine;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
commandLine = EVALID_PATH;
commandLine += " -i \"";
commandLine += SCRIPT_FILE;
commandLine += "\"";
LPTSTR lpszCommand = new TCHAR[commandLine.length() + 1];
strcpy(lpszCommand, commandLine.c_str());
CreateProcess(NULL, lpszCommand, NULL, NULL,
FALSE, 0, NULL, NULL, &si, &pi);
delete [] lpszCommand;
}
// Writes the line(s) to the evalid script file.
//
// The function waits until script file is empty
// (zero file size) before the string is written.
// Set truncate to true to override this constraint.
//
// This function makes attempts to write to the
// script file indefinitely until the script
// file has been write-unlocked.
void writeScriptLine(const std::string& scriptLine, bool truncate = false)
{
for (bool done = false; !done; Sleep(10))
{
ios_base::open_mode mode =
ios_base::out | (truncate ? 0 : ios_base::app);
ofstream out;
out.open(SCRIPT_FILE, mode);
if (!out.is_open())
continue;
// Current file position is zero
// if appending from an empty file.
out.seekp(0, ios_base::end);
int pos = out.tellp();
if (pos == 0)
{
out << scriptLine << "\n";
done = true;
}
out.close();
}
}
int main(int argc, char* argv[])
{
ifstream in;
string scriptLine;
// Gonna start eValid with a blank script file
writeScriptLine(" ", true);
// Launch eValid in Interactive Mode
runEvalid();
// Open command file for reading
in.open(COMMAND_FILE, ios_base::in);
// Process command file, reading one line from the file
// at a time and writing it to the eValid script file
while (!in.eof())
{
// Read one script line
getline(in, scriptLine, '\n');
// Write eValid command into script file
writeScriptLine(scriptLine);
// Analyze resulting logfiles (optional)
}
// Close file
in.close();
// Close eValid with the ExitNow command
writeScriptLine("ExitNow");
return 0;
} |