FILE *SettingsManager::getSettingsFile( const char *inSettingName,
                                        const char *inReadWriteFlags ) {
    char *fullFileName = getSettingsFileName( inSettingName );
    
    FILE *file = fopen( fullFileName, inReadWriteFlags );

    delete [] fullFileName;
    
    return file;
    }
void SettingsManager::setSetting( const char *inSettingName,
                                  SimpleVector<char *> *inSettingVector ) {

    
    

    char **settingParts = inSettingVector->getElementArray();
    
    char *settingString = join( settingParts, inSettingVector->size(),
                                 "\n" );
    delete [] settingParts;
    

    if( mHashingOn ) {
        
        // compute hash
        char *stringToHash = autoSprintf( "%s%s",
                                          settingString,
                                          mStaticMembers.mHashSalt );

        char *hash = computeSHA1Digest( stringToHash );

        delete [] stringToHash;
        
        char *hashFileName = getSettingsFileName( inSettingName, "hash" );
    
        FILE *file = fopen( hashFileName, "w" );

        delete [] hashFileName;

        if( file != NULL ) {
            fprintf( file, "%s", hash );
            
            fclose( file );
            }
        
        delete [] hash;
        }
    



    FILE *file = getSettingsFile( inSettingName, "w" );
    
    if( file != NULL ) {
        
        fprintf( file, "%s", settingString );
        
        fclose( file );
        }

    delete [] settingString;

    // else do nothing
    }
Beispiel #3
0
const wxString mmex::getPathUser(EUserFile f)
{
    static const wxString files[USER_FILES_MAX] = {
      getSettingsFileName(),
      getDirectory()
    };

    wxASSERT(f >= 0 && f < USER_FILES_MAX);

    wxFileName fname = GetUserDir(true);
    fname.SetFullName(files[f]);

    return fname.GetFullPath();
}
SimpleVector<char *> *SettingsManager::getSetting( 
    const char *inSettingName ) {

    char *fileName = getSettingsFileName( inSettingName );
    File *settingsFile = new File( NULL, fileName );

    delete [] fileName;
    
    char *fileContents = settingsFile->readFileContents();

    delete settingsFile;


    
    if( fileContents == NULL ) {
        // return empty vector
        return new SimpleVector<char *>();
        }
    
    if( mHashingOn ) {
        
        char *hashFileName = getSettingsFileName( inSettingName, "hash" );
        
        File *hashFile = new File( NULL, hashFileName );
        
        delete [] hashFileName;
        
        char *savedHash = hashFile->readFileContents();
        
        delete hashFile;

        if( savedHash == NULL ) {
            printf( "Hash missing for setting %s\n", inSettingName );

            delete [] fileContents;
            return new SimpleVector<char *>();
            }
    
        
        // compute hash
        char *stringToHash = autoSprintf( "%s%s",
                                          fileContents,
                                          mStaticMembers.mHashSalt );

        char *hash = computeSHA1Digest( stringToHash );

        delete [] stringToHash;
        
        int difference = strcmp( hash, savedHash );
        
        delete [] hash;
        delete [] savedHash;
        

        if( difference != 0 ) {
            printf( "Hash mismatch for setting %s\n", inSettingName );
            
            delete [] fileContents;
            return new SimpleVector<char *>();
            }
        }


    // else tokenize the file contents
    SimpleVector<char *> *returnVector = tokenizeString( fileContents );

    delete [] fileContents;
    
    return returnVector;
    }
char *SettingsManager::getSettingsFileName( const char *inSettingName ) {
    return getSettingsFileName( inSettingName, "ini" );
    }