Sponsored Content
Top Forums Programming Using ANSI color codes in gcc compiled program Post 302632231 by kristinu on Sunday 29th of April 2012 02:59:37 PM
Old 04-29-2012
I had coded the following using the letters s, v and d.

s stands for string argument
v stands for value
d stands for description

If I have a number NUM in front of the letter, it means that there are NUM arguments of that type.

Example
Code:
prUsge4_sv ( stdout, "--cmod", "FILE" );

The 4 means a shift to the right by four characters

prints
Code:
    --cmod=FILE

Another example

Code:
prUsge4_2sv ( stdout, "--bracd", "--mindacc", "DIST" );

gives

Code:
    --bracd, --mindacc=DIST

I would like to use the following function calls, but try to have the colors in each function more managable using
a method where I can declare what color I want to use when calling fprintf. Or something similar to what you have done.

The code is as follows:

Code:
#ifndef clorfncs_hh
#define clorfncs_hh

#include <stdlib.h>

void prNwln (
  FILE*  stream ) {

  fprintf ( stream, "\n", "" );

}

void prTitl (
  FILE*  stream,
  const char*  titl ) {

  const char* frTitl;
  frTitl = "\e[1;33m%s\e[0m\n";
  fprintf ( stream, frTitl, titl );

}

void prTitl4 (
  FILE*  stream,
  const char*  titl ) {

  const char* frTitl;
  frTitl = "\e[1;33m%s\e[0m\n";
  fprintf ( stream, frTitl, titl );

}

void prUsge_sv (
  FILE*  stream,
  const char*  prog,
  const char*  value ) {

  const char* frUsge_sv;
  frUsge_sv = "\e[0;31m%s\e[0m \e[4;32m%s\e[0m\n";
  fprintf ( stream, frUsge_sv, prog, value );

}

void prUsge4_sv (
  FILE*  stream,
  const char*  prog,
  const char*  value ) {

  const char* frUsge4_sv;
  frUsge4_sv = "    \e[0;31m%s\e[0m \e[4;32m%s\e[0m\n";
  fprintf ( stream, frUsge4_sv, prog, value );

}

void prUsge_s2v (
  FILE*  stream,
  const char*  prog,
  const char*  value1,
  const char*  value2 ) {

  const char* frUsge_s2v;
  frUsge_s2v = "\e[0;31m%s\e[0m \e[4;32m%s\e[0m \e[4;32m%s\e[0m\n";
  fprintf ( stream, frUsge_s2v, prog, value1, value2 );

}

void prUsge4_s2v (
  FILE*  stream,
  const char*  prog,
  const char*  value1,
  const char*  value2 ) {

  const char* frUsge4_s2v;
  frUsge4_s2v = "    \e[0;31m%s\e[0m \e[4;32m%s\e[0m \e[4;32m%s\e[0m\n";
  fprintf ( stream, frUsge4_s2v, prog, value1, value2 );

}

void prArgm_sv (
  FILE*  stream,
  const char*  actn,
  const char*  value ) {

  const char* frArgm_sv;
  frArgm_sv = "\e[0;31m%s\e[0m\e[0;37m=\e[4;32m%s\e[0m\n";
  fprintf ( stream, frArgm_sv, actn, value );

}

void prArgm4_sv (
  FILE*  stream,
  const char*  actn,
  const char*  value ) {

  const char* frArgm4_sv;
  frArgm4_sv = "    \e[0;31m%s\e[0m\e[0;37m=\e[4;32m%s\e[0m\n";
  fprintf ( stream, frArgm4_sv, actn, value );

}

void prArgm_2s (
  FILE*  stream,
  const char*  actn1,
  const char*  actn2 ) {

  const char* frArgm_2s;
  frArgm_2s = "\e[0;31m%s\e[0;37m, \e[0;31m%s\e[0m\n";
  fprintf ( stream, frArgm_2s, actn1, actn2 );

}

void prArgm4_2s (
  FILE*  stream,
  const char*  actn1,
  const char*  actn2 ) {

  const char* frArgm4_2s;
  frArgm4_2s = "    \e[0;31m%s\e[0;37m, \e[0;31m%s\e[0m\n";
  fprintf ( stream, frArgm4_2s, actn1, actn2 );

}

void prArgm_2sd (
  FILE*  stream,
  const char*  actn1,
  const char*  actn2,
  const char*  desc ) {

  const char* frArgm_2sd;
  frArgm_2sd = "\e[0;31m%s\e[0;37m, \e[0;31m%s\e[0m  \e[0;37m%s\e[0m\n";
  fprintf ( stream, frArgm_2sd, actn1, actn2, desc );

}

void prArgm4_2sd (
  FILE*  stream,
  const char*  actn1,
  const char*  actn2,
  const char*  desc ) {

  const char* frArgm4_2sd;
  frArgm4_2sd = "    \e[0;31m%s\e[0;37m, \e[0;31m%s\e[0m  \e[0;37m%s\e[0m\n";
  fprintf ( stream, frArgm4_2sd, actn1, actn2, desc );

}

void prArgm_svd (
  FILE*  stream,
  const char*  actn,
  const char*  value,
  const char*  desc ) {

  const char* frArgm_svd;
  frArgm_svd = "\e[0;31m%s\e[0m\e[0;37m=\e[4;32m%s\e[0m  \e[0;37m%s\e[0m\n";
  fprintf ( stream, frArgm_svd, actn, value, desc );

}

void prArgm4_svd (
  FILE*  stream,
  const char*  actn,
  const char*  value,
  const char*  desc ) {

  const char* frArgm4_svd;
  frArgm4_svd = "    \e[0;31m%s\e[0m\e[0;37m=\e[4;32m%s\e[0m  \e[0;37m%s\e[0m\n";
  fprintf ( stream, frArgm4_svd, actn, value, desc );

}

