//#define CUPDEBUG bool ParseCUPWayPointString(TCHAR *String,WAYPOINT *Temp) { TCHAR ctemp[(COMMENT_SIZE*2)+1]; // must be bigger than COMMENT_SIZE! TCHAR *pToken; TCHAR TempString[READLINE_LENGTH+1]; TCHAR OrigString[READLINE_LENGTH+1]; TCHAR Tname[NAME_SIZE+1]; int flags=0; unsigned int i, j; bool ishome=false; // 100310 // strtok does not return empty fields. we create them here with special char #define DUMCHAR '|' Temp->Visible = true; // default all waypoints visible at start Temp->FarVisible = true; Temp->Format = LKW_CUP; Temp->Number = WayPointList.size(); Temp->FileNum = globalFileNum; #if BUGSTOP // This should never happen LKASSERT(_tcslen(String) < sizeof(OrigString)); #endif LK_tcsncpy(OrigString, String,READLINE_LENGTH); // if string is too short do nothing if (_tcslen(OrigString)<11) return false; #ifdef CUPDEBUG StartupStore(_T("OLD:<%s>%s"),OrigString,NEWLINE); #endif for (i=0,j=0; i<_tcslen(OrigString); i++) { // skip last comma, and avoid overruning the end if ( (i+1)>= _tcslen(OrigString)) break; if ( (OrigString[i] == _T(',')) && (OrigString[i+1] == _T(',')) ) { TempString[j++] = _T(','); TempString[j++] = _T(DUMCHAR); continue; } /* we need terminations for comments if ( OrigString[i] == _T('\r') ) continue; if ( OrigString[i] == _T('\n') ) continue; */ TempString[j++] = OrigString[i]; } TempString[j] = _T('\0'); #ifdef CUPDEBUG StartupStore(_T("NEW:<%s>%s"),TempString,NEWLINE); #endif // ---------------- NAME ---------------- pToken = _tcstok(TempString, TEXT(",")); if (pToken == NULL) return false; if (_tcslen(pToken)>NAME_SIZE) { pToken[NAME_SIZE-1]= _T('\0'); } _tcscpy(Temp->Name, pToken); CleanCupCode(Temp->Name); #ifdef CUPDEBUG StartupStore(_T(" CUP NAME=<%s>%s"),Temp->Name,NEWLINE); #endif // ---------------- CODE ------------------ pToken = _tcstok(NULL, TEXT(",")); if (pToken == NULL) return false; if (_tcslen(pToken)>CUPSIZE_CODE) { pToken[CUPSIZE_CODE-1]= _T('\0'); } _tcscpy(Temp->Code, pToken); for (i=_tcslen(Temp->Code)-1; i>1; i--) { if (Temp->Code[i]==' ') { Temp->Code[i]=0; } else { break; } } _tcscpy(Tname,Temp->Code); for (j=0, i=0; i<_tcslen(Tname); i++) { //if (Tname[i]!='\"') Temp->Code[j++]=Tname[i]; if ( (Tname[i]!='\"') && (Tname[i]!=DUMCHAR) ){ Temp->Code[j++]=Tname[i]; } } Temp->Code[j]= _T('\0'); if (_tcslen(Temp->Code)>5) { // 100310 if ( _tcscmp(Temp->Code,_T("LKHOME")) == 0 ) { StartupStore(_T(". Found LKHOME inside CUP waypoint <%s>%s"),Temp->Name,NEWLINE); ishome=true; } } #ifdef CUPDEBUG StartupStore(_T(" CUP CODE=<%s>%s"),Temp->Code,NEWLINE); #endif // ---------------- COUNTRY ------------------ pToken = _tcstok(NULL, TEXT(",")); if (pToken == NULL) return false; LK_tcsncpy(Temp->Country,pToken,CUPSIZE_COUNTRY); if (_tcslen(Temp->Country)>3) { Temp->Country[3]= _T('\0'); } if ((_tcslen(Temp->Country) == 1) && Temp->Country[0]==DUMCHAR) Temp->Country[0]=_T('\0'); #ifdef CUPDEBUG StartupStore(_T(" CUP COUNTRY=<%s>%s"),Temp->Country,NEWLINE); #endif // ---------------- LATITUDE ------------------ pToken = _tcstok(NULL, TEXT(",")); if (pToken == NULL) return false; Temp->Latitude = CUPToLat(pToken); if((Temp->Latitude > 90) || (Temp->Latitude < -90)) { return false; } #ifdef CUPDEBUG StartupStore(_T(" CUP LATITUDE=<%f>%s"),Temp->Latitude,NEWLINE); #endif // ---------------- LONGITUDE ------------------ pToken = _tcstok(NULL, TEXT(",")); if (pToken == NULL) return false; Temp->Longitude = CUPToLon(pToken); if((Temp->Longitude > 180) || (Temp->Longitude < -180)) { return false; } #ifdef CUPDEBUG StartupStore(_T(" CUP LONGITUDE=<%f>%s"),Temp->Longitude,NEWLINE); #endif // ---------------- ELEVATION ------------------ pToken = _tcstok(NULL, TEXT(",")); if (pToken == NULL) return false; Temp->Altitude = ReadAltitude(pToken); #ifdef CUPDEBUG StartupStore(_T(" CUP ELEVATION=<%f>%s"),Temp->Altitude,NEWLINE); #endif if (Temp->Altitude == -9999){ Temp->Altitude=0; } // ---------------- STYLE ------------------ pToken = _tcstok(NULL, TEXT(",")); if (pToken == NULL) return false; Temp->Style = (int)_tcstol(pToken,NULL,10); switch(Temp->Style) { case STYLE_AIRFIELDGRASS: // airfield grass case STYLE_GLIDERSITE: // glider site case STYLE_AIRFIELDSOLID: // airfield solid flags = AIRPORT; flags += LANDPOINT; break; case STYLE_OUTLANDING: // outlanding flags = LANDPOINT; break; default: flags = TURNPOINT; break; } if (ishome) flags += HOME; Temp->Flags = flags; #ifdef CUPDEBUG StartupStore(_T(" CUP STYLE=<%d> flags=%d %s"),Temp->Style,Temp->Flags,NEWLINE); #endif // ---------------- RWY DIRECTION ------------------ pToken = _tcstok(NULL, TEXT(",")); if (pToken == NULL) return false; if ((_tcslen(pToken) == 1) && (pToken[0]==DUMCHAR)) Temp->RunwayDir=-1; else Temp->RunwayDir = (int)AngleLimit360(_tcstol(pToken, NULL, 10)); #ifdef CUPDEBUG StartupStore(_T(" CUP RUNWAY DIRECTION=<%d>%s"),Temp->RunwayDir,NEWLINE); #endif // ---------------- RWY LENGTH ------------------ pToken = _tcstok(NULL, TEXT(",")); if (pToken == NULL) return false; if ((_tcslen(pToken) == 1) && (pToken[0]==DUMCHAR)) Temp->RunwayLen = -1; else Temp->RunwayLen = (int)ReadLength(pToken); #ifdef CUPDEBUG StartupStore(_T(" CUP RUNWAY LEN=<%d>%s"),Temp->RunwayLen,NEWLINE); #endif // ---------------- AIRPORT FREQ ------------------ pToken = _tcstok(NULL, TEXT(",")); if (pToken == NULL) return false; if (_tcslen(pToken)>CUPSIZE_FREQ) pToken[CUPSIZE_FREQ-1]= _T('\0'); _tcscpy(Temp->Freq, pToken); TrimRight(Temp->Freq); _tcscpy(Tname,Temp->Freq); for (j=0, i=0; i<_tcslen(Tname); i++) if ( (Tname[i]!='\"') && (Tname[i]!=DUMCHAR) ) Temp->Freq[j++]=Tname[i]; Temp->Freq[j]= _T('\0'); #ifdef CUPDEBUG StartupStore(_T(" CUP FREQ=<%s>%s"),Temp->Freq,NEWLINE); #endif // ---------------- COMMENT ------------------ pToken = _tcstok(NULL, TEXT("\n\r")); if (pToken != NULL) { if (_tcslen(pToken)>=COMMENT_SIZE) pToken[COMMENT_SIZE-1]= _T('\0'); // remove trailing spaces and CR LF _tcscpy(ctemp, pToken); for (i=_tcslen(ctemp)-1; i>1; i--) { if ( (ctemp[i]==' ') || (ctemp[i]=='\r') || (ctemp[i]=='\n') ) ctemp[i]=0; else break; } // now remove " " (if there) for (j=0, i=0; i<_tcslen(ctemp); i++) if (ctemp[i]!='\"') ctemp[j++]=ctemp[i]; ctemp[j]= _T('\0'); if (_tcslen(ctemp) >0 ) { if (Temp->Comment) { free(Temp->Comment); } Temp->Comment = (TCHAR*)malloc((_tcslen(ctemp)+1)*sizeof(TCHAR)); if (Temp->Comment) _tcscpy(Temp->Comment, ctemp); } #ifdef CUPDEBUG StartupStore(_T(" CUP COMMENT=<%s>%s"),Temp->Comment,NEWLINE); #endif } else { Temp->Comment=NULL; // useless } if(Temp->Altitude <= 0) { WaypointAltitudeFromTerrain(Temp); } if (Temp->Details) { free(Temp->Details); } return true; }
bool LoadCupTask(LPCTSTR szFileName) { // LockTaskData(); mapCode2Waypoint_t mapWaypoint; // ClearTask(); size_t idxTP = 0; bool bTakeOff = true; bool bLoadComplet = true; bool bLastInvalid=true; TCHAR szString[READLINE_LENGTH + 1]; TCHAR TpCode[NAME_SIZE + 1]; szString[READLINE_LENGTH] = _T('\0'); TpCode[NAME_SIZE] = _T('\0'); memset(szString, 0, sizeof (szString)); // clear Temp Buffer WAYPOINT newPoint = {0}; WAYPOINT* WPtoAdd=NULL; enum { none, Waypoint, TaskTp, Option } FileSection = none; zzip_stream stream(szFileName, "rt"); iNO_Tasks =0; TaskIndex =0; for (int i =0 ; i< MAX_TASKS;i++) szTaskStrings[ i] = NULL; #define MULTITASKS_CUP #ifdef MULTITASKS_CUP if (stream) { while (stream.read_line(szString)) { if ((FileSection == none) && ((_tcsncmp(_T("name,code,country"), szString, 17) == 0) || (_tcsncmp(_T("Title,Code,Country"), szString, 18) == 0))) { FileSection = Waypoint; continue; } else if ((FileSection == Waypoint) && (_tcscmp(szString, _T("-----Related Tasks-----")) == 0)) { FileSection = TaskTp; continue; } if( FileSection == TaskTp) { if(_tcsstr(szString, _T("\",\""))!= NULL) // really a task? (not an option) { if(iNO_Tasks < MAX_TASKS) // Space in List left {//[READLINE_LENGTH + 1]; szTaskStrings[ iNO_Tasks] = new TCHAR[READLINE_LENGTH + 1]; if( szTaskStrings[ iNO_Tasks] != NULL) { _tcscpy(szTaskStrings[ iNO_Tasks] , szString); // copy task string // StartupStore(_T("..Cup Task : %s %s"), szTaskStrings[ iNO_Tasks], NEWLINE); iNO_Tasks++; } else StartupStore(_T("..Cup Task: no memory %s"), NEWLINE); } else StartupStore(_T("..Cup Task Too many Tasks (more than %i) %s"), MAX_TASKS, NEWLINE); } } } stream.close(); StartupStore(_T("..Cup Selected Task:%i %s %s"), TaskIndex, szTaskStrings[ TaskIndex] , NEWLINE); } int res = 0; if(iNO_Tasks >1) // Selection only if more than one task found res = dlgTaskSelectListShowModal(); for (int i =0 ; i< MAX_TASKS;i++) // free dynamic memory if(szTaskStrings[i] != NULL) { // StartupStore(_T("..Cup Task : delete dynamic memoryLine %i %s"), i,NEWLINE); delete[] szTaskStrings[i]; szTaskStrings[i] = NULL; } if(res == mrCancel) return false; /***********************************************************************************/ LockTaskData(); ClearTask(); stream.open(szFileName, "rt"); #endif FileSection = none; int i=0; if (stream) { while (stream.read_line(szString)) { if ((FileSection == none) && ((_tcsncmp(_T("name,code,country"), szString, 17) == 0) || (_tcsncmp(_T("Title,Code,Country"), szString, 18) == 0))) { FileSection = Waypoint; continue; } else if ((FileSection == Waypoint) && (_tcscmp(szString, _T("-----Related Tasks-----")) == 0)) { FileSection = TaskTp; continue; } TCHAR *pToken = NULL; TCHAR *pWClast = NULL; switch (FileSection) { case Waypoint: memset(&newPoint, 0, sizeof(newPoint)); if (ParseCUPWayPointString(szString, &newPoint)) { mapWaypoint[newPoint.Name] = newPoint; } break; case TaskTp: // 1. Description // First column is the description of the task. If filled it should be double quoted. // If left empty, then SeeYou will determine the task type on runtime. if ((pToken = strsep_r(szString, TEXT(","), &pWClast)) == NULL) { // UnlockTaskData(); // no need to skip if only name missing!!! // return false; } // 2. and all successive columns, separated by commas // Each column represents one waypoint name double quoted. The waypoint name must be exactly the // same as the Long name of a waypoint listed above the Related tasks. WPtoAdd=NULL; if (i++ == TaskIndex) // load selected task { while (bLoadComplet && (pToken = strsep_r(NULL, TEXT(","), &pWClast)) != NULL) { if (idxTP < MAXTASKPOINTS) { _tcsncpy(TpCode, pToken, NAME_SIZE); CleanCupCode(TpCode); mapCode2Waypoint_t::iterator It = mapWaypoint.find(TpCode); if(!ISGAAIRCRAFT) { if (It != mapWaypoint.end()) { if (bTakeOff) { // skip TakeOff Set At Home Waypoint int ix = FindOrAddWaypoint(&(It->second),false); if (ix>=0) { #if 0 // REMOVE // We must not change HomeWaypoint without user knowing! // The takeoff and homewaypoint are independent from task. // In addition, this is a bug because on next run the index is invalid // and we have no more HowWaypoint! HomeWaypoint = ix; #endif bTakeOff = false; } #if BUGSTOP else LKASSERT(0); // .. else is unmanaged, TODO #endif } else { int ix = FindOrAddWaypoint(&(It->second),false); if (ix>=0) Task[idxTP++].Index = ix; #if BUGSTOP else LKASSERT(0); // .. else is unmanaged, TODO #endif } bLastInvalid=false; } else { // An invalid takeoff, probably a "???" , which we ignore #if TESTBENCH if (bTakeOff) StartupStore(_T("....... CUP Takeoff not found: <%s>\n"),TpCode); #endif // in any case bTakeOff now is false bTakeOff=false; bLastInvalid=true; } } else { //ISGAIRRCRAFT if(It != mapWaypoint.end()) { if(WPtoAdd!=NULL) { //add what we found in previous cycle: it was not the last one int ix = FindOrAddWaypoint(WPtoAdd,false); if (ix>=0) Task[idxTP++].Index = ix; #if BUGSTOP else LKASSERT(0); // .. else is unmanaged, TODO #endif } if (bTakeOff) { //it's the first: may be we have a corresponding airfield //look for departure airfield and add it int ix = FindOrAddWaypoint(&(It->second),true); if (ix>=0) { Task[idxTP++].Index = ix; bTakeOff = false; } #if BUGSTOP else LKASSERT(0); // .. else is unmanaged, TODO #endif } else WPtoAdd=&(It->second); //store it for next cycle (may be it is the last one) } } } else { bLoadComplet = false; } } if(ISGAAIRCRAFT) { //For GA: check if we have an airport corresponding to the last WP if(WPtoAdd!=NULL) { //if we have the last one (probably an airfield) still to add... if(idxTP<MAXTASKPOINTS) { int ix=FindOrAddWaypoint(WPtoAdd,true); //look for arrival airport and add it if (ix>=0) { Task[idxTP++].Index= ix; } #if BUGSTOP else LKASSERT(0); // .. else is unmanaged, TODO #endif } else bLoadComplet=false; } } FileSection = Option; } break; case Option: if ((pToken = strsep_r(szString, TEXT(","), &pWClast)) != NULL) { if (_tcscmp(pToken, _T("Options")) == 0) { while ((pToken = strsep_r(NULL, TEXT(","), &pWClast)) != NULL) { if (_tcsstr(pToken, _T("NoStart=")) == pToken) { // Opening of start line PGNumberOfGates = 1; StrToTime(pToken + 8, &PGOpenTimeH, &PGOpenTimeM); } else if (_tcsstr(pToken, _T("TaskTime=")) == pToken) { // Designated Time for the task // TODO : } else if (_tcsstr(pToken, _T("WpDis=")) == pToken) { // Task distance calculation. False = use fixes, True = use waypoints // TODO : } else if (_tcsstr(pToken, _T("NearDis=")) == pToken) { // Distance tolerance // TODO : } else if (_tcsstr(pToken, _T("NearAlt=")) == pToken) { // Altitude tolerance // TODO : } else if (_tcsstr(pToken, _T("MinDis=")) == pToken) { // Uncompleted leg. // False = calculate maximum distance from last observation zone. // TODO : } else if (_tcsstr(pToken, _T("RandomOrder=")) == pToken) { // if true, then Random order of waypoints is checked // TODO : } else if (_tcsstr(pToken, _T("MaxPts=")) == pToken) { // Maximum number of points // TODO : } else if (_tcsstr(pToken, _T("BeforePts=")) == pToken) { // Number of mandatory waypoints at the beginning. 1 means start line only, two means // start line plus first point in task sequence (Task line). // TODO : } else if (_tcsstr(pToken, _T("AfterPts=")) == pToken) { // Number of mandatory waypoints at the end. 1 means finish line only, two means finish line // and one point before finish in task sequence (Task line). // TODO : } else if (_tcsstr(pToken, _T("Bonus=")) == pToken) { // Bonus for crossing the finish line // TODO : } } } else if (_tcsstr(pToken, _T("ObsZone=")) == pToken) { TCHAR *sz = NULL; CupObsZoneUpdater TmpZone; TmpZone.mIdx = _tcstol(pToken + 8, &sz, 10); if (TmpZone.mIdx < MAXTASKPOINTS) { while ((pToken = strsep_r(NULL, TEXT(","), &pWClast)) != NULL) { if (_tcsstr(pToken, _T("Style=")) == pToken) { // Direction. 0 - Fixed value, 1 - Symmetrical, 2 - To next point, 3 - To previous point, 4 - To start point TmpZone.mType = _tcstol(pToken + 6, &sz, 10); } else if (_tcsstr(pToken, _T("R1=")) == pToken) { // Radius 1 TmpZone.mR1 = ReadLength(pToken + 3); } else if (_tcsstr(pToken, _T("A1=")) == pToken) { // Angle 1 in degrees TmpZone.mA1 = _tcstod(pToken + 3, &sz); } else if (_tcsstr(pToken, _T("R2=")) == pToken) { // Radius 2 TmpZone.mR2 = ReadLength(pToken + 3); } else if (_tcsstr(pToken, _T("A2=")) == pToken) { // Angle 2 in degrees TmpZone.mA2 = _tcstod(pToken + 3, &sz); } else if (_tcsstr(pToken, _T("A12=")) == pToken) { // Angle 12 TmpZone.mA12 = _tcstod(pToken + 4, &sz); } else if (_tcsstr(pToken, _T("Line=")) == pToken) { // true For Line Turmpoint type // Exist only for start an Goalin LK TmpZone.mLine = (_tcstol(pToken + 5, &sz, 10) == 1); } } TmpZone.UpdateTask(); } } } break; case none: default: break; } memset(szString, 0, sizeof (szString)); // clear Temp Buffer } } if(!ISGAAIRCRAFT) { // Landing don't exist in LK Task Systems, so Remove It; if ( bLoadComplet && !bLastInvalid ) { RemoveTaskPoint(getFinalWaypoint()); } } UnlockTaskData(); for (mapCode2Waypoint_t::iterator It = mapWaypoint.begin(); It != mapWaypoint.end(); ++It) { if (It->second.Comment) { free(It->second.Comment); } if (It->second.Details) { free(It->second.Details); } } mapWaypoint.clear(); return ValidTaskPoint(0); }