moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kvoctrain / kvoctrain / tools / extractlang.cpp
blob6123214c0773ec0055e634b2cc494ee04bc93c96
1 #include <qtextstream.h>
2 #include <qstring.h>
4 ///////////////////////////////////////////////////////////
5 //
6 // (c) Kevin Krammer <kevin.krammer@gmx.at>
7 //
8 // 2001-12-08
9 //
10 // automatically extracts a structure from
11 // http://www.loc.gov/standards/iso639-2/englangn.html
13 // see also extractlang.README
15 ///////////////////////////////////////////////////////////
17 QString twoLangs;
18 QString threeLangs;
20 void parseLanguage(QString name, QString three, QString two)
22 int star = three.find('*');
23 if (star > 0)
24 three.remove(star, 1);
26 QString three_1 = three;
28 int slash = three_1.find('/');
29 if (slash > 0) {
30 three_1.remove(slash, 1);
31 three_1.insert(slash, ", ");
34 if (!two.contains("&nbsp;")) {
35 twoLangs.append("\t{{\"");
36 twoLangs.append(two);
37 twoLangs.append("\"}, {\"");
38 twoLangs.append(three_1);
39 twoLangs.append("\"}, {I18N_NOOP(\"");
40 twoLangs.append(name);
41 twoLangs.append("\")}},\n");
43 else
44 two = "";
46 slash = three.find('/');
47 if (slash > 0) {
48 if (two.length() == 0)
49 two = three.mid(slash+1);
50 three.remove(slash, three.length());
53 threeLangs.append("\t{{\"");
54 threeLangs.append(two);
55 threeLangs.append("\"}, {\"");
56 threeLangs.append(three);
57 threeLangs.append("\"}, {I18N_NOOP(\"");
58 threeLangs.append(name);
59 threeLangs.append("\")}},\n");
62 QString parseTD(QString& line)
64 QCString cline = line.local8Bit();
65 uint clineLength = cline.length();
66 QCString result = cline;
67 int pos = -1;
68 bool inTag = false;
70 for (uint i = 0; i < clineLength; ++i)
72 if (cline[i] == '<')
73 inTag = true;
74 else if (cline[i] == '>')
75 inTag = false;
76 else if (!inTag)
78 pos++;
79 result[pos] = cline[i];
83 QString retVal(result);
84 retVal = retVal.left(pos+1);
86 return retVal.simplifyWhiteSpace();
89 bool parseTR(QString& line, QTextIStream& in)
91 QString text[4];
93 line = in.readLine();
94 if (line.isNull())
95 return true;
98 uint count = 0;
99 while (line.contains("<td"))
101 if (count > 3)
102 return true;
104 text[count] = parseTD(line);
105 count++;
106 line = in.readLine();
109 if (count == 4)
111 parseLanguage(text[0], text[2], text[3]);
114 if (line.contains("/tr"))
115 return true;
117 return false;
120 int main()
122 QString line;
123 bool readNext = true;
125 QTextIStream in(stdin);
126 QTextOStream out(stdout);
128 while (!in.atEnd())
130 if (readNext)
131 line = in.readLine();
133 if (line.contains("<tr"))
135 readNext = parseTR(line, in);
137 else
138 readNext = true;
141 out << "struct KV_ISO639_Code {\n"
142 " const char *iso1code;\n"
143 " const char *iso2code;\n"
144 " const char *langname;\n"
145 "};\n"
146 "\n";
148 out << "KV_ISO639_Code kv_iso639_1[] = {\n"
149 << twoLangs.left(twoLangs.length()-1) << endl
150 << "\t{{0}, {0}, {0}}\n"
151 << "};\n";
153 out << endl;
155 out << "KV_ISO639_Code kv_iso639_2[] = {\n"
156 << threeLangs.left(threeLangs.length()-1) << endl
157 <<"\t{{0}, {0}, {0}}\n"
158 << "};\n";
160 return 0;