summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenny Shields <mail@kennyshields.net>2020-12-25 17:52:49 -0500
committerKenny Shields <mail@kennyshields.net>2020-12-25 17:52:49 -0500
commit88fba7df1cc033a0039724a2367e0d23bf45a444 (patch)
treec3bdbe746cddeb607d680af842b38c03157326be
parent25a9cc2fb261d20b56464c3271178c13eb81dc16 (diff)
downloaddsymlist-88fba7df1cc033a0039724a2367e0d23bf45a444.tar.gz
dsymlist-88fba7df1cc033a0039724a2367e0d23bf45a444.zip
Add Options and Collected structs
-rw-r--r--source/main.d142
1 files changed, 72 insertions, 70 deletions
diff --git a/source/main.d b/source/main.d
index 1ea41b7..f704a17 100644
--- a/source/main.d
+++ b/source/main.d
@@ -26,8 +26,33 @@ struct Symbol
string mangle;
}
-/// Array of collected symbols organized by namespace.
-Symbol[][string] symbols;
+/// Options data structure.
+struct Options
+{
+ /// User option to disable writing to standard out.
+ bool quiet;
+ /// User option to include mangled symbols in output.
+ bool showMangle;
+ /// User option to add extra spacing to the output string to make them less compact.
+ bool extraSpacing;
+ /// User option to restrict output to a set of namespaces
+ string nameSpaces;
+ /// File to scan specified by user.
+ string inputFile;
+ /// User option to specify an output file.
+ string outFile;
+}
+
+/// Collected symbols data structure.
+struct Collected
+{
+ /// Total symbol count.
+ ulong symbolCount = 0;
+ /// Total namespace count.
+ ulong namespaceCount = 0;
+ /// Array of collected symbols organized by namespace.
+ Symbol[][string] symbols;
+}
/// Regex pattern for matching parts of an NM output line.
static nmLineMatch = regex(`([0-9a-zA-Z]+) ([a-zA-Z]) (.*)`);
@@ -35,32 +60,11 @@ static nmLineMatch = regex(`([0-9a-zA-Z]+) ([a-zA-Z]) (.*)`);
/// Regex pattern for matching parts of an unmangled D symbol.
static symbolMatch = regex(`(.*) ([a-zA-z0-9\.]*)\.([a-zA-Z0-9]*)\((.*)\)`);
-/// User option to disable writing to standard out.
-bool __opt_quiet;
-
-/// User option to include mangled symbols in output.
-bool __opt_showMangle;
-
-/// User option to add extra spacing to the output string to make them less compact.
-bool __opt_extraSpacing;
-
-/// User option to restrict output to a set of namespaces
-string __opt_nameSpaces;
-
-/// User option to specify an output file.
-string __opt_outFile;
+/// List of options provided by the user.
+Options options;
-/// Array of namespaces to filter symbols by.
-string[] namespaces;
-
-/// File to scan specified by user.
-string inputFile;
-
-/// Total symbol count.
-ulong symbolCount = 0;
-
-/// Total namespace count.
-ulong namespaceCount = 0;
+/// List of options provided by the user.
+Collected collected;
/**
* Program entry point.
@@ -71,12 +75,14 @@ void main(string[] args)
GetoptResult helpInfo;
try
{
- helpInfo = getopt(args, config.passThrough,
- "quiet|q", "Don't print to standard out.", &__opt_quiet,
- "outfile|o", "Writes output to the specified file.", &__opt_outFile,
- "mangle|m", "Include mangled symbol in output.", &__opt_showMangle,
- "spacing|s", "Add extra spacing for less compact output.", &__opt_extraSpacing,
- "namespace|n", "Comma-separated list of namespaces to restrict the symbols collection to.", &__opt_nameSpaces);
+ helpInfo = getopt(args, config.passThrough, "quiet|q",
+ "Don't print to standard out.", &options.quiet, "outfile|o",
+ "Writes output to the specified file.",
+ &options.outFile, "mangle|m", "Include mangled symbol in output.",
+ &options.showMangle, "spacing|s", "Add extra spacing for less compact output.",
+ &options.extraSpacing,
+ "namespace|n", "Comma-separated list of namespaces to restrict the symbols collection to.",
+ &options.nameSpaces);
}
catch (Exception e)
{
@@ -101,16 +107,16 @@ void main(string[] args)
writeln("Error: No input file specified.");
return;
}
- inputFile = args[1];
+ options.inputFile = args[1];
// check file path
- if (!exists(inputFile) || !isFile(inputFile))
+ if (!exists(options.inputFile) || !isFile(options.inputFile))
{
writeln("Error: Invalid input file.");
return;
}
// collect symbols and perform output
- collect(inputFile);
- if (symbols.length == 0)
+ collect(options.inputFile);
+ if (collected.symbols.length == 0)
{
writeln("No symbols found.");
return;
@@ -126,9 +132,10 @@ void main(string[] args)
*/
void collect(string file)
{
- if (__opt_nameSpaces.length > 0)
+ string[] namespaces;
+ if (options.nameSpaces.length > 0)
{
- namespaces = __opt_nameSpaces.split(",");
+ namespaces = options.nameSpaces.split(",");
}
// run nm on the file and iterate over each output line
auto result = execute(["nm", file]);
@@ -171,14 +178,14 @@ void collect(string file)
}
}
// add the symbol to the list
- if ((symbolMatches[2] in symbols) is null)
+ if ((symbolMatches[2] in collected.symbols) is null)
{
- symbols[symbolMatches[2]] = [];
- namespaceCount++;
+ collected.symbols[symbolMatches[2]] = [];
+ collected.namespaceCount++;
}
- symbols[symbolMatches[2]] ~= Symbol(symbolMatches[3], symbolMatches[1],
+ collected.symbols[symbolMatches[2]] ~= Symbol(symbolMatches[3], symbolMatches[1],
symbolMatches[4].split(","), matches[3]);
- symbolCount++;
+ collected.symbolCount++;
}
}
@@ -188,33 +195,34 @@ void collect(string file)
*/
void output()
{
- // clear output file if it already exists
- if (__opt_outFile.length > 0 && exists(__opt_outFile) && isFile(__opt_outFile))
+ // clear output file
+ if (options.outFile.length > 0)
{
- write(__opt_outFile, "");
+ write(options.outFile, "");
}
// write file name, symbol count and namespace count to output file header
- writeToFile(baseName(inputFile));
- writeToFile(to!string(symbolCount) ~ " symbols in " ~ to!string(namespaceCount) ~ " namespaces");
+ writeToFile(baseName(options.inputFile));
+ writeToFile(to!string(collected.symbolCount) ~ " symbols in " ~ to!string(
+ collected.namespaceCount) ~ " namespaces");
// write namespaces from user input to output file header
- if (__opt_nameSpaces.length > 0)
+ if (options.nameSpaces.length > 0)
{
- writeToFile(to!string(__opt_nameSpaces.split(",")));
+ writeToFile(to!string(options.nameSpaces.split(",")));
}
writeToFile("------------------------------------\n");
- foreach (namespace; sort(symbols.keys))
+ foreach (namespace; sort(collected.symbols.keys))
{
- string length = to!string(symbols[namespace].length);
+ string length = to!string(collected.symbols[namespace].length);
string consoleStr = "\033[0;92m" ~ namespace ~ " (" ~ length ~ ")\033[0m\n{\n";
string fileStr = namespace ~ " (" ~ length ~ ")\n{\n";
- if (__opt_extraSpacing)
+ if (options.extraSpacing)
{
consoleStr ~= "\n";
fileStr ~= "\n";
}
// sort the list of symbols in the current namespace
- symbols[namespace].sort!("a.name < b.name");
- foreach (item; symbols[namespace])
+ collected.symbols[namespace].sort!("a.name < b.name");
+ foreach (item; collected.symbols[namespace])
{
consoleStr ~= " \033[0;95m" ~ item.type ~ "\033[0m : \033[0;36m" ~ item.name ~ "\033[0m ( ";
foreach (key, param; item.params)
@@ -226,16 +234,16 @@ void output()
}
}
consoleStr ~= "\033[0m )\n";
- if (__opt_showMangle)
+ if (options.showMangle)
{
consoleStr ~= " \033[0;37m" ~ item.mangle ~ "\n";
}
fileStr ~= " " ~ item.type ~ " : " ~ item.name ~ " ( " ~ item.params.join(",") ~ " )\n";
- if (__opt_showMangle)
+ if (options.showMangle)
{
fileStr ~= " " ~ item.mangle ~ "\n";
}
- if (__opt_extraSpacing)
+ if (options.extraSpacing)
{
consoleStr ~= "\n";
fileStr ~= "\n";
@@ -243,7 +251,7 @@ void output()
}
consoleStr ~= "\033[0m}\033[0;31m End of " ~ namespace;
fileStr ~= "} End of " ~ namespace;
- if (__opt_extraSpacing)
+ if (options.extraSpacing)
{
consoleStr ~= "\n";
fileStr ~= "\n";
@@ -262,11 +270,10 @@ void output()
*/
void writeToConsole(A...)(A args)
{
- if (__opt_quiet)
+ if (!options.quiet)
{
- return;
+ writeln(args);
}
- writeln(args);
}
/**
@@ -277,13 +284,8 @@ void writeToConsole(A...)(A args)
*/
void writeToFile(string contents)
{
- if (__opt_outFile.length == 0)
+ if (options.outFile.length != 0 && exists(options.outFile) && isFile(options.outFile))
{
- return;
- }
- if (exists(__opt_outFile) && !isFile(__opt_outFile))
- {
- return;
+ append(options.outFile, contents ~ "\n");
}
- append(__opt_outFile, contents ~ "\n");
}