void MarkdownEditAreaWidget::initContent( const QString &filePath )
{
    doc = QSharedPointer<QTextDocument>( new QTextDocument( NULL ) );
    QAbstractTextDocumentLayout *layout = new QPlainTextDocumentLayout( doc.data() );
    doc->setDocumentLayout( layout );
    editor->setDocument( doc.data() );
    highlighter = new MarkdownHighLighter( doc.data() );

    //deal file content
    if ( filePath.isEmpty() ) {
        fm->setEncodingFormatName( conf->getDefaultEncoding() ); //default encoding
        setModified( false );
        return;
    }

    QFile openFile( filePath );

    if ( !openFile.open( QIODevice::ReadOnly ) ) {
        Utils::showFileError( openFile.error(), filePath );
        return;
    }

    QTextStream openFileStream( &openFile );
    //autodetected
    QString fileContent = openFileStream.readAll();
    QByteArray cn = openFileStream.codec()->name();
    fm->setEncodingFormatName( cn );

    if ( cn.startsWith( "UTF" ) ) { //UTF8 UTF16LE UTF16BE
        fm->setHasBom( true );
    }

    if ( cn == QByteArray( "System" ) ) {
        //check if it is utf8 without bom
        openFile.seek( 0 );

        if ( Utils::isUtf8WithoutBom( openFile.readAll() ) ) {
            openFileStream.setCodec( "UTF-8" );
            fm->setEncodingFormatName( "UTF-8" );
            fm->setHasBom( false );
            openFileStream.seek( 0 );
            fileContent = openFileStream.readAll();
        }
    }

    if ( cn.isEmpty() ) {
        fm->setEncodingFormatName( conf->getDefaultEncoding() );
        fm->setHasBom( true );
    }

    doc->setPlainText( fileContent );
    doc->setModified( false );
    openFile.close();
}
void InMemoryStorageBackend::load(void) {
    bool isBackupValid = m_integrity.backupGuardExists();
    std::string bucketSuffix = "";
    std::string indexFilename = m_dbPath + m_indexFilename;
    std::string chsFilename = m_dbPath + m_chsFilename;

    if (isBackupValid) {
        bucketSuffix += m_backupFilenameSuffix;
        indexFilename += m_backupFilenameSuffix;
        chsFilename += m_backupFilenameSuffix;
    }

    try {
        std::ifstream chsStream;
        openFileStream(chsStream, chsFilename, isBackupValid);
        m_checksum.load(chsStream);

        auto indexStream = std::make_shared<std::ifstream>();
        openFileStream(*indexStream, indexFilename, isBackupValid);

        StorageDeserializer storageDeserializer(indexStream,
            std::bind(&InMemoryStorageBackend::bucketStreamOpener, this,
                      std::placeholders::_1, bucketSuffix, isBackupValid));

        storageDeserializer.initBuckets(buckets());
        storageDeserializer.loadBuckets(buckets());
    } catch (const DatabaseException &) {
        LOGC("Reading cynara database failed.");
        buckets().clear();
        throw DatabaseCorruptedException();
    }
    m_checksum.clear();

    if (!hasBucket(defaultPolicyBucketId)) {
        LOGN("Creating defaultBucket.");
        this->buckets().insert({ defaultPolicyBucketId, PolicyBucket(defaultPolicyBucketId) });
    }

    postLoadCleanup(isBackupValid);
}
std::shared_ptr<BucketDeserializer> InMemoryStorageBackend::bucketStreamOpener(
        const PolicyBucketId &bucketId, const std::string &filenameSuffix, bool isBackupValid) {
    std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix + bucketId + filenameSuffix;
    auto bucketStream = std::make_shared<std::ifstream>();
    try {
        openFileStream(*bucketStream, bucketFilename, isBackupValid);
        return std::make_shared<BucketDeserializer>(bucketStream);
    } catch (const FileNotFoundException &) {
        return nullptr;
    } catch (const std::bad_alloc &) {
        return nullptr;
    }
}
Exemple #4
0
void KardView::loadSyllables()
{
    kDebug() << "Language: " << KardSettings::selectedLanguage() << endl;
    QString myString=QString("kard/data/%1/syllables.txt").arg(KardSettings::selectedLanguage());
    QFile myFile;
    myFile.setFileName(KStandardDirs::locate("data", myString));
    QFile openFileStream(myFile.fileName());
    openFileStream.open(QIODevice::ReadOnly);
    QTextStream readFileStr(&openFileStream);
    //allData contains all the words from the file
    QStringList allData = readFileStr.readAll().split("\n");
    openFileStream.close();
    //get text[0] to text[23] from random from the file 
    //with 2 consecutives the same
    int i=0;
    while (i<24)  {
	int rand = random.getLong(allData.count());
	text[i]=text[i+1] = allData[rand];
	allData.removeAt(rand);//remove the chosen syllable from the list
	i=i+2;
    }
}
int main (void)
{
	FILE *inputStream = NULL,			/* File pointer for input file */
		 *outputStream = NULL;			/* File pointer for output file */

	int studentId1 = 0,
		studentId2 = 0,
		studentId3 = 0,
		studentId4 = 0,
		studentId5 = 0;					/* 5 student IDs */

	int studentClassStanding1 = 0,
		studentClassStanding2 = 0,
		studentClassStanding3 = 0,
		studentClassStanding4 = 0,
		studentClassStanding5 = 0;		/* 5 student class standings */

	double studentGpa1 = 0.0,
		   studentGpa2 = 0.0,
		   studentGpa3 = 0.0,
		   studentGpa4 = 0.0,
		   studentGpa5 = 0.0;			/* 5 student GPAs */

	double studentAge1 = 0.0,
		   studentAge2 = 0.0,
		   studentAge3 = 0.0,
		   studentAge4 = 0.0,
		   studentAge5 = 0.0;			/* 5 student ages */

	double sumGpa = 0.0,				/* sum of the 5 student's GPA */
		   sumClassStanding = 0.0,		/* sum of the 5 student's class standing */
		   sumAge = 0.0;				/* sum of the 5 student's ages */

	double meanGpa = 0.0,				/* mean of the 5 student's GPA */
		   meanClassStanding = 0.0,		/* mean of the 5 student's class standing */
		   meanAge = 0.0;				/* mean of the 5 student's ages */

	double deviationGpa1 = 0.0,		
		   deviationGpa2 = 0.0,
		   deviationGpa3 = 0.0,
		   deviationGpa4 = 0.0,
		   deviationGpa5 = 0.0;			/* deviation of each student's GPA */

	double varianceGpa = 0.0,			/* variance of the 5 student's GPA */
		   standardDeviationGpa = 0.0,	/* standard variance of the 5 student's GPA */
		   maxStudentGpa = 0.0,			/* maximum number out of the 5 student's GPA */
		   minStudentGpa = 0.0;			/* minimum number out of the 5 student's GPA */


	/* Opens an input file "input.dat" for reading; */
	inputStream = openFileStream (INPUT_FILE, INPUT_FILE_MODE);

    /* Opens an output file "output.dat" for writing; */
	outputStream = openFileStream (OUTPUT_FILE, OUTPUT_FILE_MODE);

	/* Checks the condition if files were successfully opened.
	 * If either file didn't open, then an error message is prompted,
	 * otherwise program will continue to execute other operations. */
	if (inputStream == NULL || outputStream == NULL)
	{
		/* Files were not successfully opened and prompts user of the error.
		 * Program will not continue to execute the rest of the program. */
		printf ("Error: Not able to open files!\n");
	}
	else
	{
		/* Reads five records from the input file (input.dat); */
	
		/* Student 1 */
		studentId1 = readInteger (inputStream);
		studentGpa1 = readDouble (inputStream);
		studentClassStanding1 = readInteger (inputStream);
		studentAge1 = readDouble (inputStream);

		/* Student 2 */
		studentId2 = readInteger (inputStream);
		studentGpa2 = readDouble (inputStream);
		studentClassStanding2 = readInteger (inputStream);
		studentAge2 = readDouble (inputStream);

		/* Student 3 */
		studentId3 = readInteger (inputStream);
		studentGpa3 = readDouble (inputStream);
		studentClassStanding3 = readInteger (inputStream);
		studentAge3 = readDouble (inputStream);

		/* Student 4 */
		studentId4 = readInteger (inputStream);
		studentGpa4 = readDouble (inputStream);
		studentClassStanding4 = readInteger (inputStream);
		studentAge4 = readDouble (inputStream);

		/* Student 5 */
		studentId5 = readInteger (inputStream);
		studentGpa5 = readDouble (inputStream);
		studentClassStanding5 = readInteger (inputStream);
		studentAge5 = readDouble (inputStream);

		/* Calculates the sum of the GPAs; */
		sumGpa = calculateSum (studentGpa1, studentGpa2, 
							   studentGpa3, studentGpa4, 
							   studentGpa5);

		/* Calculates the sum of the class standings; */
		sumClassStanding = calculateSum (studentClassStanding1, studentClassStanding2, 
										 studentClassStanding3, studentClassStanding4, 
										 studentClassStanding5);

		/* Calculates the sum of the ages; */
		sumAge = calculateSum (studentAge1, studentAge2,
								studentAge3, studentAge4,
								studentAge5);

		/* Calculates the mean of the GPAs; */
		meanGpa = calculateMean (sumGpa, NUMBER_OF_STUDENTS);

		/* Calculates the mean of the class standings; */
		meanClassStanding = calculateMean (sumClassStanding, NUMBER_OF_STUDENTS);

		/* Calculates the mean of the ages; */
		meanAge = calculateMean (sumAge, NUMBER_OF_STUDENTS);
 
		/* Calculates the deviation of each GPA from the mean */
		deviationGpa1 = calculateDeviation (studentGpa1, meanGpa);
		deviationGpa2 = calculateDeviation (studentGpa2, meanGpa);
		deviationGpa3 = calculateDeviation (studentGpa3, meanGpa);
		deviationGpa4 = calculateDeviation (studentGpa4, meanGpa);
		deviationGpa5 = calculateDeviation (studentGpa5, meanGpa);

		/* Calculates the variance of the GPAs */
		varianceGpa = calculateVariance (deviationGpa1, deviationGpa2,
										 deviationGpa3, deviationGpa4,
										 deviationGpa5, NUMBER_OF_STUDENTS);

		/* Calculates the standard deviation of the GPAs; */
		standardDeviationGpa = calculateStandardDeviation (varianceGpa);
	
		/* Determines the min of the GPAs; */
		minStudentGpa = findMin (studentGpa1, studentGpa2, 
								 studentGpa3, studentGpa4, 
								 studentGpa5);	

		/* Determines the max of the GPAs; */
		maxStudentGpa = findMax (studentGpa1, studentGpa2, 
								 studentGpa3, studentGpa4, 
								 studentGpa5);

		/* Writing the result to the output file (output.dat) */
		printDouble (outputStream, meanGpa);
		printDouble (outputStream, meanClassStanding);
		printDouble (outputStream, meanAge);
		printDouble (outputStream, standardDeviationGpa);
		printDouble (outputStream, minStudentGpa);
		printDouble (outputStream, maxStudentGpa);

		/* Closes the input and output files (i.e. input.dat and output.dat) */
		closeFileStream (inputStream);
		closeFileStream (outputStream);
	}

	return 0;
}