📄 strobj.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "simple.h"
#ifdef _STR_OBJECT_TEST_
void prnObject(AreaCode* areaCode)
{
MCO_RET rc = 0;
char pstr[10];
char val[20];
uint2 sz;
memset(val, 0, 20);
rc = AreaCode_areaCode_get(areaCode, val, 20, &sz);
if (rc != MCO_S_OK)
{
printf("\n\terror:%d", rc);
}
rc = AreaCode_strAreaCode_get(areaCode, pstr, 10);
if (rc != MCO_S_OK)
{
printf("\n\terror:%d", rc);
}
printf("<%s,%s>\n", val, pstr);
}
void doListing(mco_db_h db)
{
MCO_RET rc = 0;
mco_cursor_t csr;
mco_trans_h trn;
AreaCode areaCode;
printf("\nData listing:\n\n");
mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &trn);
/* initialize cursor */
rc = AreaCode_IareaCode_index_cursor(trn, &csr);
for (rc = mco_cursor_first(trn, &csr); rc == MCO_S_OK; rc = mco_cursor_next(trn, &csr))
{
rc = AreaCode_from_cursor(trn, &csr, &areaCode);
if (rc != MCO_S_OK)
{
printf("\n\terror:%d", rc);
break;
}
prnObject(&areaCode);
}
mco_trans_commit(trn);
return ;
}
int AddRecord(mco_trans_h t, const char* val, const char* pstr)
{
MCO_RET rc;
AreaCode areaCode;
rc = AreaCode_new(t, &areaCode);
if (rc != MCO_S_OK)
{
return rc;
}
rc = AreaCode_strAreaCode_put(&areaCode, pstr, (uint2)(strlen(pstr) + 1));
if (rc != MCO_S_OK)
{
return rc;
}
rc = AreaCode_areaCode_put(&areaCode, val, (uint2)(strlen(val)));
if (rc != MCO_S_OK)
{
return rc;
}
return MCO_S_OK;
}
int createDatabase(mco_db_h db)
{
MCO_RET rc = 0;
mco_trans_h t;
mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
AddRecord(t, "01", "ATT");
AddRecord(t, "020", "BCC");
AddRecord(t, "025", "TNT");
AddRecord(t, "03", "ANC");
AddRecord(t, "0355", "NCC");
AddRecord(t, "0355", "UDC");
AddRecord(t, "045", "WTC");
AddRecord(t, "0454", "FCC");
AddRecord(t, "05", "ABB");
AddRecord(t, "051", "DDA");
AddRecord(t, "051", "FIY");
AddRecord(t, "0513", "ASD");
AddRecord(t, "0515", "ASH");
AddRecord(t, "05183", "ASW");
AddRecord(t, "0525", "ASP");
AddRecord(t, "06", "YUI");
AddRecord(t, "064", "KEW");
AddRecord(t, "066", "WER");
AddRecord(t, "0668", "VBR");
AddRecord(t, "0668", "MPP");
AddRecord(t, "0668", "FCA");
AddRecord(t, "06689", "FCU");
AddRecord(t, "0699", "FCH");
AddRecord(t, "06683", "FCL");
AddRecord(t, "06685", "FCD");
AddRecord(t, "06687", "FCA");
AddRecord(t, "07", "MNM");
AddRecord(t, "07", "MGM");
AddRecord(t, "079", "MSM");
AddRecord(t, "0795", "EAE");
AddRecord(t, "081", "YUI");
AddRecord(t, "084", "WWW");
AddRecord(t, "0849", "OLA");
AddRecord(t, "09", "GEA");
AddRecord(t, "099", "YTR");
AddRecord(t, "099", "IUI");
AddRecord(t, "0988", "JHJ");
AddRecord(t, "0988", "DFD");
rc = mco_trans_commit(t);
if (rc != MCO_S_OK)
{
printf("Commit error %d !\n", rc);
}
return rc;
}
void searchExact(mco_db_h db, const char* strAreaCode)
{
MCO_RET rc = 0;
mco_trans_h trn;
AreaCode areaCode;
mco_cursor_t csr;
mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &trn);
rc = AreaCode_IareaCode_index_cursor(trn, &csr);
rc = AreaCode_IareaCode_exact_match(trn, &csr, strAreaCode, (uint2)(strlen(strAreaCode)));
if (rc == MCO_S_OK)
{
printf("Found ExactMatch for key %s:\n", strAreaCode);
while (rc == MCO_S_OK)
{
rc = AreaCode_from_cursor(trn, &csr, &areaCode);
if (rc != MCO_S_OK)
{
printf("\n\terror:%d", rc);
break;
}
prnObject(&areaCode);
rc = AreaCode_IareaCode_next_match(trn, &csr, strAreaCode, (uint2)(strlen(strAreaCode)));
}
}
else
{
printf("Not found ExactMatch for key %s:\n", strAreaCode);
}
rc = mco_trans_commit(trn);
if (rc != MCO_S_OK)
{
printf("Commit error %d !\n", rc);
}
}
void searchPrefix(mco_db_h db, const char* strAreaCodePref)
{
MCO_RET rc = 0;
mco_trans_h trn;
AreaCode areaCode;
mco_cursor_t csr;
uint2 sz = strlen(strAreaCodePref);
mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &trn);
rc = AreaCode_IareaCode_index_cursor(trn, &csr);
rc = AreaCode_IareaCode_prefix_match(trn, &csr, strAreaCodePref, sz);
if (rc == MCO_S_OK)
{
int found = 0;
while (rc == MCO_S_OK)
{
if (!found)
{
printf("Found PrefixMatch for key %s:\n", strAreaCodePref);
}
found = 1;
rc = AreaCode_from_cursor(trn, &csr, &areaCode);
if (rc != MCO_S_OK)
{
printf("\n\terror:%d", rc);
break;
}
prnObject(&areaCode);
rc = AreaCode_IareaCode_next_match(trn, &csr, strAreaCodePref, sz);
}
if (!found)
{
printf("Not found PrefixMatch for key %s:\n", strAreaCodePref);
}
}
else
{
printf("Not found PrefixMatch for key %s:\n", strAreaCodePref);
}
rc = mco_trans_commit(trn);
if (rc != MCO_S_OK)
{
printf("Commit error %d !\n", rc);
}
}
void searchLongest(mco_db_h db, const char* strAreaCodePref)
{
MCO_RET rc = 0;
mco_trans_h trn;
AreaCode areaCode;
mco_cursor_t csr;
int longestR = 0;
char cycle = 0;
uint2 sz = strlen(strAreaCodePref);
mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &trn);
rc = AreaCode_IareaCode_index_cursor(trn, &csr);
rc = AreaCode_IareaCode_longest_match(trn, &csr, strAreaCodePref, sz);
if (rc == MCO_S_OK)
{
int found = 0;
while (rc == MCO_S_OK)
{
if (!found)
{
printf("Found LongestMatch for key %s:\n", strAreaCodePref);
}
found = 1;
rc = AreaCode_from_cursor(trn, &csr, &areaCode);
if (rc != MCO_S_OK)
{
printf("\n\terror:%d", rc);
break;
}
prnObject(&areaCode);
rc = AreaCode_IareaCode_next_match(trn, &csr, strAreaCodePref, sz);
}
if (!found)
{
printf("Not found LongestMatch for key %s:\n", strAreaCodePref);
}
}
else
{
printf("Not found LongestMatch for key %s:\n", strAreaCodePref);
}
rc = mco_trans_commit(trn);
if (rc != MCO_S_OK)
{
printf("Commit error %d !\n", rc);
}
}
void doGoogle(mco_db_h db)
{
//////////////////////////////////////////////////////////////////////////
// Exact search
printf("\n//////////////////////////////////////\n");
printf("ExactMatch function demonstration:\n\n");
searchExact(db, "01");
searchExact(db, "055");
searchExact(db, "5");
searchExact(db, "05");
searchExact(db, "06");
searchExact(db, "07");
//////////////////////////////////////////////////////////////////////////
// prefixSearch
printf("\n//////////////////////////////////////\n");
printf("PrefixMatch function demonstration:\n\n");
searchPrefix(db, "0190");
searchPrefix(db, "055");
searchPrefix(db, "589");
searchPrefix(db, "051");
searchPrefix(db, "0511");
searchPrefix(db, "05189");
searchPrefix(db, "0668889");
searchPrefix(db, "0698889");
searchPrefix(db, "071111111");
//////////////////////////////////////////////////////////////////////////
// longestSearch
printf("\n//////////////////////////////////////\n");
printf("LongestMatch function demonstration:\n\n");
searchLongest(db, "0190");
searchLongest(db, "055");
searchLongest(db, "589");
searchLongest(db, "051");
searchLongest(db, "0511");
searchLongest(db, "05189");
searchLongest(db, "0668889");
searchLongest(db, "0698889");
searchLongest(db, "071111111");
printf("\n");
}
#endif //_STR_OBJECT_TEST_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -