LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to compare between enum elements in same struct? (https://www.linuxquestions.org/questions/programming-9/how-to-compare-between-enum-elements-in-same-struct-4175611629/)

BW-userx 08-09-2017 02:00 PM

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!

astrogeek 08-09-2017 03:05 PM

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;
...
}


BW-userx 08-09-2017 06:25 PM

Quote:

Originally Posted by astrogeek (Post 5746472)
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.

astrogeek 08-09-2017 07:48 PM

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.


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