c++ - Preprocessor to count number of strings in file -
i wanted have macro (or else works) can go through c/c++ file, , count number of occurrences of specific string (in physical c/c++ file).
#define numinfile(str) [???] int main() { printf("blahblah"); printf("you've used printf %d times", numinfile ("printf") - 2); //-2 account call return 0; }
edit: question specific using functionality exit calls. generalize use.
if understand correctly, want have unique error codes, can trace line error happened?
i address y question instead of x one:
you can use __line__
. __line__
expands integer constant of current line number. #define
quit as:
#define quit(code) (quit)(__line__+(code)) void (quit)(code) { // seperate func in case want more exit(code); }
keep in mind though exit code of process not best way encode such information. on posix, lower 8 bit of exit code guaranteed available. use 300 base value, assume on windows or other system isn't concern.
for debugging purposes, alternatively consider writing stderr
, when error happens (maybe command line flag).
if exit
example, , intend use inside application, save __line__
, __file__
in global (or _thread_local
) variables on error , store exit reason in error code.
regarding x question, preprocessor doesn't such stuff. have offload such tasks shell/perl/whatever script build script can call.
Comments
Post a Comment