#include #include using namespace std; class splitted_names { public: splitted_names(string name); string get_category() {return category;} string get_package() {return package;} string get_version() {return version;} private: string category, package, version; }; int main(int argc, char* argv[]) { if (argc != 3 || argv[1] == "-h") { cout << "Useage: " << argv[0] << " -[cpv] category/package-version" << endl << "Options:" << endl << " -c : print the category" << endl << " -p : print the package name" << endl << " -v : print the package version" << endl << " -h : print this help" << endl << "NOTE: Only one of -c, -p, -v is allowed" << endl; return EXIT_FAILURE; } splitted_names the_string((string)argv[2]); string option = (string)argv[1]; if (option == "-c") { cout << the_string.get_category() << endl; } else if (option == "-p") { cout << the_string.get_package() << endl; } else if (option == "-v") { cout << the_string.get_version() << endl; } return EXIT_SUCCESS; } splitted_names::splitted_names (string name) { category = ""; package = ""; version = ""; int start_copy = 0, stop_copy = 0; // do not copy a leading '=' if (name[0] == '=') { start_copy = 1; } stop_copy = name.find('/'); category.assign(name, start_copy, stop_copy-start_copy); if (name.length() > stop_copy) { start_copy = stop_copy+1; } for (int i = start_copy; i < name.length(); i++) { if (name.at(i) == '-') { if (name.at(i+1) == '0' || name.at(i+1) == '1' || name.at(i+1) == '2' || name.at(i+1) == '3' || name.at(i+1) == '4' || name.at(i+1) == '5' || name.at(i+1) == '6' || name.at(i+1) == '7' || name.at(i+1) == '8' || name.at(i+1) == '9' ) { stop_copy = i-1; break; } } } package.assign(name, start_copy, stop_copy-start_copy+1); if (name.length() > stop_copy) { start_copy = stop_copy+2; } stop_copy = name.length(); version.assign(name, start_copy, stop_copy-start_copy); }