troubleshooting Question

parse line function

Avatar of shepson990
shepson990 asked on
C
1 Comment1 Solution382 ViewsLast Modified:
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;
}
ASKER CERTIFIED SOLUTION
peetm

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros