📄 atbpb.c
字号:
}
/*******************************************************************************
$Function: ATB_index_Compare
$Description: This function compares two records based on the index_type specified in
index_type. It returns a value indicating whether the first record should
come before or after the second.
The function GI_pb_Compare is called first, to check whether there is
any user-specific sorting required. If not, then ATB_alpha_Compare is
called for an alpha tag, or ATB_num_Compare for a phone number.
For any other index_type, COMPARE_FIRSTBEFORE is returned as default.
$Returns: COMPARE_IDENTICAL The two records are identical
COMPARE_FIRSTBEFORE The first record should come before the second record
COMPARE_FIRSTAFTER The first record should come after the second record
$Arguments: record1 The first record to be compared
record2 The second record to be compared
index_type Indicator of the index_type which is to be compared
*******************************************************************************/
T_PB_COMPARE ATB_index_Compare(T_PB_RECORD *record1, T_PB_RECORD *record2, T_PB_INDEX index_type)
{
T_PB_COMPARE compare;
compare = GI_pb_Compare(record1, record2, index_type);
if (compare==COMPARE_DEFAULT)
{
switch(index_type)
{
case INDEX_NAME:
compare = ATB_alpha_Compare(&record1->alpha, &record2->alpha);
break;
case INDEX_NUMBER:
compare = ATB_num_Compare(record1->number, record2->number);
break;
}
}
return compare;
}
/*******************************************************************************
$Function: ATB_alpha_Compare
$Description: This function compares two alpha strings on the basis of an ascending
alphanumeric latin list, and specifies which record would come first.
$Returns: COMPARE_FIRSTBEFORE The first record should come before the second record
COMPARE_FIRSTAFTER The first record should come after the second record
$Arguments: alpha1 The first alpha tag
alpha2 The second alpha tag
*******************************************************************************/
T_PB_COMPARE ATB_alpha_Compare(T_PB_ALPHA *alpha1, T_PB_ALPHA *alpha2)
{
T_PB_COMPARE compare;
SHORT charIndex;
USHORT length1 = alpha1->length;
USHORT length2 = alpha2->length;
USHORT char1;
USHORT char2;
compare = COMPARE_IDENTICAL; /* Default */
/* Special case, length of first string is 0 */
if (length1==0)
compare = COMPARE_FIRSTBEFORE;
for (charIndex = 0; charIndex < length1; charIndex++)
{
if (charIndex==length2) /* E.g. "Johnson" and "John" */
{
compare = COMPARE_FIRSTAFTER;
break;
}
char1 = alpha1->data[charIndex];
if (char1>=(SHORT)'A' && char1<=(SHORT)'Z')
char1 += (SHORT)('a'-'A');
char2 = alpha2->data[charIndex];
if (char2>=(SHORT)'A' && char2<=(SHORT)'Z')
char2 += (SHORT)('a'-'A');
if (char1 < char2)
{
compare = COMPARE_FIRSTBEFORE;
break;
}
if (char1 > char2)
{
compare = COMPARE_FIRSTAFTER;
break;
}
if (charIndex==length1-1 && length2>length1) /*E.g. "John" and "Johnson" */
{
compare = COMPARE_FIRSTBEFORE;
break;
}
}
return compare;
}
/*******************************************************************************
$Function: ATB_alpha_Match
$Description: This function matches two alpha strings on the basis of a latin string
matched from the start, and specifies how they match.
$Returns: MATCH_NONE The strings do not match
MATCH_START The first string matches the start of the second string
MATCH_EXACT The two strings match exactly
$Arguments: alpha1 The first alpha tag
alpha2 The second alpha tag
*******************************************************************************/
T_PB_COMPARE ATB_alpha_Match(T_PB_ALPHA *alpha1, T_PB_ALPHA *alpha2)
{
T_PB_MATCH match;
SHORT charIndex;
USHORT length1 = alpha1->length;
USHORT length2 = alpha2->length;
SHORT offset;
UBYTE searching;
UBYTE some_match;
USHORT char1, char2;
match = MATCH_EXACT; /* Default */
searching = TRUE;
some_match = FALSE;
offset = 0;
do
{
for (charIndex = 0; charIndex < length1; charIndex++)
{
if (charIndex==(length2-offset)) /* E.g. "Johnson" and "John" */
{
searching = FALSE;
break; /* No match, will exit do/while */
}
char1 = alpha1->data[charIndex];
if (char1>=(SHORT)'A' && char1<=(SHORT)'Z')
char1 += (SHORT)('a'-'A');
char2 = alpha2->data[charIndex+offset];
if (char2>=(SHORT)'A' && char2<=(SHORT)'Z')
char2 += (SHORT)('a'-'A');
if (char1 != char2)
{
some_match = FALSE; /* Any fragment so far identified does not fit */
break; /* No match, keep looking */
}
some_match = TRUE;
if (charIndex==length1-1 && length2>length1) /*E.g. "John" and "Johnson" */
{
if (offset==0)
match = MATCH_START;
else
match = MATCH_FRAGMENT;
break;
}
}
if (some_match==TRUE)
{
searching = FALSE;
}
else
{
offset++;
/* If the fragment won't fit, don't keep looking */
if ((offset+length1)>length2)
{
match=MATCH_NONE;
searching = FALSE;
}
}
}while (searching);
return match;
}
/*******************************************************************************
$Function: ATB_num_Digit
$Description: Extracts the four-bit digit from a BCD number
$Returns: The four bit digit
$Arguments: num The BCD number
numIndex The position in the BCD number to look
*******************************************************************************/
UBYTE ATB_num_Digit(UBYTE *num, SHORT numIndex)
{
UBYTE digit;
UBYTE shift = (numIndex&0x1)*0x4;
digit = num[numIndex>>1]; /* Each BCD digit takes up half a byte */
digit &= (0xF<<shift); /* Isolate the digit */
digit >>= shift; /* Shift it so it has a value 0x0 - 0xF */
return digit;
}
/*******************************************************************************
$Function: ATB_num_Length
$Description: Returns the length of a BCD number in digits
$Returns: Length of BCD number
$Arguments: num The BCD number
*******************************************************************************/
SHORT ATB_num_Length(UBYTE *num)
{
SHORT length;
for (length = 0; ATB_num_Digit(num, length)!=0xF; length++)
{
}
return length;
}
/*******************************************************************************
$Function: ATB_num_Compare
$Description: This function compares two phone numbers by the standard comparison
method (ascending numeric, from the end of the number) and specifies
which record would come first if sorted in this method.
$Returns: COMPARE_FIRSTBEFORE The first record should come before the second record
COMPARE_FIRSTAFTER The first record should come after the second record
$Arguments: num1 The first number
num2 The second number
*******************************************************************************/
T_PB_COMPARE ATB_num_Compare(UBYTE *num1, UBYTE *num2)
{
T_PB_COMPARE compare;
SHORT charIndex;
SHORT length1 = ATB_num_Length(num1);
SHORT length2 = ATB_num_Length(num2);
UBYTE digit1;
UBYTE digit2;
compare = COMPARE_IDENTICAL; /* Default */
for (charIndex = 0; charIndex < length1; charIndex++)
{
if (charIndex==length2) /* E.g. "123456" and "1234" */
{
compare = COMPARE_FIRSTAFTER;
break;
}
digit1 = ATB_num_Digit(num1, (SHORT)(length1-charIndex-1));
digit2 = ATB_num_Digit(num2, (SHORT)(length2-charIndex-1));
if (digit1 < digit2)
{
compare = COMPARE_FIRSTBEFORE;
break;
}
if (digit1 > digit2)
{
compare = COMPARE_FIRSTAFTER;
break;
}
if (charIndex==length1-1 && length2>length1) /*E.g. "1234" and "123456" */
{
compare = COMPARE_FIRSTBEFORE;
break;
}
}
return compare;
}
/*******************************************************************************
$Function: ATB_num_Match
$Description: This function matches phone numbers, from the end backwards, and
specifies how they match.
$Returns: MATCH_NONE The numbers do not match
MATCH_START The first number matches the end of the second number
MATCH_EXACT The two numbers match exactly
$Arguments: num1 The first number
num2 The second number
*******************************************************************************/
T_PB_MATCH ATB_num_Match(UBYTE *num1, UBYTE *num2)
{
T_PB_MATCH match;
SHORT charIndex;
SHORT length1 = ATB_num_Length(num1);
SHORT length2 = ATB_num_Length(num2);
SHORT offset;
UBYTE searching;
UBYTE some_match;
UBYTE digit1;
UBYTE digit2;
match = MATCH_EXACT; /* Default */
searching = TRUE;
some_match = FALSE;
offset = 0;
do
{
for (charIndex = 0; charIndex < length1; charIndex++)
{
if (charIndex==(length2-offset)) /* E.g. "12345" and "123" */
{
searching = FALSE;
break; /* No match, will exit do/while */
}
digit1 = ATB_num_Digit(num1, (SHORT)(length1-charIndex-1));
digit2 = ATB_num_Digit(num2, (SHORT)(length2-charIndex-1));
if (digit1 != digit2)
{
some_match = FALSE; /* Any fragment so far identified does not fit */
break; /* No match, keep looking */
}
some_match = TRUE;
if (charIndex==length1-1 && length2>length1) /*E.g. "123" and "12345" */
{
if (offset==0)
match = MATCH_START;
else
match = MATCH_FRAGMENT;
break;
}
}
if (some_match==TRUE)
{
searching = FALSE;
}
else
{
offset++;
/* If the fragment won't fit, don't keep looking */
if ((offset+length1)>length2)
{
match=MATCH_NONE;
searching = FALSE;
}
}
} while (searching);
return match;
}
/*******************************************************************************
$Function: ATB_mem_UpdateCache
$Description: Updates the cache with the record provided. If the cache is full, the
least used record is overwritten.
$Returns: None.
$Arguments: phonebook_id The phonebook identifier
index_type The index table required.
phys_index The physical index of the record to add.
record The record to add.
*******************************************************************************/
void ATB_mem_UpdateCache(SHORT phonebook_id, SHORT phys_index, T_PB_RECORD *record)
{
T_PB_DATA *data = ATB_hnd_GetPbData(phonebook_id);
SHORT cacheIndex;
SHORT leastUsed;
SHORT leastIndex;
/* First check if record is already in RAM */
if (data->cache[phys_index] != NULL)
{
/* Copy the record in case it has changed */
ATB_pb_CopyRec(phonebook_id, data->cache[phys_index], record);
if (data->in_memory[phys_index]<0xFFFF)
data->in_memory[phys_index]++;
return;
}
/* If the cache is full, find the least accessed record */
if (data->cache_size==data->cache_max)
{
leastIndex = 0;
leastUsed = 255;
for (cacheIndex=0; cacheIndex<data->records_max; cacheIndex++)
{
if (data->cache[cacheIndex]!=NULL && data->in_memory[cacheIndex] < leastUsed)
{
leastUsed = data->in_memory[cacheIndex];
leastIndex = cacheIndex;
}
}
/* Give the new record the memory allocated for the least used record */
data->cache[phys_index] = data->cache[leastIn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -