Parser

Published on February 2017 | Categories: Documents | Downloads: 27 | Comments: 0 | Views: 256
of 7
Download PDF   Embed   Report

Comments

Content

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <dos.h> #define Bestand "C:\\Documents and Settings\\smessah\\Desktop\\Store.dat" #define NewBron "C:\\Documents and Settings\\smessah\\My Documents\\Tools\\Scan ningTool\\parsed.txt" //C:\Documents and Settings\smessah\Desktop\Store.dat char CurDir[2000]; //storing current directory GLOBAL char Dir[2000]; // original local to copy to global CurDir char *userplcGlb; // global pointer to keep position user char Separator;// global value to add to the printouts so the files are readable by the DB-import int PrintInt;// global bool value indicating if contents must be printed or not. FILE *fp; FILE *np; void arraytrans(char *src, char *dest, int frm, int t2);//prototype int parser(char lijn[]);//prototype void usertrans(char *src, char *dest, char *frm, int t2); //prototype void filetrans(char *src, char *dest, char *frm, int t2); //prototype long filesize(char *src, char *dest, char *frm);//Prototype void directotrans(char *src, char *dest, char *frm); // Prototype char *replace_str(char *str, char *orig, char *rep);// Prototype void replace(char * o_string, char * s_string, char * r_string); #define MAX_L 4096 int main(int argc, char *argv[]) { if (argc != 5) { printf("Error: Not the required amount of arguments."); printf("%i", argc); getchar(); return -1; } /*if (argc == 2) { fp = fopen(argv[1],"r"); np = fopen(NewBron, "w"); printf("Error: Only source %s", fp); getchar(); return -1; } if (argc == 3) { fp = fopen(argv[1], "r"); np = fopen(argv[2], "w"); printf("Error: No Separator %s %s", fp, np); getchar(); return -1; }*/ if (argc == 5) { fp = fopen(argv[1], "r"); np = fopen(argv[2], "w"); Separator = *argv[3]; PrintInt = atoi(argv[4]); printf("Both files & Separator %s %s %c\n", fp, np, Separator); } /*

