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

📄 namenode.c

📁 老外写的一个文件系统
💻 C
字号:
#include "Platform.h"
#include "Debug/Debug.h"
#include "StdC.h"
#include "PageStack/PageStack.h"
#include "FileSystem/Fat32/FileTree/NameNode.h"
#include "FileSystem/Fat32/FileTree/Node.h"

//#define SHOW_NAME_CREATION	// uncomment to see name-nodes get created.
//#define SHOW_NAME_VISITS	// uncomment to see name-nodes get visited.


struct NameNode
{
	NameNodeHandle parentName;

	// the allocated memory for the string itself is only large enough 
	// to hold the string.  Do not make a string longer, EVER.
	char name[255+1];	
	// nothing may be placed after the name!
};

// a string-table page is a bunch of NameNode's, packed so that
// as many will fit as possible in a page (accounting for the size of teh string it contains too!)
// which is why you can't use all 256-bytes of the name once the name has been allocated.
XDATA_AT(PAGE_STACK_ADDRESS) char _NodeTablePort[BYTES_PER_PORT];

XDATA DramPageIndex _nextNewNamePage;
XDATA unsigned short _nextNewNameOffset;


void NAMENODE_Initialize(void)
{
	XDATA static String str;
	str = _STRING_CreateStringInCODE("NAMENODE Collection");
	_nextNewNamePage = DRAM_AllocatePage(str);
	_nextNewNameOffset = 0;
}

NameNodeHandle NAMENODE_AddName(
	NameNodeHandle nameOfParentFolder,
	XDATA char *name)
{
	XDATA static unsigned short nBytes;
	XDATA static struct NameNode *self;
	XDATA static unsigned short nameOffset;
	XDATA static DramPageIndex namePage;
	
#ifdef SHOW_NAME_CREATION	
	{
		XDATA static String str;
		str = _STRING_CreateStringInCODE("NAMENODE AddName\n\r");
		DbgMessage(DC_NAMENODE, str);
		DbgMessageNewLine(DC_NAMENODE);
		DbgMessageHex16(DC_NAMENODE, (unsigned short)name);
		DbgMessageNewLine(DC_NAMENODE);
	}
#endif

	nBytes = ( sizeof(*self) - sizeof(self->name) ) + strlenXDATA(name) + 1;
	nameOffset = _nextNewNameOffset;
	namePage = _nextNewNamePage;
	_nextNewNameOffset += nBytes;
	if(_nextNewNameOffset >= ENTRIES(_NodeTablePort))
	{
		// not enough space left in this page, create a new page!
		XDATA static String str;
		str = _STRING_CreateStringInCODE("NAMENODE Collection");
		namePage = DRAM_AllocatePage(str);
		_nextNewNamePage = namePage;
		nameOffset = 0;
		_nextNewNameOffset = 0;
	}
	
	// create the handle.
	{
		XDATA static NameNodeHandle newHandle;
		newHandle = MakeHandle(namePage, nameOffset);
		
		// populate the entry
		PAGESTACK_Push(namePage);
		self = (XDATA struct NameNode *)(&_NodeTablePort[nameOffset]);
		self->parentName = nameOfParentFolder;

		{
			XDATA static unsigned char *dst;
			dst = &self->name[0];

			// copy the string!
			if(name != 0)	// only get bytes from the string if it's not NULL!!!!
			{
				XDATA static unsigned char *src;
				src = &name[0];
				while(*src)
				{
					*dst = *src;
					dst++;
					src++;
				}
			}
			*dst = 0;	// terminator
		}
	
#ifdef SHOW_NAME_CREATION	
		{
			XDATA static String str;
			str = _STRING_CreateStringInCODE("NameNode [new] 0x");
			DbgMessage(DC_NAMENODE, str);
			DbgMessageHex32(DC_NAMENODE, newHandle);
			DbgMessageNewLine(DC_NAMENODE);
//			DbgHexDumpXDATA(DC_NAMENODE, (XDATA char *)self, sizeof(*self));
		}
#endif

		PAGESTACK_Pop();

#ifdef SHOW_NAME_CREATION	
		{
			XDATA static String str;
			str = _STRING_CreateStringInCODE("NameNode 0x");
			DbgMessage(DC_NAMENODE, str);
			DbgMessageHex32(DC_NAMENODE, newHandle);
			DbgMessageNewLine(DC_NAMENODE);
		}
#endif
		return newHandle;
	}
}

void NAMENODE_GetNameString(
	NameNodeHandle h, 
	XDATA char *strToBuildInto)	// IN, OUT
{
	XDATA static struct NameNode *self;
	XDATA static char *name;

#ifdef SHOW_NAME_VISITS
	XDATA static String str;
	str = _STRING_CreateStringInCODE("NameNode [visit GetString] 0x");
	DbgMessage(DC_NAMENODE, str);
	DbgMessageHex32(DC_NAMENODE, h);
	DbgMessageNewLine(DC_NAMENODE);
#endif

	PAGESTACK_Push(GetPage(h));
	self = (XDATA struct NameNode *)(&_NodeTablePort[GetOffsetShort(h)]);
	name = &self->name[0];	// find the beginning of the string (it follows the structure)
	while(*name != 0)
	{	// copying the string...
		*strToBuildInto = *name;
		strToBuildInto++;
		name++;
	}
	*strToBuildInto = 0;	// terminate!
	PAGESTACK_Pop();
}

char NAMENODE_IsExtentionMP3(NameNodeHandle h)
{
	XDATA static struct NameNode *self;
	XDATA static char *name;

#ifdef SHOW_NAME_VISITS
	XDATA static String str;
	str = _STRING_CreateStringInCODE("NameNode [visit Ext] 0x");
	DbgMessage(DC_NAMENODE, str);
	DbgMessageHex32(DC_NAMENODE, h);
	DbgMessageNewLine(DC_NAMENODE);
#endif

	PAGESTACK_Push(GetPage(h));
	self = (XDATA struct NameNode *)(&_NodeTablePort[GetOffsetShort(h)]);
	name = &self->name[0];	// find the beginning of the string (it follows the structure)
	
	// find end of string.
	while(*name != 0)
		name++;
	
	name -= 4;
	if( (*(name++)) == '.' )
	{
		if( tolower(*(name++)) == 'm' )
		{
			if( tolower(*(name++)) == 'p' )
			{
				if( (*(name++)) == '3' )
				{
					PAGESTACK_Pop();
					return 1;
				}
			}
		}
	}
	
	// nope - no match.
	PAGESTACK_Pop();
	return 0;
}

_CONSTANT_DATA(NAMENODE)

⌨️ 快捷键说明

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