-p, -s => print, set; error on incorrect format tag

This commit is contained in:
Kristian Setälä 2010-01-07 22:01:36 +02:00
parent ee21b5b37d
commit fafaa71e6d
1 changed files with 31 additions and 20 deletions

View File

@ -9,9 +9,10 @@
// Software Foundation; either version 2 of the License, or (at your option) // Software Foundation; either version 2 of the License, or (at your option)
// any later version. // any later version.
#include <iostream>
#include <string>
#include <cstdlib> #include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include "XKeyboard.h" #include "XKeyboard.h"
using namespace std; using namespace std;
@ -19,7 +20,7 @@ using namespace std;
void print_usage(string progname) void print_usage(string progname)
{ {
cerr << endl cerr << endl
<< "xkblayout-state v1" << endl << "xkblayout-state version 1b" << endl
<< endl << endl
<< "Usage: " << endl << "Usage: " << endl
<< endl << endl
@ -54,61 +55,70 @@ void print_usage(string progname)
<< endl; << endl;
} }
void print_status(XKeyboard& xkb, string format) { bool print_status(XKeyboard& xkb, string format) {
stringstream r; // resulting string
for (size_t i = 0; i < format.length(); ++i) { for (size_t i = 0; i < format.length(); ++i) {
if (format[i] == '%' && i < format.length()-1) { if (format[i] == '%' && i < format.length()-1) {
switch (format[i+1]) { switch (format[i+1]) {
case 'c': case 'c':
cout << xkb.currentGroupNum(); r << xkb.currentGroupNum();
break; break;
case 'n': case 'n':
cout << xkb.currentGroupName(); r << xkb.currentGroupName();
break; break;
case 's': case 's':
cout << xkb.currentGroupSymbol(); r << xkb.currentGroupSymbol();
break; break;
case 'v': case 'v':
cout << xkb.currentGroupVariant(); r << xkb.currentGroupVariant();
break; break;
case 'e': case 'e':
if (xkb.currentGroupVariant().empty()) if (xkb.currentGroupVariant().empty())
cout << xkb.currentGroupSymbol(); r << xkb.currentGroupSymbol();
else else
cout << xkb.currentGroupVariant(); r << xkb.currentGroupVariant();
break; break;
case 'C': case 'C':
cout << xkb.groupCount(); r << xkb.groupCount();
break; break;
case 'N': case 'N':
for (size_t j = 0; j < xkb.groupNames().size(); ++j) for (size_t j = 0; j < xkb.groupNames().size(); ++j)
cout << xkb.groupNames()[j] << endl; r << xkb.groupNames()[j] << endl;
break; break;
case 'S': case 'S':
for (size_t j = 0; j < xkb.groupSymbols().size(); ++j) for (size_t j = 0; j < xkb.groupSymbols().size(); ++j)
cout << xkb.groupSymbols()[j] << endl; r << xkb.groupSymbols()[j] << endl;
break; break;
case 'V': case 'V':
for (size_t j = 0; j < xkb.groupVariants().size(); ++j) for (size_t j = 0; j < xkb.groupVariants().size(); ++j)
cout << xkb.groupVariants()[j] << endl; r << xkb.groupVariants()[j] << endl;
break; break;
case 'E': case 'E':
for (size_t j = 0; j < xkb.groupVariants().size(); ++j) for (size_t j = 0; j < xkb.groupVariants().size(); ++j)
if (xkb.groupVariants()[j].empty()) if (xkb.groupVariants()[j].empty())
cout << xkb.groupSymbols()[j] << endl; r << xkb.groupSymbols()[j] << endl;
else else
cout << xkb.groupVariants()[j] << endl; r << xkb.groupVariants()[j] << endl;
break;
case '%':
r << '%';
break; break;
default: default:
cout << format[i+1]; cerr << "Unknown format character: " << format[i+1] << endl;
return false;
break; break;
} }
++i; ++i;
} else { // not '%' } else { // not '%'
cout << format[i]; r << format[i];
} }
} }
cout << r.str();
return true;
} }
bool set_group(XKeyboard& xkb, string group) { bool set_group(XKeyboard& xkb, string group) {
@ -132,8 +142,8 @@ int main(int argc, char* argv[])
} else { } else {
string command(argv[1]); string command(argv[1]);
if (command == "print") { if (command == "print") {
print_status(xkb, string(argv[2])); if (!print_status(xkb, string(argv[2])))
return EXIT_SUCCESS; return EXIT_FAILURE;
} else if (command == "set") { } else if (command == "set") {
if (!set_group(xkb, string(argv[2]))) { if (!set_group(xkb, string(argv[2]))) {
cerr << "Failed to set layout" << endl; cerr << "Failed to set layout" << endl;
@ -149,4 +159,5 @@ int main(int argc, char* argv[])
cerr << e.what() << endl; cerr << e.what() << endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
return EXIT_SUCCESS;
} }