The program which should take file names from windows command line.
c:\mac_find main.cpp chars.h
the output of the program should print information like 1)total macros found 2)in which line the macros found and name of it.
example : if the file name is main.cpp, then the output should be
file:main.cpp
macros:3
macro at line:7 -> #define Test
macro at line:8 -> #define Play
If a file does not exist, then simply print an error message and continue searching for macros in other files.
In practice, I might just use grep, but if correctness is required it looks like one might be better off using the pre-processor directly.
Consider, e.g.:
1 2 3
// There is only one macro definition here
#define O(define) \
#define x
Comments are stripped before the preprocessor runs, which means that you must account for commented-out macros, too. This requires accounting for all the little pathologies of C++'s comments. And string literals.
Hmm. I have no idea how to run just the preprocessor! I know I could lift it out of a compiler's source code, but not sure where it lives in my tools. A great solution, though. I approve of less work!
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
usingnamespace std;
struct MacroInfo
{
int line;
string name;
};
vector<MacroInfo> ProcessFile(constchar *filename);
void PrintMacroInfos(constchar *filename, vector<MacroInfo> macros);
int main(int argc, char *argv[])
{
for (int i = 1; i < argc; i++)
{
vector<MacroInfo> macros = ProcessFile(argv[i]);
PrintMacroInfos(argv[i], macros);
}
}
vector<MacroInfo> ProcessFile(constchar *filename)
{
vector<MacroInfo> macros;
// TODO read the file line by line and if it contains a macro
// create a MacroInfo, fill it with the line and macro name
// add it to the vector macros
return macros;
}
void PrintMacroInfos(constchar *filename, vector<MacroInfo> macros)
{
// TODO print the macros to the console
}