📄 atbpb.c
字号:
else if (result!=PB_EXCT)
GI_pb_Error(phonebook_id, PB_FIND, result);
return result;
}
/*******************************************************************************
$Function: ATB_pb_ReadRecList
$Description: Fetches record information of sequential records in an index list, starting
at logical record start_log_index and fetching recs_count records in total.
The information will be stored in the caller allocated array of records
provided.
$Returns: PB_OK Action completed OK.
PB_BUSY Failed, phonebook is busy.
PB_FILEREADFAIL File read encountered an error
PB_RECDOESNOTEXIST Tried to access a record that does not exist
$Arguments: phonebook_id The phonebook identifier
index_type The index list to use
start_log_index Logical index in the index list of the first record to
read
recs_count Total number of sequential records to read.
record Array of entries in which to store the fetched records.
The array must be recs_count in size. (Caller allocated).
*******************************************************************************/
PB_RET ATB_pb_ReadRecList(SHORT phonebook_id, T_PB_INDEX index_type, SHORT start_log_index, SHORT num_recs, T_PB_LIST *list)
{
PB_RET result;
SHORT log_index;
tracefunction("ATB_pb_ReadRecList");
list->records_used = 0;
/* Make sure phonebook is not busy */
if (ATB_pb_Status(phonebook_id)==PB_BUSY)
{
trace("* ERROR * - Phonebook is busy");
/* Report any errors to the GI */
GI_pb_Error(phonebook_id, PB_READRECLIST, PB_BUSY);
return PB_BUSY;
}
/* Read in list of records */
for (log_index = start_log_index; log_index<start_log_index+num_recs && log_index<list->records_max; log_index++)
{
result = ATB_mem_ReadRec(phonebook_id, index_type, log_index, &list->record[log_index-start_log_index]);
if (result!=PB_OK)
break;
list->records_used++;
}
/* Close any open file */
FS_pb_Finished(phonebook_id);
/* Send success/failure info to the GI */
if (result==PB_OK)
GI_pb_OK(phonebook_id, PB_READRECLIST, NULL);
else if (result!=PB_EXCT)
GI_pb_Error(phonebook_id, PB_READRECLIST, result);
return result;
}
/*******************************************************************************
$Function: ATB_pb_Search
$Description: Searches every record in the index list for a fragment. The index list
must be either INDEX_NAME or INDEX_NUMBER. If it is INDEX_NAME,
then a name fragment will be searched for; if it is INDEX_NUMBER,
a number fragment will be searched for.
The results are stored in an index list. They may be accessed by
using the normal read and write functions, using the index type
INDEX_SEARCH. The size of the search list is returned in the variable
recs_count.
$Returns: PB_OK Action completed OK.
PB_BUSY Failed, phonebook is busy.
PB_FILEREADFAIL File read encountered an error
PB_RECDOESNOTEXIST Tried to access a record that does not exist
(should never happen)
$Arguments: phonebook_id The phonebook identifier
index_type The index list to use
record Record containing the fragment to search for
recs_count Pointer to an int (user allocated) in which will be returned
the number of search results.
*******************************************************************************/
PB_RET ATB_pb_Search(SHORT phonebook_id, T_PB_INDEX index_type, T_PB_RECORD *record, SHORT *recs_count)
{
T_PB_DATA *data = ATB_hnd_GetPbData(phonebook_id);
PB_RET result;
SHORT count = 0;
SHORT log_index;
T_PB_MATCH match;
T_PB_RECORD *cur_record;
/* Ensure that phonebook exists */
if (!data)
{
trace("**ERROR** Phonebook does not exist");
return PB_BOOKDOESNOTEXIST;
}
/* Allocate record for temporary use */
cur_record = ATB_pb_AllocRec(phonebook_id);
/* Make sure phonebook is not busy */
if (ATB_pb_Status(phonebook_id)==PB_BUSY)
{
trace("* ERROR * - Phonebook is busy");
GI_pb_Error(phonebook_id, PB_SEARCH, PB_BUSY);
return PB_BUSY;
}
/* Make sure we're searching a valid index type */
if (index_type==INDEX_PHYSICAL || index_type==INDEX_SEARCH)
{
GI_pb_Error(phonebook_id, PB_SEARCH, PB_INDEXINVALID);
return PB_INDEXINVALID;
}
for (log_index=0; log_index<data->records_used; log_index++)
{
result = ATB_mem_ReadRec(phonebook_id, index_type, log_index, cur_record);
if (result!=PB_OK)
break;
match = ATB_index_Match(record, cur_record, index_type);
if (match==MATCH_EXACT || match==MATCH_START || match==MATCH_FRAGMENT)
{
data->search_table[count] = ATB_index_GetPhysIndex(phonebook_id, index_type, log_index);
count++;
}
}
data->search_results = count;
*recs_count = count;
/* Free allocated record */
ATB_pb_FreeRec(phonebook_id, cur_record);
/* Close any open file */
FS_pb_Finished(phonebook_id);
/* Send success/failure info to the GI */
if (result==PB_OK)
GI_pb_OK(phonebook_id, PB_SEARCH, NULL);
else if (result!=PB_EXCT)
GI_pb_Error(phonebook_id, PB_SEARCH, result);
return result;
}
/*******************************************************************************
$Function: ATB_pb_ConvIndex
$Description: Returns the index in table dest_index_type corresponding to the index
in table src_index_type.
$Returns: PB_OK Action completed OK.
$Arguments: phonebook_id The phonebook identifier.
index_type The index table of the original index.
log_index The original logical index.
new_index_type The index table required.
new_log_index Pointer to where the new logical index will be stored
*******************************************************************************/
PB_RET ATB_pb_ConvIndex(SHORT phonebook_id, T_PB_INDEX index_type, SHORT log_index,
T_PB_INDEX new_index_type, SHORT *new_log_index)
{
T_PB_DATA *data = ATB_hnd_GetPbData(phonebook_id);
SHORT phys_index;
/* Ensure that phonebook exists */
if (!data)
{
trace("**ERROR** Phonebook does not exist");
return PB_BOOKDOESNOTEXIST;
}
if (index_type==new_index_type)
{
*new_log_index = log_index;
}
else
{
phys_index = ATB_index_GetPhysIndex(phonebook_id, index_type, log_index);
*new_log_index = ATB_index_GetLogIndex(phonebook_id, new_index_type, phys_index);
}
return PB_OK;
}
/*******************************************************************************
$Function: ATB_pb_CharToBCD
$Description: Converts an ascii string of digits into BCD form, with 4 bits representing
each digit.
$Returns: None
$Arguments: src - source string (ascii)
dest - destination string for BCD digits
*******************************************************************************/
void ATB_pb_CharToBCD(UBYTE *dest, char *src, int max_len)
{
UBYTE srcIndex = 0;
UBYTE destIndex = 0;
BOOL leftbits = TRUE; /* Left or right nibble */
UBYTE digit = 0;
while (digit!=0xF && srcIndex<max_len)
{
if (src[srcIndex]==NULL)
digit = 0xF; /* 0xF terminates BCD */
else
digit = src[srcIndex]-'0'; /* The digit, 0 to 9. */
if (leftbits)
{
dest[destIndex] = digit;
leftbits = FALSE;
}
else
{
dest[destIndex] |= digit<<4; /* *16 shifts right 4 bits */
leftbits = TRUE;
destIndex++;
}
srcIndex++;
}
return;
}
/*******************************************************************************
$Function: ATB_pb_BCDToChar
$Description: Converts a BCD string to ascii digits
$Returns: None
$Arguments: src - source string (BCD)
dest - destination string for ascii digits
*******************************************************************************/
void ATB_pb_BCDToChar(char *dest, UBYTE *src, int max_len)
{
SHORT numIndex = 0;
UBYTE digit = 0xF; //dummy
while (digit!=NULL && numIndex<max_len)
{
/*HELLO!*/
digit = ATB_num_Digit(src, numIndex);
if (digit==0xF)
digit = NULL;
else
digit+='0';
dest[numIndex] = digit;
numIndex++;
}
return;
}
/*******************************************************************************
$Function: ATB_pb_AllocRec
$Description: Allocates memory for a record
$Returns:
$Arguments: phonebook_id The phonebook identifier
*******************************************************************************/
T_PB_RECORD *ATB_pb_AllocRec(SHORT phonebook_id)
{
T_PB_DATA *data = ATB_hnd_GetPbData(phonebook_id);
T_PB_RECORD *record;
record = (T_PB_RECORD *)GI_pb_MemAlloc(sizeof(T_PB_RECORD));
record->alpha.data = (USHORT *)GI_pb_MemAlloc(data->alpha_max*sizeof(USHORT));
record->number = (UBYTE *)GI_pb_MemAlloc(data->number_max/2);
if (data->ext_max>0)
{
record->ext_data = (UBYTE *)GI_pb_MemAlloc(data->ext_max);
}
else
{
record->ext_data = NULL;
}
record->ton_npi = 0;
return record;
}
/*******************************************************************************
$Function: ATB_pb_FreeRec
$Description: Frees memory allocated for a record
$Returns:
$Arguments: phonebook_id The phonebook identifier
record The record to destroy
*******************************************************************************/
void ATB_pb_FreeRec(SHORT phonebook_id, T_PB_RECORD *record)
{
T_PB_DATA *data = ATB_hnd_GetPbData(phonebook_id);
if (data->ext_max>0 && record->ext_data!=NULL)
{
GI_pb_MemFree((UBYTE *)record->ext_data, data->ext_max);
}
GI_pb_MemFree((UBYTE *)record->number, data->number_max/2);
GI_pb_MemFree((UBYTE *)record->alpha.data, data->alpha_max*sizeof(USHORT));
GI_pb_MemFree((UBYTE *)record, sizeof(T_PB_RECORD));
return;
}
/*******************************************************************************
$Function: ATB_pb_AllocRecList
$Description: Allocates memory for a list of records
$Returns:
$Arguments: phonebook_id The phonebook identifier
num_recs The number of records to allocate
*******************************************************************************/
T_PB_LIST *ATB_pb_AllocRecList(SHORT phonebook_id, SHORT num_recs)
{
T_PB_DATA *data = ATB_hnd_GetPbData(phonebook_id);
T_PB_LIST *list;
SHORT rec_index;
list = (T_PB_LIST *)GI_pb_MemAlloc(sizeof(T_PB_LIST));
list->records_max = num_recs;
list->records_used = 0;
list->record = (T_PB_RECORD *)GI_pb_MemAlloc(sizeof(T_PB_RECORD)*num_recs);
for (rec_index=0; rec_index<num_recs; rec_index++)
{
list->record[rec_index].alpha.data = (USHORT *)GI_pb_MemAlloc(data->alpha_max*sizeof(USHORT));
list->record[rec_index].number = (UBYTE *)GI_pb_MemAlloc(data->number_max/2);
if (data->ext_max>0)
{
list->record[rec_index].ext_data = (UBYTE *)GI_pb_MemAlloc(data->ext_max);
}
else
{
list->record[rec_index].ext_data = NULL;
}
}
return list;
}
/*******************************************************************************
$Function: ATB_pb_FreeRecList
$Description: Frees memory allocated for a list of records
$Returns:
$Arguments: phonebook_id The phonebook identifier
record The records to destroy
num_recs Number of records in the list
*******************************************************************************/
void ATB_pb_FreeRecList(SHORT phonebook_id, T_PB_LIST *list)
{
T_PB_DATA *data = ATB_hnd_GetPbData(phonebook_id);
SHORT rec_index;
for (rec_index=0; rec_index<list->records_max; rec_index++)
{
if (data->ext_max>0 && list->record[rec_index].ext_data!=NULL)
{
GI_pb_MemFree((UBYTE *)list->record[rec_index].ext_data, data->ext_max);
}
GI_pb_MemFree((UBYTE *)list->record[rec_index].number, data->number_max/2);
GI_pb_MemFree((UBYTE *)list->record[rec_index].alpha.data, data->alpha_max*sizeof(USHORT));
}
GI_pb_MemFree((UBYTE *)list->record, sizeof(T_PB_RECORD)*list->records_max);
GI_pb_MemFree((UBYTE *)list, sizeof(T_PB_LIST));
return;
}
/*******************************************************************************
$Function: ATB_mem_CopyRec
$Description: Copies a record from one location to another. The destination should
have memory allocated for its strings.
$Returns:
$Arguments: dest_record The destination record
src_record The source record
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -