shell script style in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rudra

    shell script style in C++

    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*|athl on*|i586-i686)
    return 0 ;;
    *) return 1 ;;
    instead of those casceding if-else?



  • Ian Collins

    #2
    Re: shell script style in C++

    rudra wrote:
    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?

    --
    Ian Collins.

    Comment

    • Mirco Wahab

      #3
      Re: shell script style in C++

      rudra wrote:
      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*|athl on*|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.*|ath lon.*|i586-i686");
      return regex_match(hos t_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.

      Comment

      • Daniel Kraft

        #4
        Re: shell script style in C++

        rudra wrote:
        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[i], strlen (ACCEPTABLE[i]) == 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

        --
        Done: Arc-Bar-Sam-Val-Wiz, Dwa-Elf-Gno-Hum-Orc, Law-Neu-Cha, Fem-Mal
        To go: Cav-Hea-Kni-Mon-Pri-Ran-Rog-Tou

        Comment

        • Juha Nieminen

          #5
          Re: shell script style in C++

          rudra wrote:
          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*|athl on*|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(sysp tr, values[i].str, values[i].length))
          {
          t = 1;
          break;
          }

          Comment

          Working...