sources/TPrefs.cp

//  TPrefs.cp - Preference Object  
// 
// Apple Macintosh Developer Technical Support
// Written by:  Vinnie Moscaritolo
//
//  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
//
// You may incorporate this sample code into your applications without
// restriction, though the sample code has been provided "AS IS" and the
// responsibility for its operation is 100% yours.  However, what you are
// not permitted to do is to redistribute the source as "DSC Sample Code"
// after having made changes. If you're going to re-distribute the source,
// we require that you make it clear in the source that the code was
// descended from Apple Sample Code, but that you've made changes.
// 
 
#include <PLStringFuncs.h>
#include <string.h>
#include <Files.h>
 
#include "TPrefs.h"
#include "TMacException.h"
 
 
// ---------------------------------------------------------------------------
    TPrefs::TPrefs() //   
// ---------------------------------------------------------------------------
//  
{
 
    fCollection = NewCollection();
    ThrowMacErrIfNil(fCollection, nilHandleErr);    
    
 }
 
 
// ---------------------------------------------------------------------------
    TPrefs::~TPrefs() //   
// ---------------------------------------------------------------------------
//  
{ 
    DisposeCollection (fCollection);
 
}
 
// ---------------------------------------------------------------------------
OSErr TPrefs::Set(  FourCharCode    tag, 
                    SInt32          id,
                    SInt32          itemSize,
                    void *          itemData)
// ---------------------------------------------------------------------------
//  
{ 
    return AddCollectionItem(fCollection, tag, id, itemSize, itemData);
 
}
 
// ---------------------------------------------------------------------------
OSErr TPrefs::Get(  FourCharCode    tag, 
                    SInt32          id,
                    SInt32*         itemSize,
                    void *          itemData)
// ---------------------------------------------------------------------------
//  
{ 
    return GetCollectionItem(fCollection, tag, id, itemSize, itemData);
}
 
 
 
// ---------------------------------------------------------------------------
OSErr TPrefs::Read() //   
// ---------------------------------------------------------------------------
//  
{ 
    FSSpec  fss;
    OSErr   err;
    
    err = this->GetPrefSpec(&fss);
    if(!err)
    {
        short   fileRefNum;
        Size    eof;
        
        SInt32  ByteCount;
        SInt32  size;
    
        FSpOpenDF (&fss,fsRdPerm,&fileRefNum);
 
        GetEOF (fileRefNum, &eof);
    
        size = sizeof (SInt32);
        err = FSRead (fileRefNum ,&size, &ByteCount);
        if(!err)
        {
            if(ByteCount + sizeof (SInt32) <= eof)  
            {
                Handle buffer = NewHandle (ByteCount);
                if (!buffer) err = MemError ( );
                HLockHi (buffer);
 
                size = ByteCount;
                err = FSRead (fileRefNum ,&size, *buffer);
                if(!err)
                {
                    err = UnflattenCollectionFromHdl ( fCollection, buffer);
                }
                HUnlock (buffer);
            }
            else 
                err = eofErr;
        }
        
        FSClose (fileRefNum);
    }
    return err;
}
 
 
 
// ---------------------------------------------------------------------------
OSErr TPrefs::CreatePrefFile(FSSpec *fss)   
// ---------------------------------------------------------------------------
//  
{ 
    return ( FSpCreate (fss, '????' ,kPrefsFileType,smSystemScript) );
 
}
 
 
// ---------------------------------------------------------------------------
OSErr TPrefs::GetPrefSpec(FSSpec *fss)   
// ---------------------------------------------------------------------------
//  
{ 
    FSSpec      prefsFSS;
 
// find the pref folder
    ThrowIfMacErr( FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &prefsFSS.vRefNum, &prefsFSS.parID ));
 
    // make a file spec for the prefs file 
    FSMakeFSSpec(prefsFSS.vRefNum, prefsFSS.parID , "\ppref file", fss);
 
// needs to be written to general case.
    return (-1);
 
}
 
// ---------------------------------------------------------------------------
void TPrefs::Write() //   
// ---------------------------------------------------------------------------
//  
{ 
    FSSpec  fss;
    OSErr   err;
    short   fileRefNum;
 
 
    if(fCollection )
    {
 
        err = this->GetPrefSpec(&fss);
 
        if( err == fnfErr   )
        {
            err = this->CreatePrefFile(&fss);
        }
 
        if(!err)
        {
 
            Handle flattened = NewHandle(0);
            SInt32 size;
            SInt32 ByteCount;
            
            
            ThrowIfMacErr( FSpOpenDF (&fss,fsWrPerm,&fileRefNum));
 
            err = FlattenCollectionToHdl(fCollection, flattened);
            ByteCount = GetHandleSize(flattened);
            
            size = sizeof (SInt32);
            FSWrite (fileRefNum, &size ,&ByteCount);
            
            size = ByteCount;
            FSWrite (fileRefNum, &size ,*flattened);
            
            DisposeHandle(flattened);
        
            FSClose (fileRefNum);
            
            FlushVol (nil, fss.vRefNum);
        }
        
    }
 
 
}