/** * Reads the selected LanguageFile into the cache */ void ReadLanguageFile() { StartupStore(TEXT("Loading language file\n")); TCHAR szFile1[MAX_PATH] = TEXT("\0"); FILE *fp=NULL; // Read the language filename from the registry GetRegistryString(szRegistryLanguageFile, szFile1, MAX_PATH); ExpandLocalPath(szFile1); // Reset filename in registry in case language // loading crashes the application SetRegistryString(szRegistryLanguageFile, TEXT("\0")); // If the language file is not set use the default one if (_tcslen(szFile1)==0) { _tcscpy(szFile1,TEXT("default.xcl")); } // Open the language file fp = _tfopen(szFile1, TEXT("rt")); // Return if file error if (fp == NULL) return; // TODO code: Safer sizes, strings etc - use C++ (can scanf restrict length?) TCHAR buffer[2049]; // key from scanf TCHAR key[2049]; // key from scanf TCHAR value[2049]; // value from scanf int found; // Entries found from scanf // Read from the file while ((GetTextData_Size < MAXSTATUSMESSAGECACHE) && _fgetts(buffer, 2048, fp) && ((found = _stscanf(buffer, TEXT("%[^#=]=%[^\r\n][\r\n]"), key, value)) != EOF)) { // Check valid line? if ((found != 2) || key[0] == 0 || value[0] == 0) continue; // Save parsed translation to the cache GetTextData[GetTextData_Size].key = StringMallocParse(key); GetTextData[GetTextData_Size].text = StringMallocParse(value); // Global counter GetTextData_Size++; } // file was OK, so save filename to registry again ContractLocalPath(szFile1); SetRegistryString(szRegistryLanguageFile, szFile1); fclose(fp); }
void StatusMessageList::LoadFile(TLineReader &reader) { int ms; // Found ms for delay const TCHAR **location; // Where to put the data bool some_data; // Did we find some in the last loop... // Init first entry _init_Status(StatusMessageData_Size); some_data = false; /* Read from the file */ TCHAR *buffer; const TCHAR *key, *value; while ((StatusMessageData_Size < MAXSTATUSMESSAGECACHE) && (buffer = reader.read()) != NULL) { // Check valid line? If not valid, assume next record (primative, but works ok!) if (*buffer == _T('#') || !parse_assignment(buffer, key, value)) { // Global counter (only if the last entry had some data) if (some_data) { StatusMessageData_Size++; some_data = false; _init_Status(StatusMessageData_Size); } } else { location = NULL; if (_tcscmp(key, _T("key")) == 0) { some_data = true; // Success, we have a real entry location = &StatusMessageData[StatusMessageData_Size].key; } else if (_tcscmp(key, _T("sound")) == 0) { StatusMessageData[StatusMessageData_Size].doSound = true; location = &StatusMessageData[StatusMessageData_Size].sound; } else if (_tcscmp(key, _T("delay")) == 0) { TCHAR *endptr; ms = _tcstol(value, &endptr, 10); if (endptr > value) StatusMessageData[StatusMessageData_Size].delay_ms = ms; } else if (_tcscmp(key, _T("hide")) == 0) { if (_tcscmp(value, _T("yes")) == 0) StatusMessageData[StatusMessageData_Size].doStatus = false; } // Do we have somewhere to put this && // is it currently empty ? (prevent lost at startup) if (location && (_tcscmp(*location, _T("")) == 0)) { // TODO code: this picks up memory lost from no entry, but not duplicates - fix. if (*location) { // JMW fix memory leak free((void*)*location); } *location = StringMallocParse(value); } } } // How many we really got (blank next just in case) StatusMessageData_Size++; _init_Status(StatusMessageData_Size); }
void StatusMessageList::LoadFile() { StartupStore(TEXT("Loading status file\n")); TCHAR szFile1[MAX_PATH] = TEXT("\0"); FILE *fp=NULL; // Open file from registry GetRegistryString(szRegistryStatusFile, szFile1, MAX_PATH); ExpandLocalPath(szFile1); SetRegistryString(szRegistryStatusFile, TEXT("\0")); if (_tcslen(szFile1)>0) fp = _tfopen(szFile1, TEXT("rt")); // Unable to open file if (fp == NULL) return; // TODO code: Safer sizes, strings etc - use C++ (can scanf restrict length?) TCHAR buffer[2049]; // Buffer for all TCHAR key[2049]; // key from scanf TCHAR value[2049]; // value from scanf int ms; // Found ms for delay const TCHAR **location; // Where to put the data int found; // Entries found from scanf bool some_data; // Did we find some in the last loop... // Init first entry _init_Status(StatusMessageData_Size); some_data = false; /* Read from the file */ while ( (StatusMessageData_Size < MAXSTATUSMESSAGECACHE) && _fgetts(buffer, 2048, fp) && ((found = _stscanf(buffer, TEXT("%[^#=]=%[^\n]\n"), key, value)) != EOF) ) { // Check valid line? If not valid, assume next record (primative, but works ok!) if ((found != 2) || key[0] == 0 || value[0] == 0) { // Global counter (only if the last entry had some data) if (some_data) { StatusMessageData_Size++; some_data = false; _init_Status(StatusMessageData_Size); } } else { location = NULL; if (_tcscmp(key, TEXT("key")) == 0) { some_data = true; // Success, we have a real entry location = &StatusMessageData[StatusMessageData_Size].key; } else if (_tcscmp(key, TEXT("sound")) == 0) { StatusMessageData[StatusMessageData_Size].doSound = true; location = &StatusMessageData[StatusMessageData_Size].sound; } else if (_tcscmp(key, TEXT("delay")) == 0) { if (_stscanf(value, TEXT("%d"), &ms) == 1) StatusMessageData[StatusMessageData_Size].delay_ms = ms; } else if (_tcscmp(key, TEXT("hide")) == 0) { if (_tcscmp(value, TEXT("yes")) == 0) StatusMessageData[StatusMessageData_Size].doStatus = false; } // Do we have somewhere to put this && is it currently empty ? (prevent lost at startup) if (location && (_tcscmp(*location, TEXT("")) == 0)) { // TODO code: this picks up memory lost from no entry, but not duplicates - fix. if (*location) { // JMW fix memory leak free((void*)*location); } *location = StringMallocParse(value); } } } // How many we really got (blank next just in case) StatusMessageData_Size++; _init_Status(StatusMessageData_Size); // file was ok, so save it to registry ContractLocalPath(szFile1); SetRegistryString(szRegistryStatusFile, szFile1); fclose(fp); }
bool ReplayLogger::ScanBuffer(TCHAR *buffer, double *Time, double *Latitude, double *Longitude, double *Altitude) { int DegLat, DegLon; int MinLat, MinLon; TCHAR NoS, EoW; int iAltitude; int bAltitude; int Hour=0; int Minute=0; int Second=0; int lfound=0; int found=0; if ((lfound = swscanf(buffer, TEXT("B%02d%02d%02d%02d%05d%c%03d%05d%cA%05d%05d"), &Hour, &Minute, &Second, &DegLat, &MinLat, &NoS, &DegLon, &MinLon, &EoW, &iAltitude, &bAltitude )) != EOF) { if (lfound==11) { *Latitude = DegLat+MinLat/60000.0; if (NoS==_T('S')) { *Latitude *= -1; } *Longitude = DegLon+MinLon/60000.0; if (EoW==_T('W')) { *Longitude *= -1; } if (bAltitude>0) *Altitude = bAltitude; else *Altitude = iAltitude; *Time = Hour*3600+Minute*60+Second; } } TCHAR event[200]; TCHAR misc[200]; found = _stscanf(buffer, TEXT("LPLT event=%[^ ] %[A-Za-z0-9 \\/().,]"), event,misc); if (found>0) { pt2Event fevent = InputEvents::findEvent(event); if (fevent) { if (found==2) { TCHAR *mmisc = StringMallocParse(misc); if (mmisc!=NULL) { fevent(mmisc); free(mmisc); } } else { fevent(TEXT("\0")); } } } return (lfound==11 && (*Latitude!=0 && *Longitude!=0) ); }
void ParseInputFile(InputConfig &config, TLineReader &reader) { // TODO code - Safer sizes, strings etc - use C++ (can scanf restrict length?) TCHAR *new_label = NULL; // Init first entry // Did we find some in the last loop... bool some_data = false; // Multiple modes (so large string) TCHAR d_mode[1024] = _T(""); TCHAR d_type[256] = _T(""); TCHAR d_data[256] = _T(""); unsigned event_id = 0; TCHAR d_label[256] = _T(""); int d_location = 0; TCHAR d_event[256] = _T(""); TCHAR d_misc[256] = _T(""); int line = 0; // Read from the file // TODO code: What about \r - as in \r\n ? // TODO code: Note that ^# does not allow # in key - might be required (probably not) // Better way is to separate the check for # and the scanf TCHAR *buffer; while ((buffer = reader.read()) != NULL) { TrimRight(buffer); line++; const TCHAR *key, *value; // experimental: if the first line is "#CLEAR" then the whole default config is cleared // and can be overwritten by file if (line == 1 && _tcscmp(buffer, _T("#CLEAR")) == 0) { config.clear_all_events(); } else if (buffer[0] == _T('\0')) { // Check valid line? If not valid, assume next record (primative, but works ok!) // General checks before continue... if (some_data && (d_mode != NULL) && (_tcscmp(d_mode, _T("")) != 0)) { TCHAR *token; // For each mode token = _tcstok(d_mode, _T(" ")); // General errors - these should be true assert(d_location >= 0); assert(d_location < 1024); // Scott arbitrary limit assert(d_mode != NULL); assert(d_type != NULL); assert(d_label != NULL); // These could indicate bad data - thus not an ASSERT (debug only) // assert(_tcslen(d_mode) < 1024); // assert(_tcslen(d_type) < 1024); // assert(_tcslen(d_label) < 1024); while (token != NULL) { // All modes are valid at this point int mode_id = config.make_mode(token); assert(mode_id >= 0); // Make label event // TODO code: Consider Reuse existing entries... if (d_location > 0) { // Only copy this once per object - save string space if (!new_label) { new_label = StringMallocParse(d_label); } config.append_menu(mode_id, new_label, d_location, event_id); } // Make key (Keyboard input) // key - Hardware key or keyboard if (_tcscmp(d_type, _T("key")) == 0) { // Get the int key (eg: APP1 vs 'a') unsigned key = InputEvents::findKey(d_data); if (key > 0) config.Key2Event[mode_id][key] = event_id; else LogStartUp(_T("Invalid key data: %s at %i"), d_data, line); // Make gce (Glide Computer Event) // GCE - Glide Computer Event } else if (_tcscmp(d_type, _T("gce")) == 0) { // Get the int key (eg: APP1 vs 'a') int key = InputEvents::findGCE(d_data); if (key >= 0) config.GC2Event[mode_id][key] = event_id; else LogStartUp(_T("Invalid GCE data: %s at %i"), d_data, line); // Make ne (NMEA Event) // NE - NMEA Event } else if (_tcscmp(d_type, _T("ne")) == 0) { // Get the int key (eg: APP1 vs 'a') int key = InputEvents::findNE(d_data); if (key >= 0) config.N2Event[mode_id][key] = event_id; else LogStartUp(_T("Invalid GCE data: %s at %i"), d_data, line); // label only - no key associated (label can still be touch screen) } else if (_tcscmp(d_type, _T("label")) == 0) { // Nothing to do here... } else { LogStartUp(_T("Invalid type: %s at %i"), d_type, line); } token = _tcstok(NULL, _T(" ")); } } // Clear all data. some_data = false; _tcscpy(d_mode, _T("")); _tcscpy(d_type, _T("")); _tcscpy(d_data, _T("")); event_id = 0; _tcscpy(d_label, _T("")); d_location = 0; new_label = NULL; } else if (string_is_empty(buffer) || buffer[0] == _T('#')) { // Do nothing - we probably just have a comment line // JG removed "void;" - causes warning (void is declaration and needs variable) // NOTE: Do NOT display buffer to user as it may contain an invalid stirng ! } else if (parse_assignment(buffer, key, value)) { if (_tcscmp(key, _T("mode")) == 0) { if (_tcslen(value) < 1024) { some_data = true; // Success, we have a real entry _tcscpy(d_mode, value); } } else if (_tcscmp(key, _T("type")) == 0) { if (_tcslen(value) < 256) _tcscpy(d_type, value); } else if (_tcscmp(key, _T("data")) == 0) { if (_tcslen(value) < 256) _tcscpy(d_data, value); } else if (_tcscmp(key, _T("event")) == 0) { if (_tcslen(value) < 256) { _tcscpy(d_event, _T("")); _tcscpy(d_misc, _T("")); int ef; #if defined(__BORLANDC__) memset(d_event, 0, sizeof(d_event)); memset(d_misc, 0, sizeof(d_event)); if (_tcschr(value, ' ') == NULL) { _tcscpy(d_event, value); } else { #endif ef = _stscanf(value, _T("%[^ ] %[A-Za-z0-9 \\/().,]"), d_event, d_misc); #if defined(__BORLANDC__) } #endif // TODO code: Can't use token here - breaks // other token - damn C - how about // C++ String class ? // TCHAR *eventtoken; // eventtoken = _tcstok(value, _T(" ")); // d_event = token; // eventtoken = _tcstok(value, _T(" ")); if ((ef == 1) || (ef == 2)) { // TODO code: Consider reusing existing identical events pt2Event event = InputEvents::findEvent(d_event); if (event) { TCHAR *allocated = StringMallocParse(d_misc); event_id = config.append_event(event, allocated, event_id); /* not freeing the string, because InputConfig::append_event() stores the string point without duplicating it; strictly speaking, this is a memory leak, but the input file is only loaded once at startup, so this is acceptable; in return, we don't have to duplicate the hard-coded defaults, which saves some memory */ //free(allocated); } else { LogStartUp(_T("Invalid event type: %s at %i"), d_event, line); } } else { LogStartUp(_T("Invalid event type at %i"), line); } } } else if (_tcscmp(key, _T("label")) == 0) { _tcscpy(d_label, value); } else if (_tcscmp(key, _T("location")) == 0) { d_location = _ttoi(value); } else { LogStartUp(_T("Invalid key/value pair %s=%s at %i"), key, value, line); } } else { LogStartUp(_T("Invalid line at %i"), line); } } // end while }