static string dump_float(float f) { string str; str = (string) f; if (!has_substring(str, ".") && !has_substring(str, "e")) { str += ".0"; } return str; }
// Will parse a certain segment and return only the function. Returns "" if there is no function std::string parse_segment_to_function(std::string segment){ std::string s = segment; s = delete_non_function_lines(s); if (!has_substring(s, "(") || !has_substring(s, ")")) return ""; s = delete_non_function_chars(s); return s; }
// Deletes everything between two found strings std::string delete_all_between(std::string s, const char* a, const char* b){ std::size_t a_pos = s.find(a); std::size_t b_pos = s.find(b, a_pos); while (has_substring(s, a) && has_substring(s, b)) { s.replace(a_pos, static_cast<int>(b_pos-a_pos) + strlen(b), ""); std::size_t a_pos = s.find(a); std::size_t b_pos = s.find(b, a_pos); } return s; }
// Delete all stuff that's non-function related in a line std::string delete_non_function_chars(std::string s){ s = replace_all(s, "__extension__", ""); s = replace_all(s, "__inline", ""); if (has_substring(s, "__attribute__")){ parse_parentices(s, "__attribute__"); } s = replace_all(s, " ", " "); s = replace_all(s, "extern ", ""); s = replace_all(s, "const ", ""); s = replace_all(s, "struct ", ""); return s; }
// Deletes all lines that do not contain a function std::string delete_non_function_lines(std::string s){ std::stringstream lines(s); // Stringstream used to read lines std::string out = ""; // Output std::string line; // Single line that's read in the while loop bool is_arguments = false; while(std::getline(lines, line, '\n')){ bool isFunctionLine = true; // Variable that determines if the line read is from a function isFunctionLine &= !has_substring(line, "#"); isFunctionLine &= !has_substring(line, "typedef"); isFunctionLine &= !has_substring(line, "["); isFunctionLine &= !has_substring(line, "]"); isFunctionLine &= !has_substring(line, "{"); isFunctionLine &= !has_substring(line, "}"); isFunctionLine &= !has_substring(line, "return"); if (isFunctionLine){ out += line + " "; } } return out; }
Function(std::string code){ is_function = true; if (code.find("...") != std::string::npos){ is_function = false; return; } while(code.find(" ") == 0){ code.erase(0, 1); } std::stringstream codestream(code); std::getline(codestream, return_type, ' '); if (return_type.find("static") != std::string::npos){ std::getline(codestream, return_type, ' '); } std::getline(codestream, name, '('); name = replace_all(name, " ", ""); if (name.empty()){ std::cout << code << std::endl; } if (has_substring(name, "*")){ // Our return type is actually a pointer name.erase(name.find("*"), 1); // Remove it from the name return_type += "*"; // And add it to the return type } if (name.empty()){ std::getline(codestream, name, '('); } if (has_substring(name, "*")){ // * is from the function pointer, not the return type name.erase(name.find("*"), 1); // Just delete it and don't add it to the return type } name = replace_all(name, ")", ""); // This somehow happens, and I don't understand. if (name.empty()){ is_function = false; return; } std::string segment; while(std::getline(codestream, segment, ',')){ if (segment.find("(") != std::string::npos){ is_function = false; return; } // Cut off spaces in the beginning while(segment.find(" ") == 0){ segment.erase(0, 1); } // Cut off after spaces if (has_substring(segment, "*")){ segment.erase(segment.find("*") + 1, std::string::npos); } else if (has_substring(segment, "const") || has_substring(segment, "unsigned")){ size_t cutoff = std::min(segment.find(" ", segment.find(" ") + 1), segment.find(")")); if (cutoff != std::string::npos){ segment.erase(cutoff, std::string::npos); // Cut off after second space } } else if (has_substring(segment, " ")){ segment.erase(segment.find(" "), std::string::npos); } replace_all(segment, ")", ""); // Only the type is left -- TODO if (!segment.empty() && !has_substring(segment, "void") && segment.size() > 1){ arguments.push_back(segment); } } }