The routine I have got now is this one. However, it does not seem to work. Something is going wrong but cannot figure it out yet.
void Parsing::ParseCmdLine2(
const int argc,
char* argv[]) {
int i;
int StrSize; // Size of string
int EndsSize; // Size of string
List<int> Ord;
Ord += 0;
cout << "ParseCmdLine2 argv" << endl;
cout << " i = " << i << ", argv = " << argv << endl;
cout << "argc = " << argc << endl;
for (i = 1; i < argc; i++) {
// S = String(argv);
// StrSize = S.size();
cout << "i = " << i << endl;
// First character is '?', e.g. "?abcd"[0] is '?'.
if (argv[0] == '?') {
cout << "Check ?" << endl;
ifstream ifs(argv+1); // "?abcd"+1 is "abcd", so open filename "abcd".
if ( ifs.bad() ) error("Error including file " + String(argv) );
else ParseFile(ifs);
} // end of code block. ifs closes here.
// First 2 chars are '-'. A zero value indicates that the characters compared in both
// strings are all equal.
else if (strncmp(argv, "--", 2) == 0)
{
cout << "Check -- long option" << endl;
// Pointer to first occurrence of '='
// strchr("--asdf=b", '=') is "=b". If '=' is not found, function returns null pointer.
const char* After = strchr(argv, '=');
const char* Name;
// String Ends; // Make it a 'String' so we can use ToUpper.
// '=' is not found. Get user input from next argument.
if (After == NULL)
{
i++; // ++i is more efficient than i++.
if (i == argc) { // If next argument not found, quit instead of crashing.
error("Out of arguments");
continue;
}
Name = argv; // Set user input
// '=' is found.
} else {
Name = After + 1; // Set user input. "=file"+1 is "file".
}
String Ends = ToUpper(String(Name)); // Conversion of 'Name' to a 'String' so we can use ToUpper.
cout << "Name = " << Name << ", Ends = " << Ends << endl;
Index += ParseEl(Name, Ends, Ord);
}
}
Ptr = 0;
}
---------- Post updated at 06:19 PM ---------- Previous update was at 06:13 PM ----------
I think I am making some progress. Name should have the string to the left of '=' whereas Ends should have the string to the right of '='.
The following is the original code:
void Parsing::ParseCmd(
const int argc,
char* argv[]) {
int i;
int poseq;
String S;
String Name;
String File;
String Ends;
List<int> Ord; Ord += 0;
ifstream ifs;
for (i = 1; i < argc; i++) {
S = String(argv);
if (S[0] == '?') {
File = S.ToEnd(1);
ifs.open((char *)File);
if (ifs.bad()) { error("Error including file"); }
ParseFile(ifs);
ifs.close();
} else {
Search(S, "=", poseq);
Name = ToUpper( S.Substr(0, poseq-1) );
Ends = S.ToEnd( poseq + 1 );
Index += ParseEl(Name, Ends, Ord);
}
}
Ptr = 0;
}
---------- Post updated at 06:34 PM ---------- Previous update was at 06:19 PM ----------
We are not capturing the Name.
If the user parses vmod=jcdint-z30.cmd, I want to have Name=VMOD and Ends=jcdint-z30.cmd
---------- Post updated at 07:02 PM ---------- Previous update was at 06:34 PM ----------
Some more progress. I now have the code
void Parsing::ParseCmdLine2(
const int argc,
char* argv[]) {
int i;
int StrSize; // Size of string
int EndsSize; // Size of string
List<int> Ord;
Ord += 0;
cout << "ParseCmdLine2 argv" << endl;
cout << " i = " << i << ", argv = " << argv << endl;
cout << "argc = " << argc << endl;
for (i = 1; i < argc; i++) {
// S = String(argv);
// StrSize = S.size();
cout << "i = " << i << endl;
cout << "argv = " << argv << endl;
cout << "strncmp = " << strncmp(argv, "--", 2) << endl;
// First character is '?', e.g. "?abcd"[0] is '?'.
if (argv[0] == '?') {
cout << "Check ?" << endl;
ifstream ifs(argv+1); // "?abcd"+1 is "abcd", so open filename "abcd".
if ( ifs.bad() ) error("Error including file " + String(argv) );
else ParseFile(ifs);
} // end of code block. ifs closes here.
// First 2 chars are '-'. A zero value indicates that the characters compared in both
// strings are all equal.
else if (strncmp(argv, "--", 2) == 0)
{
cout << "Check long option" << endl;
// Pointer to first occurrence of '='
// strchr("--asdf=b", '=') is "=b". If '=' is not found, function returns null pointer.
const char* After = strchr(argv, '=');
const char* optName;
const char* Ends;
String Name; // Make it a 'String' so we can use ToUpper.
// '=' is not found. Get user input from next argument.
if (After == NULL)
{
i++; // ++i is more efficient than i++.
if (i == argc) { // If next argument not found, quit instead of crashing.
error("Out of arguments");
continue;
}
Ends = argv; // Set user input
// '=' is found.
} else {
Ends = After + 1; // Set user input. "=file"+1 is "file".
}
Name = "VMOD";
// String optName = ToUpper(); // Conversion of 'Name' to a 'String' so we can use ToUpper.
cout << "Name = " << Name << ", Ends = " << Ends << endl << endl;
// Index += ParseEl(Name, Ends, Ord);
}
}
Ptr = 0;
}
After passing
/home/chrisd/tatsh/trunk/hstmy/bin/prog/raytrac --vmod=jcdint-z30.cmd --srfile=jcdint.sr --rcfile=jcdint.rc --phases=P --level=twopt --format="X T" --dtau=0.1 mdacc=0.3 --mindist=0.03 --maxitertp=25 --ray=jcdint-z30.ry --out=jcdint-z30.xt --vrb=medium | & tee jcdint-z30-raytrac.log
I get the following out put
ParseCmdLine2 argv
i = 0, argv = /home/chrisd/tatsh/trunk/hstmy/bin/prog/raytrac
argc = 14
i = 1
argv = --vmod=jcdint-z30.cmd
strncmp = 0
Check long option
Name = VMOD, Ends = jcdint-z30.cmd
i = 2
argv = --srfile=jcdint.sr
strncmp = 0
Check long option
Name = VMOD, Ends = jcdint.sr
i = 3
argv = --rcfile=jcdint.rc
strncmp = 0
Check long option
Name = VMOD, Ends = jcdint.rc
i = 4
argv = --phases=P
strncmp = 0
Check long option
Name = VMOD, Ends = P
i = 5
argv = --level=twopt
strncmp = 0
Check long option
Name = VMOD, Ends = twopt
i = 6
argv = --format=X T
strncmp = 0
Check long option
Name = VMOD, Ends = X T
i = 7
argv = --dtau=0.1
strncmp = 0
Check long option
Name = VMOD, Ends = 0.1
i = 8
argv = mdacc=0.3
strncmp = 1
i = 9
argv = --mindist=0.03
strncmp = 0
Check long option
Name = VMOD, Ends = 0.03
i = 10
argv = --maxitertp=25
strncmp = 0
Check long option
Name = VMOD, Ends = 25
i = 11
argv = --ray=jcdint-z30.ry
strncmp = 0
Check long option
Name = VMOD, Ends = jcdint-z30.ry
i = 12
argv = --out=jcdint-z30.xt
strncmp = 0
Check long option
Name = VMOD, Ends = jcdint-z30.xt
i = 13
argv = --vrb=medium
strncmp = 0
Check long option
Name = VMOD, Ends = medium
What I need to do is get the Name to be VMOD, SRFILE, RCFILE, ...