?? c_key_value.cpp
字號:
/***********************************************************************
* Module: c_key_value.cpp
* Author: Administrator
* Modified: 2005年9月15日 16:38:32
* Purpose: Implementation of the class c_key_value
* Comment: 關鍵字值類. 這樣在類中應用了大量的new操作, 一定要防止發生數據溢出的情況。
***********************************************************************/
#include <key_info.h>
#include <c_key_value.h>
////////////////////////////////////////////////////////////////////////
// Name: c_key_value::operator<() 定義小于號操作符
// Purpose: Implementation of c_key_value::operator<()
// Return: int
////////////////////////////////////////////////////////////////////////
void c_key_value::setKeyCount( unsigned char c )
{
key_count = c;
keyInfo = new KEY_INFO[(int)key_count];
if( keyInfo == NULL )
{
cout<<"內存不夠"<<endl;
exit(1);
}
}
c_key_value::c_key_value( const c_key_value & p_key )
{
key_count = p_key.key_count;
keyInfo = new KEY_INFO[(int)key_count];
// keyInfo = (KEY_INFO*)malloc( sizeof(KEY_INFO)*( 1 ) );
if( keyInfo == NULL )
{
cout<<"keyInfo 內存不足, 程序退出"<<endl;
exit(1);
}
//復制元素
for( int i = 0; i< (int)key_count; i++ )
{
//拷貝實際指針
keyInfo[i].buf = NULL;
keyInfo[i].buf = new char[ (int)p_key.keyInfo[i].length ];
if( keyInfo[i].buf == NULL )
{
cout<<"keyInfo buf 內存不足, 程序退出"<<endl;
exit(1);
}
//拷貝實際數據
memcpy( keyInfo[i].buf, p_key.keyInfo[i].buf, p_key.keyInfo[i].length );
keyInfo[i].data_type = p_key.keyInfo[i].data_type;
keyInfo[i].length = p_key.keyInfo[i].length;
}
}
c_key_value& c_key_value::operator=( const c_key_value& p_key )
{
key_count = p_key.key_count;
keyInfo = new KEY_INFO[(int)key_count];
//復制元素
for( int i = 0; i< (int)key_count; i++ )
{
//拷貝實際指針
keyInfo[i].buf = new char[ (int)p_key.keyInfo[i].length ];
//拷貝實際數據
memcpy( keyInfo[i].buf, p_key.keyInfo[i].buf, p_key.keyInfo[i].length );
keyInfo[i].data_type = p_key.keyInfo[i].data_type;
keyInfo[i].length = p_key.keyInfo[i].length;
}
//復制了一個對象
return *this;
}
//所有的比較都歸為兩類. 一類strcmp, 一類memcmp
//自己與自己比較
int c_key_value::keyCmp( const c_key_value& p_key ) const
{
// TODO : implement, 不允許key_count為零.否則就沒有意義
int ret = 0;
for( int i = 0; i< (int)key_count; i++ )
{
//進行比較
switch( keyInfo[i].data_type )
{
case 0: //string , 字符串
ret = strcmp( keyInfo[i].buf, p_key.keyInfo[i].buf);
break;
/* case 1: //char, 單字符
case 2: //int
case 3: //double
case 4: //float 等等 */
default:
// ret = memcmp( keyInfo[i].buf, p_key.keyInfo[i].buf, p_key.keyInfo[i].length);
ret = strcmp( keyInfo[i].buf, p_key.keyInfo[i].buf);
break;
}
// printf("%s, %s, %d, %d\n", keyInfo[i].buf, p_key.keyInfo[i].buf, ret, p_key.keyInfo[i].length );
if( ret != 0 )
break;
}
return ret;
}
//只設計數據, 用于查找時用, 不用每次new一個.每次new, 很浪費時間
//主要用于快速查找
void c_key_value::setData( int key_seq, void * buf_v )
{
if( key_seq < key_count )
{
memcpy( keyInfo[key_seq].buf, buf_v, keyInfo[key_seq].length );
}
else
{
cout<<" key_seq 大于 key_count , 程序退出 "<<endl;
exit(1);
}
return;
}
////////////////////////////////////////////////////////////////////////
// Name: c_key_value::addKey()
// Purpose: Implementation of c_key_value::addKey()
// Comment: addKey的參數, 可以是常見的所有數據類型。 如char, string, int , double等。
// Return: int
////////////////////////////////////////////////////////////////////////
//目前類型type_id 可以只為0 和 1
int c_key_value::setKeyAttr(int key_seq, int type_id, int buf_size )
{
// TODO : implement
keyInfo[key_seq].data_type = type_id;
keyInfo[key_seq].length = buf_size;
//將buf設置好
keyInfo[key_seq].buf = new char[buf_size];
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -