Skip to content

Commit

Permalink
adding json and yaml output formats
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed May 10, 2021
1 parent b92a23e commit c6f121e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
10 changes: 10 additions & 0 deletions include/smeagle/corpora.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ namespace smeagle {
*/
void toAsp();

/**
* @brief Dump a corpus to json
*/
void toJson();

/**
* @brief Dump a corpus to yaml
*/
void toYaml();

/**
* @brief Get the name of a symbol type from the enum int
* @param symbol the symbol object
Expand Down
4 changes: 2 additions & 2 deletions include/smeagle/smeagle.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using namespace SymtabAPI;
namespace smeagle {

/** Output Format codes to be used with the Smeagle class */
enum class FormatCode { Terminal, Json, Asp };
enum class FormatCode { Yaml, Json, Asp };

/**
* @brief A class for saying hello in multiple languages
Expand All @@ -35,7 +35,7 @@ namespace smeagle {
* @param fmt the format to print to the screen
* @return a string containing the output
*/
int parse(FormatCode fmt = FormatCode::Terminal);
int parse(FormatCode fmt = FormatCode::Yaml);
};

} // namespace smeagle
43 changes: 39 additions & 4 deletions source/corpora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ std::string Corpus::getParamLocationOffset(localVar * param){
for (auto i = locs.begin(); i != locs.end(); ++i) {
VariableLocation current = *i;
offset = current.frameOffsetAbs;
//std::cout << offset << std::endl;
//std::cout << current.stClass << std::endl;
}

return offset;
Expand Down Expand Up @@ -172,11 +172,46 @@ void Corpus::toAsp() {
std::cout << "corpus(" << library << ")," << std::endl;
for (auto &typeloc : typelocs) {

std::cout << "abi_typelocation(" << library << ", " << typeloc.type
<< ", " << typeloc.parent << ", " << typeloc.name << ", "
<< typeloc.type << ", " << typeloc.location << ")" << std::endl;
std::cout << "abi_typelocation(" << library << ", " << typeloc.parent
<< ", " << typeloc.name << ", " << typeloc.type << ", "
<< typeloc.location << ")" << std::endl;
}
}

// dump all Type Locations to yaml output
void Corpus::toYaml() {

std::cout << "library: \"" << library << "\"\nlocations: " << std::endl;
for (auto &typeloc : typelocs) {

std::cout << " - library: " << library << "\n parent: " << typeloc.parent
<< "\n name: " << typeloc.name << "\n type: " << typeloc.type
<< "\n location: " << typeloc.location << "\n" << std::endl;
}
}


// dump all Type Locations to json
void Corpus::toJson() {

std::cout << "{ \"library\": \"" << library << "\", \"locations\": [" << std::endl;
for (auto &typeloc : typelocs) {

// Check if we are at the last entry (no comma) or not
std::string endcomma;
if (&typeloc == &typelocs.back())
endcomma = "";
else {
endcomma = ",";
}
std::cout << "{\"library\": \"" << library << "\", \"parent\": \""
<< typeloc.parent << "\", \"name\": \"" << typeloc.name << "\", \"type\": \""
<< typeloc.type << "\", \"location\": \"" << typeloc.location
<< "\"}" << endcomma << std::endl;

}
std::cout << "]}" << std::endl;

}

// parse a function for parameters and abi location
Expand Down
8 changes: 4 additions & 4 deletions source/smeagle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ int Smeagle::parse(FormatCode fmt) {
switch (fmt) {
default:
case FormatCode::Json:
corpus.toAsp();
break;
case FormatCode::Terminal:
corpus.toAsp();
corpus.toJson();
break;
case FormatCode::Asp:
corpus.toAsp();
break;
case FormatCode::Yaml:
corpus.toYaml();
break;
}

return 0;
Expand Down
5 changes: 3 additions & 2 deletions standalone/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

auto main(int argc, char** argv) -> int {
const std::unordered_map<std::string, smeagle::FormatCode> formats{
{"terminal", smeagle::FormatCode::Terminal},
{"yaml", smeagle::FormatCode::Yaml},
{"asp", smeagle::FormatCode::Asp},
{"json", smeagle::FormatCode::Json},
};

Expand All @@ -27,7 +28,7 @@ auto main(int argc, char** argv) -> int {
("h,help", "Show help")
("v,version", "Print the current version number")
("l,library", "Library to inspect", cxxopts::value(library))
("f,fmt", "Format to output in", cxxopts::value(fmt)->default_value("terminal"))
("f,fmt", "Format to output in", cxxopts::value(fmt)->default_value("yaml"))
;
// clang-format on

Expand Down

0 comments on commit c6f121e

Please sign in to comment.