ok. thats what we've(Cory, Taylor and me) done on irc. thanks for debugging
guys. hope it works. for any possible bugs, just respond to this thread

usage: on windows, drop the file over exe. it'll create a file named
"<original_name>.processed". on linux, i dont know. it reads the input
filename as argv[1] and appends ".processed". command-line should work.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dir.h>

typedef  unsigned int uint;

struct logdata{
    unsigned int address;
    unsigned int data[4];
    void *next;
};

class memoryloglist{
    public:
    logdata *loglist;
    int len;
    logdata *head;

    void addMember(uint address,uint d1, uint d2, uint d3, uint d4)
    {
        logdata *tempData=(logdata *)calloc(1,sizeof(logdata));
        tempData->address=address;
        tempData->data[0]=d1;
        tempData->data[1]=d2;
        tempData->data[2]=d3;
        tempData->data[3]=d4;
        tempData->next=NULL;
        if(loglist)
        {
            head->next=tempData;
            head=tempData;
        }
        else
        {
            loglist=tempData;
            head=tempData;
        }
        len++;
    }

    memoryloglist()
    {
        loglist=NULL;
        len=0;
        head=NULL;
    }

    void sort()
    {
        // bubble-sort, we dont have to sort huge lists, do we?
        if(!loglist) return;

        bool reSort=true;
        puts("sorting buddy");
        while(reSort)
        {

            logdata *This=loglist;
            reSort=false;
            while(This->next)
            {
                //puts("first iteration");
                if((This->address) > (((logdata *)(This->next))->address))
                {
                    //puts("gotcha!");
                    logdata temp;
                    temp=*((logdata *)This);
                    This->address=((logdata *)(This->next))->address;
                    This->data[0]=((logdata *)(This->next))->data[0];
                    This->data[1]=((logdata *)(This->next))->data[1];
                    This->data[2]=((logdata *)(This->next))->data[2];
                    This->data[3]=((logdata *)(This->next))->data[3];
                    This=(logdata *)This->next;
                    //printf("%0x\n",This);
                    This->address=temp.address;
                    This->data[0]=temp.data[0];
                    This->data[1]=temp.data[1];
                    This->data[2]=temp.data[2];
                    This->data[3]=temp.data[3];
                    reSort=true;
                }
                else
                    This=(logdata *)This->next;
            }
            //puts("sorting");
        }
        puts("i'm done sorting");
    }


};

int processFile(FILE *inFile, memoryloglist &list)
{
    char str[1024];
    char *t;
    int count=0;

    if(!inFile) // error check
        return -1;

    t=fgets(str,1024,inFile);



    while(t==str)
    {
        if(str[0]=='0' && str[1]=='x')
        {
            uint address,data[4];
            if(sscanf(str,"0x%x: %x %x %x 
%x",&address,data,data+1,data+2,data+3)==5)
            // ok, we've read 5 digits
            {
                list.addMember(address,data[0],data[1],data[2],data[3]);
                count++;
            }
            else
            {
                printf("are you trying to trick me?\n\"%s\"\nERROR!\n",str);
            }

        }
        t=fgets(str,1024,inFile);
    }
    return count;
}

int putSomeOutput(FILE *outFile,memoryloglist &list)
{
    int count=0;
    struct logdata *head=list.loglist;
    while(head)
    {
        fprintf(outFile,"0x%08x: %08x %08x %08x %08x",head->address,
                                                        head->data[0],
                                                        head->data[1],
                                                        head->data[2],
                                                        head->data[3]);

        head=(logdata *)head->next;
        if(head) fprintf(outFile,"\n");
        count++;
    }
    return count;
}

int main(int argc, char **argv)
{
    if(argc!=2)
    {
        printf("write filename on command-line please :)\n");
        return 0;
    }

    char tempFileName[2048]; //buffer for output name

    strrchr(argv[0],'\\')[0]=0; // find applications launching directory

    chdir(argv[0]); // go to this directory

    strcpy(tempFileName,argv[1]);          // prepare output name
    strcat(tempFileName,".processed");

    FILE *inFile=fopen(argv[1],"r");
    if(inFile)
    {
        puts("opened input");
        memoryloglist list;
        int count=0;
        count=processFile(inFile,list);
        puts("read file");
        //putSomeOutput(stdout,list);
        //puts("1st dump's over");
        list.sort();
        puts("sorting's over");


        FILE *outFile=fopen(tempFileName,"w");
        if(outFile)
        {
            int count2=0;
            count2=putSomeOutput(outFile,list);
            if(count!=count2)
            {
                printf("wow, thats strange. we've read %d values and printed 
%d.\n",count,count2);
            }
            fclose(outFile);
        }
        fclose(inFile);
    }
    else
    {
        printf("can't open input file \"%s\"\nsorry dude :(\n");
        return 0;
    }

    return 0;

}
_______________________________________________
Linux4nano-dev mailing list
[email protected]
https://mail.gna.org/listinfo/linux4nano-dev
http://www.linux4nano.org

Reply via email to