Beispiel #1
0
/*!
 * Setup a file for a possible append. The append will happen if the file
 * already exists, in which case it is read out, and the closing statement
 * removed so append can be performed
 */
void ProtocolSourceFile::prepareToAppend(void)
{
    QFile file(fileName());

    if(file.exists())
    {
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            std::cout << "Failed to open " << fileName().toStdString() << " for append" << std::endl;
            return ;
        }

        // Read the entire file, and store as existing text string data
        contents = file.readAll();

        // Close the file, we don't need it anymore
        file.close();

        // Remove the trailing closing statement from the file so we can append further stuff
        contents.remove(getClosingStatement());

        // we are appending
        appending = true;

    }// If the file already exists

}// ProtocolSourceFile::prepareToAppend
Beispiel #2
0
/*!
 * Write the file to disc, including any prologue/epilogue
 * \return true if the file is written, else there is a problem opening\overwriting the file
 */
bool ProtocolHeaderFile::flush(void)
{
    if(!dirty)
        return false;

    // Got to have a name
    if(module.isEmpty())
    {
        std::cout << "Empty module name when writing protocol header file" << std::endl;
        return false;
    }

    QFile file(fileName());

    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        std::cout << "Failed to open " << fileName().toStdString() << std::endl;
        return false;
    }

    if(!appending)
    {
        // Tag for when the file was generated
        if(versionOnly)
            file.write(qPrintable("// " + file.fileName() + " was generated by ProtoGen version " + ProtocolParser::genVersion + "\n\n"));
        else
            file.write(qPrintable("// " + file.fileName() + " was generated by ProtoGen version " + ProtocolParser::genVersion + " on " + QDateTime::currentDateTime().toString() + "\n\n"));

        // The opening #ifdef
        QString define = "_" + module.toUpper() + "_H";
        file.write(qPrintable("#ifndef " + define + "\n"));
        file.write(qPrintable("#define " + define + "\n"));

        file.write("\n// C++ compilers: don't mangle us\n");
        file.write("#ifdef __cplusplus\n");
        file.write("extern \"C\" {\n");
        file.write("#endif\n\n");
    }

    // The actual interesting contents
    file.write(qPrintable(contents));

    // close the file out
    file.write(qPrintable(getClosingStatement()));

    // And the file
    file.close();

    // Empty our data
    clear();

    return true;

}// ProtocolHeaderFile::flush
/*!
 * Setup a file for a possible append. The append will happen if the file
 * already exists, in which case it is read out, and the closing statement
 * removed so append can be performed
 */
void ProtocolHeaderFile::prepareToAppend(void)
{    
    QFile file(fileNameAndPathOnDisk());

    if(file.exists())
    {
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            std::cerr << "Failed to open " << fileName().toStdString() << " for append" << std::endl;
            return ;
        }

        // Read the entire file, and store as existing text string data
        contents = file.readAll();

        // Close the file, we don't need it anymore
        file.close();

        // Remove the trailing closing statement from the file so we can append further stuff
        contents.remove(getClosingStatement());

        // we are appending
        appending = true;

    }// If the file already exists
    else
    {
        // If this file does not yet exist, then put the stuff on top that is always included

        // Tag for what generated the file
        write(qPrintable("// " + fileName() + " was generated by ProtoGen version " + ProtocolParser::genVersion + "\n\n"));

        if (!license.isEmpty())
        {
            write(license);
            makeLineSeparator();
        }

        // The opening #ifdef
        QString define = "_" + module.toUpper() + "_H";
        write(qPrintable("#ifndef " + define + "\n"));
        write(qPrintable("#define " + define + "\n"));

        if(!iscpp)
        {
            write("\n// C++ compilers: don't mangle us\n");
            write("#ifdef __cplusplus\n");
            write("extern \"C\" {\n");
            write("#endif\n\n");
        }
    }

}// ProtocolHeaderFile::prepareToAppend
Beispiel #4
0
/*!
 * Write the file to disc, including any prologue/epilogue
 * \return true if the file is written, else there is a problem opening\overwriting the file
 */
bool ProtocolSourceFile::flush(void)
{
    if(!dirty)
        return false;

    // Got to have a name
    if(module.isEmpty())
    {
        std::cout << "Empty module name when writing protocol source file" << std::endl;
        return false;
    }

    QFile file(fileName());

    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        std::cout << "Failed to open " << fileName().toStdString() << std::endl;
        return false;
    }

    if(!appending)
    {
        // Tag for when the file was generated
        if(versionOnly)
            file.write(qPrintable("// " + file.fileName() + " was generated by ProtoGen version " + ProtocolParser::genVersion + "\n\n"));
        else
            file.write(qPrintable("// " + file.fileName() + " was generated by ProtoGen version " + ProtocolParser::genVersion + " on " + QDateTime::currentDateTime().toString() + "\n\n"));

        // The source file includes the header
        file.write(qPrintable("#include \"" + module + ".h\"\n"));
    }

    // The part of the file that goes before the main body
    file.write(qPrintable(prototypeContents));

    // The actual interesting contents
    file.write(qPrintable(contents));

    // Close it out
    file.write(qPrintable(getClosingStatement()));

    // And the file
    file.close();

    // Empty our data
    clear();

    return true;

}// ProtocolSourceFile::flush
/*!
 * Setup a file for a possible append. The append will happen if the file
 * already exists, in which case it is read out, and the closing statement
 * removed so append can be performed
 */
void ProtocolSourceFile::prepareToAppend(void)
{
    QFile file(fileNameAndPathOnDisk());

    if(file.exists())
    {
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            std::cerr << "Failed to open " << file.fileName().toStdString() << " for append" << std::endl;
            return ;
        }

        // Read the entire file, and store as existing text string data
        contents = file.readAll();

        // Close the file, we don't need it anymore
        file.close();

        // Remove the trailing closing statement from the file so we can append further stuff
        contents.remove(getClosingStatement());

        // we are appending
        appending = true;

    }// If the file already exists
    else
    {
        // If this file does not yet exist, then put the stuff on top that is always included

        // Tag for what generated the file
        write(qPrintable("// " + fileName() + " was generated by ProtoGen version " + ProtocolParser::genVersion + "\n\n"));

        if (!license.isEmpty())
        {
            write(license);
            makeLineSeparator();
        }

        // The source file includes the header
        write(qPrintable("#include \"" + module + ".h\"\n"));
    }

}// ProtocolSourceFile::prepareToAppend
/*!
 * Write the file to disc, including any prologue/epilogue
 * \return true if the file is written, else there is a problem opening\overwriting the file
 */
bool ProtocolSourceFile::flush(void)
{
    // Nothing to write
    if(!dirty || contents.isEmpty())
        return false;

    // Got to have a name
    if(module.isEmpty())
    {
        std::cerr << "Empty module name when writing protocol source file" << std::endl;
        return false;
    }

    QFile file(fileNameAndPathOnDisk());

    // Make sure the path exists
    if(!path.isEmpty())
        QDir::current().mkpath(path);

    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        std::cerr << "Failed to open " << fileName().toStdString() << std::endl;
        return false;
    }

    // The actual interesting contents
    file.write(qPrintable(contents));

    // Close it out
    file.write(qPrintable(getClosingStatement()));

    // And the file
    file.close();

    // Empty our data
    clear();

    return true;

}// ProtocolSourceFile::flush