#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>


//put additional include files here

//put your own support static functions here

//put your own support static functions here
///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
//#define STR_C_W_D "current working directory"
///end UPDATE_XF_ERR_THROW

///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
static bool _is_pattern_unassigned(LPCSTR lpcszPattern)
{
	string str(lpcszPattern);
	if(str.IsEmpty() || okutil_cvt_str_to_predefined_type(str) == PDS_UNASSIGNED)
		return true;
	
	return false;
}
///end UPDATE_XF_ERR_THROW
	

void dir(const string& pattern, string& cwd)
{
	///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
	string strMsg;  
	string strCWD;
	ocu_load_msg_str(XF_SYSTEM_CURRENT_WORKING_FOLDER, &strCWD);
	///end UPDATE_XF_ERR_THROW
	
	string workingfolder;
	workingfolder = okutil_get_custom_path(true);
	if(workingfolder.IsEmpty())
		///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
		//printf("%s is not defined\n", STR_C_W_D);
	///-----Kit 10/22/2010 CLEANUP_XFS_ERROR_CODE
		//warning_msg_box(XF_CD_UNDIFINE, strCWD, false, 'E');
	{
		XF_THROW_EX(XF_CD_UNDIFINE, strCWD);
	}
	///-----End CLEANUP_XFS_ERROR_CODE
		///end UPDATE_XF_ERR_THROW
	else
	{
		//printf("%s:\n%s\n\n", STR_C_W_D, workingfolder);
		if(cwd) // str variable is specified, default is <optional> which will result into invalid str
			cwd = workingfolder;
		
		// directories
		int nDirectories=0;
		vector<string> saSubFolders;
		int numSubDirs = okutil_find_subfolders( &saSubFolders, workingfolder, 0);
		if(numSubDirs > 0)
		{
			//printf("\n--- subfolders ---\n\n");
			for(int ii=0; ii<numSubDirs; ii++)
			{
				  // filter pattern
				  ///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
				  //if( 0<pattern.GetLength() && 0!=pattern.CompareNoCase("<unassigned>") &&  0!=pattern.CompareNoCase("*") && !saSubFolders[ii].Match(pattern) )
				  if( 0<pattern.GetLength() && !_is_pattern_unassigned(pattern) &&  0!=pattern.CompareNoCase("*") && !saSubFolders[ii].Match(pattern) )
				  ///end UPDATE_XF_ERR_THROW
						continue;
				
				  nDirectories++;
				  ///Cheney 2007-5-30 UPDATE_XF_ERR_THROW
				  //printf("%16s  %8s  %s\n", "", "<DIR>", saSubFolders[ii]);
				  strMsg.Format("%16s  %8s  %s", "", "<DIR>", saSubFolders[ii]);
				  warning_msg_box(strMsg, false, 'W');
				  ///end UPDATE_XF_ERR_THROW
			}
			
		}
	
		//-- if pattern specified, find all files
		///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
		//if( 0!=pattern.CompareNoCase("<unassigned>") )
		if(!_is_pattern_unassigned(pattern))	
		///end UPDATE_XF_ERR_THROW
		{
			//-- for files, reformat pattern into form xxx.xxx
			// a*c* -> a*c*.*
			// abc -> abc.*
			// .oxf -> .oxf.*  maybe not what user intended, but consistent with DOS dir
			// oxf -> oxf.*    maybe not what user intended, but consistent with DOS dir
			// this is so dir acts like DOS dir
			// to list files with ext abc, use *.abc
			string pattern2 = pattern;
			if(pattern2.Find('.')<0 && 0!=pattern2.CompareNoCase("*") )
				pattern2 = pattern2 + ".*";
				
			int nAllFiles=0;
			vector<string> saAllFiles;
			//int numAllFiles = okutil_find_files_from_name_or_ext(&saAllFiles, workingfolder, pattern, FALSE);
			bool bSuc = okutil_find_files_from_name_or_ext(&saAllFiles, workingfolder, pattern2, FALSE);
			int numAllFiles = saAllFiles.GetSize();
			//printf("\n--- all files ---\n\n");
			for(int jj=0; jj<numAllFiles; jj++)
			{
				  // filter pattern
				  if( 0!=pattern2.CompareNoCase("*") && !saAllFiles[jj].Match(pattern2) )
						continue;
				
				  nAllFiles++;
				  //printf("%16s  %8d  %s\n", "", 0, GetFileName(saAllFiles[jj]));
				  //-- need to get size and date
				  
				  	string strFile = workingfolder + saAllFiles[jj];
					double dLastWriteTime;
					LPSTR lpstr = strFile.GetBuffer(strFile.GetLength() + 10);
					BOOL bb =  okutil_get_file_last_write_time_as_julian( strFile,  &dLastWriteTime);
					long lSize = okutil_get_file_size_from_name(lpstr);	
				  
				  
				  
				  
				  
				  // printf("%16s  %8s  %s\n", "", "", GetFileName(saAllFiles[jj]));
				  ///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
				  //printf( "%16s  %8d  %s\n", okutil_date_time_str_from_julian_date(dLastWriteTime), lSize, GetFileName(strFile) );
				  strMsg.Format("%16s  %8d  %s", okutil_date_time_str_from_julian_date(dLastWriteTime), lSize, GetFileName(strFile));
				  warning_msg_box(strMsg, false, 'W');
				  ///end UPDATE_XF_ERR_THROW
			}	
		}
		else //-- if no pattern specified, list ogs xf and dir only
		{
		
			//-- ogs
			int nOGSCount=0;
			vector<string> vsOGSFiles;	
			int nNumFiles = okutil_find_ogs_files_from_map(NULL, &vsOGSFiles);
			//printf("--- LabTalk Script files (.ogs) ---\n\n");
			for(int ii=0; ii<nNumFiles; ii++)
			{
			  string strTemp = GetFileName(vsOGSFiles[ii], false);
			  
			  // filter pattern
			  ///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
			  //if( 0<pattern.GetLength() && 0!=pattern.CompareNoCase("<unassigned>") && 0!=pattern.CompareNoCase("*") && !strTemp.Match(pattern) )
			  if( 0<pattern.GetLength() && !_is_pattern_unassigned(pattern) && 0!=pattern.CompareNoCase("*") && !strTemp.Match(pattern) )
			  ///end UPDATE_XF_ERR_THROW		
			  	continue;
			  
			 
			  // strip extension
			  // out_str(GetFileName(strTemp, true));
			  
			  long lSize;
			  double dLastModTime;
			  string strDate;
			  if(okutil_get_ogs_file_info_from_map(GetFileName(strTemp, true), NULL, NULL, NULL, NULL, NULL, &lSize, &dLastModTime))		      	  
			  {
			  	///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
			  	//printf("%16s  %8d  %s\n", okutil_date_time_str_from_julian_date(dLastModTime), lSize, GetFileName(strTemp));
			 	strMsg.Format("%16s  %8d  %s", okutil_date_time_str_from_julian_date(dLastModTime), lSize, GetFileName(strTemp));
			  	warning_msg_box(strMsg, false, 'W');
			 	///end UPDATE_XF_ERR_THROW
			  	nOGSCount++;	
			  }
			}
		      
			//-- xf
			int nXFCount=0;
			DoubleArray arrdTime;
			IntArray arrSize;
			vector<string> vsCompositeNames, vsFiles;	
			nNumFiles = okutil_find_files_from_map(&vsCompositeNames, &vsFiles, SUPPORTFILE_XF, FF_REMOVE_CATEGORY|FF_REMOVE_PATH, NULL, 0, NULL, &arrdTime, ORIGIN_PATH_CUSTOM, 0x0, &arrSize);
			if(nNumFiles > 0)
			{
				//printf("\n--- x-functions (.oxf) ---\n\n");
				for(ii=0; ii<nNumFiles; ii++)
				{
					  string strName = GetFileName(vsFiles[ii], false);
					  
					  // filter pattern
					  ///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
					  //if( 0<pattern.GetLength() && 0!=pattern.CompareNoCase("<unassigned>") &&  0!=pattern.CompareNoCase("*") && !strName.Match(pattern) )
					  if( 0<pattern.GetLength() && !_is_pattern_unassigned(pattern) &&  0!=pattern.CompareNoCase("*") && !strName.Match(pattern) )
					  ///end UPDATE_XF_ERR_THROW		
					  	continue;
					
					  nXFCount++;
					  ///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
					  //printf("%16s  %8d  %s\n", okutil_date_time_str_from_julian_date(arrdTime[ii]), arrSize[ii], strName);
					  strMsg.Format("%16s  %8d  %s", okutil_date_time_str_from_julian_date(arrdTime[ii]), arrSize[ii], strName);
					  warning_msg_box(strMsg, false, 'W');
					  ///end UPDATE_XF_ERR_THROW
				}	
			}
			
			///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
			//string strOut;
			//strOut.Format("\n\t%10d LabTalk Script (ogs) Files", nOGSCount);
			//out_str(strOut);
			//
			//strOut.Format("\t%10d X-Functions", nXFCount);
			//out_str(strOut);
			//
			//strOut.Format("\t%10d Directories", nDirectories);
			//out_str(strOut);
			//
			//strOut.Format("\tUse dir * to list all files and directories", nDirectories);
			//out_str(strOut);
			string strtemp;
			
			ocu_load_msg_str(XF_DIR_OGS, &strtemp);
			strMsg.Format("\r\n\t%10d %s", nOGSCount, strtemp);
			warning_msg_box(strMsg, false, 'W');
			
			ocu_load_msg_str(XF_DIR_XFUNCTIONS, &strtemp);
			strMsg.Format("\t%10d %s", nXFCount, strtemp);
			warning_msg_box(strMsg, false, 'W');
			
			ocu_load_msg_str(XF_DIR_DIRECTORIES, &strtemp);
			strMsg.Format("\t%10d %s", nDirectories, strtemp);
			warning_msg_box(strMsg, false, 'W');
			
			ocu_load_msg_str(XF_DIR_ALL_FILES_AND_DIRS, &strtemp);
			strMsg.Format("\t%s", strtemp);
			warning_msg_box(strMsg, false, 'W');
			///end UPDATE_XF_ERR_THROW
		}
			
		
	
		
	}
}

//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void dir_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
