📄 findx.c
字号:
// fill in the ffblk structure
ffres->ff_reserved = (long)ffInfo;
ffres->ff_attrib = curEntry->attribute;
ffres->ff_ftime = curEntry->time;
ffres->ff_fdate = curEntry->date;
ffres->ff_fsize = curEntry->fsize;
ffres->ff_drive = RAMDISK_ID;
// copy the name from dir entry to result
i = 0;
// modified by chilong 01/29/2002
//while (curEntry->name[i] != '\0')
while(curEntry->name[i] != '\0' && i < RD_FNAME_LEN)
{
ffres->ff_name[i] = curEntry->name[i];
i++;
}
ffres->ff_name[i] = '\0';
#ifdef CURRENT_DIR_ENABLE
ap_free(completeName);
#endif
return 0;
} // if loop status
// not found, try next entry
curEntry = (struct directory *)((unsigned long)curEntry + dirSize);
} // for loop i
// not found in this cluster, try next cluster in the chain
curBlock = (struct diskBlock *)curBlock->nextBlock;
}
// all clusters in the chain have been searched, file not found
#ifdef CURRENT_DIR_ENABLE
ap_free(completeName);
#endif
ffres->ff_reserved = 0;
RDerrno = ERROR_FILE_NOT_EXIST;
return -1;
}
/*************************************************************
Function : RD_findnext
Description:
find the next file that match the requirement
Input:
ffres - a structure to hold the results of the search
Output:
0 if a match has been found
-1 if the searching failed
Note:
There is a hidden attribute in [attrib]. If the highest
bit in [attrib] is 0, only directories will be found
if DA_DIR is set. If the highest bit in [attrib] is 1,
both files and directories will be found if DA_DIR is
set.
**************************************************************/
int RD_findnext(struct ffblk *ffres)
{
int status;
RDerrno = 0;
if (InitRD == FALSE)
{
RDerrno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
if (ffres->ff_reserved == 0)
{
RDerrno = ERROR_FILE_NOT_EXIST;
return -1;
}
if (ffres->ff_drive != RAMDISK_ID)
{
RDerrno = ERROR_INVALID_DEVICE;
return -1;
}
sc_waitSemaphore(RD_SemaphoreID);
status = RD_findnext_r(ffres);
sc_signalSemaphore(RD_SemaphoreID);
return status;
}
int RD_findnext_r(struct ffblk *ffres)
{
struct ffInternal *ffInfo; /* internal information for findnext */
struct diskBlock *curBlock;
struct directory *curEntry;
long dirSize;
long i;
int nThEntry; /* the n-th entry in root block */
// get the pointer to the internal filefind structure
ffInfo = (struct ffInternal *)ffres->ff_reserved;
// start searching from this cluster
curBlock = ffInfo->block;
nThEntry = (int)ffInfo->nThEntry;
dirSize = sizeof(struct directory);
while (curBlock != NULL)
{
curEntry = (struct directory *)((unsigned long)curBlock + sizeof(struct diskBlock) + (nThEntry + 1) * dirSize);
// compare entry by entry in the sector
for (i = (nThEntry + 1) * dirSize; i < DIR_BLOCK_SIZE; i+= dirSize)
{
// this entry and every following entry are empty => not found
if (curEntry->name[0] == '\0')
{
RDerrno = ERROR_FILE_NOT_EXIST;
return -1;
}
// skip this entry if it an deleted entry
if (curEntry->name[0] == DELETED_ENTRY)
{
curEntry = (struct directory *)((unsigned long)curEntry + dirSize);
continue;
}
// compare the attribute of this entry with the given attribute
// go to next entry if the attributes unmatch
if ((curEntry->attribute & ffInfo->attrib) != curEntry->attribute)
{
curEntry = (struct directory *)((unsigned long)curEntry + dirSize);
continue;
}
/* marked by chilong 03/07/2002
// DIR are shown only if DA_DIR is specified by user
if ((ffInfo->attrib & DA_DIR) == DA_DIR)
if ((curEntry->attribute & DA_DIR) != DA_DIR)
{
curEntry = (struct directory *)((unsigned long)curEntry + dirSize);
continue;
}
marked by chilong 03/07/2002 */
/**** modified by MSC 03/07/2002 ****/
if ((ffInfo->attrib & FF_DA_DIR_OR) == 0)
{
// DIR are shown only if DA_DIR is specified by user
if ((ffInfo->attrib & DA_DIR) == DA_DIR)
if ((curEntry->attribute & DA_DIR) != DA_DIR)
{
curEntry = (struct directory *)((unsigned long)curEntry + dirSize);
continue;
}
}
/**** modified by MSC 03/07/2002 ****/
// attributes match, compare filename with the wildcard name
if (RD_nameCompare(ffInfo->wildname, curEntry->name) == 1)
{
// update the ffInternal structure
ffInfo->nThEntry = i / dirSize;
ffInfo->block = curBlock;
// update the ffblk structure
// copy field from dir entry to result
ffres->ff_attrib = curEntry->attribute;
ffres->ff_ftime = curEntry->time;
ffres->ff_fdate = curEntry->date;
ffres->ff_fsize = curEntry->fsize;
// copy the name from dir entry to result
i = 0;
// modified by chilong 01/29/2002
//while (curEntry->name[i] != '\0')
while(curEntry->name[i] != '\0' && i < RD_FNAME_LEN)
{
ffres->ff_name[i] = curEntry->name[i];
i++;
}
ffres->ff_name[i] = '\0';
return 0;
} // if loop status
// not found, try next entry
curEntry = (struct directory *)((unsigned long)curEntry + dirSize);
} // for loop i
// not found in this cluster, try next cluster in the chain
curBlock = (struct diskBlock *)curBlock->nextBlock;
nThEntry = -1;
}
// all clusters in the chain have been searched, file not found
RDerrno = ERROR_FILE_NOT_EXIST;
return -1;
}
/*************************************************************
Function : RD_findclose
Description:
clean up the memory occupied by the previous search
Input:
ffres - a structure that holds the results of the previous search
Output:
0 SUCCESS
-1 FAILURE
**************************************************************/
int RD_findclose(struct ffblk *ffres)
{
struct ffInternal *ffInfo; /* internal information for findnext */
if (InitRD == FALSE)
{
RDerrno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
if (ffres->ff_reserved == 0)
{
RDerrno = ERROR_FILE_NOT_EXIST;
return -1;
}
if (ffres->ff_drive != RAMDISK_ID)
{
RDerrno = ERROR_INVALID_DEVICE;
return -1;
}
sc_waitSemaphore(RD_SemaphoreID);
ffInfo = (struct ffInternal *)ffres->ff_reserved;
ap_free(ffInfo);
ffres->ff_reserved = 0;
sc_signalSemaphore(RD_SemaphoreID);
return 0;
}
/*************************************************************
Function : RD_nameCompare
Description:
compare a filename with the wildcard name
Input:
wild - a wildcard name
name - filename
Output:
1 the filename matchs
0 the filename not matchs
**************************************************************/
int RD_nameCompare(unsigned char *wild, unsigned char *name)
{
int index;
// added by chilong 01/30/2002
short cmpIndex = 0;
// still have to deal with single-byte and double-byte mix
while (*wild != '\0')
{
// recursively compare the next part if the current char is a '*'
// for example, wild = "*a.txt", name = "aba.txt"
// RD_nameCompare("a.txt", "aba.txt")
// unmatch => RD_nameCompare("a.txt", "ba.txt")
// unmatch => RD_nameCompare("a.txt", "a.txt")
// match!
if (*wild == '*')
{
// skip all successive '*'
while (*wild == '*')
wild++;
if (*wild == '\0')
return 1;
index = 0;
while (*(name + index) != '\0')
{
if (RD_nameCompare(wild, (name + index)) == 1)
return 1;
if (*(name + index) > 0x80)
index += 2; // a double byte char
else
index++; // a single byte char
}
return 0;
}
// skip one char in name if the current wild name is '?'
else if (*wild == '?')
{
if (*name == '\0')
return 0;
else
{
wild++;
if (*name > 0x80)
name += 2; // a double byte char
else
name++; // a single byte char
}
}
else if (*wild == '.' && *(wild + 1) == '*' && *(wild + 2) == '\0')
return 1;
else
{
if (*name > 0x80)
{
// the current name char is a double-byte char
if (*wild != *name || *(wild + 1) != *(name + 1))
return 0;
else
{
wild += 2;
name += 2;
}
}
else
{
// the current char is a single-byte char
if (iCompareChar(*wild, *name) != 1)
return 0;
else
{
wild++;
name++;
}
// added by chilong 01/30/2002
cmpIndex++;
}
}
}
/* marked by chilong 01/30/2002
if (*wild == '\0' && *name == '\0')
return 1;
else
return 0;
marked by chilong 01/30/2002 */
/**** modified by chilong 01/30/2002 ****/
if (*wild == '\0' && (*name == '\0' || cmpIndex == RD_FNAME_LEN) )
return 1;
else
return 0;
/**** modified by chilong 01/30/2002 ****/
}
#endif // #ifdef RAMDISK_ID
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -