Revert "Device/Driver/LX: Add small delay after baud rate change"
[xcsoar.git] / test / src / FixGRecord.cpp
blobceba826e2ce30096b5f7a6bdfe3f36d357e6af84
1 /* Copyright_License {
3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "Logger/GRecord.hpp"
24 #include "OS/Args.hpp"
25 #include "IO/FileTransaction.hpp"
26 #include "IO/FileLineReader.hpp"
27 #include "IO/TextWriter.hpp"
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
33 static bool
34 FixGRecord(NLineReader &reader, TextWriter &writer)
36 GRecord grecord;
37 grecord.Initialize();
39 char digest[GRecord::DIGEST_LENGTH + 1];
40 grecord.GetDigest(digest);
42 char *line;
43 while ((line = reader.ReadLine()) != nullptr) {
44 if (line[0] == 'G')
45 break;
47 if (memcmp(line, "HFFTYFRTYPE:XCSOAR,XCSOAR ", 26) == 0) {
48 char *v = strstr(line + 25, " 6.5 ");
49 if (v != nullptr) {
50 static char buffer[1024], *p = buffer;
52 size_t n = v + 4 - line;
53 memcpy(p, line, n);
54 p += n;
56 memcpy(p, "fix", 3);
57 p += 3;
59 strcpy(p, v + 4);
61 line = buffer;
65 grecord.AppendRecordToBuffer(line);
67 if (!writer.WriteLine(line))
68 return false;
71 grecord.FinalizeBuffer();
72 grecord.WriteTo(writer);
73 return true;
76 static bool
77 FixGRecord(NLineReader &reader, const TCHAR *dest_path)
79 TextWriter writer(dest_path);
80 return writer.IsOpen() && FixGRecord(reader, writer) && writer.Flush();
83 int
84 main(int argc, char **argv)
86 Args args(argc, argv, "FILE.igc");
87 tstring path = args.ExpectNextT();
88 args.ExpectEnd();
91 GRecord grecord;
92 grecord.Initialize();
94 if (!grecord.VerifyGRecordInFile(path.c_str())) {
95 fprintf(stderr, "Invalid G record\n");
96 return EXIT_FAILURE;
100 printf("Valid G record found\n");
102 FileTransaction transaction(path.c_str());
105 FileLineReaderA reader(path.c_str());
106 if (reader.error()) {
107 fprintf(stderr, "Failed to open input file\n");
108 return EXIT_FAILURE;
111 if (!FixGRecord(reader, transaction.GetTemporaryPath())) {
112 fprintf(stderr, "Failed to write output file\n");
113 return EXIT_FAILURE;
117 if (!transaction.Commit()) {
118 fprintf(stderr, "Failed to commit output file\n");
119 return EXIT_FAILURE;
122 printf("New G record written\n");
123 return EXIT_SUCCESS;