if(fp!= NULL) { printf("opened file.\n"); } else{ printf("could not open file.\n");} if(np!= NULL) { printf("opened write file.\n"); } else{ printf("could not open write file.\n"); return -1; } getchar(); printf("Give me the separator ';' or '|' : "); scanf("%c",&Separator);*/ if(PrintInt == 1){printf("%c", Separator); getchar(); } fprintf(np,"Opened%c", Separator); fprintf(np,"Day%c", Separator); fprintf(np,"Month%c", Separator); fprintf(np,"Year%c", Separator); fprintf(np,"TimeStampSort%c", Separator); fprintf(np,"Hour%c", Separator); fprintf(np,"Minute%c", Separator); fprintf(np,"Size_Bytes%c", Separator); fprintf(np,"Post\\UserName%c", Separator); fprintf(np,"ParentFolder%c", Separator); fprintf(np,"Filename%c", Separator); fprintf(np,"Full_URL\n"); if(PrintInt == 1){ //Screen Refresh parameter test printf("Opened%c", Separator); printf("Day%c", Separator); printf("Month%c", Separator); printf("Year%c", Separator); printf("TimeStampSort%c", Separator); printf("Hour%c", Separator); printf("Minute%c", Separator); printf("Size_KB%c", Separator); printf("Post\\UserName%c", Separator); printf("ParentFolder%c", Separator); printf("Filename%c", Separator); printf("Full_URL"); getchar(); // No screen refresh means no validation } char line[300]; while(fgets(line,sizeof(line),fp)) {//loop through file parser(line); // printf( "%s \n" , line); }//end off while

fclose(fp); fclose(np); if(PrintInt == 1){getchar();} return 0; } int parser(char lijn[2000]) { char char char char char char char char char char char long CurDate[19]; // date from file Date[5]; // modified Date month[3]; //modified Date day[3]; //modified Day hour[3];// minute[3];// dycopy[3]; //copy day file[200]; //filename *userplc; // position of user indicated by 'POST\\' preset user[13];// size[20];//to get fieldsize flsize; // long integer for filesize

if (strstr(lijn, "Directory of")> 0) { char *carriageplc = strstr(lijn, "\n"); //printf("%d %d", carriageplc, lijn); directotrans(lijn, CurDir, carriageplc); strncpy(Dir,CurDir,sizeof(Dir)-2); //printf("Found directory of:\n %s \n", CurDir); return 0; } if (strstr(lijn, "<DIR>")> 0) { //printf("Found DIR. \n"); return 0; } arraytrans(lijn, CurDir, 14, 200); if ((lijn[2] == '/') && (lijn[5]== '/') && (lijn[14] == ':')) { arraytrans(lijn, CurDate, 0, sizeof(CurDate)-1); userplc = strstr(lijn, "POST\\"); if(userplc)userplcGlb = userplc; else{userplc = userplcGlb;} usertrans(lijn, user, userplc, sizeof(user)); arraytrans(lijn, day, 0, sizeof(day)); //printf("%s;", day); strcpy(day, dycopy); arraytrans(lijn, month, 3, sizeof(month)); arraytrans(lijn, Date, 6, sizeof(Date)); arraytrans(lijn, hour, 12, sizeof(hour)); arraytrans(lijn, minute, 15, sizeof(minute)); filetrans(lijn, file, userplc, sizeof(file)); flsize = filesize(lijn, size , userplc); //printf("Found Date. %s:00; %s; %s; %s; %ld; %s; %s; %s

//

; %s\\%s \n", CurDate, Date, day, month, flsize, user, Dir, file, Dir, file ); fprintf(np,"%s:00%c", CurDate, Separator); fprintf(np,"%s%c", day, Separator); fprintf(np,"%s%c", month, Separator); fprintf(np,"%s%c", Date, Separator); fprintf(np,"%s%s%s%s%s%c", Date, month, day, hour, minut e, Separator); fprintf(np,"%s%c", hour, Separator); fprintf(np,"%s%c", minute, Separator); fprintf(np,"%ld%c", flsize, Separator); fprintf(np,"%s%c", user, Separator); fprintf(np,"%s%c", Dir, Separator); fprintf(np,"%s%c", file, Separator); fprintf(np,"%s\\%s\n", Dir, file, Separator); if(PrintInt == 1){ //Screen Refresh parameter test printf("%s:00%c", CurDate, Separator); printf("%s%c", day, Separator); printf("%s%c", month, Separator); printf("%s%c", Date, Separator); printf("%s%s%s%s%s%c", Date, month, day, hour, minute, S eparator); printf("%s%c", hour, Separator); printf("%s%c", minute, Separator); printf("%ld%c", flsize, Separator); printf("%s%c", user, Separator); printf("%s%c", Dir, Separator); printf("%s%c", file, Separator); printf("%s\\%s", Dir, file, Separator); } // getchar(); }

} void arraytrans(char *src, char *dest, int frm, int t2) { int i; int j =0; strncpy(dest, src + frm, t2-1); dest[t2-1] = '\0'; } void usertrans(char *src, char *dest, char *frm, int t2) { int i; int j = 0; int k = sizeof(char); //printf("%d", k);

strncpy(dest, frm, t2-1); dest[t2-1] = '\0'; } void filetrans(char *src, char *dest, char *frm, int t2) { int k = sizeof(char); //printf("%i", k); strncpy(dest, frm+23,23+ t2-1); dest[23+t2-1] = '\0'; int i = 0; if( strrchr(dest, '\n') != NULL) { char *plc = strrchr(dest, '\n'); //printf("\n\n\n recopying\n %p\n", plc); strncpy(dest, plc - (plc-dest) , (plc-dest)); dest[(plc-dest)] = '\0'; } } void directotrans(char *src, char *dest, char *frm) { strncpy(dest, src+14, 4000); dest[4000] = '\0'; int i = 0; for( i = 0;i < 4000; i++) {if( strchr(dest, '\n') != NULL) {char *plc = strchr(dest, '\n'); //printf("recopying"); strncpy(dest, plc - (plc-dest) , (plc-dest)); dest[(plc-dest)] = '\0'; break; } } } long filesize(char *src, char *dest, char *frm) { int i; int j = 0; int k = sizeof(char); //printf("%i", k); strncpy(dest, frm-18, 18); dest[18] = '\0'; replace(dest, ".", ""); //replace_str(dest, ".", ""); //replace_str(dest, ".", ""); return atol(dest); } char *replace_str(char *str, char *orig, char *rep) { static char buffer[4096]; char *p;

if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'? return str; strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$ buffer[p-str] = '\0'; sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig)); return buffer; } /** * The replace function * * Searches all of the occurrences using recursion * and replaces with the given string * @param char * o_string The original string * @param char * s_string The string to search for * @param char * r_string The replace string * @return void The o_string passed is modified */ void replace(char * o_string, char * s_string, char * r_string) { //a buffer variable to do all replace things char buffer[MAX_L]; //to store the pointer returned from strstr char * ch; //first exit condition if(!(ch = strstr(o_string, s_string))) return; //copy all the content to buffer before the first occurrence of the search string strncpy(buffer, o_string, ch-o_string); //prepare the buffer for appending by adding a null to the end of it buffer[ch-o_string] = 0; //append using sprintf function sprintf(buffer+(ch - o_string), "%s%s", r_string, ch + strlen(s_string)); //empty o_string for copying o_string[0] = 0; strcpy(o_string, buffer); //pass recursively to replace other occurrences return replace(o_string, s_string, r_string); } /* char[] clear(char passstr[200], int arg) { int i;//forloop int t; //count spaces t = 0; char res[200]; for(int i = 0; i < sizeof(passstr); i++) { if(passstr[i]== " ") { t++; }

else { res[i - t] = passtr[i];}//endof if-else } } */

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close