LinuxQuestions.org
Help answer threads with 0 replies.
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 08-09-2017, 03:00 PM   #1
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,737

Rep: Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877
how to compare between enum elements in same struct?


Ok I am in error coding mode (for a bit) and I need to know how to get a comparison from a typedef enum.

Code:
typedef enum   {

// whatever size the image is display it modes. 1  2       3      4         5         6        7      8      9       10
	IMAGE_MODE_DUMMY_ELEMENT = 0, FULLSCREEN, FILL, CENTER,  FLIPIMGD, FLIPIMGH, FLIPIMGV, TILE, TILEV, TILEH, TILEHV,
 // modified size from original size image modes
 //   11        12       13        14         15     16      17      18
   DCENTER, DFLIPIMGH, DFLIPIMGV, DFLIPIMGD, DTILE, DTILEH, DTILEV, DTILEHV
} ImageMode;
off the switch to set options
Code:
switch (c)
{
  case 'F':
   opts.mode = FULLSCREEN;
   break;
  case 'f':
    opts.mode = FILL;
	break;
   case 'C':
      opts.mode = CENTER;
	break; 
    case 'c':
	opts.mode = DCENTER;
	break;
...
}
if a user does something silly like this on the command line.
Code:
programName -C -c -g 300x200 filename.jpg
it will go through because all of the requirements have been made being no-argument for -C and -c because it just sets the mode, but is a bad setting that confusing the program due to two types of settings being passed to set the image on the desktop.

I know how to get the element number but how to get the names to perhaps do a string compare, or whatever means to compare them after the (fact) switch because they are in the same struct.

because this is always going to match. (prob because its always using the last set mode to look at both sides) .. yes?
Code:
void check_options(void)
{
  if((int)opts.mode == (int)opts.mode)
    printf("you cannot have center and resize center together and 
            expect it to actually work.\n");
       exit(1);
}
I could split the typedef into two, one for original size images, and the other for resized images. then figure out the math for every stinking way that it could be matched but who wants to do what? not me!

Last edited by BW-userx; 08-09-2017 at 03:12 PM.
 
Old 08-09-2017, 04:05 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,506
Blog Entries: 6

Rep: Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421
You cannot get the name from the value in an enum directly.

When handling mutually exclusive options I typically organize things such that the first (or last) one given in input order wins and ignore others or throw up an error if necessary. If I am reading your post correctly, your code would only use the last value assigned to opts.mode if the opts struct is only actually used after the switch.

Adapting your check_options(...) example test to set, and prevent resetting the mode, something like this might do it:

Code:
void check_set_mode(int mode)
{
  if(opts.mode>0){
      printf("Attempt to reset mode! Only one of -F, -C, -c may be applied!\n");
      exit(1);
  }
  opts.mode=mode;
}
Then in the switch...

Code:
switch (c)
{
  case 'F':
    check_set_mode(FULLSCREEN);
    break;
  case 'f':
    check_set_mode(FILL);
    break;
  case 'C':
    check_set_mode(CENTER);
    break; 
  case 'c':
    check_set_mode(DCENTER);
    break;
...
}

Last edited by astrogeek; 08-09-2017 at 05:19 PM. Reason: Added example code
 
Old 08-09-2017, 07:25 PM   #3
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,737

Original Poster
Rep: Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877
Quote:
Originally Posted by astrogeek View Post
You cannot get the name from the value in an enum directly.
yeah I kind of thought it maybe the case, then again, (now that I've finally got all of the other coding done to get it working like I wanted it to, finally)

I can put my mind to this better,

there has to be a way to get the name itself out of it, I have a vague memory seeing something on that, getting the actual element name, then it is just then a matter of order - putting the set value perhaps in a temp and using that to keep the value of the first mode setting from being over written on another pass within the loop, if it loops around picks up the first -C store that value in a temp, second loop pass opts.mode gets changed to -c


Code:
first pass
int tempMode = (int)opts.mode;

if (tempMode == (int)opts.mode)
printf("no you can only pick one mode at a time yo!\n");
maybe something like that perhaps.

or maybe dropping the second mode setting, then taking that first mode setting and using that to set the image, perhaps, doing or trying what you suggested or do a little bit of rewriting - rethinking and rewriting.

time and testing will tell.
Thanks for your suggestion.

Last edited by BW-userx; 08-09-2017 at 07:29 PM.
 
Old 08-09-2017, 08:48 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,506
Blog Entries: 6

Rep: Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421Reputation: 2421
You are welcome!

If you are free to add a member to the opts struct, then one easy way to get both option names (characters) together might be...

Add a character member to the opts struct...
Code:
...
char modec;
...
Redefine the check/set function...
Code:
void check_set_mode(int mode, char opt)
{
  if(opts.mode && opts.mode!=mode){
      printf("You cannot use options %c and %c together!\n", opts.modec, opt);
      exit(1);
  }
  opts.mode=mode;
  opts.modec=opt;
}
Then in the options switch

Code:
switch (c)
{
  case 'F':
    check_set_mode(FULLSCREEN, 'F');
    break;
  case 'f':
    check_set_mode(FILL, 'f');
    break;
  case 'C':
    check_set_mode(CENTER, 'C');
    break; 
  case 'c':
    check_set_mode(DCENTER, 'c');
    break;
...
}
You could easily extend that to the full name string if you want.

Last edited by astrogeek; 08-09-2017 at 11:59 PM. Reason: Added last line, and typo...
 
  


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
[SOLVED] Question about Kernel pid_namespace - struct pid and struct upid sreeharsha.t Linux - Kernel 4 03-19-2010 05:41 AM
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 05:40 PM
Questions about enum. What would be a good use of enum? RHLinuxGUY Programming 5 06-11-2006 02:13 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 11:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 05:30 PM

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

All times are GMT -5. The time now is 07:07 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