📄 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 + -