r2util.c
来自「这是一个C程序分析工具」· C语言 代码 · 共 744 行 · 第 1/3 页
C
744 行
*cval = read;
return (1);
} /* getRecordType */
/*--------------------------------------------------------------------*/
/* FUNCTION SeekOut */
/*--------------------------------------------------------------------*/
/* PURPOSE Will return an indexed File name */
/* DESCRIPTION Uses the index number to traverse the linked list */
/* that is incorperated within the Binary Tree */
/* */
/* HISTORY: */
/* VER DATE AUTHOR DESCRIPTION */
/* 1.00 30 Mar 97 Wayne Flood Created function. */
/* 2.2 31 Jan 98 W. Hall Deleted unused variable 'temp'. */
/* Added a return statement to clean */
/* up a compiler warning issue. */
/*--------------------------------------------------------------------*/
char *SeekOut(int nodenum, Tree *ptree)
{
int i;
Node *root;
root = ptree->last;
if ((nodenum <= 0) || (nodenum > ptree->nodenum))
{
printf("Binary Tree Corrupted at\n");
printf("nodenum %d ptree %d\n",nodenum,ptree->nodenum);
AbortMsg("Corrupted file");
}
/* Knowing the size of the tree and the index number, allows */
/* the reverse traversal of the linked list until the node is */
/* found. Then a comparison between the index numbers is made. */
/* to make sure that it is a match */
for (i = (ptree->nodenum - nodenum); i >= 1; i--)
root = root->last;
if (nodenum == (*root).item.nodenum)
return ((*root).item.file);
AbortMsg("Corrupted file");
return(0); /*althought this return statement will never be */
/*executed, it has been included to clean up a */
/*compiler warning issue. */
}
/*--------------------------------------------------------------------*/
/* FUNCTION MakeNode */
/*--------------------------------------------------------------------*/
/* PURPOSE Will return a Node that was dynamically allocated */
/* Will Place data into the Node prior to return */
/* HISTORY: */
/* VER DATE AUTHOR DESCRIPTION */
/* 1.00 30 Mar 97 Wayne Flood Created function. */
/*--------------------------------------------------------------------*/
Node *MakeNode(Item pi)
{
Node *one;
if ((one = (Node *) malloc(sizeof(Node))) == NULL)
{
printf("Not enough memory to allocate buffer\n");
exit(1); /* terminate program if out of memory */
}
(*one).item.size = pi.size;
(*one).item.nodenum = pi.nodenum;
(*one).item.tfl = pi.tfl;
strcpy((*one).item.file, pi.file);
one->left = NULL;
one->right = NULL;
one->last = NULL;
return (one);
}
/*--------------------------------------------------------------------*/
/* FUNCTION AddItem */
/*--------------------------------------------------------------------*/
/* PURPOSE Will return an index number */
/* DESCRIPTION Will determine if the tree is empty. If empty will*/
/* start tree. If not then will call SeekIn to place */
/* the new item into the tree. */
/* */
/* HISTORY: */
/* VER DATE AUTHOR DESCRIPTION */
/* 1.00 30 Mar 97 Wayne Flood Created function. */
/*--------------------------------------------------------------------*/
int AddItem(Item pi, Tree *ptree)
{
Node *root;
root = ptree->root;
pi.tfl = pi.file[ pi.size - 3];
if (root == NULL)
{
ptree->root = WorkTree(pi, ptree);
return ((*(*ptree).root).item.nodenum);
}
else
return (SeekIn(pi, root, ptree));
}
/*--------------------------------------------------------------------*/
/* FUNCTION SeekIn */
/*--------------------------------------------------------------------*/
/* PURPOSE Will return an index number */
/* DESCRIPTION Does three different comparisons to locate */
/* a file name in the binary tree. The first compares*/
/* the size of the new file to the size of the File */
/* in the current node. When a match has been found */
/* a comparision between the third form last char */
/* in the File. If this is a match than a Memory */
/* comparison will be done comparing the new file */
/* name and the current node file name. If a match is*/
/* found the current number is returned. If a match */
/* is not found than SeekIn is recursivly called until*/
/* the end of the tree is found. A new node will be */
/* created and placed into the list, and the index */
/* number will be returned */
/* */
/* */
/* HISTORY: */
/* VER DATE AUTHOR DESCRIPTION */
/* 1.00 30 Mar 97 Wayne Flood Created function. */
/*--------------------------------------------------------------------*/
int SeekIn(Item pi, Node *root, Tree *ptree)
{
int cmp;
if (pi.size == (*root).item.size)
{
if (pi.tfl == (*root).item.tfl)
{
cmp = memcmp(pi.file, (*root).item.file, (*root).item.size);
if (cmp == 0)
return (*root).item.nodenum;
else if (cmp < 0)
if (root->left == NULL)
{
root->left = WorkTree(pi, ptree);
return ((*(*root).left).item.nodenum);
}
else return (SeekIn(pi, root->left, ptree));
else if (root->right == NULL)
{
root->right = WorkTree(pi, ptree);
return ((*(*root).right).item.nodenum);
}
else return (SeekIn(pi, root->right, ptree));
}
else if (pi.tfl < (*root).item.tfl)
if (root->left == NULL)
{
root->left = WorkTree(pi, ptree);
return ((*(*root).left).item.nodenum);
}
else return (SeekIn(pi, root->left, ptree));
else if (root->right == NULL)
{
root->right = WorkTree(pi, ptree);
return ((*(*root).right).item.nodenum);
}
else return (SeekIn(pi, root->right, ptree));
}
else if (pi.size < (*root).item.size)
if (root->left == NULL)
{
root->left = WorkTree(pi, ptree);
return ((*(*root).left).item.nodenum);
}
else return (SeekIn(pi, root->left, ptree));
else if (root->right == NULL)
{
root->right = WorkTree(pi, ptree);
return ((*(*root).right).item.nodenum);
}
else return (SeekIn(pi, root->right, ptree));
}
/*--------------------------------------------------------------------*/
/* FUNCTION WorkTree */
/*--------------------------------------------------------------------*/
/* PURPOSE Was created to do listed code in one location */
/* instead of seven. */
/* HISTORY: */
/* VER DATE AUTHOR DESCRIPTION */
/* 1.00 30 Mar 97 Wayne Flood Created function. */
/*--------------------------------------------------------------------*/
Node *WorkTree(Item pi, Tree *ptree)
{
Node *root = NULL;
pi.nodenum = ++ptree->nodenum; /* Increment index number */
root = MakeNode(pi);
root->last = ptree->last; /* Initialize the root->last */
ptree->last = root; /* To Initialize the Tree->last */
return(root);
}
/*--------------------------------------------------------------------*/
/* FUNCTION InitTree */
/*--------------------------------------------------------------------*/
/* PURPOSE Will initialize the Binary Tree */
/* */
/* HISTORY: */
/* VER DATE AUTHOR DESCRIPTION */
/* 30 Mar 97 Wayne Flood Created function. */
/*--------------------------------------------------------------------*/
void InitTree(Tree *ptree)
{
ptree->nodenum = 0;
ptree->root = NULL;
ptree->last = NULL;
}
/*--------------------------------------------------------------------*/
/* FUNCTION FreeTree */
/*--------------------------------------------------------------------*/
/* PURPOSE Will free the memory allocated in the Binary Tree */
/* */
/* HISTORY: */
/* VER DATE AUTHOR DESCRIPTION */
/* 30 Mar 97 Wayne Flood Created function. */
/*--------------------------------------------------------------------*/
RESULT FreeTree(Tree *ptree)
{
Node *temp;
if (ptree->nodenum > 0)
while (ptree->last != NULL)
{
temp = ptree->last;
ptree->last = ptree->last->last;
free(temp);
}
else return(FAIL);
ptree->root = NULL;
ptree->nodenum = 0;
return(SUCCEED);
}
/*====================================================================*/
/* EOF : r2util.c */
/*====================================================================*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?