Changeset 2870 for vidalia/trunk/src

Show
Ignore:
Timestamp:
07/13/08 04:59:59 (4 months ago)
Author:
edmanm
Message:

Add "-f <from-codec>" and "-t <to-codec>" options to po2nsh. Default to utf-8
for input and iso-8859-1 for output.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vidalia/trunk/src/tools/po2nsh/po2nsh.cpp

    r2856 r2870  
    6565 * The next line read from <b>po</b> will be the first non-header line in the 
    6666 * document. */ 
    67 QHash<QString,QString> 
    68 parse_po_header(QTextStream *po) 
    69 { 
    70   QString line, key, value; 
    71   QHash<QString,QString> header; 
    72    
     67void 
     68skip_po_header(QTextStream *po) 
     69{ 
     70  QString line; 
    7371  /* Skip any leading whitespace before the header */ 
    7472  po->skipWhiteSpace(); 
    7573  /* Read to the first empty line */ 
    7674  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()) 
    8976    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 a 
    97  * NULL QTextCodec. If there was either no Content-Type header, or it did not 
    98  * 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"))); 
    11877} 
    11978 
     
    12382 * be set. */ 
    12483int 
    125 po2nsh(QTextStream *po, QTextStream *nsh, const QString &language, 
     84po2nsh(QTextStream *po, QString *nsh, const QString &language, 
    12685      QString *errorMessage) 
    12786{ 
     
    13594  Q_ASSERT(nsh); 
    13695  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); 
    14898  line = read_next_line(po); 
    14999  while (!po->atEnd()) { 
     
    195145 
    196146    /* 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)); 
    199150    if (msgstr.isEmpty()) 
    200       *nsh << "\"" << msgid << "\""; 
     151      nsh->append("\"" + msgid + "\""); 
    201152    else 
    202       *nsh << "\"" << msgstr << "\""; 
    203     *nsh << "\n"; 
     153      nsh->append("\"" + msgstr + "\""); 
     154    nsh->append("\n"); 
    204155 
    205156    n_strings++; 
     
    214165  QTextStream error(stderr); 
    215166  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"; 
    221174  error.flush(); 
    222175  exit(1); 
     
    227180{ 
    228181  QTextStream error(stderr); 
    229   QString nshString, language; 
    230   QString errorMessage; 
     182  QString language, errorMessage; 
    231183  char *infile, *outfile; 
    232184  bool quiet = false; 
    233  
     185  QTextCodec *from_codec = QTextCodec::codecForName("utf-8"); 
     186  QTextCodec *to_codec   = QTextCodec::codecForName("iso-8859-1"); 
     187   
    234188  /* Check for the correct number of input parameters. */ 
    235   if (argc < 7 || argc > 8) 
     189  if (argc < 7 || argc > 12) 
    236190    print_usage_and_exit(); 
    237191  for (int i = 1; i < argc; i++) { 
     
    245199    else if (!arg.compare("-l", Qt::CaseInsensitive) && ++i < argc) 
    246200      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 
    248214      print_usage_and_exit();  
    249215  } 
     
    257223  } 
    258224 
     225  QString nsh; 
    259226  QTextStream po(&poFile); 
    260   QTextStream nsh(&nshString); 
     227  po.setCodec(from_codec); 
    261228  int n_strings = po2nsh(&po, &nsh, language, &errorMessage); 
    262229  if (n_strings < 0) { 
     
    276243  /* Finally write the .nsh output. */ 
    277244  QTextStream out(&nshFile); 
    278   out.setCodec(nsh.codec()); 
    279   out << nshString; 
     245  out.setCodec(to_codec); 
     246  out << nsh; 
    280247 
    281248  if (!quiet) {