Sources/Volume.cp

/*
    File:       Volume.cp
 
    Contains:   TVolume is a simple volume based utility class (provides information about volumes)     
                TVolume.cp contains the TVolume member functions. 
 
    Written by: Kent Sandvik    
 
    Copyright:  Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source 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 source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/18/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
#ifndef _VOLUME_
#include "Volume.h"
#endif
 
// CONSTRUCTORS & DESTRUCTORS
#pragma segment Volume
TVolume::TVolume()
{
    // Assume we will start with the default boot volume.
    fVRefNum = this->GetBootVRefNum();
    fIndex = 1;                                 // boot block index
}
 
 
#pragma segment Volume
TVolume::TVolume(short refNum)
{
    // Use specified refNum.
    fVRefNum = refNum;
}
 
 
#pragma segment Volume
TVolume::~TVolume()
{
}
 
 
// MAIN INTERFACE
#pragma segment Volume
short TVolume::GetBootVRefNum()
// Return out the VRefNum of the boot volume.
{
    short ourVRefNum = 0;
    long ourDirID = 0;
 
    fError = ::FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &ourVRefNum, &ourDirID);
    VASSERT(fError == noErr, ("Problems with FindFolder = %d", fError));
 
    return ourVRefNum;
}
 
 
#pragma segment Volume
long TVolume::GetKFreeSpace()
// Get amount of free space (Kb) on defined volume.
{
    if (this->GetHParamBlock())
        return ((fPB.volumeParam.ioVFrBlk * fPB.volumeParam.ioVAlBlkSiz) / 1024);
    else
        return -1;
}
 
 
#pragma segment Volume
long TVolume::GetFileCount()
// Return amount of files on volume.
{
    if (this->GetHParamBlock())
        return fPB.volumeParam.ioVFilCnt;
    else
        return -1;
}
 
 
#pragma segment Volume
long TVolume::GetDirCount()
// Return amount of folders on volume.
{
    if (this->GetHParamBlock())
        return fPB.volumeParam.ioVDirCnt;
    else
        return -1;
}
 
 
#pragma segment Volume
long TVolume::GetCreationDate()
// Return creation date of volume.
{
    if (this->GetHParamBlock())
        return fPB.volumeParam.ioVCrDate;
    else
        return -1;
}
 
 
#pragma segment Volume
short TVolume::GetDriverNumber()
// Get specified driver number.
{
    if (this->GetHParamBlock())
        return fPB.volumeParam.ioVDrvInfo;
    else
        return 0;
}
 
 
#pragma segment Volume
Boolean TVolume::GetName(Str255& name)
// Get name of volume.
{
 
    fPB.volumeParam.ioNamePtr = (StringPtr)name;
    fPB.volumeParam.ioVRefNum = fVRefNum;
    fPB.volumeParam.ioVolIndex = fIndex;
 
    fError = ::PBHGetVInfoSync(&fPB);
    VASSERT(fError == noErr, ("Problems with PBHGetVInfo = %d", fError));
 
    return (fError == noErr);
}
 
 
#pragma segment Volume
short TVolume::GetNumVolumes()
// Get the amount of mounted volumes (at this instance).
{
    short num = 0;
 
    for (this->Reset(); !this->Last(); this->Next())
        num++;
 
    return num;
}
 
 
// GET/SET FUNCTIONS
#pragma segment Volume
void TVolume::SetVRefNum(const short refNum)
// Set used VRefNum.
{
    fVRefNum = refNum;
}
 
 
#pragma segment Volume
short TVolume::GetVRefNum() const
// Get used VRefNum.
{
    return fVRefNum;
}
 
 
// ITERATORS
#pragma segment Volume
void TVolume::Reset()
// Get back to boot block references.
{
    fVRefNum = this->GetBootVRefNum();          // boot block VRefNum
    fIndex = 1;                                 // boot block index
    fLast = false;                              // don't know that yet
    this->Next();                               // step once to the right position (boot drive)
}
 
 
#pragma segment Volume
Boolean TVolume::Last()
// Return boolan if this is the last volume or not.
{
    return fLast;
}
 
 
#pragma segment Volume
void TVolume::Next()
// Iterate to next volume mounted.
{
    if (!fLast)
    {
        fPB.volumeParam.ioNamePtr = NULL;
        fPB.volumeParam.ioVRefNum = fVRefNum;
        fPB.volumeParam.ioVolIndex = fIndex;
 
        fError = ::PBHGetVInfoSync(&fPB);
 
        if (fError != nsvErr)                   // "No such volume" indicates that we are the end
        {
            fIndex++;                           // OK, increase the index
            fVRefNum = fPB.volumeParam.ioVRefNum;// keep track of the VRefNum
        }
        else
            fLast = true;                       // we hit the end, raise the flag
    }
}
 
 
// INTERNAL FUNCTIONS
#pragma segment Volume
Boolean TVolume::GetHParamBlock()
// Generate a lookup in a pre-defined param block (used by other member functions).
{
    fPB.volumeParam.ioNamePtr = NULL;
    fPB.volumeParam.ioVRefNum = fVRefNum;
    fPB.volumeParam.ioVolIndex = 0;
 
    fError = ::PBHGetVInfoSync(&fPB);
    VASSERT(fError == noErr, ("Problems with PBHGetVInfo = %d", fError));
 
    return (fError == noErr);
}
 
 
// _________________________________________________________________________________________________________ //
 
 
/*  Change History (most recent last):
  No        Init.   Date        Comment
  1         khs     1/5/93      New file
  2         khs     1/7/93      Cleanup
*/