#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
std::string GetFieldId(std::string sentenceId, int fieldIndex)
{
// Combine the sentence ID and field index to create the field ID
std::ostringstream oss;
oss << sentenceId << "-" << fieldIndex;
return oss.str();
}
std::vector<std::string> SplitString(std::string str, char delimiter)
{
std::vector<std::string> parts;
std::stringstream ss(str);
std::string part;
while (std::getline(ss, part, delimiter)) {
parts.push_back(part);
}
return parts;
}
std::map<std::string, std::string> ParseNmeaString(std::string nmeaString)
{
std::map<std::string, std::string> components;
if (nmeaString.length() < 7 || nmeaString[0] != '$' || nmeaString[nmeaString.length()-3] != '*') {
return components;
}
std::string sentence = nmeaString.substr(1, nmeaString.length()-4);
std::string checksum = nmeaString.substr(nmeaString.length()-2, 2);
std::vector<std::string> parts = SplitString(sentence, ',');
std::string sentenceId = parts[0];
for (int i = 1; i < parts.size(); i++) {
components[GetFieldId(sentenceId, i)] = parts[i];
}
return components;
}
int main()
{
std::string sentence = "$GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76";
std::map<std::string, std::string> components = ParseNmeaString(sentence);
// Print the components to the console
for (auto const& component : components) {
std::cout << component.first << ": " << component.second << std::endl;
}
return 0;
}