LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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-12-2009, 11:46 PM   #1
eryn
Member
 
Registered: Jul 2009
Posts: 43

Rep: Reputation: 15
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.
 
Old 08-13-2009, 02:08 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,280

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
So, which lang is this? Also, please show us the relevant code you've done so far.
 
Old 08-13-2009, 03:56 AM   #3
eryn
Member
 
Registered: Jul 2009
Posts: 43

Original Poster
Rep: Reputation: 15
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.
 
Old 08-13-2009, 06:33 AM   #4
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 141

Rep: Reputation: 32
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
 
Old 08-25-2009, 02:44 AM   #5
eryn
Member
 
Registered: Jul 2009
Posts: 43

Original Poster
Rep: Reputation: 15
I am still too new to Linux

I do not know how to do advanced code.
 
Old 08-25-2009, 08:02 AM   #6
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 141

Rep: Reputation: 32
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, ", ");
}
 
Old 08-25-2009, 08:43 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 08-26-2009, 02:19 AM   #8
eryn
Member
 
Registered: Jul 2009
Posts: 43

Original Poster
Rep: Reputation: 15
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.
 
Old 08-26-2009, 05:14 AM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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);
}
 
Old 08-26-2009, 12:03 PM   #10
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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

Last edited by Wim Sturkenboom; 08-26-2009 at 01:01 PM. Reason: fixed copy/paste error
 
Old 08-26-2009, 01:09 PM   #11
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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?
 
  


Reply

Tags
condition, display, parameter, printf


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
--display parameter never works lilsim89 Linux - Desktop 6 05-26-2009 04:53 PM
functions in case conditions viveksnv Programming 4 04-02-2008 10:15 PM
Multiple If conditions keysorsoze Programming 4 12-12-2007 01:07 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
PuTTY doesn't display curses based stuff correctly vrillusions Linux - Software 1 06-22-2004 02:39 PM


All times are GMT -5. The time now is 03:48 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration