Changeset 2870 for vidalia/trunk/src
- Timestamp:
- 07/13/08 04:59:59 (4 months ago)
- Files:
-
- 1 modified
-
vidalia/trunk/src/tools/po2nsh/po2nsh.cpp (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
vidalia/trunk/src/tools/po2nsh/po2nsh.cpp
r2856 r2870 65 65 * The next line read from <b>po</b> will be the first non-header line in the 66 66 * document. */ 67 QHash<QString,QString> 68 parse_po_header(QTextStream *po) 69 { 70 QString line, key, value; 71 QHash<QString,QString> header; 72 67 void 68 skip_po_header(QTextStream *po) 69 { 70 QString line; 73 71 /* Skip any leading whitespace before the header */ 74 72 po->skipWhiteSpace(); 75 73 /* Read to the first empty line */ 76 74 line = po->readLine(); 77 while (!line.isEmpty() && !po->atEnd()) { 78 if (line.contains(":")) { 79 line.replace("\\n", ""); 80 if (line.startsWith("\"")) 81 line = line.remove(0, 1); 82 if (line.endsWith("\"")) 83 line.chop(1); 84 85 key = line.section(":", 0, 0).trimmed(); 86 value = line.section(":", 1, -1).trimmed(); 87 header.insert(key, value); 88 } 75 while (!po->atEnd() && !line.isEmpty()) 89 76 line = po->readLine(); 90 }91 return header;92 }93 94 /** Parse the Content-Type header line for a <i>charset=ENCODING</i>95 * specifier. If found, return a QTextCodec created using the given ENCODING.96 * If the Content-Type header specified an invalid encoding, then return a97 * NULL QTextCodec. If there was either no Content-Type header, or it did not98 * specify a charset, then return an ISO-8859-1 QTextCodec. */99 QTextCodec*100 parse_charset(const QHash<QString,QString> &header)101 {102 QString line, charset;103 QHash<QString,QString> content_type;104 QTextCodec *codec;105 bool ok;106 107 if (!header.contains("Content-Type"))108 return QTextCodec::codecForName("ISO-8859-1");109 line = header.value("Content-Type");110 line = line.replace(";", " ");111 112 content_type = string_parse_keyvals(line, &ok);113 if (!ok)114 return 0;115 if (!content_type.contains("charset"))116 return QTextCodec::codecForName("ISO-8859-1");117 return QTextCodec::codecForName(qPrintable(content_type.value("charset")));118 77 } 119 78 … … 123 82 * be set. */ 124 83 int 125 po2nsh(QTextStream *po, Q TextStream*nsh, const QString &language,84 po2nsh(QTextStream *po, QString *nsh, const QString &language, 126 85 QString *errorMessage) 127 86 { … … 135 94 Q_ASSERT(nsh); 136 95 Q_ASSERT(errorMessage); 137 138 /* Parse the text encoding from the header */ 139 header = parse_po_header(po); 140 codec = parse_charset(header); 141 if (!codec) { 142 *errorMessage = "Unable to parse valid text encoding."; 143 return -1; 144 } 145 po->setCodec(codec); 146 nsh->setCodec(codec); 147 96 97 skip_po_header(po); 148 98 line = read_next_line(po); 149 99 while (!po->atEnd()) { … … 195 145 196 146 /* Add the message translation to the .nsh document */ 197 *nsh << QString("LangString ") << msgctxt 198 << QString(" ${LANG_%1} ").arg(language); 147 nsh->append(QString("LangString ")); 148 nsh->append(msgctxt); 149 nsh->append(QString(" ${LANG_%1} ").arg(language)); 199 150 if (msgstr.isEmpty()) 200 *nsh << "\"" << msgid << "\"";151 nsh->append("\"" + msgid + "\""); 201 152 else 202 *nsh << "\"" << msgstr << "\"";203 *nsh << "\n";153 nsh->append("\"" + msgstr + "\""); 154 nsh->append("\n"); 204 155 205 156 n_strings++; … … 214 165 QTextStream error(stderr); 215 166 error << "usage: po2nsh [-q] -i <infile.po> -o <outfile.nsh> " 216 "-l <language>\n"; 217 error << " -q (optional) Quiet mode (errors are still displayed)\n"; 218 error << " -i <infile.po> Input .po file\n"; 219 error << " -o <outfile.nsh> Output .nsh file\n"; 220 error << " -l <language> NSIS language table name\n"; 167 "-l <language> [-f <from-encoding>] [-t <to-encoding>]\n"; 168 error << " -q (optional) Quiet mode (errors are still displayed)\n"; 169 error << " -i <infile.po> Input .po file\n"; 170 error << " -o <outfile.nsh> Output .nsh file\n"; 171 error << " -l <language> NSIS language table name\n"; 172 error << " -f <from-encoding> .po file encoding (default: utf-8)\n"; 173 error << " -t <to-encoding> .nsh file encoding (default: iso-8859-1)\n"; 221 174 error.flush(); 222 175 exit(1); … … 227 180 { 228 181 QTextStream error(stderr); 229 QString nshString, language; 230 QString errorMessage; 182 QString language, errorMessage; 231 183 char *infile, *outfile; 232 184 bool quiet = false; 233 185 QTextCodec *from_codec = QTextCodec::codecForName("utf-8"); 186 QTextCodec *to_codec = QTextCodec::codecForName("iso-8859-1"); 187 234 188 /* Check for the correct number of input parameters. */ 235 if (argc < 7 || argc > 8)189 if (argc < 7 || argc > 12) 236 190 print_usage_and_exit(); 237 191 for (int i = 1; i < argc; i++) { … … 245 199 else if (!arg.compare("-l", Qt::CaseInsensitive) && ++i < argc) 246 200 language = QString(argv[i]).toUpper(); 247 else 201 else if (!arg.compare("-f", Qt::CaseInsensitive) && ++i < argc) { 202 from_codec = QTextCodec::codecForName(argv[i]); 203 if (!from_codec) { 204 error << "Invalid input encoding: " << argv[i] << "\n"; 205 return 1; 206 } 207 } else if (!arg.compare("-t", Qt::CaseInsensitive) && ++i < argc) { 208 to_codec = QTextCodec::codecForName(argv[i]); 209 if (!to_codec) { 210 error << "Invalid output encoding: " << argv[i] << "\n"; 211 return 1; 212 } 213 } else 248 214 print_usage_and_exit(); 249 215 } … … 257 223 } 258 224 225 QString nsh; 259 226 QTextStream po(&poFile); 260 QTextStream nsh(&nshString);227 po.setCodec(from_codec); 261 228 int n_strings = po2nsh(&po, &nsh, language, &errorMessage); 262 229 if (n_strings < 0) { … … 276 243 /* Finally write the .nsh output. */ 277 244 QTextStream out(&nshFile); 278 out.setCodec( nsh.codec());279 out << nsh String;245 out.setCodec(to_codec); 246 out << nsh; 280 247 281 248 if (!quiet) {
