shell script style in C++

R

rudra

dear friends,
in my code, i am testing the machine system using popen command.
but for testing , i have to write a casceding if-else-if list as

if (strncmp(sysptr, "i686",4)==0)
{
t=1;
}
else {
if (strncmp(sysptr, "i386",4)==0) {
t=1;
}
else {
t=0;
}
}

is it possible to test it as it is done in configure file? eg:

case "$host_arch" in
i[3-9]86|x86|x86pc|k5|k6|k6-2|k6-3|pentium*|athlon*|i586-i686)
return 0 ;;
*) return 1 ;;
instead of those casceding if-else?
 
I

Ian Collins

rudra said:
dear friends,
in my code, i am testing the machine system using popen command.
but for testing , i have to write a casceding if-else-if list as
Why don't you wrap the system command you call in a script that returns
something usable by your application?
 
M

Mirco Wahab

rudra said:
in my code, i am testing the machine system using popen command.
but for testing , i have to write a casceding if-else-if list as

if (strncmp(sysptr, "i686",4)==0)
{
t=1;
}
else {
if (strncmp(sysptr, "i386",4)==0) {
t=1;
}
else {
t=0;
}
}

is it possible to test it as it is done in configure file? eg:

case "$host_arch" in
i[3-9]86|x86|x86pc|k5|k6|k6-2|k6-3|pentium*|athlon*|i586-i686)
return 0 ;;
*) return 1 ;;
instead of those casceding if-else?

This is a regular expression. To get it like that,
use boost_regex:

...
#include <boost/regex.hpp>
...

int find_host_arch_id(const std::string& host_arch)
{
using namespace boost;
smatch m;
static regex r("i[3-9]86|x86|x86pc|k5|k6|k6-2|k6-3|pentium.*|athlon.*|i586-i686");
return regex_match(host_arch, m, r) ? 1 : 0;
}
...

This can be used then like:

...

std::string host_arch("i286");

std::cout << host_arch << ": "
<< find_host_arch_id(host_arch)
<< std::endl;
...

Regards

M.
 
D

Daniel Kraft

rudra said:
dear friends,
in my code, i am testing the machine system using popen command.
but for testing , i have to write a casceding if-else-if list as

if (strncmp(sysptr, "i686",4)==0)
{
t=1;
}
else {
if (strncmp(sysptr, "i386",4)==0) {
t=1;
}
else {
t=0;
}
}

You could either do it in one large if OR'ed together:

if (strncmp (sysptr, "i686", 4) == 0 || strncmp (sysptr, "i386", 4) == 0
|| ...)
t = 1;
else
t = 0;

Or what's about this one:

{
static const char* ACCEPTABLE[] = {"i686", "i386", ...};
unsigned i;

t = 0;
for (i = 0;
i != sizeof (ACCEPTABLE) / sizeof (ACCEPTABLE[0]) && t == 0;
++i)
if (strncmp (sysptr, ACCEPTABLE, strlen (ACCEPTABLE) == 0)
t = 1;
}

Where you could, if performance matters, also replace the strlen-call
either my a constant "4" if that's the length of all platforms testet or
use a second array with the lengths or something like that.

Code is not testet, but you should get the idea.

Daniel
 
J

Juha Nieminen

rudra said:
is it possible to test it as it is done in configure file? eg:

case "$host_arch" in
i[3-9]86|x86|x86pc|k5|k6|k6-2|k6-3|pentium*|athlon*|i586-i686)
return 0 ;;
*) return 1 ;;
instead of those casceding if-else?

The more convoluted your regexp is, the harder it becomes to perform
the check in any other way than using a third-party regexp library.

If you don't want to use a third-party regexp library, what you can do
is to create a simple array with your strings and then perform the
comparisons in a loop. This will be more limited than the regexp, but at
least you don't have to write an if block for every possible value.
Something like:

struct Value
{
const char* const str;
const int length;
};

const Value values[] = { { "i386", 4 }, { "k6-2", 4 }, ... };
const int valuesAmount = sizeof(values)/sizeof(values[0]);

for(int i = 0; i < valuesAmount; ++i)
if(strncmp(sysptr, values.str, values.length))
{
t = 1;
break;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,475
Latest member
NovellaSce

Latest Threads

Top