void prArgm8_svd (
  FILE*  stream,
  const char*  actn,
  const char*  value,
  const char*  desc ) {

  const char* frArgm8_svd;
  frArgm8_svd = "        \e[0;31m%s\e[0m\e[0;37m=\e[4;32m%s\e[0m  \e[0;37m%s\e[0m\n";
  fprintf ( stream, frArgm8_svd, actn, value, desc );

}

void prDesc (
  FILE*  stream,
  const char*  desc ) {

  const char* frDesc;
  frDesc = "\e[0;37m%s\e[0m\n";
  fprintf ( stream, frDesc, desc );

}

void prDesc4 (
  FILE*  stream,
  const char*  desc ) {

  const char* frDesc4;
  frDesc4 = "    \e[0;37m%s\e[0m\n";
  fprintf ( stream, frDesc4, desc );

}

void prUsgeMscl (
  FILE*  stream ) {

  const char* desc;

  prNwln ( stream );
  prTitl ( stream, "MISCELLANEOUS" );

  desc="print verbose messages";
  prArgm8_svd ( stream, "--vb", "LEVEL", desc );

  desc="   displays usage";
  prArgm4_2sd ( stream, "-u", "--usage", desc );

  desc="   prints some examples using getveltest";
  prArgm4_2sd ( stream, "-e", "--examples", desc );

  desc="    display this usage information";
  prArgm4_2sd ( stream, "-h", "--help", desc );

}

#endif

Do you think I should not use something like

Code:
void prDesc4 (
  FILE*  stream,
  const char*  desc ) {

  const char* frDesc4;
  frDesc4 = "    \e[0;37m%s\e[0m\n";
  fprintf ( stream, frDesc4, desc );

}


Last edited by kristinu; 04-29-2012 at 05:31 PM..
 

10 More Discussions You Might Find Interesting

1. Linux

gcc compiled executable not working across x86_64 linux platforms

Hi I compiled a hello world program on two different 64-bit Linux machines, named quimby and node0331. When I compile on quimby and run on node0331 I get a "Floating exception (core dumped)" error. But if I do it in reverse, things work fine. Here's my compilation on quimby: $ uname -a... (3 Replies)
Discussion started by: same1290
3 Replies

2. UNIX for Dummies Questions & Answers

Using color escape codes in less

Hi all, Not sure how "for dummies" this question is, but I'd better use understatement... A. My Environment ============== I am using RedHat Linux, version 2.6.18-53.el5. When I type less --version I get: less 394 Copyright (C) 1984-2005 Mark Nudelman ... My terminal is configured... (1 Reply)
Discussion started by: Source2Exe
1 Replies

3. UNIX for Dummies Questions & Answers

Regular expression on hex color codes

I want to have all hex color codes in a given stylesheet in uppercase, so #fff should be converted to #FFF for instance. Here is the regular expression I use to match and convert hex color codes to uppercase: sed -e 's/^#({3}$)|({6}$)/^#({3}$)|({6}$)/' main.css However, no conversion to uppercase... (6 Replies)
Discussion started by: figaro
6 Replies

4. Programming

pthread_mutex_lock in ANSI C vs using Atomic builtins of GCC

I have a program which has 7-8 threads, and lots of shared variables; these variables (and also they may not the primitive type, they may be enum or struct ), then they may read/write by different threads at the same time. Now, my design is like this, typedef unsigned short int UINT16;... (14 Replies)
Discussion started by: sehang
14 Replies

5. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

6. Shell Programming and Scripting

zsh, prompt, variable expansion, ANSI color sequences

Hi! I am using latest ZSH and I have setopt prompt_subst I have a global hash array variable which contains some color definitions with color names as keys and 256-color ANSI codes (with %{ and %}) as values, eg %{\ePS1="$FG Hello World (%M) > " or PS1=$'$FG Hello World (%M) > ' then it... (3 Replies)
Discussion started by: galanom
3 Replies

7. UNIX for Dummies Questions & Answers

Display file with escaped color codes

Hi, I have a file containing color codes: Fri May 25 17:13:04 2012: Starting MTA: exim4^ Loading cpufreq kernel modules...^How can I display it colorized on a linux terminal? (4 Replies)
Discussion started by: ripat
4 Replies

8. Solaris

Newly Compiled GCC 4.4.4 on Solaris sparc gives problem with -m32/-m64 flags

Hello experts, This issue has kept me busy all day long. It started off with openssl compilation which was giving linking error with following message: /usr/local/bin/ld: target elf32-sparc not found collect2: ld returned 1 exit status I tried every step possible thing that I could think... (2 Replies)
Discussion started by: d_shanke
2 Replies

9. Shell Programming and Scripting

Help with awk color codes based on condition

HI i have two files say test and test1 Test.txt Code: Lun01 2TB 1.99TB 99.6% Lun02 2TB 1.99TB 99.5% Lun03 2TB 1.99TB 99.5% Lun04 2TB 1.55TB 89.6% Code: Test1.txt Lun01 2TB 1.99TB 89.5% Lun02 2TB 1.99TB 99.5% Lun03 2TB 1.99TB 99.5% Requirement is to compare... (6 Replies)
Discussion started by: venkitesh
6 Replies

10. Programming

Local variable in a C function is not getting created in stack when its compiled with GCC

Hi, I am working in UEFI EDK2 Bios source. We created a platform related new package in the EDK2 source. I find a strange issue with the platform related code we added. When I did source level debugging I noticed the local variable in a C function is not getting created in stack when its... (6 Replies)
Discussion started by: Divya R
6 Replies
All times are GMT -4. The time now is 03:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy