#include "StdAfx.h" #include "bstreelinkedlist.h" #include "Patcher.h" CNode::CNode(void) { m_pParent = NULL; m_pLeftChild = NULL; m_pRightChild = NULL; m_pNext_node = NULL; m_pPrev_node = NULL; m_pcKey = NULL; //µ¥ÀÌÅÍ Å°°ª m_psPatchfile = new Patchfile; memset( m_psPatchfile->fileName, 0, sizeof( m_psPatchfile->fileName)); //m_psPatchfile = NULL; } CNode::~CNode(void) { m_pParent = NULL; m_pLeftChild = NULL; m_pRightChild = NULL; m_pNext_node = NULL; m_pPrev_node = NULL; m_pcKey = NULL; //µ¥ÀÌÅÍ Å°°ª if( m_psPatchfile != NULL ) delete m_psPatchfile; m_psPatchfile = NULL; } CBSTreeLinkedList::CBSTreeLinkedList(void) { m_pLinked_List_Head = NULL; m_pLinked_List_Tail = NULL; m_pBSTreeRoot = NULL; } CBSTreeLinkedList::~CBSTreeLinkedList(void) { if( m_pLinked_List_Head != NULL) if( false == Delete_linked_list(m_pLinked_List_Head) ) { assert(0); printf("error:¸µÅ©µå ¸®½ºÆ® »èÁ¦¿¡ ½ÇÆÐÇÏ¿´½À´Ï´Ù.\n"); } m_pLinked_List_Head = NULL; m_pLinked_List_Tail = NULL; m_pBSTreeRoot = NULL; } bool CBSTreeLinkedList::ReleaseMemory() { if( m_pLinked_List_Head != NULL) if( false == Delete_linked_list(m_pLinked_List_Head) ) { assert(0); printf("error:¸µÅ©µå ¸®½ºÆ® »èÁ¦¿¡ ½ÇÆÐÇÏ¿´½À´Ï´Ù.\n"); return false; } m_pLinked_List_Head = NULL; m_pLinked_List_Tail = NULL; m_pBSTreeRoot = NULL; return true; } bool CBSTreeLinkedList::Add_Node(Patchfile* input_data) { if( strlen( input_data->fileName ) == 0 ) { assert(0); return false; } else { //--------Æ®¸®¿¡ Ãß°¡-------- CNode *pInsert_pos_finder = NULL, *pInsert_pos_parent = NULL; CNode *new_node = NULL; pInsert_pos_finder = m_pBSTreeRoot; //rootºÎÅÍ Å½»ö //Æ®¸®¿¡ µé¾î°¥ À§Ä¡ ã±â. while( pInsert_pos_finder != NULL ) { int ret= stricmp( pInsert_pos_finder->m_pcKey, input_data->fileName ); pInsert_pos_parent = pInsert_pos_finder; //ºÎ¸ð ³ëµå ¼ÂÆÃ //Ãß°¡ÇÒ µ¥ÀÌÅͰ¡ µé¾î°¥ ³ëµå À§Ä¡¸¦ ã±â À§ÇØ °ª ºñ±³ if( ret < 0 ) { pInsert_pos_finder = pInsert_pos_finder->m_pRightChild; //°ªÀÌ ´õ Å©´Ù¸é ¿À¸¥ÂÊ ³ëµå·Î~ } else if( ret > 0 ) { pInsert_pos_finder = pInsert_pos_finder->m_pLeftChild; //°ªÀÌ ´õ ÀÛ´Ù¸é ¿ÞÂÊ ³ëµå·Î } else /*if( pInsert_pos_finder->m_pcKey == m_pcKey )*///°ªÀÌ °°´Ù¸é Áߺ¹µÈ °ªÀÌ µé¾î¿ÔÀ¸¹Ç·Î ¿¡·¯ ¹ß»ý { // assert(0); return false; } } //»õ·Ó°Ô Ãß°¡ÇÒ ¼ö ÀÖ´Â Á¶°ÇÀ» ¸¸Á·ÇßÀ¸¹Ç·Î ¸Þ¸ð¸® ÇÒ´ç new_node = new CNode; if(new_node == NULL) { printf("error:»õ·Î¿î ³ëµå ¸Þ¸ð¸® ÇÒ´ç ½ÇÆÐ·Î ³ëµå Ãß°¡ ½ÇÆÐ.\n"); return false; } new_node->m_psPatchfile->sizeHigh = input_data->sizeHigh; new_node->m_psPatchfile->sizeLow = input_data->sizeLow; new_node->m_psPatchfile->writeTime = input_data->writeTime; strncpy( new_node->m_psPatchfile->fileName, input_data->fileName, strlen( input_data->fileName ) ); new_node->m_pcKey = new_node->m_psPatchfile->fileName; //Æ®¸®¿¡ µé¾î°¥ À§Ä¡¿¡ »ðÀÔ. if( m_pBSTreeRoot == pInsert_pos_finder ) //ºñ¾îÀÖ´Â Æ®¸®¿¡ »ðÀÔ { m_pBSTreeRoot = new_node; } else if( stricmp( new_node->m_pcKey, pInsert_pos_parent->m_pcKey ) > 0 ) //ºÎ¸ð³ëµåº¸´Ù Ŭ °æ¿ì ¿À¸¥ÂÊ ³ëµå·Î ¼ÂÆÃ { pInsert_pos_parent->m_pRightChild = new_node; new_node->m_pParent = pInsert_pos_parent; } else /*if( strcmp( new_node->m_pcKey, pInsert_pos_parent->m_pcKey ) < 0 )*/ //ºÎ¸ð³ëµåº¸´Ù ÀÛÀ» °æ¿ì ¿ÞÂÊ ³ëµå·Î ¼ÂÆÃ { pInsert_pos_parent->m_pLeftChild = new_node; new_node->m_pParent = pInsert_pos_parent; } //--------¸µÅ©µå ¸®½ºÆ®¿¡ Ãß°¡-------- //ºñ¾îÀÖ´Â ¸µÅ©µå ¸®½ºÆ®¿¡ Ãß°¡. if(m_pLinked_List_Head == NULL) { m_pLinked_List_Head = new_node; m_pLinked_List_Tail = new_node; } else //ºñ¾îÀÖÁö ¾ÊÀº ¸®½ºÆ®¿¡ Ãß°¡. { m_pLinked_List_Tail->m_pNext_node = new_node; new_node->m_pPrev_node = m_pLinked_List_Tail; m_pLinked_List_Tail = m_pLinked_List_Tail->m_pNext_node; } return true; } return false; } bool CBSTreeLinkedList::Delete_linked_list(CNode *header) //¸µÅ©µå ¸®½ºÆ®ÀÇ ¸ðµç µ¥ÀÌÅÍ Áö¿ì±â { if(header->m_pPrev_node != NULL) { assert(0); //printf("error:»èÁ¦½ÇÆÐ! ¸µÅ©µå ¸®½ºÆ®ÀÇ Çì´õ°¡ ¾Æ´Õ´Ï´Ù.\n"); return false; } CNode *pdel_node = NULL; while( header != NULL) //¸ðµç µ¥ÀÌÅ͸¦ Áö¿ï¶§±îÁö { pdel_node = header; header = header->m_pNext_node; delete pdel_node; } return true; } Patchfile* CBSTreeLinkedList::Search_Node(char *key, bool msg_off) //³ëµå ã±â. { CNode *pNode = m_pBSTreeRoot; if(pNode == NULL) { if(msg_off == false) printf("error:Æ®¸®°¡ ºñ¾îÀÖ½À´Ï´Ù\n"); return NULL; } if( strlen( key ) == 0 ) //۰¡ À߸øµÇ¾ú´Ù¸é °Ë»ö ½ÇÆÐ { if(msg_off == false) printf("error:À߸øµÈ µ¥ÀÌÅ͸¦ ÀÌ¿ëÇÏ¿© °Ë»öÇϼ̽À´Ï´Ù.\n"); assert(0); return NULL; } while(pNode != NULL) { int ret; ret = stricmp( pNode->m_pcKey, key); //Ãß°¡ÇÒ µ¥ÀÌÅͰ¡ µé¾î°¥ ³ëµå À§Ä¡¸¦ ã±â À§ÇØ °ª ºñ±³ if( ret < 0 ) { pNode = pNode->m_pRightChild; //°ªÀÌ ´õ Å©´Ù¸é ¿À¸¥ÂÊ ³ëµå·Î~ } else if( ret > 0) { pNode = pNode->m_pLeftChild; //°ªÀÌ ´õ ÀÛ´Ù¸é ¿ÞÂÊ ³ëµå·Î } else /*if( pNode->m_pcKey == key_num )*/ //°ªÀÌ °°´Ù¸é °Ë»ö ¼º°ø { return pNode->m_psPatchfile; } } //while¹®À» ºüÁ®³ª¿Ô´Ù¸é ºñ¾îÀÖ´Â °÷À» ã¾ÒÀ¸¹Ç·Î °Ë»ö ½ÇÆÐ // if(msg_off == false) // printf("°Ë»ö½ÇÆÐ:ÇØ´ç µ¥ÀÌÅ͸¦ ãÀ» ¼ö ¾ø½À´Ï´Ù.\n"); return NULL; }