Example #1
0
int main( int argc, char **argv )
{
    QString defaultContext = "@default";
    MetaTranslator fetchedTor;
    QByteArray codecForTr;
    QByteArray codecForSource;
    QStringList tsFileNames;
    QStringList uiFileNames;

    bool verbose = false;
    bool noObsolete = false;
    bool metSomething = false;
    int numFiles = 0;
    bool standardSyntax = true;
    bool metTsFlag = false;

    int i;

    for ( i = 1; i < argc; i++ ) {
        if ( qstrcmp(argv[i], "-ts") == 0 )
            standardSyntax = false;
    }

    for ( i = 1; i < argc; i++ ) {
        if ( qstrcmp(argv[i], "-help") == 0 ) {
            printUsage();
            return 0;
        } else if ( qstrcmp(argv[i], "-noobsolete") == 0 ) {
            noObsolete = true;
            continue;
        } else if ( qstrcmp(argv[i], "-verbose") == 0 ) {
            verbose = true;
            continue;
        } else if ( qstrcmp(argv[i], "-version") == 0 ) {
            fprintf( stderr, "pylupdate4 version %s\n", QT_VERSION_STR );
            return 0;
        } else if ( qstrcmp(argv[i], "-ts") == 0 ) {
            metTsFlag = true;
            continue;
        }

        numFiles++;

        QString fullText;

        if ( !metTsFlag ) {
            QFile f( argv[i] );
            if ( !f.open(QIODevice::ReadOnly) ) {
#if defined(_MSC_VER) && _MSC_VER >= 1400
                char buf[100];
                strerror_s(buf, sizeof(buf), errno);
                fprintf( stderr, "pylupdate4 error: Cannot open file '%s': %s\n",
                         argv[i], buf );
#else
                fprintf( stderr, "pylupdate4 error: Cannot open file '%s': %s\n",
                         argv[i], strerror(errno) );
#endif
                return 1;
            }

            QTextStream t( &f );
            fullText = t.readAll();
            f.close();
        }

        QString oldDir = QDir::currentPath();
        QDir::setCurrent( QFileInfo(argv[i]).path() );

        if ( standardSyntax ) {
            fetchedTor = MetaTranslator();
            codecForTr.clear();
            codecForSource.clear();
            tsFileNames.clear();
            uiFileNames.clear();

            QMap<QString, QString> tagMap = proFileTagMap( fullText );
            QMap<QString, QString>::Iterator it;

            for ( it = tagMap.begin(); it != tagMap.end(); ++it ) {
                QStringList toks = it.value().split(' ');
                QStringList::Iterator t;

                for ( t = toks.begin(); t != toks.end(); ++t ) {
                    if ( it.key() == "SOURCES" ) {
                        fetchtr_py( (*t).toAscii(), &fetchedTor, defaultContext.toAscii(), true, codecForSource );
                        metSomething = true;
                    } else if ( it.key() == "TRANSLATIONS" ) {
                        tsFileNames.append( *t );
                        metSomething = true;
                    } else if ( it.key() == "CODEC" ||
                                it.key() == "DEFAULTCODEC" ||
                                it.key() == "CODECFORTR" ) {
                        codecForTr = (*t).toLatin1();
                        fetchedTor.setCodecForTr(codecForTr);
                    } else if ( it.key() == "CODECFORSRC" ) {
                        codecForSource = (*t).toLatin1();
                    } else if ( it.key() == "FORMS" ) {
                fetchtr_ui( (*t).toAscii(), &fetchedTor, defaultContext.toAscii(), true);
            }
                }
            }

            updateTsFiles( fetchedTor, tsFileNames, codecForTr, noObsolete, verbose );

            if ( !metSomething ) {
                fprintf( stderr,
                         "pylupdate4 warning: File '%s' does not look like a"
                         " project file\n",
                         argv[i] );
            } else if ( tsFileNames.isEmpty() ) {
                fprintf( stderr,
                         "pylupdate4 warning: Met no 'TRANSLATIONS' entry in"
                         " project file '%s'\n",
                         argv[i] );
            }
        } else {
            if ( metTsFlag ) {
                if ( QString(argv[i]).toLower().endsWith(".ts") ) {
                    QFileInfo fi( argv[i] );
                    if ( !fi.exists() || fi.isWritable() ) {
                        tsFileNames.append( argv[i] );
                    } else {
                        fprintf( stderr,
                                 "pylupdate4 warning: For some reason, I cannot"
                                 " save '%s'\n",
                                 argv[i] );
                    }
        } else {
                    fprintf( stderr,
                             "pylupdate4 error: File '%s' lacks .ts extension\n",
                             argv[i] );
                }
            } else {
                QFileInfo fi(argv[i]);
        if ( fi.suffix() == "py" || fi.suffix() == "pyw" ) {
            fetchtr_py( fi.fileName().toAscii(), &fetchedTor, defaultContext.toAscii(), true, codecForSource );
        } else {
            fetchtr_ui( fi.fileName().toAscii(), &fetchedTor, defaultContext.toAscii(), true);
        }
            }
        }
        QDir::setCurrent( oldDir );
    }

    if ( !standardSyntax )
        updateTsFiles( fetchedTor, tsFileNames, codecForTr, noObsolete, verbose );

    if ( numFiles == 0 ) {
        printUsage();
        return 1;
    }
    return 0;
}
Example #2
0
int main( int argc, char** argv )
{
	QString defaultContext = "@default";
	MetaTranslator fetchedTor;
	QCString codec;
	QStringList tsFileNames;

	bool verbose = FALSE;
	bool noObsolete = FALSE;
	bool metSomething = FALSE;
	int numFiles = 0;
	bool standardSyntax = TRUE;
	bool metTsFlag = FALSE;

	int i;

	for ( i = 1; i < argc; i++ )
	{
		if ( qstrcmp( argv[i], "-ts" ) == 0 )
			standardSyntax = FALSE;
	}

	for ( i = 1; i < argc; i++ )
	{
		if ( qstrcmp( argv[i], "-help" ) == 0 )
		{
			printUsage();
			return 0;
		}
		else if ( qstrcmp( argv[i], "-noobsolete" ) == 0 )
		{
			noObsolete = TRUE;
			continue;
		}
		else if ( qstrcmp( argv[i], "-verbose" ) == 0 )
		{
			verbose = TRUE;
			continue;
		}
		else if ( qstrcmp( argv[i], "-version" ) == 0 )
		{
			fprintf( stderr, "lupdate version %s\n", QT_VERSION_STR );
			return 0;
		}
		else if ( qstrcmp( argv[i], "-ts" ) == 0 )
		{
			metTsFlag = TRUE;
			continue;
		}

		numFiles++;

		QString fullText;

		if ( !metTsFlag )
		{
			QFile f( argv[i] );
			if ( !f.open( IO_ReadOnly ) )
			{
				fprintf( stderr, "lupdate error: Cannot open file '%s': %s\n", argv[i], strerror( errno ) );
				return 1;
			}

			QTextStream t( &f );
			fullText = t.read();
			f.close();
		}

		QString oldDir = QDir::currentDirPath();
		QDir::setCurrent( QFileInfo( argv[i] ).dirPath() );

		if ( standardSyntax )
		{
			fetchedTor = MetaTranslator();
			codec.truncate( 0 );
			tsFileNames.clear();

			QMap<QString, QString> tagMap = proFileTagMap( fullText );
			QMap<QString, QString>::Iterator it;

			for ( it = tagMap.begin(); it != tagMap.end(); ++it )
			{
				QStringList toks = QStringList::split( ' ', it.data() );
				QStringList::Iterator t;

				for ( t = toks.begin(); t != toks.end(); ++t )
				{
					if ( it.key() == "HEADERS" || it.key() == "SOURCES" )
					{
						fetchtr_cpp( *t, &fetchedTor, defaultContext, TRUE );
						metSomething = TRUE;
					}
					else if ( it.key() == "INTERFACES" || it.key() == "FORMS" )
					{
						fetchtr_ui( *t, &fetchedTor, defaultContext, TRUE );
						fetchtr_cpp( *t + ".h", &fetchedTor, defaultContext, FALSE );
						metSomething = TRUE;
					}
					else if ( it.key() == "TRANSLATIONS" )
					{
						tsFileNames.append( *t );
						metSomething = TRUE;
					}
					else if ( it.key() == "CODEC" || it.key() == "DEFAULTCODEC" )
					{
						codec = ( *t ).latin1();
					}
					else if ( it.key() == "WPDEFINITIONS" )
					{
						lookForDefinitions( *t, "*.xml", &fetchedTor, &fetchtr_xml );
					}
					else if ( it.key() == "WPSCRIPTS" )
					{
						lookForDefinitions( *t, "*.py", &fetchedTor, &fetchtr_py );
					}
				}
			}
			
			updateTsFiles( fetchedTor, tsFileNames, codec, noObsolete, verbose );

			if ( !metSomething )
			{
				fprintf( stderr, "lupdate warning: File '%s' does not look like a"
			 " project file\n", argv[i] );
			}
			else if ( tsFileNames.isEmpty() )
			{
				fprintf( stderr, "lupdate warning: Met no 'TRANSLATIONS' entry in"
			 " project file '%s'\n", argv[i] );
			}
		}
		else
		{
			if ( metTsFlag )
			{
				if ( QString( argv[i] ).lower().endsWith( ".ts" ) )
				{
					QFileInfo fi( argv[i] );
					if ( !fi.exists() || fi.isWritable() )
					{
						tsFileNames.append( argv[i] );
					}
					else
					{
						fprintf( stderr, "lupdate warning: For some reason, I cannot"
				 " save '%s'\n", argv[i] );
					}
				}
				else
				{
					fprintf( stderr, "lupdate error: File '%s' lacks .ts extension\n", argv[i] );
				}
			}
			else
			{
				QFileInfo fi( argv[i] );
				if ( QString( argv[i] ).lower().endsWith( ".ui" ) )
				{
					fetchtr_ui( fi.fileName(), &fetchedTor, defaultContext, TRUE );
					fetchtr_cpp( QString( fi.fileName() ) + ".h", &fetchedTor, defaultContext, FALSE );
				}
				else if ( QString( argv[i] ).lower().endsWith( ".xml" ) )
				{
					fetchtr_xml( fi.fileName(), &fetchedTor, 0, TRUE );
				}
				else if ( QString( argv[i] ).lower().endsWith( ".py" ) )
				{
					fetchtr_py( fi.fileName(), &fetchedTor, 0, TRUE );
				}
				else
				{
					fetchtr_cpp( fi.fileName(), &fetchedTor, defaultContext, TRUE );
				}
			}
		}
		QDir::setCurrent( oldDir );
	}

	if ( !standardSyntax )
		updateTsFiles( fetchedTor, tsFileNames, codec, noObsolete, verbose );

	if ( numFiles == 0 )
	{
		printUsage();
		return 1;
	}
	return 0;
}