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

📄 parse_cfgfile.cpp

📁 参照网上的例子改写的多叉树的读写程序
💻 CPP
字号:
#include "parse_cfgfile.h"
#include <stdlib.h>
#include <string.h>
#include "mystack.h"

void load_cfgndvar(struct tree_node **root,char *cfg_filepath)
{

	//  还没有写函数体 请补充完整




}

//node_type

void read_fromcfgfile(FILE *fp,struct tree_node **root,struct mystack *s)
{
	//  还没有写函数体 请补充完整
	struct tree_node *next_node;
	struct tree_node *curr_node=*root;
	enum node_type tp;
	int lastOp =1; // 0  叶子节点  1  内部节点  2 出栈
	curr_node->nextsibling = NULL;

	
	while( (next_node=next_node_fromfile(fp,&tp) ) != NULL)
	{
		switch (tp)
		{
		case m_leaf:  //叶节点
			{
				if(lastOp == 0 )
				{
					curr_node->firstchild = NULL;
					curr_node->nextsibling =next_node;
				}
				else if(lastOp == 2)
				{
					curr_node->nextsibling =next_node;
				}
				else if (lastOp == 1)
				{
					curr_node->firstchild = next_node;
				}
				curr_node = next_node;
				lastOp = 0;
			}
			break;
		case m_bran0:  //进栈
			{
				if(lastOp == 2  )
				{
				   curr_node->nextsibling = next_node;
				}
				else if (lastOp == 0)
				{
					curr_node->firstchild =NULL;
					curr_node->nextsibling =next_node;
				}
				else if (lastOp == 1 )
				{
					curr_node->firstchild = next_node;
				}
	
				push_stack(s,next_node);
				curr_node = next_node;
				lastOp = 1;
			}
			break;
		case m_bran1:  // 退栈
			{
				if(lastOp == 2)
				{
					curr_node->nextsibling = NULL;
				}
				else if(lastOp == 0)
				{
					curr_node->firstchild = NULL;
					curr_node->nextsibling = NULL;
				}
				else if(lastOp == 1)
				{
					printf("file error");
				}

				curr_node = (struct tree_node *)pop_stack(s);

				//if ( strcmp (curr_node->data,next_node->data) != 0)
				//	printf("file error");

				lastOp = 2;
			}
			break;

		}
	}





}


struct tree_node *next_node_fromfile(FILE *fp,enum node_type *tp)
{
   char tmpLine[512]; 
   char rtnval; 
   int i = 0; 
   int flag = 0;  // 0
   struct tree_node *ptreenode=NULL;
   
   char * tmpS,*tmpE,*p; 
   
   if(  feof(fp)  )
	   return NULL;

    while (!feof(fp) &&  ( (rtnval=fgetc(fp) ) != '\n')  ) 
    {
		
	   if ( rtnval == EOF ) 
      { 
         break; 
      } 
      else 
      { 
         tmpLine[i] = rtnval; 
      } 

		if(rtnval == '[')
		{
			flag =1;
			tmpS =&tmpLine[i];
		}
		if(flag==1 && tmpLine[i]=='/' && tmpLine[i-1]=='[')
		{
			flag = 2; 
			tmpS =&tmpLine[i];
		}
		if(flag  &&  rtnval == ']')
		{
			tmpE =&tmpLine[i];
		}
		i++;
	}
	tmpS++;
	tmpLine[i]='\0';

	printf("%s\n",tmpLine);

	ptreenode = (struct tree_node *)malloc(sizeof(struct tree_node));

	switch (flag)
	{
	case 0:  // 叶子节点
		{
			*tp = m_leaf;
			p =strchr(tmpLine,'=');
			
			char  *p1 = tmpLine;
			while(*p1 ==' ') p1++;
			
			strncpy(ptreenode->data,p1,p-p1);
			ptreenode->data[p-p1] = '\0';
			
			strncpy(ptreenode->value,p+1,strlen(tmpLine)-(p-1-tmpLine));
			
			ptreenode->value[strlen(tmpLine)-(p-1-tmpLine) ]  = '\0';
			
			
			ptreenode->flag = 0;
			ptreenode->firstchild = NULL;
			ptreenode->nextsibling = NULL;
		}
		break;
	case 1:   //中间节点开始
		*tp = m_bran0;
		strncpy(ptreenode->data,tmpS,tmpE-tmpS);
		ptreenode->data[tmpE-tmpS] = '\0';
		ptreenode->flag = 1;
		ptreenode->firstchild = NULL;
		ptreenode->nextsibling = NULL;

		break;
	case 2:  //中间节点结束

		*tp = m_bran1;
		strncpy(ptreenode->data,tmpS,tmpE-tmpS);
		ptreenode->data[tmpE-tmpS] = '\0';
		ptreenode->flag = 1;
		ptreenode->firstchild = NULL;
		ptreenode->nextsibling = NULL;

		break;

	}
	
     return (ptreenode);
 
}




void insertnode_byparent(struct tree_node *root, struct tree_node *defaultnode)
{
	//  还没有写函数体 请补充完整

}

