NDK中提供了一个嵌入式的文件系统,其中
// efs_fread // // Returns number of objects read (set size to 1 to read bytes) size_t efs_fread( void *ptr, size_t size, size_t nobj, EFS_FILE *stream ) { FILEPTR *pf = (FILEPTR *)stream; FILEHEADER *pfh = pf->pfh; UINT8 *pb = ptr; INT32 didread = 0; INT32 canread;
// Check for Bozo if( !size ) return(0);
for(;;) { // Get number of bytes we can read canread = pfh->Length - pf->Pos;
// Get number of objects we can read canread /= (INT32)size;
// Check for max user request if( (canread+didread) > (INT32)nobj ) canread = (INT32)nobj - didread;
// If done reading, return bytes read if( canread <= 0 ) return( (size_t)didread * size );
// Else "read" memcpy( pb, pfh->pData + pf->Pos, (size_t)canread*size );
// Bump pointers pb += canread*(INT32)size; pf->Pos += canread*(INT32)size; didread += canread; } }
我觉得这个函数写法好怪哦,为什么不这么写呢,
size_t efs_fread( void *ptr, size_t size, size_t nobj, EFS_FILE *stream ) { FILEPTR *pf = (FILEPTR *)stream; FILEHEADER *pfh = pf->pfh; UINT8 *pb = ptr; INT32 canreadsize;
// Check for Bozo if( !(size * nobj)) return(0);
// Get number of bytes we can read canreadsize = pfh->Length - pf->Pos;
if((canreadsize / (INT32)size) > (INT32)nobj){ canreadsize = nobj * size; } memcpy( pb, pfh->pData + pf->Pos, (size_t)canreadsize ); return(canread); }