示例#1
0
void SftpSession::handleLsOutput(SftpJobId jobId, const QByteArray &output)
{
    QList<SftpFileInfo> allFileInfo;
    for (const QByteArray &line : output.split('\n')) {
        if (line.startsWith("ls") || line.isEmpty())
            continue;
        const QByteArrayList components = line.simplified().split(' ');
        if (components.size() < 9) {
            qCWarning(sshLog) << "Don't know how to parse sftp ls output:" << line;
            continue;
        }
        const QByteArray typeAndPermissions = components.first();
        if (typeAndPermissions.size() != 10) {
            qCWarning(sshLog) << "Don't know how to parse sftp ls output:" << line;
            continue;
        }
        SftpFileInfo fileInfo;
        fileInfo.type = typeFromLsOutput(typeAndPermissions.at(0));
        fileInfo.permissions = permissionsFromLsOutput(QByteArray::fromRawData(
                                                           typeAndPermissions.constData() + 1,
                                                           typeAndPermissions.size() - 1));
        bool isNumber;
        fileInfo.size = components.at(4).toULongLong(&isNumber);
        if (!isNumber) {
            qCWarning(sshLog) << "Don't know how to parse sftp ls output:" << line;
            continue;
        }
        // TODO: This will not work for file names with weird whitespace combinations
        fileInfo.name = QFileInfo(QString::fromUtf8(components.mid(8).join(' '))).fileName();
        allFileInfo << fileInfo;
    }
    emit fileInfoAvailable(jobId, allFileInfo);
}
示例#2
0
void LyricsParser_KFN::parse(QIODevice * file, LyricsLoader::Container& output, LyricsLoader::Properties &properties)
{
    // Parse the song.ini and fill up the sync and text arrays
    QByteArrayList texts;
    QList< int > syncs;

    // Analyze each line
    int idx_text = 0, idx_sync = 0;

    QByteArrayList lines = load( file ).split( '\n' );
    bool inGeneral = false;

    for ( int i = 0; i < lines.size(); i++ )
    {
        QByteArray line = lines[i];

        // Section detector
        if ( line.startsWith( '[' ) && line.endsWith( ']') )
        {
            inGeneral = (line == "[General]");
            continue;
        }

        if ( inGeneral )
        {
            if ( line.startsWith( "Title=" ) )
            {
                QString v = QString::fromUtf8( line.mid(6) ).trimmed();

                if ( !v.isEmpty() )
                    properties[ LyricsLoader::PROP_TITLE ] = v;
            }

            if ( line.startsWith( "Artist=" ) )
            {
                QString v = QString::fromUtf8( line.mid(7) ).trimmed();

                if ( !v.isEmpty() )
                    properties[ LyricsLoader::PROP_ARTIST ] = v;
            }
        }

        // Try to match the sync first
        char matchbuf[128];
        sprintf( matchbuf, "Sync%d=", idx_sync );

        if ( line.startsWith( matchbuf ) )
        {
            idx_sync++;

            // Syncs are split by comma
            QByteArrayList values = line.mid( strlen(matchbuf) ).split( ',' );

            for ( int v = 0; v < values.size(); v++ )
                syncs.push_back( values[v].toInt() );
        }

        // Now the text
        sprintf( matchbuf, "Text%d=", idx_text );

        if ( line.startsWith( matchbuf ) )
        {
            idx_text++;
            QByteArray textvalue = line.mid( strlen(matchbuf) );

            if ( !textvalue.isEmpty() )
            {
                // Text is split by word and optionally by the slash
                QByteArrayList values = textvalue.split(' ');

                for ( int v = 0; v < values.size(); v++ )
                {
                    QByteArrayList morevalues = values[v].split( '/' );

                    for ( int vv = 0; vv < morevalues.size(); vv++ )
                        texts.push_back( morevalues[vv] );

                    // We split by space, so add it at the end of each word
                    texts.last() = texts.last() + " ";
                }
            }

            // Line matched, so make it a line
            if ( texts.size() > 2 && texts[ texts.size() - 2 ] != "\n" )
                texts.push_back( "\n" );
        }
    }

    int curr_sync = 0;
    bool has_linefeed = false;
    int lines_no_block = 0;
    int lastsync = -1;

    // The original timing marks are not necessarily sorted, so we add them into a map
    // and then output them from that map
    QMap< int, QByteArray > sortedLyrics;

    for ( int i = 0; i < texts.size(); i++ )
    {
        if ( texts[i] == "\n" )
        {
            if ( lastsync == -1 )
                continue;

            if ( has_linefeed )
                lines_no_block = 0;
            else if ( ++lines_no_block > 6 )
            {
                lines_no_block = 0;
                sortedLyrics[ lastsync ] += "\n";
            }

            has_linefeed = true;
            sortedLyrics[ lastsync ] += "\n";
            continue;
        }
        else
            has_linefeed = false;

        // Get the time if we have it
        if ( curr_sync >= syncs.size() )
            continue;

        lastsync = syncs[ curr_sync++ ];
        sortedLyrics.insert( lastsync, texts[i] );
    }

    // Generate the content for encoding detection
    QByteArray lyricsForEncoding;

    for ( QMap< int, QByteArray >::const_iterator it = sortedLyrics.begin(); it != sortedLyrics.end(); ++it )
        lyricsForEncoding.append( it.value() );

    // Detect the encoding
    QTextCodec * codec = detectEncoding( lyricsForEncoding, properties );

    for ( QMap< int, QByteArray >::const_iterator it = sortedLyrics.begin(); it != sortedLyrics.end(); ++it )
    {
        qint64 timing = it.key() * 10;
        Lyric lyr( timing, codec->toUnicode( it.value() ) );

        // Last line?
        if ( lyr.text.endsWith( '\n') )
        {
            // Remove the \n
            lyr.text.chop( 1 );
            output.push_back( lyr );

            // and add an empty lyric
            output.push_back( Lyric( timing ) );
        }
        else
        {
            output.push_back( lyr );
        }
    }
}