📄 busdb.cpp
字号:
#include "common.h"
#include "BusDB.h"
/*
Station data table
StationName, rCount
num of rCount: rIndex, rDir, rStationIndex
array of char UInt8
UInt16 UInt8 UInt8
Route data table
RouteName, Dir, sCount
num of sCount: sIndex
array of char UInt8 UInt8
UInt16
*/
void CBusDB::SetCount(UInt16 c)
{
m_iCount = c;
m_iNow = 0;
}
void CBusDB::StepIt()
{
#define LEFT 40
#define TOP 45
#define MAX 60
#define HEIGHT 10
m_iNow++;
RectangleType r;
float scale, upto;
scale = MAX / (float)m_iCount;
upto = scale * (float)m_iNow;
r.topLeft.x = LEFT;
r.topLeft.y = TOP;
r.extent.y = HEIGHT;
r.extent.x = upto;
r.topLeft.x++; r.topLeft.y++;
r.extent.x-=2; r.extent.y-=2;
if( r.extent.x < 0 ) r.extent.x = 0;
WinDrawRectangle( &r, 0 );
r.topLeft.x--; r.topLeft.y--;
r.extent.x+=2; r.extent.y+=2;
r.topLeft.x = LEFT + upto;
r.extent.x = MAX - upto;
WinEraseRectangle( &r, 0 );
r.extent.x = MAX;
r.topLeft.x = LEFT;
WinDrawRectangleFrame( simpleFrame, &r );
}
CBusDB::CBusDB()
{
m_bDatabaseCached = false;
m_bDatabaseOpened = false;
}
CBusDB::~CBusDB()
{
Close();
}
Err CBusDB::Close()
{
if (m_bDatabaseOpened) {
m_Database.Close();
m_bDatabaseOpened = false;
m_bDatabaseCached = false;
}
return errNone;
}
Err CBusDB::Open(UInt16 wCardNo, LocalID rLiDbID)
{
Close();
//get card number and local id
// m_Database.Open(appFileCreator, appDatabaseType, dmModeReadOnly);
m_Database.Open(wCardNo, rLiDbID, dmModeReadOnly);
m_bDatabaseOpened = true;
CString strTemp;
CRecordStream rs(&m_Database);
rs.Open(0);
//这里把数据库的信息读取出来,注意跟Delphi的BaseRecordType一样
rs >> m_CityName
>> strTemp
>> strTemp
>> strTemp
>> strTemp
>> strTemp
>> m_StationCount >> m_RouteCount;
rs.Close();
return errNone;
}
Err CBusDB::CheckIfDatabaseCached()
{
if (!m_bDatabaseCached) {
CWaitForm frmWait(PleaseWaitForm);
CacheStation();
CacheRoute();
m_bDatabaseCached = true;
}
return errNone;
}
void CBusDB::CacheStation()
{
m_arrStation.DeleteAll();
SetCount(m_StationCount);
CRecordStream rs(&m_Database);
for (int i = 1; i < 1 + m_StationCount; i++) {
StepIt();
rs.Open(i);
CStationData& sData = m_arrStation.Add();
rs >> sData.StationName;
rs.Close();
sData.Index = i;
}
}
void CBusDB::CacheRoute()
{
m_arrRoute.DeleteAll();
SetCount(m_RouteCount);
CRecordStream rs(&m_Database);
for (int i = (1 + m_StationCount); i < (1 + m_StationCount + m_RouteCount); i++) {
StepIt();
rs.Open(i);
CRouteData& rData = m_arrRoute.Add();
rs >> rData.RouteName >> rData.Dir;
rs.Close();
rData.Index = i;
}
}
CString CBusDB::GetStationName(UInt16 idx)
{
CheckIfDatabaseCached();
return m_arrStation[idx].StationName;
}
CString CBusDB::GetRouteName(UInt16 idx)
{
CheckIfDatabaseCached();
return m_arrRoute[idx].RouteName;
}
UInt8 CBusDB::GetRouteDir(UInt16 idx)
{
CheckIfDatabaseCached();
return m_arrRoute[idx].Dir;
}
UInt16 CBusDB::GetStationAbsoluteIndex(UInt16 idx)
{
CheckIfDatabaseCached();
return m_arrStation[idx].Index;
}
UInt16 CBusDB::GetRouteAbsoluteIndex(UInt16 idx)
{
CheckIfDatabaseCached();
return m_arrRoute[idx].Index;
}
CArray<CBusDB::CStationRouteData>* CBusDB::GetStationData(UInt16 idx)
{
CheckIfDatabaseCached();
CRecordStream rs(&m_Database);
rs.Open(GetStationAbsoluteIndex(idx));
CString sStation;
UInt8 iCount;
rs >> sStation;
rs >> iCount;
m_arrStationData.DeleteAll();
for (int i = 0; i < iCount; i++) {
CStationRouteData& sd = m_arrStationData.Add();
rs >> sd.Index >> sd.Dir >> sd.StationIndex;
}
rs.Close;
return &m_arrStationData;
}
CArray<UInt16>* CBusDB::GetRouteData(UInt16 idx)
{
CheckIfDatabaseCached();
CRecordStream rs(&m_Database);
rs.Open(GetRouteAbsoluteIndex(idx));
CString sRoute;
UInt8 iDir;
UInt8 iCount;
rs >> sRoute;
rs >> iDir;
rs >> iCount;
m_arrRouteData.DeleteAll();
UInt16 j;
for (int i = 0; i < iCount; i++) {
rs >> j;
m_arrRouteData.Add(j);
}
rs.Close;
return &m_arrRouteData;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -