1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
/**
* Read block of data from an opened file
*
* Reads an block of uiCount bytes, from the file and stores
* in into the memory block named uchBuffer.
* The postion indicator of the file is moved foward of the total
* amount of bytes read
*
* If end of file is reached, no bytes would be read (returns 0).
*
*
* Notes: Works for FAT16 and FAT32
*
* @param uiCount Maximum number of bytes to be read [1 to 512]
* @param *pFile Pointer to a file object
*
* @return ... The total number of bytes successfully read
* If this number differs from the uiCount parameter,
* either an error occured or the End Of File was reached.
**/
unsigned int FILE_Read (unsigned int uiCount, _FILE * pFile)
{
unsigned long ulgSector, ulgSector2;
unsigned int i;
_SD_ERROR status;
if (pFile->CurrentByte == pFile->FileSize)
{ /* EOF reached, no more bytes to read */
return 0;
}
if ((pFile->CurrentByte + (unsigned long) uiCount) > pFile->FileSize)
{
/* can't read more than "FileSize" bytes */
uiCount = (unsigned int) (pFile->FileSize - pFile->CurrentByte);
}
ulgSector =
FAT_ClusterToSector (pFile->CurrentCluster) + pFile->SectorOffset;
if ((pFile->ByteOffset + uiCount) > BLOCK_LENGTH)
{
/* We need to cross a sector boundary */
pFile->SectorOffset++; /* next sector */
if (pFile->SectorOffset == uchFAT_SectorsPerCluster)
{
/* end of cluster, need to get the following */
/* Set the new current cluster */
pFile->CurrentCluster = FAT_GetNextCluster (pFile->CurrentCluster);
ulgSector2 = FAT_ClusterToSector (pFile->CurrentCluster);
pFile->SectorOffset = 0; /* first sector of the new cluster */
}
else
{
/* stay in same cluster, but get next sector */
ulgSector2 = ulgSector + 1;
}
if (pFile->ByteOffset == BLOCK_LENGTH)
{
status =
SD_SingleBlockRead (ulgSector2, (unsigned char *) uchBuffer);
}
else
{
status =
SD_MultipleBlockRead (ulgSector, ulgSector2, pFile->ByteOffset,
uiCount, (unsigned char *) uchBuffer);
}
if (status != SD_Valid)
{
return 0;
}
pFile->ByteOffset += uiCount;
pFile->ByteOffset -= BLOCK_LENGTH;
}
else
{
/* Read a single sector */
if (SD_SingleBlockRead (ulgSector, (unsigned char *) uchBuffer) !=
SD_Valid)
{
return 0;
}
for (i = 0; i < uiCount; i++)
{
uchBuffer[i] = uchBuffer[pFile->ByteOffset + i];
}
pFile->ByteOffset += uiCount;
}
pFile->CurrentByte += (unsigned long) uiCount;
return uiCount;
} |
Partager