0

I'm writing a program for an Arduino that takes information in a sort of NMEA format which is read from a .txt file stored in a List< String >. I need to strip out strings that begin with certain prefixes ($GPZDA, $GPGSA, $GPGSV) because these are useless to me and therefore I only need $GPRMC and $GPGGA which contains a basic time stamp and the location which is all I'm using anyway. I'm looking to use as little external libraries (SPRINT, BOOST) as possible as the DUE doesn't have a fantastic amount of space as-is.

All I really need is a method to remove lines from the LIST<STRING> that doesn't start with a specific prefix, Any ideas?

The method I'm currently using seems to have replaced the whole output with one specific string yet kept the file length/size the same (1676 and 2270, respectively), these outputs are achieved using two While statements that put the two input files into List<STRING>

Below is a small snipped from what I'm trying to use, which is supposed to sort the file into a correct order (Working, they are current ordered by their numerical value, which works well for the time which is the second field in the string) however ".unique();" appears to have taken each "Unique" value and replaced all the others with it so now I have a 1676 line list that basically goes 1,1,1,2,2,2,3,3,4... 1676 ???

    while (std::getline(GPS1,STRLINE1)){
        ListOne.push_back("GPS1: " + STRLINE1 + "\n");
        ListOne.sort();
        ListOne.unique();
        std::cout << ListOne.back() << std::endl;
        GPSO1 << ListOne.back();
    }

Thanks

10074405
  • 1
  • 3

1 Answers1

0

If I understand correctly and you want to have some sort of white list of prefixes. You could use remove_if to look for them, and use a small function to check whether one of the prefixes fits(using mismatch like here) for example:

#include <iostream>
#include <algorithm>
#include <string>
#include <list>
using namespace std;

int main() {
    list<string> l = {"aab", "aac", "abb", "123", "aaw", "wws"};
    list<string> whiteList = {"aa", "ab"};
    auto end = remove_if(l.begin(), l.end(), [&whiteList](string item)
        {
            for(auto &s : whiteList)
            {
                auto res = mismatch(s.begin(), s.end(), item.begin());
                if (res.first == s.end()){
                    return false; //found allowed prefix
                }
            }
            return true; 
        });
    for (auto it = l.begin(); it != end; ++it){
        cout<< *it << endl;
    }
    return 0;
}

(demo)

Community
  • 1
  • 1
Scis
  • 2,934
  • 3
  • 23
  • 37