⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nandfind.c

📁 嵌入式系统中文件系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
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
**************************************************************/
int NAND_FFS_findnext(struct ffblk *ffres)
{
  int status;

  NAND_FFS_Errno = 0;

  if (NAND_FFS_Init_OK == FALSE)
  {
	NAND_FFS_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }

  if (ffres->ff_reserved == 0)
  {
	NAND_FFS_Errno = ERROR_FILE_NOT_EXIST;
	return -1;
  }

  if (ffres->ff_drive != NAND_FLASH_DISK_ID)
  {
	NAND_FFS_Errno = ERROR_INVALID_DEVICE;
	return -1;
  }

  sc_waitSemaphore(NAND_FFS_SemaphoreID);

  status = NAND_FFS_findnext_r(ffres);

  sc_signalSemaphore(NAND_FFS_SemaphoreID);

  return status;
}


int NAND_FFS_findnext_r(struct ffblk *ffres)
{
  struct NAND_ffInternal *ffInfo;	/* internal information for findnext */
  struct NAND_diskLump *curLump;
  struct NAND_directory *curEntry;
  long dirSize;
  long i;
  int nThEntry;			/* the n-th entry in root block */

  /**** added by chilong 12/25/2001 ****/
  int curLumpNo;
  /**** added by chilong 12/25/2001 ****/

  // get the pointer to the internal filefind structure
  ffInfo = (struct NAND_ffInternal *)ffres->ff_reserved;

  // start searching from this cluster
//  curBlock = ffInfo->block;
//  nThEntry = (int)ffInfo->nThEntry;
  curLumpNo = ffInfo->lumpNo;
  nThEntry = ffInfo->nThEntry;

#ifdef NAND_FFS_DEBUG
  sprintf(NAND_FFS_DebugString, "[NAND_FFS_findnext] curLumpNo: %d, nThEntry: %d", 
  	curLumpNo, nThEntry);
  SprintStringLn(NAND_FFS_DebugString);
#endif

  dirSize = sizeof(struct NAND_directory);

  if (NAND_FFS_readLump(curLumpNo, (unsigned char**) &curLump) == -1)
  	return -1;
  if (curLump == NULL)
  	return -1;
  	
  while (curLumpNo != NAND_END_LUMP)
  {
	curEntry = (struct NAND_directory *)((unsigned long)curLump + sizeof(struct NAND_diskLump) + (nThEntry + 1) * dirSize);

	// compare entry by entry in the sector
	for (i = (nThEntry + 1) * dirSize; i < NAND_LUMP_SIZE; i+= dirSize)
	{
		// this entry and every following entry are empty => not found
		if (curEntry->name[0] == NAND_FREE_ENTRY)
		{
			NAND_FFS_Errno = ERROR_FILE_NOT_EXIST;
			return -1;
		}

		// skip this entry if it an deleted entry
		if (curEntry->name[0] == DELETED_ENTRY)
		{
			curEntry = (struct NAND_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 NAND_directory *)((unsigned long)curEntry + dirSize);
			continue;
		}
		// 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 NAND_directory *)((unsigned long)curEntry + dirSize);
				continue;
			}

		// attributes match, compare filename with the wildcard name
		if (NAND_FFS_nameCompare(ffInfo->wildname, curEntry->name) == 1)
		{
			// update the ffInternal structure
			ffInfo->nThEntry = i / dirSize;
			ffInfo->lumpNo = curLumpNo;
			// 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;
			while (curEntry->name[i] != '\0')
			{
				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 NAND_directory *)((unsigned long)curEntry + dirSize);
	} // for loop i

	// not found in this cluster, try next cluster in the chain
//	curBlock = (struct diskBlock *)curBlock->nextBlock;
	curLumpNo = curLump->nextLump;
	if (curLumpNo == NAND_END_LUMP)
		break;

	if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
		return -1;
	if (curLump == NULL)
		return -1;
		
	nThEntry = -1;
  }

  // all clusters in the chain have been searched, file not found
  NAND_FFS_Errno = ERROR_FILE_NOT_EXIST;
  return -1;
}

/*************************************************************
Function : NAND_FFS_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 NAND_FFS_findclose(struct ffblk *ffres)
{
  struct NAND_ffInternal *ffInfo;	/* internal information for findnext */

  if (NAND_FFS_Init_OK == FALSE)
  {
	NAND_FFS_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }

  if (ffres->ff_reserved == 0)
  {
	NAND_FFS_Errno = ERROR_FILE_NOT_EXIST;
	return -1;
  }

  if (ffres->ff_drive != NAND_FLASH_DISK_ID)
  {
	NAND_FFS_Errno = ERROR_INVALID_DEVICE;
	return -1;
  }

  sc_waitSemaphore(NAND_FFS_SemaphoreID);

  //ffInfo = (struct ffInternal *)ffres->ff_reserved;
  ffInfo = (struct NAND_ffInternal *)ffres->ff_reserved; // by zhang xue ping

  ap_free(ffInfo);

  ffres->ff_reserved = 0;

  sc_signalSemaphore(NAND_FFS_SemaphoreID);

  return 0;
}



/*************************************************************
Function : NAND_FFS_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 NAND_FFS_nameCompare(unsigned char *wild, unsigned char *name)
{
  int index;

  // 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"
	//		NAND_FFS_nameCompare("a.txt", "aba.txt")
	// unmatch =>	NAND_FFS_nameCompare("a.txt", "ba.txt")
	// unmatch =>	NAND_FFS_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 (NAND_FFS_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++;
			}
		}
	}
  }

  if (*wild == '\0' && *name == '\0')
	return 1;
  else
  	return 0;
}

#endif	// #ifdef NAND_FLASH_DISK_ID



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -