Finding a File

25 Mar

Finding a File
The FILEfind function searches for a specific file in a directory or for an empty entry. The function accepts two FILEOBJ pointers to FILE structures. One structure (foCompareTo) contains a file name to search for and the other (fodest) will hold information about a file or empty entry if found. The third parameter (cmd) indicates whether to search for an entry that matches the file name in foCompareTo (1) or an empty entry (2). The function returns a status code.

The function calls the Cache_File_Entry function from Chapter 9 and the Fill_File_Object function above. In the passed file structure, the dirclus member must contain the number of the first cluster in the directory to search.

#define ATTR_MASK 0×3f
#define ATTR_HIDDEN 0×02
#define ATTR_VOLUME 0×08
#define FOUND 0 // directory entry match
CETYPE FILEfind(FILEOBJ foDest, FILEOBJ foCompareTo, byte cmd)
{
word attrib;
byte character;
word fHandle = 0;
byte index;
byte state;
CETYPE statusB = CE_FILE_NOT_FOUND;
byte test;

// Set the destination FILE structure’s current cluster to the directory’s cluster.
foDest -> dirccls = foDest -> dirclus;
// Read a directory entry.
if ( Cache_File_Entry(foDest, &fHandle, TRUE) == NULL)
{
statusB = CE_BADCACHEREAD;
}
else
{
// Read entries until finding the file or the end of the directory.
while (1)
{
if (statusB!=CE_GOOD)
{
// Store information about the file.
state = Fill_File_Object(foDest, &fHandle);
if (state == NO_MORE)
{
// The entry doesn’t exist or is empty.
break;
}
}
else
{
// There was a problem in reading the file information.
break;
}
if (state == FOUND)
{
// An entry was found. Read the attributes.
attrib = foDest -> attributes;

attrib &= ATTR_MASK;

// If the entry is for a volume ID or hidden file, skip it.
if ((attrib != ATTR_VOLUME) && (attrib & ATTR_HIDDEN) != ATTR_HIDDEN)
{
statusB = CE_GOOD;
character = (byte)’m'; // random value
// Look for a name match.
for (index = 0; (statusB == CE_GOOD) && index < DIR_NAMECOMP; index++)
{
// Get a character from the found file’s name.
character = foDest -> name[index];
// Get the corresponding character from the file name we’re searching for.
test = foCompareTo -> name[index];
if (tolower(character) != tolower(test))
// Quit the loop if a character doesn’t match.
statusB = CE_FILE_NOT_FOUND;
}
}
} // End: An entry was found.
else
{
// An empty or deleted entry was found.
if ( cmd == 2)
statusB = CE_GOOD;
}
// Increment the number of the entry in the directory.
fHandle++;
} // End: loop until found or end of directory.
} // End: Cache_File_Entry was successful.

return(statusB);
}

Random Posts

No comments yet

Leave a Reply

You must be logged in to post a comment.