<commit_before>#include "atmmc2.h"

#include <string.h>
#include "atmmc2io.h"
#include "atmmc2def.h"
#include "diskio.h"
#include "ff.h"
#include "wildcard.h"

#pragma udata fildata

BYTE res;

BYTE globalIndex;
WORD globalAmount;
BYTE globalDataPresent;
BYTE rwmode;

DIR dir;

int filenum = -1;


FILINFO filinfodata[4];
FIL fildata[4];

FATFS fatfs;

extern BYTE windowData[];

#define WILD_LEN	16

char	WildPattern[WILD_LEN+1];

#ifdef INCLUDE_SDDOS

int lastDriveNo = -1;

imgInfo driveInfo[4];

BYTE globalCurDrive;
DWORD globalLBAOffset;

BYTE SDDOS_seek();

#endif

#pragma udata

// use only immediately after open
extern void get_fileinfo_special(FILINFO *);




void at_initprocessor(void)
{
   int i;
   rwmode = 0;

   fatfs.win = windowData;

#ifdef INCLUDE_SDDOS
   memset(&driveInfo[0], 0xff, sizeof(imgInfo)*4);
#endif

  f_chdrive(0);
  f_mount(0, &fatfs);
  
  for (i = 0; i < 4; i++) {
    fildata[i].fs = NULL;
  }
}


void GetWildcard(void)
{
	int	Idx			= 0;
	int	WildPos		= -1;
	int	LastSlash	= -1;
	
	//log0("GetWildcard() %s\n",(const char *)globalData);
	
	while ((Idx<strlen((const char*)globalData)) && (WildPos<0)) 
	{
		// Check for wildcard character
		if((globalData[Idx]=='?') || (globalData[Idx]=='*')) 
			WildPos=Idx;

		// Check for path seperator
		if((globalData[Idx]=='\\') || (globalData[Idx]=='/'))
			LastSlash=Idx;
			
		Idx++;
	}
	
	//log0("GetWildcard() Idx=%d, WildPos=%d, LastSlash=%d\n",Idx,WildPos,LastSlash);
	
	if(WildPos>-1)
	{
		if(LastSlash>-1)
		{
			// Path followed by wildcard
			// Terminate dir filename at last slash and copy wildcard
			globalData[LastSlash]=0x00;
			strncpy(WildPattern,(const char*)&globalData[LastSlash+1],WILD_LEN);
		}
		else
		{
			// Wildcard on it's own
			// Copy wildcard, then set path to null
			strncpy(WildPattern,(const char*)globalData,WILD_LEN);
			globalData[0]=0x00;
		}
	}
	else
	{
		// No wildcard, show all files
#if (PLATFORM==PLATFORM_PIC)
		strcpypgm2ram((char*)&WildPattern[0], (const rom far char*)"*");
#elif (PLATFORM==PLATFORM_AVR)
		strncpy_P(WildPattern,PSTR("*"),WILD_LEN);
#endif
	}
	
	//log0("GetWildcard() globalData=%s WildPattern=%s\n",(const char*)globalData,WildPattern); 
}

void wfnDirectoryOpen(void)
{
	// Separate wildcard and path 
	GetWildcard();
   
	res = f_opendir(&dir, (const char*)globalData);
   if (FR_OK != res)
   {
      WriteDataPort(STATUS_COMPLETE | res);
      return;
   }

   WriteDataPort(STATUS_OK);
}




