//
//Author			: walk.djg  
//Time			: 2009-06-26
//Version		: 1.0.0
//Description	: find files in directory

// use like this
/* 
   string path = "c:\\windows";
   array collection;
   //仅获取"ini"格式的文件
   GetFiles(collection, path, ".ini");
   //获取"ini", "txt"格式的文件
   GetFiles(collection, path, ".ini | .txt");
*/

void GetFiles(array& files, string strPath, string strDot)
{
   wkDb db;
   array oDots = db.sqlSubsByDeli(strDot, "|");
   for (int i = 0; i < oDots.getSize(); ++i)
   {
	   oDots[i].trimLeft();
	   oDots[i].trimRight();
   }
   
   wkFile finder;

   // build a string with wildcards
   string strWildcard = strPath;
	if (strWildcard.isEmpty())
		return;
   strWildcard.trimRight("\\");
   strWildcard += "\\*.*";
	
   // start working for files
   bool bWorking = finder.findFile(strWildcard);

   while (bWorking)
   {
      bWorking = finder.findNextFile();

      // skip . and .. files; otherwise, we'd
      // recur infinitely!

	   //Is dot? 4 -- dot
      if (finder.findFileIs(4))
         continue;

      // if it's a directory, recursively search it
	  // 3 -- directory
      if (finder.findFileIs(3))
      {
		  // 2 -- file path
         string ss = finder.findFileGet(2);  
         GetFiles(files, ss, strDot);
      }
	  else
	  {
		  string s = finder.findFileGet(2);
		  int n = s.reverseFind(".");
		  if (n>0 && IsInDefineType(s.mid(n), oDots))
			files.add(finder.findFileGet(2));
	  }
   }

   finder.findClose();
}

bool IsInDefineType(string s, array& o)
{
	for (int i = 0; i < o.getSize(); ++i)
	{
		if (o[i].compareNoCase(s)==0)
			return true;
	}
	return false;
}