//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// Reads one line from UTF-8 encoded file and converts it to TCHAR Unicode /// string. /// maxChars-1 characters are returned at most, leaving 1 char for /// terminating '\0'. /// /// The method strips any newline character ('\r' and '\n'). /// /// @param unicode output buffer (will be terminated with '\0'); must be /// large enough to contain full line (otherwise rest /// of line will be discarded) /// @param maxChars output buffer size (nb of TCHARs) /// /// @retval true line has been read successfully /// @retval false error during reading from the file /// bool Utf8File::ReadLn(TCHAR* unicode, int maxChars) { if (fp == NULL) return(false); // in worst case each char can be encoded in 4 bytes char cstr[4 * maxChars]; if (fgets(cstr, countof(cstr), fp) == NULL) return(false); // strip new-line separators size_t len = strlen(cstr); while (len > 0) { char last = cstr[len - 1]; if (last == '\r' || last == '\n') cstr[--len] = '\0'; else break; } if (utf2unicode(cstr, unicode, maxChars) < 0 && !convErReported) { StartupStore(_T("Invalid UTF8-WC conversion for '%s'%s"), path, NEWLINE); convErReported = true; } return(true); } // ReadLn()
// Reads line from UTF-8 encoded text file. // File must be open in binary read mode. // ATTENTION: if buffer is not large enough, the zzip_fread will fail // and ReadULine will return , but you will not be able to know if it returned // for a string overflow (managed) or because it reached EOF! bool ReadULine(ZZIP_FILE* fp, TCHAR *unicode, int maxChars) { // This is a char, and we need space for at least MAX_HELP TCHARS! // unsigned char buf[1500 * 2]; long startPos = zzip_tell(fp); if (startPos < 0) { StartupStore(_T(". ftell() error = %d%s"), errno, NEWLINE); return(false); } size_t nbRead = zzip_fread(buf, 1, sizeof(buf) - 1, fp); if (nbRead == 0) return(false); buf[nbRead] = '\0'; // find new line (CR/LF/CRLF) in the string and terminate string at that position size_t i; for (i = 0; i < nbRead; i++) { if (buf[i] == '\n') { buf[i++] = '\0'; if (buf[i] == '\r') i++; break; } if (buf[i] == '\r') { buf[i++] = '\0'; if (buf[i] == '\n') i++; break; } } // next reading will continue after new line zzip_seek(fp, startPos + i, SEEK_SET); // skip leading BOM char* begin = (char*) buf; if (buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF) begin += 3; return(utf2unicode(begin, unicode, maxChars) >= 0); }
bool CTaskFileHelper::Load(const TCHAR* szFileName) { CScopeLock LockTask(LockTaskData, UnlockTaskData); StartupStore(_T(". LoadTask : <%s>%s"), szFileName, NEWLINE); ClearTask(); FILE* stream = _tfopen(szFileName, TEXT("rb")); if (stream) { fseek(stream, 0, SEEK_END); // seek to end of file long size = ftell(stream); // get current file pointer fseek(stream, 0, SEEK_SET); // seek back to beginning of file char * buff = (char*) calloc(size + 1, sizeof (char)); long nRead = fread(buff, sizeof (char), size, stream); if (nRead != size) { fclose(stream); free(buff); return false; } fclose(stream); TCHAR * szXML = (TCHAR*) calloc(size + 1, sizeof (TCHAR)); utf2unicode(buff, szXML, size + 1); free(buff); XMLNode rootNode = XMLNode::parseString(szXML, _T("lk-task")); if (rootNode) { LoadWayPointList(rootNode.getChildNode(_T("waypoints"), 0)); if (!LoadTaskPointList(rootNode.getChildNode(_T("taskpoints"), 0))) { free(szXML); return false; } if (!LoadStartPoint(rootNode.getChildNode(_T("startpoints"), 0))) { free(szXML); return false; } LoadOptions(rootNode); } free(szXML); } RefreshTask(); TaskModified = false; TargetModified = false; _tcscpy(LastTaskFileName, szFileName); return true; }
int wince_open (const char *path, int oflag, ...) { TCHAR wpath[MAX_PATH]; DWORD fileaccess; DWORD fileshare; DWORD filecreate; DWORD fileattrib; HANDLE hnd; size_t path_len = strlen (path); if (path_len >= MAX_PATH) return -1; switch (oflag & (O_RDONLY | O_WRONLY | O_RDWR)) { case O_RDONLY: fileaccess = GENERIC_READ; break; case O_WRONLY: fileaccess = GENERIC_WRITE; break; case O_RDWR: fileaccess = GENERIC_READ | GENERIC_WRITE; break; default: return -1; } switch (oflag & (O_CREAT | O_EXCL | O_TRUNC)) { case 0: case O_EXCL: /* ignore EXCL w/o CREAT */ filecreate = OPEN_EXISTING; break; case O_CREAT: filecreate = OPEN_ALWAYS; break; case O_CREAT | O_EXCL: case O_CREAT | O_TRUNC | O_EXCL: filecreate = CREATE_NEW; break; case O_TRUNC: case O_TRUNC | O_EXCL: /* ignore EXCL w/o CREAT */ filecreate = TRUNCATE_EXISTING; break; case O_CREAT | O_TRUNC: filecreate = CREATE_ALWAYS; break; default: /* this can't happen ... all cases are covered */ return -1; } utf2unicode(path, wpath, MAX_PATH); //mbstowcs (wpath, path, path_len + 1); fileshare = FILE_SHARE_READ | FILE_SHARE_WRITE; fileattrib = FILE_ATTRIBUTE_NORMAL; hnd = CreateFileW (wpath, fileaccess, fileshare, NULL, filecreate, fileattrib, NULL); if (hnd == INVALID_HANDLE_VALUE) return -1; if (oflag & O_APPEND) SetFilePointer (hnd, 0, NULL, FILE_END); return (int) hnd; }
// Rescale automatically dialogs, using negative values to force rescaling // Notice: SHOULD BE CALLED ONLY IF rWidth is negative, in order to avoid useless SetWindowPos int RescaleWidth(const int rWidth) { // Always rescale negative widths if (rWidth <-1) { // Special case is when width is also the scale unit, which demonstrate we have a bug to fix here! #if USEIBOX if (rWidth == (int)(-246*InfoBoxLayout::dscale)){ #else if (rWidth == (int)(-246*ScreenDScale)){ #endif return LKwdlgConfig; } double i=(246.0 / abs(rWidth)); if (i==0) { FailStore(_T("INTERNAL ERROR RESCALEWIDTH rWidth=%d"),rWidth); DoStatusMessage(_T("RESCALE ERR-001")); return rWidth; } #if USEIBOX int ri=(int)( (LKwdlgConfig/i) *InfoBoxLayout::dscale ); #else int ri=(int)( (LKwdlgConfig/i) *ScreenDScale ); #endif // StartupStore(_T("... RescaleWidth(): rescale %d to %d\n"),rWidth, ri); if (ri>ScreenSizeX) return(ScreenSizeX); return (ri); } // else use the incoming rWidth but it is clearly an error DoStatusMessage(_T("RESCALE WARN-001")); return rWidth; } void ChangeWindCalcSpeed(const int newspeed) { WindCalcSpeed += (double)newspeed/SPEEDMODIFY; } // runmode 0: exec inside LocalPath home of LK8000 // runmode 1: exec inside bool LKRun(const TCHAR *prog, const int runmode, const DWORD dwaitime) { if (_tcslen(prog) <5) { StartupStore(_T("... LKRun failure: invalid exec path <%s>%s"),prog,NEWLINE); return false; } TCHAR path[MAX_PATH]; if (runmode<0 || runmode>1) { StartupStore(_T("... LKRun failure: invalid runmode=%d %s"),runmode,NEWLINE); return false; } // mode 0: localpath , forced execution, with warnings if something goes wrong // mode 1: optional execution, no warnings if nothing found if (runmode<2) { LocalPath(path,prog); if (runmode==0) StartupStore(_T(". LKRun: exec <%s> background=%u%s"),path,dwaitime,NEWLINE); PROCESS_INFORMATION pi; STARTUPINFO si; ZeroMemory(&si,sizeof(STARTUPINFO)); si.cb=sizeof(STARTUPINFO); si.wShowWindow= SW_SHOWNORMAL; si.dwFlags = STARTF_USESHOWWINDOW; // if (!::CreateProcess(_T("C:\\WINDOWS\\notepad.exe"),_T(""), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) if (!::CreateProcess(path,_T(""), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) { if (runmode==0) StartupStore(_T("... LKRun exec FAILED%s"),NEWLINE); return false; } ::WaitForSingleObject(pi.hProcess, dwaitime); StartupStore(_T(". LKRun exec terminated%s"),NEWLINE); return true; } return false; } void GotoWaypoint(const int wpnum) { if (!ValidWayPoint(wpnum)) { DoStatusMessage(_T("ERR-639 INVALID GOTO WPT")); return; } if (ValidTaskPoint(ActiveWayPoint) && ValidTaskPoint(1)) { TCHAR wpname[NAME_SIZE+1]; _tcscpy(wpname,WayPointList[wpnum].Name); wpname[10] = '\0'; if (MessageBoxX(hWndMapWindow, // LKTOKEN _@M158_ = "CONFIRM GOTO, ABORTING TASK?" gettext(TEXT("_@M158_")), // LKTOKEN _@M40_ = "A task is running!" gettext(TEXT("_@M40_")), MB_YESNO|MB_ICONQUESTION) == IDYES) { LockTaskData(); FlyDirectTo(wpnum); OvertargetMode=OVT_TASK; UnlockTaskData(); } } else { LockTaskData(); FlyDirectTo(wpnum); OvertargetMode=OVT_TASK; UnlockTaskData(); } } void ToggleBaroAltitude() { if (!GPS_INFO.BaroAltitudeAvailable) { // LKTOKEN _@M121_ = "BARO ALTITUDE NOT AVAILABLE" DoStatusMessage(gettext(TEXT("_@M121_"))); return; } EnableNavBaroAltitude=!EnableNavBaroAltitude; if (EnableNavBaroAltitude) // LKTOKEN _@M756_ = "USING BARO ALTITUDE" DoStatusMessage(gettext(TEXT("_@M756_"))); else // LKTOKEN _@M757_ = "USING GPS ALTITUDE" DoStatusMessage(gettext(TEXT("_@M757_"))); } TCHAR * GetSizeSuffix(void) { static TCHAR suffixname[12]; _stprintf(suffixname,_T("%03dx%03d"),ScreenSizeX,ScreenSizeY); return(suffixname); } void LKRunStartEnd(bool start) { if (start) { LKRun(_T("PREROTATE1.EXE"),1,5000); LKRun(_T("PREROTATE2.EXE"),1,5000); LKRun(_T("PREROTATE3.EXE"),1,5000); LKRun(_T("PRELOAD_00.EXE"),1,0); LKRun(_T("PRELOAD_05.EXE"),1,5000); LKRun(_T("PRELOAD_30.EXE"),1,30000); LKRun(_T("PRELOAD_60.EXE"),1,60000); LKRun(_T("PRELOAD_99.EXE"),1,INFINITE); } else { LKRun(_T("ENDLOAD_00.EXE"),1,0); LKRun(_T("ENDLOAD_05.EXE"),1,5000); LKRun(_T("ENDLOAD_30.EXE"),1,30000); LKRun(_T("ENDLOAD_60.EXE"),1,60000); LKRun(_T("ENDROTATE1.EXE"),1,5000); LKRun(_T("ENDROTATE2.EXE"),1,5000); LKRun(_T("ENDROTATE3.EXE"),1,5000); LKRun(_T("ENDLOAD_99.EXE"),1,INFINITE); } } // Reads line from UTF-8 encoded text file. // File must be open in binary read mode. bool ReadULine(ZZIP_FILE* fp, TCHAR *unicode, int maxChars) { unsigned char buf[READLINE_LENGTH * 2]; long startPos = zzip_tell(fp); if (startPos < 0) { StartupStore(_T(". ftell() error = %d%s"), errno, NEWLINE); return(false); } size_t nbRead = zzip_fread(buf, 1, sizeof(buf) - 1, fp); if (nbRead == 0) return(false); buf[nbRead] = '\0'; // find new line (CR/LF/CRLF) in the string and terminate string at that position size_t i; for (i = 0; i < nbRead; i++) { if (buf[i] == '\n') { buf[i++] = '\0'; if (buf[i] == '\r') i++; break; } if (buf[i] == '\r') { buf[i++] = '\0'; if (buf[i] == '\n') i++; break; } } // next reading will continue after new line zzip_seek(fp, startPos + i, SEEK_SET); // skip leading BOM char* begin = (char*) buf; if (buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF) begin += 3; return(utf2unicode(begin, unicode, maxChars) >= 0); }