c macro for print colour based on source file - c

I was wondering if anyone has come up with a clever macro that would change the console print colour based on which source file has called printf.
The best way I've been able to figure out how to do this is.
//print.h
#define PRINT_NORMAL printf("\033[0m");
#define PRINTF(style, ...) printf(style); printf(__VA_ARGS__); PRINT_NORMAL
//myfile.h
#define MYFILE_STYLE "\033[1;34m"
//myfile.c
...
PRINTF(MYFILE_STYLE, "Something with myfile style");
...
I was wondering if there is some sneaky way I could define PRINTF so that it wouldn't need the style parameter, essentially grabbing a local variable to use as the style.
EDIT:
It kind of came to me immediately after writing the last sentence.
I should be able to define a local style variable in each of my .c files and change my macro to
#define PRINTF(...) printf(style); printf(__VA_ARGS__); PRINT_NORMAL
The next question: Is there a better way to combine those 3 printf statements?

Create a function calculateStyle() in your (new) print.c module which calculates a hash from the filename, then selects a color based on this hash, and returns the style command.
Then your PRINTF() macro will become:
#define PRINTF(...) printf(calculateStyle(__FILE__)); printf(__VA_ARGS__); PRINT_NORMAL
Of course you can't guarantee a unique color using a hash, but it at least saves you from keeping a list of used colors somewhere, and keeps these obscure commands out of your source files (nicely hiding them in one single print.c module).

Related

Switching between implementations in C precompiler

I'm fairly new to programming in C. My problem is that I have two implementations of a function and I want to be able to switch between them easily.
Right now I define the two implementations of the function as function_implementation1 and function_implementation1 in the files "funtion_implementation1.h" and "funtion_implementation2.h" respectively. To switch between them I have the following file:
#define IMPLEMENTATION1
#ifdef IMPLEMENTATION_1
#include "funtion_implementation1.h"
#define myFunction function_implementation1
#endif
#ifdef IMPLEMENTATION_2
#include "funtion_implementation2.h"
#define myFunction function_implementation2
#endif
In order to switch from one implementation to the other I just have to change the first line. This approach works, and I was satisfied with it for a while, but now it is bugging me that I have to open this file so often. I have a parameters.h file where I define all my parameters and I would rather choose which implementation to use in that file. Sadly, moving the first line to that file does not work. If I do that myFunction is not defined.
What is the best way to do this?
you should include your parameters file where you use alias, macros, etc:
#include "Parameters.h"
also, all your headers files should start with:
#ifndef __FILE_H__
#define __FILE_H__
// definitions go there
#endif
This prevents nested include of header files
Use preprocessor options, specifically the -D option. If you wanted to use IMPLEMENTATION1, when you are compiling that file on the command line (or in IDE), add -D IMPLEMENTATION1. This defines that macro. Same works for any macro

Using macros to generalise code for function calls