/*      load_envvar(&defaultnode);
        load_cfgndvar(&defaultnode, cfg_filepath);
        load_taskdvar(&defaultnode, task_filepath);
        load_cmdvar(&defaultnode, argv);
*/

//    char path2[]= "1:7:9";                        /* 结点 2 的路径      */

#define MAX_PATH_LEN 100

/*
void get_node(struct tree_node *root,const char *path, struct tree_node **subroot)
{
	//  还没有写函数体 请补充完整
	int lastpathflag = 0;
	char pathcpy[MAX_PATH_LEN];
    char pathpart[MAX_PATH_LEN];
	char pathremain[MAX_PATH_LEN];
	

	if(root==NULL)
	{
		subroot=NULL;
		return;
	}
	
	if(root->flag == -1) //根节点
	{
		if(root->firstchild)
			root = root->firstchild;
	}

	if(path==NULL )
	{
		subroot=NULL;
		return ;
	}

	strcpy(pathcpy,path);
	strcpy(pathpart,path);
	char  *pPos=strchr(pathcpy,':');
	if(pPos != NULL)
	{
		strncpy(pathpart,pathcpy,pPos-pathcpy);
		pathpart[pPos-pathcpy]='\0';

		strcpy(pathremain,pPos+1);
		
		printf("zong路径=%s \n",path);
		printf("pre路径=%s \n",pathpart);
		printf("remain路径=%s \n",pathremain);

	}
	else // pPos =NULL
	{
		lastpathflag = 1;
	}
	
	if (lastpathflag)
	{
		if( strcmp(root->data,pathpart) == 0 )
		{
			subroot = &root;
			return ;
		}
		else  
		{
			struct tree_node *sibling = root->nextsibling;
			while(sibling != NULL)
			{
				if( strcmp(sibling->data,pathpart) == 0) 
				{
					subroot = &sibling;
					return;
				}
				sibling = sibling->nextsibling;
			}
		}

	}
	else  //没有到最后层
	{
		if(  strcmp(root->data,pathpart) == 0 )
		{
			struct tree_node *child = root->firstchild;
			if(child != NULL)
			{
				get_node(child,pathremain,subroot);
				if(subroot != NULL)
					return;
			}
			
		}
		else
		{
			struct tree_node *sibling = root->nextsibling;
			while(sibling != NULL)
			{
				if(strcmp(sibling->data,pathpart) == 0)
				{
					if(sibling->firstchild)
					{
						get_node(sibling->firstchild,pathremain,subroot);
						if(subroot != NULL) return;
					}
				}
				
				sibling = sibling->nextsibling;
			}
		}

	}





}

*/

struct tree_node *get_node(struct tree_node *root,const char *path)
{
	//  还没有写函数体 请补充完整
	int lastpathflag = 0;
	char pathcpy[MAX_PATH_LEN];
    char pathpart[MAX_PATH_LEN];
	char pathremain[MAX_PATH_LEN];
	static struct tree_node  *subroot=NULL;

	if(root==NULL)
	{
		subroot = NULL;
		return subroot;
	}
	
	if(root->flag == -1) //根节点
	{
		if(root->firstchild)
			root = root->firstchild;
	}

	if(path==NULL )
	{
		subroot=NULL;
		return subroot;
	}

	strcpy(pathcpy,path);
	strcpy(pathpart,path);
	char  *pPos=strchr(pathcpy,':');
	if(pPos != NULL)
	{
		strncpy(pathpart,pathcpy,pPos-pathcpy);
		pathpart[pPos-pathcpy]='\0';

		strcpy(pathremain,pPos+1);
		
		//printf("zong路径=%s \n",path);
		//printf("pre路径=%s \n",pathpart);
		//printf("remain路径=%s \n",pathremain);

	}
	else // pPos =NULL
	{
		lastpathflag = 1;
	}
	
	if (lastpathflag)
	{
		if( strcmp(root->data,pathpart) == 0 )
		{
			subroot = root;
			return subroot;
		}
		else  
		{
			struct tree_node *sibling = root->nextsibling;
			while(sibling != NULL)
			{
				if( strcmp(sibling->data,pathpart) == 0) 
				{
					subroot = sibling;
					return subroot;
				}
				sibling = sibling->nextsibling;
			}
		}

	}
	else  //没有到最后层
	{
		if(  strcmp(root->data,pathpart) == 0 )
		{
			struct tree_node *child = root->firstchild;
			if(child != NULL)
			{
				if( get_node(child,pathremain) ) 
					return subroot;
			}
			
		}
		else
		{
			struct tree_node *sibling = root->nextsibling;
			while(sibling != NULL)
			{
				if(strcmp(sibling->data,pathpart) == 0)
				{
					if(sibling->firstchild)
					{
						if( get_node(sibling->firstchild,pathremain) )
							return subroot;
					}
				}
				
				sibling = sibling->nextsibling;
			}
		}

	}
	return subroot;

}




void recursion_dump(struct tree_node *node,FILE *fp) 
{
	//  还没有写函数体 请补充完整


}

void trim(char *parentpath)
{



}


⌨️ 快捷键说明

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