views:

131

answers:

4

I have some MET data I want to validate which would look something like these:

char validBuffer[] = {"N51374114W1160437"};
char invalidBuffer[] = {"bad data\n"};
char emptyBuffer[] = {""};

I've tried a simple sscanf, but that failed:

int _tmain(int argc, _TCHAR* argv[])
{

    char validBuffer[] = {"N51374114W1160437"};
    char invalidBuffer[] = {"bad data\n"};
    char emptyBuffer[] = {""};

    char ns = ' ';
    char ew = ' ';
    int northing = -999;
    int easting = -999;

    int paramsConverted = sscanf_s(validBuffer, "%c%d%c%d", &ns, &northing, &ew, &easting);
    printf("Converted \"%s\"; Found %d params [%c,%d,%c,%d]\n", validBuffer, paramsConverted, ns, northing, ew, easting);

    paramsConverted = sscanf_s(invalidBuffer, "%c%d%c%d", &ns, &northing, &ew, &easting);
    printf("Converted \"%s\"; Found %d params [%c,%d,%c,%d]\n", invalidBuffer, paramsConverted, ns, northing, ew, easting);

    paramsConverted = sscanf_s(validBuffer, "%c%d%c%d", &ns, &northing, &ew, &easting);
    printf("Converted \"%s\"; Found %d params [%c,%d,%c,%d]\n", emptyBuffer, paramsConverted, ns, northing, ew, easting);

    getchar();

    return 0;
}

gives me:

Converted "N51374114W1160437"; Found 2 params [N,-999,",-1024]
Converted "bad data
"; Found 1 params [b,-999,",-1024]
Converted ""; Found 2 params [N,-999,",-1024]

I'd rather not use any external libraries if at all possible, so is there a nice simple way of doing this without parsing it one character at a time?

+3  A: 

How about using Regular expressions from TR1?

Nikola Smiljanić
I was hoping for something which didn't involve adding additional libraries.
Jon Cage
This is a part of C++ language.
Nikola Smiljanić
I should read more before engaging my mouth :-)
Jon Cage
+2  A: 
char validBuffer[] = {"N51374114W1160437"};
    char invalidBuffer[] = {"bad data\n"};
    char emptyBuffer[] = {""};

    if(strlen(validBuffer)!=18)
    {
//Error not valid buffer
    }
    char ns = validBuffer[0];
    char ew = validBuffer[9];
    int N = atoi(&validBuffer[1]);
    int W = atoi(&validBuffer[10]);
    if(N==0 || W==0)
        //Error not valid buffer

Not simple not the best but better than nothing

zabulus
`atoi` returning `0` is ambiguous. Best to use `strtol` or similar.
ezpz
If you add that as a solution ezpz I'll accept it as an answer.
Jon Cage
+2  A: 

You could use std::stringstream and the stream operators...

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

bool parse(string & s) {
    stringstream ss(s);
    char n = 0, w = 0;
    int x = 0, y = 0;
    ss >> n;
    if (! ss.good ()) return false;
    ss >> x;
    if (! ss.good ()) return false;
    ss >> w;
    if (! ss.good ()) return false;
    ss >> y;
    if ( ss.bad ()) return false;

    cout << "Parsed {" << n << ", " << x << ", "
         << w << ", " << y << "}" << endl;
    return true;
}

int main ()
{

    string validBuffer = "N51374114W1160437";
    string invalidBuffer = "bad data\n";
    string emptyBuffer = "";

    if (! parse (validBuffer))
        cout << "Unable to parse: '" << validBuffer << "'" << endl;
    if (! parse (invalidBuffer))
        cout << "Unable to parse: '" << invalidBuffer << "'" << endl;
    if (! parse (emptyBuffer))
        cout << "Unable to parse: '" << emptyBuffer << "'" << endl;

    return 0;
}

My output using the above:

Parsed {N, 51374114, W, 1160437}
Unable to parse: 'bad data
'
Unable to parse: ''
ezpz
MSalters
+1: That looks like a neat solution. Thanks!
Jon Cage
+1  A: 
Corwin
I tried that but it doesn't appear to make any difference?
Jon Cage