LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2009, 03:41 PM   #1
xtothat
Member
 
Registered: Sep 2008
Location: Middle of Nowhere, England
Distribution: Slackware 14.1, Ubuntu 13.10
Posts: 39

Rep: Reputation: 15
[C++] Help simplifying massive nested conditional.


Hi, I'm fairly new to C++, and I'm still learning the ropes. My problem is I'm building a little program to print to stdout values I've obtained through string manipulation of iwconfig output. The problem is that in order to make sure I work around some odd behaviour which I've seen I've had to do 5 separate "if file exists" checks with the following code:

Code:
if(! FileExists("/sys/class/net/wlan0"))
{
  if(! FileExists("/sys/class/net/wlan1"))
  {
    if(! FileExists("/sys/class/net/wlan2"))
    {
      if(! FileExists("/sys/class/net/wlan3"))
      {
        if(! FileExists("/sys/class/net/wlan4"))
        {
          if(! FileExists("/sys/class/net/ath0"))
          {
            cout << "ESSID: NULL" << endl;
            exit(0);
          }
        }
      }
    }
  }
}
As you can see, this is a godawful mess, and I was just wondering if anybody had any ideas on clearing it up. I'm using the following to check for the files' existence. Something like being able to use wildcards in the FileExists function is exactly what I'm looking for.

Code:
bool FileExists(string strFilename)
{
  struct stat stFileInfo;
  bool blnReturn;
  int intStat;

  // Try to get file attributes
  intStat = stat(strFilename.c_str(), &stFileInfo);
  if(intStat == 0)
  {
    // File exists because we got the attributes
    blnReturn = true;
  }
  else
  {
    blnReturn = false;
  }

  return(blnReturn);
}
Anyway... Thanks for any help in advance!

X-T
 
Old 03-10-2009, 04:25 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Something like this perhaps:
Code:
static char *lan_list[] = {
  "/sys/class/net/wlan0",
  "/sys/class/net/wlan1",
  "/sys/class/net/wlan2",
  "/sys/class/net/wlan3",
  "/sys/class/net/wlan4",
  "/sys/class/net/ath0",
  NULL
};

bool
check_for_lan ()
{
  for (char **s = lan_list; (*s); s++)
  {
    if (FileExists (*s))
    {
      cout << "Success: " << *s << endl;
      return true;
    }
  }
  cout << "ESSID: NULL" << endl;
  return false;
}
The above is completely untested pseudo-code, but you get the general idea...

Last edited by paulsm4; 03-10-2009 at 04:26 PM.
 
Old 03-10-2009, 04:28 PM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
There's probably many ways to simplify your task. Here's one way:
Code:
#include <string>
#include <vector>
#include <iostream>
#include <cstdlib>

bool FileExists(const std::string& devPath)
{
  return false;
}

int main()
{
  const std::string devs[] = { "wlan0", "wlan1", "wlan2", "wlan4", "ath0"};
  std::vector<std::string> devices(devs, devs + sizeof(devs)/sizeof(std::string));
  bool  avail = true;

  for (size_t i = 0; i < devices.size(); ++i)
  {
    avail &= FileExists(std::string("/sys/class/net" + devices[i]));
  }
  if (!avail)
  {
     std::cout << "ESSID: NULL" << std::endl;
     exit(0);
  }
}
Basically, I created a "list" of the devices of interest.
 
Old 03-10-2009, 04:34 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Quote:
Originally Posted by xtothat View Post
being able to use wildcards in the FileExists function is exactly what I'm looking for.
I think the earlier responders missed that in your post.

That's what you want, so that is what you should have asked for clearly, instead of burying it in a post that seems to ask something else.

I've done that (wild card in a similar FileExists function), but long ago and don't recall even the function name to search for. But I don't think it's hard. Ask clearly and I think someone else will remember the answer.
 
Old 03-10-2009, 04:53 PM   #5
xtothat
Member
 
Registered: Sep 2008
Location: Middle of Nowhere, England
Distribution: Slackware 14.1, Ubuntu 13.10
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks for all your answers. I think I'll probably go with the first method, as to me, it seems clearer of the purpose (but I am a total n00b!).

The wildcard thing was just an idea I had while writing the post, so no real bother on it. I'll not lose hope of finding a way to do it though.

Thanks again y'all!

X-T
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help simplifying a stack calculator program darkangel29 Programming 0 10-14-2008 11:52 AM
simplifying wine Four Linux - Newbie 6 12-28-2005 05:02 PM
Simplifying guest-login - usability kiwibird Linux - General 1 07-24-2005 09:12 PM
Adding only new text/simplifying lists invisible_ink Linux - Newbie 1 11-24-2004 02:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration