Sponsored Content
Top Forums Programming Using ANSI color codes in gcc compiled program Post 302632035 by kristinu on Sunday 29th of April 2012 12:49:02 AM
Old 04-29-2012
Using ANSI color codes in gcc compiled program

I have put some yellow color codes and works well.

I call the funstion using

Code:
print_usage(stderr, 0);

I would like to know if there is any way, to store the ansi color codes in variables and then call them inside fprintf.
Or have a format followed by the strings I want to output.

Code:
void print_usage(FILE* stream, int exit_code) {

  fprintf (stream,
      "\033[1;33mUSAGE\e[0m\n"
      "   %s options\n\n", program_name);

  fprintf (stream,
    "\033[1;33mACTIONS\e[0m\n"
    "    --cmod=FILE   Velocity model file (required)\n"
    "    --vdx=DV      Interval in x direction in velocity file\n"
    "    --nx=NUM      Interval calculated with vdx = Lx / Nx (default: 20)\n"
    "    --vdz=DV      Interval in z direction in velocity file or\n"
    "    --nz=NUM      Interval calculated with vdz = Lz / Nz (default: 20)\n"
    "    --vdi=DV      Interval along interface\n"
    "    --ni=NUM      Interval calculated with vdi = Li / Ni (default: 20)\n"
    "    --vel=FILE    File with P wave velocity in 'x z v' format\n"
    "    --velp=FILE   File with P wave velocity in 'x z v' format\n"
    "    --vels=FILE   File with S wave velocity in 'x z v' format\n"
    "    --lay=ON/OFF  If --lay=ON, output files are in 'x z v l' format\n"
    "    --intf=       Output file for interfaces in 'x z' format, separated by '>'\n"
    "    -h  --help    Display this usage information\n"
    "    -o  --output=FILE  Write output to file\n"
    "    --vb=LEVEL    Print verbose messages\n"
    "\n");

  exit (exit_code);

}

---------- Post updated at 11:49 PM ---------- Previous update was at 09:56 PM ----------

I have now coded the following which is working well.

How can I simplify this code. For example, it would be good to construct a format, followed by the things I want to print.

Something similar to the following

Code:
frmt_sv = "nRd%snWh=uGn%srcl"
frmt_desc="    %s\n"

fprintf (stream, frmt_sv, "--nx", "NUM");
fprintf (stream, frmt_desc, "interval calculated with vdx = Lx / Nx (default: 20)");

The code is currently as follows

Code:
void print_usage(FILE* stream, int exit_code) {

  const char* progname;
  const char* bYl;
  const char* nRd;
  const char* uGn;
  const char* nWh;
  const char* rcl;
  char* frArgm_sv;

  progname = "getveltest";

  bYl = "\e[1;33m";
  nRd = "\e[0;31m";
  uGn = "\e[4;32m";
  nWh = "\e[0;37m";
  rcl = "\e[0m";

  fprintf (stream,"%s%s%s\n",bYl,"USAGE",rcl);
  fprintf (stream,"    %s%s %s%s%s\n\n",nRd,progname,uGn,"ACTIONS",rcl);

  fprintf (stream,"%s%s%s\n",bYl,"ACTIONS",rcl);
  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--cmod",nWh,"=",uGn,"FILE",rcl);
  fprintf (stream,"  %s\n"," sound speed model (mandatory)");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--vdx",nWh,"=",uGn,"DX",rcl);
  fprintf (stream,"  %s\n","    interval in x direction in velocity file");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--nx",nWh,"=",uGn,"NUM",rcl);
  fprintf (stream,"  %s\n","    interval calculated with vdx = Lx / Nx (default: 20)");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--vdz",nWh,"=",uGn,"DZ",rcl);
  fprintf (stream,"  %s\n","    interval in z direction in velocity file");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--nz",nWh,"=",uGn,"NUM",rcl);
  fprintf (stream,"  %s\n","    interval calculated with vdz = Lz / Nz (default: 20)");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--vdi",nWh,"=",uGn,"DI",rcl);
  fprintf (stream,"  %s\n","    interval along interface");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--ni",nWh,"=",uGn,"NUM",rcl);
  fprintf (stream,"  %s\n","    interval calculated with vdi = Li / Ni (default: 20)");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--vel",nWh,"=",uGn,"FILE",rcl);
  fprintf (stream,"  %s\n","  file with P wave velocity in 'x z v' format");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--velp",nWh,"=",uGn,"FILE",rcl);
  fprintf (stream,"  %s\n"," file with P wave velocity in 'x z v' format");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--vels",nWh,"=",uGn,"FILE",rcl);
  fprintf (stream,"  %s\n"," file with S wave velocity in 'x z v' format");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--lay",nWh,"=",uGn,"ON/OFF",rcl);
  fprintf (stream,"  %s\n","if --lay=ON, output files are in 'x z v l' format");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--intf",nWh,"=",uGn,"FILE",rcl);
  fprintf (stream,"  %s\n"," output file for interfaces in 'x z' format, separated by '>'");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"--vb",nWh,"=",uGn,"LEVEL",rcl);
  fprintf (stream,"  %s\n","  print verbose messages");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"-u",nWh," ",nRd,"--usage",rcl);
  fprintf (stream,"  %s\n","  displays usage");

  fprintf (stream,"    %s%s%s%s%s%s%s",nRd,"-h",nWh," ",nRd,"--help",rcl);
  fprintf (stream,"  %s\n","   display this usage information");

  exit (exit_code);

}

 

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 06:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy