1 /***************************************************************************
2                           ldef.cpp  -  description
3                              -------------------
4     begin                : Sun Jan 26 2003
5     copyright            : (C) 2003 by Sheldon Lee Wen
6     email                : leewsb@hotmail.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include <iterator>
19 #include <algorithm>
20 #include <lineak/ldef.h>
21 #include <lineak/defloader.h>
22 #include <lineak/lconfigdata.h>
23 
24 //using namespace lineak_util_functions;
25 
LDef()26 LDef::LDef(){
27 	deffilename = snull;
28 	table.clear();
29 }
LDef(string file)30 LDef::LDef(string file) : deffilename(file) {
31 }
32 
LDef(DefLoader & def)33 LDef::LDef(DefLoader &def){
34 	if (def.getFile() != snull) {
35     	setTable(def.loadDef().getTable());
36      	setFilename(def.getFile());
37     }
38     else
39     	setFilename(snull);
40 
41 }
~LDef()42 LDef::~LDef(){
43 }
44 /** Return a list of keyboard identifiers */
getKeyboards() const45 vector<string> LDef::getKeyboards() const {
46 	vector<string> result;
47     for (map<string,LKbd*>::const_iterator m = table.begin(); m != table.end(); m++) {
48 		result.push_back(m->first);
49 	}
50 	return result;
51 }
52 /** Return a sorted list of brands */
getBrands() const53 vector<string> LDef::getBrands() const {
54 	vector<string> result;
55     for (map<string,LKbd*>::const_iterator m = table.begin(); m != table.end(); m++) {
56 		result.push_back(m->second->brand);
57 	}
58 	sort(result.begin(),result.end());
59 	vector<string> ret(result.begin(), unique(result.begin(), result.end()));
60 	return ret;
61 }
getModels() const62 vector<string> LDef::getModels() const {
63 	vector<string> result;
64     for (map<string,LKbd*>::const_iterator m = table.begin(); m != table.end(); m++) {
65 		result.push_back(m->second->model);
66 	}
67 	sort(result.begin(),result.end());
68 	vector<string> ret(result.begin(), unique(result.begin(), result.end()));
69 	return ret;
70 }
getModels(string brand)71 map<string,LKbd*> LDef::getModels(string brand) {
72     map<string,LKbd*> models;
73     for (map<string,LKbd*>::const_iterator m = table.begin(); m != table.end(); m++) {
74     	if (m->second->brand == brand)
75      		models[m->first] = m->second;
76     }
77     return models;
78 }
getTable()79 map<string,LKbd*>& LDef::getTable() {
80 	return table;
81 }
getKeyboard(string brand,string model)82 LKbd& LDef::getKeyboard(string brand, string model) {
83 
84     for (map<string,LKbd*>::const_iterator m = table.begin(); m != table.end(); m++) {
85     	if (m->second->brand == brand && m->second->model == model)
86      		return *(m->second);
87     }
88     // If we can't find our keyboard, return a reference to our empty keyboard.
89     blank.name = snull;
90     return (blank);
91 }
getKeyboard(string name)92 LKbd& LDef::getKeyboard(string name) {
93     //cout << "Getting keyboard: " << name << endl;
94     for (map<string,LKbd*>::const_iterator m = table.begin(); m != table.end(); m++) {
95         //cout << "Examining keyboard " << m->first << endl;
96     	if (m->first == name) {
97 	        //cout << "returning keyboard " << m->first << endl;
98 		//cout << "returning keyboard " << m->second->name << endl;
99 		//cout << *(m->second);
100      		return *(m->second);
101 	}
102     }
103     cerr << "Returning a blank keyboard." << endl;
104     // If we can't find our keyboard, return a reference to our empty keyboard.
105     blank.name = snull;
106     return (blank);
107 }
hasKeyboard(string name)108 const bool LDef::hasKeyboard(string name) {
109     for (map<string,LKbd*>::const_iterator m = table.begin(); m != table.end(); m++) {
110     	if (m->first == name)
111      		return true;
112     }
113    return false;
114 }
115 
setFilename(string file)116 void LDef::setFilename(string file) {
117 	deffilename = file;
118 }
isEmpty()119 const bool LDef::isEmpty() {
120 	if (table.empty() || (deffilename == snull))
121 		return true;
122 	else
123 		return false;
124 	//return (table.empty() || (deffilename == snull));
125 }
print(ostream & out)126 void LDef::print(ostream &out) {
127     out << DEF_HEADER;
128     out << endl;
129     for (map<string,LKbd*>::const_iterator m = table.begin(); m != table.end(); m++) {
130     	out << *(m->second);
131     }
132 }
addKeyboard(LKbd & newkb)133 void LDef::addKeyboard(LKbd &newkb) {
134     // Add a keyboard.
135 	table[newkb.name] = &newkb;
136 
137 }
addKeyboards(LDef & newdef)138 void LDef::addKeyboards(LDef &newdef) {
139     map<string,LKbd*> &tmp = newdef.getTable();
140     for (map<string,LKbd*>::const_iterator m = tmp.begin(); m != tmp.end(); m++) {
141     	// Add a keyboard.
142 	table[m->first] = m->second;
143     }
144 }
145