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

C

Avatar of undefined
Last Comment
peetm

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
peetm

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes