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

📄 parse.c

📁 一个操作系统源代码 用于嵌入式设备 在Vc++环境下仿真 成功移植到多款处理器上
💻 C
字号:
#include <stdio.h>
#include <string.h>
#include <filesys\fs.h>
#include "parse.h"

/*
 * If this macro is defined, some code below for 
 * checking parameters will not work.
 */
//#define FS_NOT_CHECK_ERROR

/* Check magic:
 *  0 --- success
 *  1 --- error */
#define _CHECK_PATH_NODE_MAGIC(a) ((a)->magic != PATH_NODE_MAGIC)
#define _SET_PATH_NODE_MAGIC(a) ((a)->magic = PATH_NODE_MAGIC)


/* Path:
 *  1. '.' mustn't appear at first
 *  2. '//' or more coninuous '/' is forbidden
 *  3. '/' should be the first char
 *  4. Name of file and directory have the same max length
 *  5. Drectory tree has max depth
  */
int parse_path( char *path, PATH_NODE_S *path_node ){
	register char *q;
	register int i=0,j=0;
	register char c;

#ifndef FS_NOT_CHECK_ERROR	
	/* Path should start with '/' */
	if ( *path!='/' ){
		PRINT_ERROR("Not start with /\n");
		return FS_ERROR;
	}
	
	if ( !path_node ){
		PRINT_ERROR("NULL POINTER: path_node\n");
		return FS_ERROR;
	}
#endif

	memset( path_node, 0, sizeof(PATH_NODE_S));
	for (;;){
		q = (char *)&path_node->node[i];
		for (;;){
			/* Name of directory or file is too long */
			if ( j == NODE_LEN ){
				PRINT_ERROR("Name too long\n");
				return FS_ERROR;
			}
			
			c = *path++;
			
			/* Check path's character: only '/', '.', '-','_',
			 * 'a'-'z', 'A'-'Z' and '0'-'9' is correct.*/
			if (('a'<=c && c<='z')
				||('A'<=c && c<='Z')
				||('0'<=c && c<='9')
				||(c=='-')||(c=='_')){
				*q++ = c; j++;
				
			}else if ( c=='/' ){
				/* Check next char of the path */
				if ( c==*path ){
					PRINT_ERROR("Double / error\n");
					return FS_ERROR;
				}
				/* A directory is found */
				*q++ = c; *q = 0; j = 0;
				break; /* End of a node */
				
			}else if ( c=='.' ){
				/* '.' should not appear at 0 */
				if ( j==0 ){
					PRINT_ERROR(". have error\n");
					return FS_ERROR;
				}
				else{
					*q++ = c; j++;
				}
				
			}else if ( c==0 ){
				/* End of path */
				*q = 0;
				_SET_PATH_NODE_MAGIC(path_node);
				
				/* If last node is a directory, 
				 * it needn't increase depth. */
				if ( *(path-2)!='/' )
					path_node->depth++;
				
				return FS_OK;
			}else
			{
				PRINT_ERROR("Wrong character\n");
				return FS_ERROR;/* Wrong character */
			}
		}
		/* Next node */
		i++;
		path_node->depth++;
		
		/* Depth of path is too long */
		if ( i>NODE_NUM && *path){
			PRINT_ERROR("Depth of path is too long\n");
			return FS_ERROR;
		}
	}
}

/* Get name of file from inputed path node */
int get_file_name( PATH_NODE_S *path_node, char *fname ){
	register const char *p;

#ifndef FS_NOT_CHECK_ERROR
	if ( path_node==NULL || _CHECK_PATH_NODE_MAGIC(path_node)){
		PRINT_ERROR("Wrong PATH_NODE\n");
		return FS_ERROR;
	}
	
	if ( !fname ){
		PRINT_ERROR("NULL POINTER: fname\n");
		return FS_ERROR;
	}
#endif

	p = (const char *)&(path_node->node[path_node->depth-1]);
	if ( *p ){
		strcpy( fname, p );
	}else{
		PRINT_ERROR("Not found filename\n");
		return FS_ERROR;
	}
	
	return FS_OK;
}

#if 0
int main( void ){
	char buf[400];
	char fname[30];
	PATH_NODE_S pathnode;
	int i=0;
	
	for(;;){
		memset(buf,0,400);
		memset(fname,0,30);
		scanf("%s", buf);
		if (!parse_path(buf,&pathnode)){
			for(i=0;i<NODE_NUM;i++)
				if(pathnode.node[i])
					printf("%s\n",(char*)pathnode.node[i]);
			printf("depth=%d\n",pathnode.depth);
			get_file_name(&pathnode, fname);
			printf("FILENAME: %s\n", fname);
			
		}
	}
	
	return 100;
}
#endif

⌨️ 快捷键说明

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