I'm writing C code which requires me to use multiple function calls of the same definition which differ only by single characters. Is there a way I can make a macro function which takes say a number and can insert these calls into my code for me where I call the macro given I know the numbers at compile time:
i.e.
#define call_pin_macro(X)
enable_pin#X();
do_thing_pin#X();
do_other_thing_pin#X();
.
.
void pin_function(void){
call_pin_macro(1);
call_pin_macro(2);
call_pin_macro(3);
}
Instead of:
void pin_function(void){
enable_pin1();
do_thing_pin1();
do_other_thing_pin1();
enable_pin2();
do_thing_pin2();
do_other_thing_pin2();
enable_pin3();
do_thing_pin3();
do_other_thing_pin3();
}
As a note I have looked at stringification (Hence the included #X's) in gcc however I cannot get the above code to compile which I get an error "error: '#' is not followed by a macro parameter". And it thus it seems this isn't exactly the functionality I am after. Thanks in advance.
In gcc you can do it like this:
#define call_pin_macro(X) \
enable_pin##X(); \
do_thing_pin##X(); \
do_other_thing_pin##X();
The double hash is the macro concatenation operator. You don't want to use stringify because that will put quotes around it.
The backslashes allow you to continue the macro over several lines.

Can C include use macros?

I want to include the result of a macro expansion. It seems include only knows <> ""?
This fails:
#define audio sinwave
#ifdef audio
#include audio".c"
/*#include "sinwave.c"*/
#endif
But this works:
#ifdef audio
if(i==0){set_audio((char *)audio);return;}
#endif
You could do something like this:
#define audio audio
#define FF(X) #X
#define F(X) FF(X.c)
#ifdef audio
#include F(audio)
#endif
that is you'd have to append the .c before you place everything into a string. The usual concatenation "audio" ".c" -> "audio.c" of adjacent strings happens in a later compilation phase than preprocessing, so an #include directive cannot deal with this.
No. Preprocessor directives cannot be used like this. You can use macros to concatenate and stringify names, but that's another case. If you need this, you should most probably re-think your design because it's not good enough at the moment.
Maybe it's not clear to me... But I see a few different questions that you're asking...
I think your asking if you can include source files, yes you can, but its not the best idea; see here for a good discussion why.
If you're wondering about including files with "..." vs <...>, the difference is the quotes are when files are in your local directory (or you want to include the path to the file) the <> are for when the file is in your search path.
If you want to stringify the file name, then
Jens Guestedt answer is what you want... But I question the logic behind doing this...
Why not include the .c file in your project normally (add it to your makefile or whatever) then just wrap the code in question (or the while file) in the #ifdef? That's a much more standard way to conditionally compile the code.

Passing values between two c files of the same project in VC++

Well, I want to pass a string value from one file to another, how do I do that ? I don't want to save it to a text file and read it in the other one, I directly want to pass it to another c file .Thanks in advance.
maybe just a #define MY_STR "your value" will do the job.
just create a .h file and include in both your C files
#ifndef _MY_HEADER_H
#define _MY_HEADER_H
#define MY_STR "your value"
#endif
then in your sources
#include "yourfile.h"
and use your MY_STR as a constant (please note that MY_STR will be a macro)
You probably want an extern char commonstr[] somewhere at the top (possibly in a header?) and a char commonstr[LENGTH] in one of the .c files. Then commonstr will be available throughout your project.
There is an awesome post about extern variables in C.

How can I keep doxygen from documenting #defines in a C file?

I have #define values in headers that I certainly want Doxygen to document but I have others in C files that I treat as static constants and I don't want Doxygen to document them. Something as simple and stupid as
#define NUMBER_OF(a) (sizeof((a))/sizeof((a)[0]))
#define MSTR(e) #e
How can I keep Doxygen from putting those #defines in the documentation it creates? I've tried marking it with #internal but that didn't seem to help.
A somewhat-related question on Doxygen and #define, how can I get:
#define SOME_CONSTANT 1234 /**< An explanation */
to put "SOME_CONSTANT" and "An explanation" but not "1234" in the output?
There is no need to use the \cond and \endcond commands. You can hide the initializer by simply using the \hideinitializer command:
#define SOME_CONSTANT 1234 /**< An explanation #hideinitializer */
Regarding the first question, you may set HIDE_UNDOC_MEMBERS = YES and only the macros having a Doxygen documentation block will be shown in the output.
You can set MAX_INITIALIZER_LINES = 0 in your doxyfile to hide the values of your defines.
You can exclude any part of code from Doxygen parsing with \cond ... \endcond tags.
edit: Some related questions:
How can Doxygen exclude a C++ class?
Exclude some classes from doxygen documentation
You only want to document what is declared in the .h files. I'm assuming you declare all static functions and variables as static in your .c files. All the remaining are declared in .h corresponding files also. These are your "public" members.
What I like to do in this case, and I believe doxygen was more designed to be used this way is:
in your Doxyfile, set EXTRACT_ALL = NO and add the directory where your .h files are to INPUT
add /** \file */ to all your .h files (but not your .c files).
This will index only what is contained in your .h files. You can still add the directory containing your .c files to INPUT at your Doxyfile, and they will be scanned for additional documentation for your "public" members...
It will no doubt still seem noisy and unnatural, but to address your other question, try:
/** An explanation */
#define SOME_CONSTANT /** #cond */ 1234 /** #endcond */
I solved this problem by moving my documentation from the .c file to the .h file. Then run doxygen only on the .h file.
Then the items that I want to document (the 'public' items) are intrinsically what doxygen picks up.
Because I have been previously careful to put 'public' items in the .h file and 'private' items in the .c file this works very well.
This technique came to mind when I noticed that doxygen was pulling in the includes. It struck me that if I were to also move the subset of includes that the calling module would need to use my module, then that list would be documented as well.
This technique has an additional benefit: I can put the documentation in one terminal window and the source in a different terminal window while updating the documentation.
Sometimes you may have a define which you want to document, but want doxygen to treat it differently (or even ignore it completely to avoid parsing errors).
For this you can define the #define in doxygen differently than in your sourcecode.
Example:
Some compilers allow variable linkage to specific segments, i.e.:
const int myvar # "segment_of_myvar_in_memory"=123;
=> doxygen would parse the "segment_of_myvar_in_memory" part as variable name which is not desired.
We could use a define for it:
#define __link_to_segment(name) # name
const int myvar __link_to_segment("segment_of_myvar_in_memory")=123;
If Preprocessing is active, Doxygen interprets our variable now as a function because of the function-like define using brackets..
But if we redefine our define within the Doxyfile, behaviour changes:
PREDEFINED = __link_to_segment(a)=
now the variable is parsed correctly as variable - also all types or keywords in front are correctly shown as keywords.
A nice side effekt:
In case you use 2 different IDEs with your code (one IDE for compiling&debugging, one for editing), you will also discover that some IDEs (i.e. Eclipse) have problems parsing variables with #"segment name". Using the approach above, you can redefine the __link_to_segment(name) there too:
#define __link_to_segment(name)
i.e. Eclipse will then show and parse the variable correctly, whereas the "compiling&debugging" IDE can still link the variable to its segment name.

Resources