I haven't programed in C in a few years. I have been doing a lot of shell scripting, I.E. not really programming anything heavy.
That said, I have a script that gives hourly usage statistics for our email server. It runs w-a-y to slow as a script for my impatience, and needs to be converted to C. That and some Windows admins have expressed interest in it. Porting to C would be easier to cross compile that a script.
In the shell script I have an output function where I can pass text color, formatting (4.3f, 6s, etc.) and value.
Shell Code
Here's what I have in C so far.
Is there a way to pass the format parameter as a variable? I.E. pearl uses something like this %${fmt}f. Can this be done in C?
There are 5 columns, each can have their own color, width, format(d,s,f...) and value.
Obviously, the colors won't show up here. But you get the idea.
It's not a show stopper, but it would save me from having to create routines for every different format.
I am just giving small example here, since you said you know programming probably you can experiment and can write your own function, the code you shown doesn't clear color I guess.
This User Gave Thanks to Akshay Hegde For This Post:
Thanks! Never thought about breaking it down further. I told ya I haven't programmed in C in years. LOL
I've got an appointment to get to, but I'll try it tonight.
Case 3: should print in white. This project is just getting started, when I get further along I'll have to find a way to get the current color and reset it upon leaving the function.
You say case 3 should print white characters on a white background, but using color code 0 (as given in your sample code) prints BLACK characters on a white background.
Have you considered making your output() function use variable arguments like printf()? In the example below, output() uses the last character in the given format string argument to determine the type of the value to be retrieved from the argument list and passed on to printf(). It sets the selected color, uses the format given to print the final argument, and then resets the color back to the defaults.
This certainly isn't robust code and just uses different colors for the different output columns (instead of ranges of values within columns), but it may help you see an alternative approach.
With an input file named file containing:
it produces the output:
Thanks for the input. Sorry, I should have been more specific. case 3 "does" print white... because I've only tested this on a tty with black bg and white fg. When it is compiled on the Windoze side, they'll be using a black and white terminal, so it's not that big of a deal, but that's no reason for sloppy code. That said, I have about a half dozen other boxes I work on, each having their own bg / fg color combos. Just so I know what machine I'm working on. I'll have to see what output they yield.
Like I said, I haven't used C for years. This project is just getting the juices flowing. I've been pulling snippets from some of my old code. Heck, I look back at some of it, some 15+ years old, and say. I DID THAT?!?!?
For now I'm taking the KIS (Keep It Simple) approach. Your example is a bit much to digest at the moment. However, reading through it does help!
Thanks for the input. Sorry, I should have been more specific. case 3 "does" print white... because I've only tested this on a tty with black bg and white fg. When it is compiled on the Windoze side, they'll be using a black and white terminal, so it's not that big of a deal, but that's no reason for sloppy code.
Windows terminal can do color, but ansi.sys ceased to work after Windows 98, leaving no option but direct terminal system calls.
Thanks for the input. Sorry, I should have been more specific. case 3 "does" print white... because I've only tested this on a tty with black bg and white fg. When it is compiled on the Windoze side, they'll be using a black and white terminal, so it's not that big of a deal, but that's no reason for sloppy code. That said, I have about a half dozen other boxes I work on, each having their own bg / fg color combos. Just so I know what machine I'm working on. I'll have to see what output they yield.
Like I said, I haven't used C for years. This project is just getting the juices flowing. I've been pulling snippets from some of my old code. Heck, I look back at some of it, some 15+ years old, and say. I DID THAT?!?!?
For now I'm taking the KIS (Keep It Simple) approach. Your example is a bit much to digest at the moment. However, reading through it does help!
The ANSI terminal escape sequence you're using for case 3 produce black text on a white background. Having your terminal set to invert foreground and background colors makes it appear as white text on a black background when it is rendered on your screen.
I fully understand and appreciate KISS principles.
Here you have a choice between having an output function to print string arguments, an output function to print integer arguments, and an output function to print floating point arguments; or a single function that can handle all three. If you need to worry about various types of integers (int, long, long long) and floating point (float, double, long double) arguments then the processing gets more complicated. If you want to pass this routine a format string that ends with anything other than a printf() conversion specifier character, then the processing gets considerably more complicated (as in you'd have to actually interpret the entire format string argument instead of just the last character). There is also the question of what needs to be simple? Does it need to be simple for the person writing the output*() function (or functions), or does it need to be simple for the person using those functions? (Of course, in this case both of those might be the same person!)
Programming is full of trade-offs. I just wanted to give you some options to consider.
Hi All,
I am attempting to create a fixed length tilde delimited file using printf.
The variables are initialized to fixed length blank spaces
a=' '
b=' '
c=' '
d=' '
Sometimes the variable might contain values and sometimes they are... (5 Replies)
Getting the below error while executing this. Able to run the below commands Individually.
#!/bin/bash
a=$(printf "%d\n" 0x01E);
b=$(printf "%d\n" 0x01A);
echo $a
echo $b
c=`expr $a - $b`
echo $c
syntax error at line 2: `a=$' unexpected (2 Replies)
Hi Friends,
I am trying to insert lines of the below format in a file:
# x3a4914 Joe 2010/04/07
# seh Lane 2010/04/07
# IN01379 Larry 2010/04/07
I am formatting the strings as follows using awk printf:
awk 'printf "# %s %9s %18s\n", $2,$3,$4}'
... (2 Replies)
Hi I'm having a problem with converting a file:
ID X
1 7
1 8
1 3
2 5
2 7
2 2
To something like this:
ID X1 X2 X3
1 7 8 3
2 5 7 2
I've tried the following loop:
for i in `cat tst.csv| awk -F "," '{print $1}'| uniq`;do grep -h $i... (4 Replies)
seems simple but i've not been successfull in adding the value of a Variable to a every line of a file using AWK & printf
i've come up with the following, but it's not working.:mad:
--- mydatafile.dat
e.g. of data in file:
cau_actvty_fa_lrf.ksh
fan_soco_fa.ksh
ny_sum_lst.ksh... (4 Replies)
Hi all,
My simple AWK code does C = A - B
If C can be a negative number, how awk printf formating handles it using string format specifier.
Thanks in advance
Kanu
:confused: (9 Replies)
hi,
i am passing around 14 parameters for a script
a=$1
b=$2
c=$3
d=$4
e=$5
f=$6
g=$7
h=$8
i=\"${9}\"
shift
j=\"${1}\"
still for j it is displaying the 1st parameter value..how to make it take the 10th parameter (2 Replies)
A good place to start is simple variable passing....
Passing variables from one script to another
The next level is passing a variable into a more complex command such as using a variable in a sed command. There are some simple quoting techniques that are very general. These are mentioned... (0 Replies)