?? parse_cfgfile.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 + -