void wfnDirectoryRead(void)
{
  FILINFO *filinfo = &filinfodata[0];
   char len;
	int	Match;

	while (1)
	{
		char n = 0;

		res = f_readdir(&dir, filinfo);
		if (res != FR_OK || !filinfo->fname[0])
		{
			// done
			WriteDataPort(STATUS_COMPLETE | res);
			return;
		}

		// Check to see if filename matches current wildcard
		//
		Match=wildcmp(WildPattern,filinfo->fname);
		//log0("WildPattern=%s, filinfo->fname=%s, Match=%d\n",WildPattern,filinfo->fname,Match);
		if(Match)
		{
			len = (char)strlen(filinfo->fname);

			if (filinfo->fattrib & AM_DIR)	
			{
				n = 1;
				globalData[0] = '<';
			}

			strcpy((char*)&globalData[n], (const char*)filinfo->fname);

			if (filinfo->fattrib & AM_DIR)
			{
				globalData[len+1] = '>';
				globalData[len+2] = 0;
				len += 2; // brackets
			}

			// just for giggles put the attribute & filesize in the buffer
			//
			globalData[len+1] = filinfo->fattrib;
			memcpy(&globalData[len+2], (void*)(filinfo->fsize), sizeof(DWORD));

			WriteDataPort(STATUS_OK);
			return;
		}
	}

#if 0
   while (1)
   {
      char n = 0;

      res = f_readdir(&dir, &filinfo);
      if (res != FR_OK || !filinfo->fname[0])
      {
         // done
         WriteDataPort(STATUS_COMPLETE | res);
         return;
      }

      // no LFNs here ;)
      //
      len = (char)strlen(filinfo->fname);

      if (filinfo->fattrib & AM_DIR)
      {
         n = 1;
         globalData[0] = '<';
      }

      strcpy((char*)&globalData[n], (const char*)filinfo->fname);<commit_msg>Fixed bug in atommc firmware dir read<commit_after>#include "atmmc2.h"

#include <string.h>
#include "atmmc2io.h"
#include "atmmc2def.h"
#include "diskio.h"
#include "ff.h"
#include "wildcard.h"

#pragma udata fildata

BYTE res;

BYTE globalIndex;
WORD globalAmount;
BYTE globalDataPresent;
BYTE rwmode;

DIR dir;

int filenum = -1;


FILINFO filinfodata[4];
FIL fildata[4];

FATFS fatfs;

extern BYTE windowData[];

#define WILD_LEN	16

char	WildPattern[WILD_LEN+1];

#ifdef INCLUDE_SDDOS

int lastDriveNo = -1;

imgInfo driveInfo[4];

BYTE globalCurDrive;
DWORD globalLBAOffset;

BYTE SDDOS_seek();

#endif

#pragma udata

// use only immediately after open
extern void get_fileinfo_special(FILINFO *);




void at_initprocessor(void)
{
   int i;
   rwmode = 0;

   fatfs.win = windowData;

#ifdef INCLUDE_SDDOS
   memset(&driveInfo[0], 0xff, sizeof(imgInfo)*4);
#endif

  f_chdrive(0);
  f_mount(0, &fatfs);
  
  for (i = 0; i < 4; i++) {
    fildata[i].fs = NULL;
  }
}


void GetWildcard(void)
{
	int	Idx			= 0;
	int	WildPos		= -1;
	int	LastSlash	= -1;
	
	//log0("GetWildcard() %s\n",(const char *)globalData);
	
	while ((Idx<strlen((const char*)globalData)) && (WildPos<0)) 
	{
		// Check for wildcard character
		if((globalData[Idx]=='?') || (globalData[Idx]=='*')) 
			WildPos=Idx;

		// Check for path seperator
		if((globalData[Idx]=='\\') || (globalData[Idx]=='/'))
			LastSlash=Idx;
			
		Idx++;
	}
	
	//log0("GetWildcard() Idx=%d, WildPos=%d, LastSlash=%d\n",Idx,WildPos,LastSlash);
	
	if(WildPos>-1)
	{
		if(LastSlash>-1)
		{
			// Path followed by wildcard
			// Terminate dir filename at last slash and copy wildcard
			globalData[LastSlash]=0x00;
			strncpy(WildPattern,(const char*)&globalData[LastSlash+1],WILD_LEN);
		}
		else
		{
			// Wildcard on it's own
			// Copy wildcard, then set path to null
			strncpy(WildPattern,(const char*)globalData,WILD_LEN);
			globalData[0]=0x00;
		}
	}
	else
	{
		// No wildcard, show all files
#if (PLATFORM==PLATFORM_PIC)
		strcpypgm2ram((char*)&WildPattern[0], (const rom far char*)"*");
#elif (PLATFORM==PLATFORM_AVR)
		strncpy_P(WildPattern,PSTR("*"),WILD_LEN);
#endif
	}
	
	//log0("GetWildcard() globalData=%s WildPattern=%s\n",(const char*)globalData,WildPattern); 
}

void wfnDirectoryOpen(void)
{
	// Separate wildcard and path 
	GetWildcard();
   
	res = f_opendir(&dir, (const char*)globalData);
   if (FR_OK != res)
   {
      WriteDataPort(STATUS_COMPLETE | res);
      return;
   }

   WriteDataPort(STATUS_OK);
}




void wfnDirectoryRead(void)
{
  FILINFO *filinfo = &filinfodata[0];
   char len;
	int	Match;

	while (1)
	{
		char n = 0;

		res = f_readdir(&dir, filinfo);
		if (res != FR_OK || !filinfo->fname[0])
		{
			// done
			WriteDataPort(STATUS_COMPLETE | res);
			return;
		}

		// Check to see if filename matches current wildcard
		//
		Match=wildcmp(WildPattern,filinfo->fname);
		//log0("WildPattern=%s, filinfo->fname=%s, Match=%d\n",WildPattern,filinfo->fname,Match);
		if(Match)
		{
			len = (char)strlen(filinfo->fname);

			if (filinfo->fattrib & AM_DIR)	
			{
				n = 1;
				globalData[0] = '<';
			}

			strcpy((char*)&globalData[n], (const char*)filinfo->fname);

			if (filinfo->fattrib & AM_DIR)
			{
				globalData[len+1] = '>';
				globalData[len+2] = 0;
				len += 2; // brackets
			}

			// just for giggles put the attribute & filesize in the buffer
			//
			globalData[len+1] = filinfo->fattrib;
			memcpy(&globalData[len+2], (void*)&(filinfo->fsize), sizeof(DWORD));

			WriteDataPort(STATUS_OK);
			return;
		}
	}

#if 0
   while (1)
   {
      char n = 0;

      res = f_readdir(&dir, &filinfo);
      if (res != FR_OK || !filinfo->fname[0])
      {
         // done
         WriteDataPort(STATUS_COMPLETE | res);
         return;
      }

      // no LFNs here ;)
      //
      len = (char)strlen(filinfo->fname);

      if (filinfo->fattrib & AM_DIR)
      {
         n = 1;
         globalData[0] = '<';
      }

      strcpy((char*)&globalData[n], (const char*)filinfo->fname);
{'fraction_non_alphanumeric': 0.16241036608378412, 'fraction_numerical': 0.014341426594540194, 'mean_word_length': 3.304277206280455, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4586327', 'n_tokens_mistral': 3504, 'n_tokens_neox': 3128, 'n_words': 629}