2020-09-20 21:09:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2020-10-15 23:16:57 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2020-09-20 21:09:47 +00:00
|
|
|
#include "format.h"
|
2021-03-05 22:19:04 +00:00
|
|
|
#include "ognconv.h"
|
2020-09-20 21:09:47 +00:00
|
|
|
|
2021-03-05 22:19:04 +00:00
|
|
|
static bool Earlier(const char *Line1, const char *Line2) { return strcmp(Line1, Line2)<0; }
|
2020-10-15 23:16:57 +00:00
|
|
|
|
2022-08-07 04:01:03 +00:00
|
|
|
static int Verbose = 0;
|
2020-09-20 21:09:47 +00:00
|
|
|
static int GeoidSepar = 40;
|
|
|
|
|
2022-08-07 04:01:03 +00:00
|
|
|
const int MaxLineLen = 256;
|
|
|
|
const int MaxIGClen = 64;
|
|
|
|
|
2020-09-20 21:09:47 +00:00
|
|
|
static FILE *OutFile = 0;
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
2020-10-19 01:02:31 +00:00
|
|
|
{ if(argc<2)
|
2020-09-20 21:09:47 +00:00
|
|
|
{ printf("Usage: %s <own-aircraft-APRS-call> <input-file.aprs>\n", argv[0]);
|
|
|
|
return 0; }
|
|
|
|
|
2022-08-07 04:01:03 +00:00
|
|
|
const char *OwnAcft = argv[1]; int OwnAcftLen = strlen(OwnAcft); // target aircraft APRS name
|
|
|
|
char OutFileName[32]; strcpy(OutFileName, OwnAcft); strcat(OutFileName, ".IGC"); // create the IGC file name
|
2020-09-20 21:09:47 +00:00
|
|
|
|
|
|
|
const char *InpFileName = argv[2];
|
2020-10-19 01:02:31 +00:00
|
|
|
FILE *InpFile;
|
|
|
|
if(InpFileName==0 || strcmp(InpFileName,"-")==0) InpFile=stdin;
|
|
|
|
else
|
|
|
|
{ InpFile=fopen(InpFileName, "rt");
|
|
|
|
if(InpFile==0) { printf("Cannot open %s for read\n", InpFileName); return 0; }
|
|
|
|
}
|
2020-09-20 21:09:47 +00:00
|
|
|
|
2022-08-07 04:01:03 +00:00
|
|
|
std::vector<char *> OutLine; // list of APRS lines for the selected aircraft
|
|
|
|
char InpLine[MaxLineLen];
|
|
|
|
// char OutLine[MaxLineLen];
|
2020-09-20 21:09:47 +00:00
|
|
|
int InpLines=0;
|
2020-10-15 23:16:57 +00:00
|
|
|
// int OutLines=0;
|
2022-08-07 04:01:03 +00:00
|
|
|
char *Out=0; // (new) IGC line allocated
|
2020-09-20 21:09:47 +00:00
|
|
|
for( ; ; )
|
2022-08-07 04:01:03 +00:00
|
|
|
{ if(fgets(InpLine, MaxLineLen, InpFile)==0) break; // read line from the input
|
2020-09-20 21:09:47 +00:00
|
|
|
char *EOL = strchr(InpLine, '\n'); if(EOL==0) break;
|
|
|
|
*EOL = 0;
|
|
|
|
InpLines++;
|
2022-08-07 04:01:03 +00:00
|
|
|
if(memcmp(InpLine, OwnAcft, OwnAcftLen)) continue; // must start with the selected APRS name
|
|
|
|
if(Out==0) Out = (char *)malloc(MaxIGClen+MaxLineLen); // allocated (new) IGC line
|
|
|
|
int OutLen=APRS2IGC(Out, InpLine, GeoidSepar); // convert APRS to IGC B-record
|
|
|
|
if(OutLen>0) // if correct conversion
|
|
|
|
{ // if(Verbose) printf("%s => %s [%d]\n", InpLine, Out, OutLen);
|
|
|
|
OutLen += Format_String(Out+OutLen, "LGNE ");
|
|
|
|
OutLen += Format_String(Out+OutLen, InpLine);
|
|
|
|
Out[OutLen++] = '\n'; Out[OutLen] = 0;
|
|
|
|
OutLine.push_back(Out); Out=0; } // add the new IGC line to the list
|
2020-09-20 21:09:47 +00:00
|
|
|
}
|
2020-10-15 23:16:57 +00:00
|
|
|
if(Out) { free(Out); Out=0; }
|
2020-10-19 01:02:31 +00:00
|
|
|
if(InpFile!=stdin) fclose(InpFile);
|
2020-09-20 21:09:47 +00:00
|
|
|
printf("%d lines from %s\n", InpLines, InpFileName);
|
2020-10-15 23:16:57 +00:00
|
|
|
|
2022-08-07 04:01:03 +00:00
|
|
|
std::sort(OutLine.begin(), OutLine.end(), Earlier); // sort the IGC B-record lines
|
2020-10-15 23:16:57 +00:00
|
|
|
OutFile=fopen(OutFileName, "wt");
|
|
|
|
if(OutFile==0) { printf("Cannot open %s for write\n", OutFileName); return 0; }
|
2022-08-07 04:01:03 +00:00
|
|
|
for(size_t Idx=0; Idx<OutLine.size(); Idx++) // look over (now sorted) IGC B-records
|
2021-03-06 20:19:08 +00:00
|
|
|
{ fprintf(OutFile, "%s", OutLine[Idx]); }
|
2020-09-20 21:09:47 +00:00
|
|
|
fclose(OutFile);
|
2021-03-05 22:19:04 +00:00
|
|
|
printf("%lu lines to %s\n", OutLine.size(), OutFileName);
|
2020-09-20 21:09:47 +00:00
|
|
|
|
|
|
|
return 0; }
|