LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to display (printf) selective parameter based on conditions (https://www.linuxquestions.org/questions/programming-9/how-to-display-printf-selective-parameter-based-on-conditions-747180/)

eryn 08-12-2009 11:46 PM

How to display (printf) selective parameter based on conditions
 
Hi,

I am working on messaging quite a large amount of parameters.
I would like to simplify it and only display those parameters that are in active status.

For example,

I have 10 switches. (A1, A2, A3, ... ..., A9, A10)
In the message, I only display the label of the switches that are active and its status code (0-inactive, 1-active)

e.g. If only switch 1 and switch 8 is active.
The message I would like to display is as following:
A1_1, A8_1

The other will not appear until they are activated.

How can I do this in the function that is specially created to display these parameters. Other function will pass the parameter value to this function.

Currently, I only manage to display all the parameters which is a large amount of parameters and is not good to display all.


Thank you in advance.

chrism01 08-13-2009 02:08 AM

So, which lang is this? Also, please show us the relevant code you've done so far.

eryn 08-13-2009 03:56 AM

I am programming for limit switches.
By default, value of limit switches is 0. When it is activated, the value will change to 1.

Let say I have 10 limit switches right now. I denoted them as following:
Switch 1 S1_
Switch 2 S2_
Switch 3 S3_
Switch 4 S4_
Switch 5 S5_
Switch 6 S6_
Switch 7 S7_
Switch 8 S8_
Switch 9 S9_
Switch 10 S10_

After the labeling is to show the status 0 or 1.
However, I only want the whole thing appear when that switch's status is 1.
I means that when Switch 1, 5 and 8 is activated, the display screen will only display as following:
S1_1, S5_1, S8_1

Currently, all the switches will appear no matter what is the status that is
S1_1, S2_0, S3_0, S4_0, S5_1, S6_0, S7_0, S8_1, S9_0, S10_0

My example display code for this is

strcat(buffer,",S3_");
sprintf(cS,"%d",S3);
strcat(buffer,cS);

and this is repeated for every switch.

I thought of using switch and case. However, I am stucked halfway.

pgpython 08-13-2009 06:33 AM

Personally i feel have 10 switches and 10 variables makes it more complicated than it needs to be.

my advice would be instead have 2 variables:
one a switch to be bit wised and'ed on.
the other either a *char[] or **char depending on what you need

Then you can loop through find out if a switch is set by doing bitwise and, then concatenate the string if it is set.

Is there any particular reason why you can't do this

eryn 08-25-2009 02:44 AM

I am still too new to Linux

I do not know how to do advanced code.

pgpython 08-25-2009 08:02 AM

Ok the below code is probably not perfect but it should give you an idea. That what you do is have you have an char** and an integer value. If you want to know wether a switch you do bitwise and. So first value is 2**0, second is 2**1 and so forth. If its set the bitwise and returns 1 therefore you can operate on it. I decided for this to format the string so I copied it into a new char** holding just the values I need. Then i could process it at the end making the code simpler and easier to reuse. That would be my advice anyway if you can do it as then you wouldn't have 20 variables to worry about

Code:

char* joinString(int size, char **data, char delimiter[]){
      int char_length = size;
      int forloop;
      char *joined_string;
      for (forloop=0; forloop < size; forloop++){
          char_length = char_length + strlen(data[forloop]);
      }   
      joined_string = (char *) calloc(sizeof(char), char_length);
      for (forloop=0; forloop < size; forloop++){
            sprintf(joined_string, "%s%s", joined_string, data[forloop]);
            if (forloop < size -1){
                sprintf(joined_string, "%s%s", joined_string, delimiter);
            }
      }
      return joined_string;
}   

char* output_results(char** data, int arr_size, int switch){
      char** outdata = (char**) calloc(sizeof(char *), 1);
      int outsize, forloop;
      outsize = 0;
      for (forloop = 0; forloop < arr_size; forloop++){
          if (2**forloop & switch){
              outdata = (char*) realloc(outdata, (outsize + 1) * sizeof(char*));
              outdata[outsize] = calloc(sizeof(char), strlen(data[forloop]) + 1);
              strcpy(outdata[outsize], data[forloop]);
              outsize++;
          }
      }
      return joinString(outsize, outdata, ", ");
}


theNbomr 08-25-2009 08:43 AM

Use an array of switches:
Code:

unsigned char swtich[10];
Loop through the array, testing which are closed/open, and only print the ones of interest:
Code:

#define CLOSED 0
#define OPEN  1
int i;
  for( i = 0; i < 10; i++ ){
        if( switch[i] == CLOSED ){
            printf( "S%d_%d\n", i, switch[i] );
        }
  }

You can be thrifty with your data and store the state of the switches as bits, as suggested by pgpython, but the extra code to display the bits will much more than make up for your saving.

--- rod.

eryn 08-26-2009 02:19 AM

Thank you all.

To pgpython, the code is really advanced. I cannot understand how it works.

To theNbomr, we tried to think to put the switches as array before. But if we would do so, we need to change many parts in many source code files and it would be rather much more works and easily miss out something. Thus, we used the most fundamental one.

In the function that is to display the result after checking the switches, we put all switches one by one and add a condition for its display.
An example of the code is as follow:

if (S1==1)
{
strcat(buffer,",S1_");
sprintf(cS,"%d",S1);
strcat(buffer,cS);
}

and repeat for all others.

Anyway, thanks for all your help.

graemef 08-26-2009 05:14 AM

I would urge you to at least adopt the idea of the #defines that theNbomr had in the sample code, that is:
Code:

#define INACTIVE 0
#define ACTIVE  1

And use ACTIVE rather than 1 in your code. I will make it so much more readable,particularly in three months time when you need to look at the code again.
Code:

if (S1==ACTIVE)
{
strcat(buffer,",S1_");
sprintf(cS,"%d",S1);
strcat(buffer,cS);
}


Wim Sturkenboom 08-26-2009 12:03 PM

Code:

#include <stdio.h>

#define ACTIVE 1
#define INACTIVE 0

int main()
{
int S1;
char buffer[255];

        S1=0;
        sprintf(buffer,"%s",S1&ACTIVE?"S1_ACTIVE\n":"");
        printf("S1 = %d, %s\n",S1,buffer);

        S1=1;
        sprintf(buffer,"%s",S1&ACTIVE?"S1_ACTIVE\n":"");
        printf("S1 = %d, %s\n",S1,buffer);

        return 0;
}

The bold line is the effective line.

Note: it's better to use snprintf to prevent buffer overflows

Wim Sturkenboom 08-26-2009 01:09 PM

With regards to arrays. What will happen if you ever have to add an additional switch (or two or three or ...). All over the show you have to check the code and copy blocks of code. Forget one and the compiler will probably not warn you because the code is valid.

The simple example that you gave (modified to add a few more switches)
Code:

if (S1==1)
{
    strcat(buffer,",S1_");
    sprintf(cS,"%d",S1);
    strcat(buffer,cS);
}
if (S1==1)
{
    strcat(buffer,",S2_");
    sprintf(cS,"%d",S2);
    strcat(buffer,cS);
}
...
...
if (S10==1)
{
    strcat(buffer,",S10_");
    sprintf(cS,"%d",S10);
    strcat(buffer,cS);
}

As you can see, I already forgot to change the second S1 to S2 in an if statement during a copy/paste operation.

Your code is simply not maintainable. So you have to suffer once to get it maintainable. How many source files are we talking about?


All times are GMT -5. The time now is 03:32 PM.