📄 cityinformation.cpp
字号:
//
// CityInformation.cpp
//
// Copyright (C) 2006 Sophia Cradle Incorporated
#include "CityInformation.hpp"
#define CITYINFORMATION_FILE_HEADER "# 搒巗僨乕僞\r\n# 搒巗柤, 帪嵎 (+0930 = +9帪娫30暘), 宱搙, 堒搙, 崅搙 (m)"
CityInformation::CityInformation(Void) : _changed(false)
{
SFUFileStream stream;
SFXAnsiString string;
CITYINNER city;
if (stream.Open(CITYINFORMATION_FILENAME, _OFM_READ) == SFERR_NO_ERROR) {
while (!stream.IsEOS()) {
ReadLine(&string, &stream);
if (ParseText(&city, string)) {
_array.Append(city);
}
}
stream.Close();
}
if (_array.GetSize() < 1) {
// 搒巗忣曬僼傽僀儖偑尒偮偐傜側偄偲偒
city._name.Set("搶嫗");
city._timeDifference.Set(9 * 3600);
city._longitude.Set("139.741");
city._latitude.Set("35.658");
city._height.Set("0.0");
if (_array.Append(city) == SFERR_NO_ERROR) {
_changed = true;
}
}
}
// 曄峏偑偁傟偽僼傽僀儖偵曐懚
CityInformation::~CityInformation(Void)
{
SFUFileStream stream;
SFBFileMgrSmp fileMgr(SFBFileMgr::NewInstance());
SFXAnsiString outString(CITYINFORMATION_FILE_HEADER), str;
SInt32 i, val;
CITYINNER city;
if (_changed && _array.GetSize() > 0) {
for (i = 0; i < _array.GetSize(); ++i) {
outString.Add("\r\n");
city = _array.Get(i);
outString.Add(city._name);
val = city._timeDifference.ToSInt32();
if (val > 0) {
outString.Add(",+");
}
else {
outString.Add(",-");
}
outString.Add(SFXAnsiString::Format("%02d%02d", val / 3600, (val % 3600) / 60));
outString.Add(',');
outString.Add(city._longitude);
outString.Add(',');
outString.Add(city._latitude);
outString.Add(',');
outString.Add(city._height);
}
if (fileMgr->Test(CITYINFORMATION_FILENAME) == SUCCESS) {
// 婛偵僼傽僀儖偑偁傞応崌偼嶍彍偡傞
fileMgr->Remove(CITYINFORMATION_FILENAME);
}
if (stream.Open(CITYINFORMATION_FILENAME, _OFM_CREATE) == SFERR_NO_ERROR) {
stream.Put(outString.GetCString(), outString.GetLengthCString());
}
}
}
// 僥僉僗僩僼傽僀儖傪堦峴撉傒庢傞 (嬻峴偼撉傒旘偽偡)
Void CityInformation::ReadLine(SFXAnsiStringPtr string, SFUFileStreamPtr stream)
{
AChar ch;
Bool started(false);
if (stream->static_catch() != SFERR_NO_ERROR) {
return;
}
string->Clear();
while (!stream->IsEOS() && stream->static_catch() == SFERR_NO_ERROR) {
stream->Get(&ch, sizeof(ch));
if (ch == '\r' || ch == '\n') {
if (!started) {
// 峴摢偺夵峴僐乕僪偼撉傒旘偽偡
continue;
}
else {
// 夵峴僐乕僪傪尒偮偗偨傜廔椆
break;
}
}
started = true;
string->Add(ch);
}
}
// 1 峴暘偺僥僉僗僩傪夝愅偡傞 (晄惓側傕偺丄嬻側傕偺偼 false 傪曉偡)
Bool CityInformation::ParseText(CITYINNERPtr city, SFXAnsiStringConstRef string)
{
SInt32 index, i, val;
SFXAnsiString cleanedString, column[5];
Bool result(true);
// # 埲崀偵彂偐傟偰偄傞傕偺偼僐儊儞僩偲傒側偡
index = string.IndexOf('#');
if (index == -1) {
cleanedString = string;
}
else {
cleanedString = string.Substring(0, index);
}
cleanedString.Trim(); // 梋暘側嬻敀傪庢傝彍偔
// 楍偛偲偵暘夝偡傞
// 楍偼慡晹偱 5 楍偁傞
// 傕偟梋寁側傕偺偑捛壛偝傟偰偄偨傜丄僄儔乕偲偼偣偢偵柍帇偡傞
for (i = 0; i < 4; ++i) {
index = cleanedString.IndexOf(',');
if (index == -1) {
// 楍偑懌傝偰偄側偄
result = false;
break;
}
column[i] = cleanedString.Substring(0, index);
cleanedString = cleanedString.Remove(0, index + 1);
cleanedString.Trim();
}
if (result) {
// 僄儔乕偼敪惗偟偰偄側偄
if (cleanedString.IsEmptyCString()) {
// 5 楍栚偑懚嵼偟側偄
result = false;
}
if (column[0].IsEmptyCString()) {
// 搒巗柤偑側偄
result = false;
}
}
if (result) {
index = cleanedString.IndexOf(',');
if (index == -1) {
column[4] = cleanedString;
}
else {
column[4] = cleanedString.Substring(0, index);
}
// 暥帤楍傪昁梫側宍幃偵曄姺
city->_name.Set(column[0]);
val = column[1].ToSInt32();
if (val < 0) {
val = -val;
val = - ((val / 100) * 3600 + (val % 100) * 60);
}
else {
val = (val / 100) * 3600 + (val % 100) * 60;
}
city->_timeDifference.Set(val);
city->_longitude.Set(column[2]);
city->_latitude.Set(column[3]);
city->_height.Set(column[4]);
}
return result;
}
// 搒巗忣曬傪庢摼偡傞
CityInformation::CITY CityInformation::Get(SInt32 index) const
{
CITYINNERConst cityInner(_array.Get(index));
CITY city;
ACharPtr p;
city._name.Set(cityInner._name);
city._timeDifference.Set(cityInner._timeDifference);
city._longitude = SFXHelper::strtod(cityInner._longitude.GetCString(), &p);
city._latitude = SFXHelper::strtod(cityInner._latitude.GetCString(), &p);
city._height = SFXHelper::strtod(cityInner._height.GetCString(), &p);
return city;
}
// 搒巗忣曬傪搊榐偡傞
// 僨乕僞偼忋彂偒
SFCError CityInformation::Append(SFXWideStringConstRef cityName, SFXWideStringConstRef timeDifference, SFXWideStringConstRef longitude, SFXWideStringConstRef latitude, SFXWideStringConstRef height)
{
CITYINNER city;
SFCError result;
SInt32 index, val;
city._name.Set(cityName);
val = timeDifference.ToSInt32();
if (val < 0) {
val = -val;
val = - ((val / 100) * 3600 + (val % 100) * 60);
}
else {
val = (val / 100) * 3600 + (val % 100) * 60;
}
city._timeDifference.Set(val);
city._longitude.Set(longitude);
city._latitude.Set(latitude);
city._height.Set(height);
if ((index = Find(cityName)) == -1) {
// 怴婯搊榐
result = _array.Append(city);
}
else {
// 僨乕僞傪峏怴
result = _array.Set(index, city);
}
if (result == SFERR_NO_ERROR) {
_changed = true;
}
return result;
}
// 搒巗忣曬傪搊榐偡傞
// 僨乕僞偼忋彂偒
// appended : true = 怴婯搊榐
// index : 搊榐埵抲
SFCError CityInformation::Append(SFXWideStringConstRef cityName, SFXWideStringConstRef timeDifference, SFXWideStringConstRef longitude, SFXWideStringConstRef latitude, SFXWideStringConstRef height, BoolPtr appended, SInt32Ptr index)
{
CITYINNER city;
SFCError result;
SInt32 i, val;
city._name.Set(cityName);
val = timeDifference.ToSInt32();
if (val < 0) {
val = -val;
val = - ((val / 100) * 3600 + (val % 100) * 60);
}
else {
val = (val / 100) * 3600 + (val % 100) * 60;
}
city._timeDifference.Set(val);
city._longitude.Set(longitude);
city._latitude.Set(latitude);
city._height.Set(height);
if ((i = Find(cityName)) == -1) {
// 怴婯搊榐
result = _array.Append(city);
if (result == SFERR_NO_ERROR) {
*index = Find(cityName);
*appended = true;
}
else {
*index = -1;
*appended = false;
}
}
else {
// 僨乕僞傪峏怴
result = _array.Set(i, city);
*index = i;
*appended = false;
}
if (result == SFERR_NO_ERROR) {
_changed = true;
}
return result;
}
// 柤慜偐傜搒巗忣曬傪扵偡
// 懚嵼偟側偄応崌偼 -1 傪曉偡
SInt32 CityInformation::Find(SFXWideStringConstRef cityName) const
{
CITYINNER city;
SFXAnsiStringConst name(cityName);
SInt32 i;
for (i = 0; i < _array.GetSize(); ++i) {
city = _array.Get(i);
if (city._name == name) {
return i;
}
}
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -