GME  13
CompassData.cpp
Go to the documentation of this file.
00001 // CompassData.cpp: implementation of the CCompassData class.
00002 //
00004 
00005 #include "stdafx.h"
00006 #include "objectinspector.h"
00007 #include "CompassData.h"
00008 
00009 #ifdef _DEBUG
00010 #undef THIS_FILE
00011 static char THIS_FILE[]=__FILE__;
00012 #define new DEBUG_NEW
00013 #endif
00014 
00015 
00016 bool CCompassData::ParseCompassValue(CString strValue, UINT &uValue)
00017 {
00018         TCHAR szDelimiters[] = _T(",;\t ");
00019         TCHAR* token=NULL;
00020                                                 
00021         TCHAR *str = new TCHAR[strValue.GetLength()+1];
00022         _tcscpy(str,strValue);
00023 
00024         token=_tcstok(str,szDelimiters);
00025 
00026         bool bIsProcessed=false;
00027         UINT uCompassTemp=0;
00028 
00029         while(token!=NULL)
00030         {               
00031                 bIsProcessed=false;
00032                 
00033                 CString strTemp;
00034                 strTemp = _T("North");
00035                 if(!strTemp.CompareNoCase(token))
00036                 {
00037                         uCompassTemp|=CMPS_NORTH;
00038                         bIsProcessed=true;
00039                 }
00040 
00041                 strTemp = _T("South");
00042                 if(!strTemp.CompareNoCase(token))
00043                 {
00044                         uCompassTemp|=CMPS_SOUTH;
00045                         bIsProcessed=true;
00046                 }
00047                 strTemp = _T("East");
00048                 if(!strTemp.CompareNoCase(token))
00049                 {
00050                         uCompassTemp|=CMPS_EAST;
00051                         bIsProcessed=true;
00052                 }
00053 
00054                 strTemp = _T("West");
00055                 if(!strTemp.CompareNoCase(token))
00056                 {
00057                         uCompassTemp|=CMPS_WEST;
00058                         bIsProcessed=true;
00059                 }
00060 
00061                 strTemp = _T("Northeast");
00062                 if(!strTemp.CompareNoCase(token))
00063                 {
00064                         uCompassTemp|=CMPS_NORTHEAST;
00065                         bIsProcessed=true;
00066                 }
00067 
00068                 strTemp = _T("Southeast");
00069                 if(!strTemp.CompareNoCase(token))
00070                 {
00071                         uCompassTemp|=CMPS_SOUTHEAST;
00072                         bIsProcessed=true;
00073                 }
00074 
00075                 strTemp = _T("SouthWest");
00076                 if(!strTemp.CompareNoCase(token))
00077                 {
00078                         uCompassTemp|=CMPS_SOUTHWEST;
00079                         bIsProcessed=true;
00080                 }
00081 
00082                 strTemp = _T("NorthWest");
00083                 if(!strTemp.CompareNoCase(token))
00084                 {
00085                         uCompassTemp|=CMPS_NORTHWEST;
00086                         bIsProcessed=true;
00087                 }
00088                 strTemp = _T("Center");
00089                 if(!strTemp.CompareNoCase(token))
00090                 {
00091                         uCompassTemp|=CMPS_CENTER;
00092                         bIsProcessed=true;
00093                 }
00094 
00095                 if(!bIsProcessed)break;
00096                 // Next token
00097                 token = _tcstok(NULL,szDelimiters);
00098         }
00099         delete[] str;
00100 
00101         if(bIsProcessed) uValue=uCompassTemp;
00102 
00103         return bIsProcessed;
00104 }
00105 
00106 CString CCompassData::toString(UINT uData)
00107 {
00108 
00109         CString strRetVal;
00110 
00111         if(uData&CMPS_NORTH)
00112         {
00113                 strRetVal += _T("North;");
00114         }
00115 
00116         if(uData&CMPS_EAST)
00117         {
00118                 strRetVal += _T("East;");
00119         }
00120         
00121         if(uData&CMPS_SOUTH)
00122         {
00123                 strRetVal += _T("South;");
00124         }
00125 
00126         if(uData&CMPS_WEST)
00127         {
00128                 strRetVal += _T("West;");
00129         }
00130 
00131         if(uData&CMPS_NORTHEAST)
00132         {
00133                 strRetVal += _T("Northeast;");
00134         }
00135 
00136         if(uData&CMPS_SOUTHEAST)
00137         {
00138                 strRetVal += _T("Southeast;");
00139         }
00140 
00141         if(uData&CMPS_SOUTHWEST)
00142         {
00143                 strRetVal += _T("Southwest;");
00144         }
00145 
00146         if(uData&CMPS_NORTHWEST)
00147         {
00148                 strRetVal += _T("Northwest;");
00149         }
00150 
00151         if(uData&CMPS_CENTER)
00152         {
00153                 strRetVal += _T("Center;");
00154         }
00155 
00156         // Trimming the last semicolon
00157         if(!strRetVal.IsEmpty())
00158         {
00159                 strRetVal=strRetVal.Left(strRetVal.GetLength()-1);
00160         }
00161 
00162         return strRetVal;
00163 
00164 }
00165 
00166 bool CCompassData::bIsSingle(UINT uData)
00167 {
00168         int count=0;
00169         if(uData&CMPS_NORTH)
00170         {
00171                 count++;
00172         }
00173 
00174         if(uData&CMPS_EAST)
00175         {
00176                 count++;
00177         }
00178         
00179         if(uData&CMPS_SOUTH)
00180         {
00181                 count++;
00182         }
00183 
00184         if(uData&CMPS_WEST)
00185         {
00186                 count++;
00187         }
00188 
00189         if(uData&CMPS_NORTHEAST)
00190         {
00191                 count++;
00192         }
00193 
00194         if(uData&CMPS_SOUTHEAST)
00195         {
00196                 count++;
00197         }
00198 
00199         if(uData&CMPS_SOUTHWEST)
00200         {
00201                 count++;
00202         }
00203 
00204         if(uData&CMPS_NORTHWEST)
00205         {
00206                 count++;
00207         }
00208 
00209         if(uData&CMPS_CENTER)
00210         {
00211                 count++;
00212         }
00213         return (count<=1);
00214 }
00215 
00216 
00217 // Convarts MGA format to compass value. 
00218 // The MGA format is a number from 0 (NORTH) to 8 (CENTER) clockwise.
00219 UINT CCompassData::ParseMgaCompassValueOption(const CString &strValue)
00220 {
00221                 UINT uCompassTemp=0;
00222                 
00223                 CString strTemp;
00224                 strTemp = _T("0");
00225                 if(!strTemp.CompareNoCase(strValue))
00226                 {
00227                         uCompassTemp|=CMPS_NORTH;
00228                 }
00229 
00230                 strTemp = _T("4");
00231                 if(!strTemp.CompareNoCase(strValue))
00232                 {
00233                         uCompassTemp|=CMPS_SOUTH;
00234                 }
00235                 strTemp = _T("2");
00236                 if(!strTemp.CompareNoCase(strValue))
00237                 {
00238                         uCompassTemp|=CMPS_EAST;
00239                 }
00240 
00241                 strTemp = _T("6");
00242                 if(!strTemp.CompareNoCase(strValue))
00243                 {
00244                         uCompassTemp|=CMPS_WEST;
00245                 }
00246 
00247                 strTemp = _T("1");
00248                 if(!strTemp.CompareNoCase(strValue))
00249                 {
00250                         uCompassTemp|=CMPS_NORTHEAST;
00251                 }
00252 
00253                 strTemp = _T("3");
00254                 if(!strTemp.CompareNoCase(strValue))
00255                 {
00256                         uCompassTemp|=CMPS_SOUTHEAST;
00257                 }
00258 
00259                 strTemp = _T("5");
00260                 if(!strTemp.CompareNoCase(strValue))
00261                 {
00262                         uCompassTemp|=CMPS_SOUTHWEST;
00263                 }
00264 
00265                 strTemp = _T("7");
00266                 if(!strTemp.CompareNoCase(strValue))
00267                 {
00268                         uCompassTemp|=CMPS_NORTHWEST;
00269                 }
00270                 strTemp = _T("8");
00271                 if(!strTemp.CompareNoCase(strValue))
00272                 {
00273                         uCompassTemp|=CMPS_CENTER;
00274                 }
00275 
00276                 ASSERT(uCompassTemp); // Invalid strValue. Value must be in {0..8}.
00277 
00278                 return uCompassTemp;
00279 }
00280 
00281 
00282 // Converts MGA format to compass value. 
00283 // MGA format here (Yes, another invention for the same thing !!!): "neswNESW"
00284 void CCompassData::ParseMgaCompassValueCheck(const CString &strValue, UINT &uSmallCaseValue, UINT &uCapitalValue)
00285 {
00286         uSmallCaseValue=0;
00287         uCapitalValue=0;
00288 
00289 // Small case value
00290         if(strValue.Find(_T('n'))!=-1)
00291         {
00292                 uSmallCaseValue|=CMPS_NORTH;
00293         }
00294 
00295         if(strValue.Find(_T('e'))!=-1)
00296         {
00297                 uSmallCaseValue|=CMPS_EAST;
00298         }
00299 
00300         if(strValue.Find(_T('s'))!=-1)
00301         {
00302                 uSmallCaseValue|=CMPS_SOUTH;
00303         }
00304 
00305         if(strValue.Find(_T('w'))!=-1)
00306         {
00307                 uSmallCaseValue|=CMPS_WEST;
00308         }
00309 
00310 // Capital value
00311         if(strValue.Find(_T('N'))!=-1)
00312         {
00313                 uCapitalValue|=CMPS_NORTH;
00314         }
00315 
00316         if(strValue.Find(_T('E'))!=-1)
00317         {
00318                 uCapitalValue|=CMPS_EAST;
00319         }
00320 
00321         if(strValue.Find(_T('S'))!=-1)
00322         {
00323                 uCapitalValue|=CMPS_SOUTH;
00324         }
00325 
00326         if(strValue.Find(_T('W'))!=-1)
00327         {
00328                 uCapitalValue|=CMPS_WEST;
00329         }
00330 }
00331 
00332 
00333 void CCompassData::toMgaStringCheck(CString& strValue, UINT uData, bool bIsCapital)
00334 {
00335         strValue.Empty();
00336 
00337         if(uData&CMPS_NORTH)
00338         {
00339                 strValue+=bIsCapital ? _T("N") : _T("n");
00340         }
00341         if(uData&CMPS_EAST)
00342         {
00343                 strValue+=bIsCapital?_T("E"):_T("e");
00344         }
00345         if(uData&CMPS_SOUTH)
00346         {
00347                 strValue+=bIsCapital?_T("S"):_T("s");
00348         }
00349 
00350         if(uData&CMPS_WEST)
00351         {
00352                 strValue+=bIsCapital?_T("W"):_T("w");
00353         }
00354 
00355 }
00356 
00357 void CCompassData::toMgaStringOption(CString& strValue, UINT uData)
00358 {
00359         ASSERT(bIsSingle(uData));
00360 
00361         UINT uMgaValue = CMPS_SOUTH;
00362 
00363         if(uData&CMPS_NORTH)
00364         {
00365                 uMgaValue=0;
00366         }
00367         else if(uData&CMPS_EAST)
00368         {
00369                 uMgaValue=2;
00370         }
00371         else if(uData&CMPS_SOUTH)
00372         {
00373                 uMgaValue=4;
00374         }
00375 
00376         else if(uData&CMPS_WEST)
00377         {
00378                 uMgaValue=6;
00379         }
00380 
00381         else if(uData&CMPS_NORTHEAST)
00382         {
00383                 uMgaValue=1;
00384         }
00385 
00386         else if(uData&CMPS_SOUTHEAST)
00387         {
00388                 uMgaValue=3;
00389         }
00390 
00391         else if(uData&CMPS_SOUTHWEST)
00392         {
00393                 uMgaValue=5;
00394         }
00395 
00396         else if(uData&CMPS_NORTHWEST)
00397         {
00398                 uMgaValue=7;
00399         }
00400 
00401         else if(uData&CMPS_CENTER)
00402         {
00403                 uMgaValue=8;
00404         }
00405         else
00406         {
00407                 ASSERT(0); // Invalid compass value
00408         }
00409 
00410         strValue.Format(_T("%u"),uMgaValue);
00411 }