Link to home
Start Free TrialLog in
Avatar of shepson990
shepson990

asked on

parse line function

need to turn that code into a function. I dont really do any c programming, this is a one off application needed for work.

function would be called like from main like:

while ((row = mysql_fetch_row(res)) != NULL) {
    if ( strstr ( row[2], "inseaaCC" ) ) {
         output = parselinefunction(row[2]);                           //call the function here
    }
......

the fucntion would need to return a string which i would form using sprintf from the parsed line


//2010-02-18T18:05:06+11:00 ADMIN-PC MWFileMonitor - - - 15.02.2010 - 09:51:51:836 inscondkslCC;048928494;09:51:51;0;data_AutoA

/*

d[0] = "15.02.2010"
d[1] = "09:51:51"
d[2] = "inscondkslCC"       < not a set length
d[3] = "048928494"         < not a set length
d[4] = "09:51:51"
d[5] = "data_AutoA"        < not a set length

*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


typedef struct // can't stand stuff not being straight!
{
    char date                   [50];
    char time                   [50];
    char weirdThing             [50];
    char weirdNumber            [50];
    char anotherTime            [50];
    char funnyThingToBeIgnored  [50];
    char funnyDataThing         [50];

} record;



int main(void)
{
    // Example line.
    //
    char line[] = "2010-02-18T18:05:06+11:00 ADMIN-PC MWFileMonitor - - - 15.02.2010 - 09:51:51:836 inscondkslCC;048928494;09:51:51;0;data_AutoA";

    char * probe = NULL;

    char * startToken = "- - - ";  // Not interested in anything before this.

    int n = 0;

    static record r; // static for nulls.
    
    // We check for some sort of validity here - but not for long!
    //
    if ((probe = strstr(line, startToken)) != NULL)
    {
        // inc past the token to the meat.
        //
        probe += strlen(startToken);

        // the dates are always of xx-xx-xxxx length?
        //
        strncpy(r.date, probe, strlen("xx-xx-xxxx")); 

        // inc a bit
        //
        probe += strlen(r.date);
        //
        probe += strlen(" - ");

        // the time always in this format?
        //
        strncpy(r.time, probe, strlen("xx:xx:xx"));

        // there has to be a better way - but, hey ho...
        //
        while(*(probe++) != ' ')
            ; // null statement

        // now the strtok stuff
        //
        probe = strtok (probe,";");

        while (probe != NULL)
        {
            
            switch(n++)
            {
                case 0:

                    strcpy(r.weirdThing, probe);
                    
                case 1:

                    strcpy(r.weirdNumber, probe);
                    
                case 2:

                    strcpy(r.anotherTime, probe);

                case 3:

                    strcpy(r.funnyThingToBeIgnored, probe);

                case 4:

                    strcpy(r.funnyDataThing, probe);

            }
            
            probe = strtok (NULL, ";");
        }

    }

    // and the result is ...
    //
    puts("'r' contains ...");
    puts(r.date);
    puts(r.time);
    puts(r.weirdThing);
    puts(r.weirdNumber);
    puts(r.anotherTime);
    puts(r.funnyThingToBeIgnored);
    puts(r.funnyDataThing);

    return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial