Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <regex.h>
  5.  
  6. #define LEN 100
  7.  
  8. int getstr(char **s, FILE *f);
  9. int maxl = LEN;
  10.  
  11. typedef struct a{
  12.     char* time;
  13.     char* hostname;
  14.     char* app;
  15.     char* message;
  16. } info;
  17.  
  18. int main(){
  19.     const char pattern[] = "[a-zA-Z0-9_-]+ [0-9][0-9] ([0-9][0-9]:[0-9][0-9]:[0-9][0-9]) ([a-zA-Z0-9_-]+) ([a-zA-Z0-9_-]+)(\\[[0-9]+\\])?: (.+)";
  20.     regex_t expr_compiled;
  21.     regcomp(&expr_compiled, pattern, REG_EXTENDED);
  22.     regmatch_t groups[6];
  23.     char* string = (char*)calloc(maxl, sizeof(char));
  24.     info* l = (info*)calloc(1, sizeof(info));
  25.     unsigned int len = 0;
  26.  
  27.     FILE *in;
  28.     char name[] = "input.txt";
  29.     in = fopen(name, "r");
  30.  
  31.     while(getstr(&string, in)){
  32.         if (regexec(&expr_compiled, (const char *)string, 6, groups, 0) == 0){
  33.             len++;
  34.             l = (info*)realloc(l, sizeof(struct a) * (len+1));
  35.  
  36.             l[len].time = (char*)calloc(groups[1].rm_eo - groups[1].rm_so + 10, sizeof(char));
  37.             strncpy(l[len].time, &(string[groups[1].rm_so]), groups[1].rm_eo - groups[1].rm_so);
  38.  
  39.             l[len].hostname = (char*)calloc(groups[2].rm_eo - groups[2].rm_so + 10, sizeof(char));
  40.             strncpy(l[len].hostname, &(string[groups[2].rm_so]), groups[2].rm_eo - groups[2].rm_so);
  41.  
  42.             l[len].app = (char*)calloc(groups[3].rm_eo - groups[3].rm_so + 10, sizeof(char));
  43.             strncpy(l[len].app, &(string[groups[3].rm_so]), groups[3].rm_eo - groups[3].rm_so);
  44.  
  45.             l[len].message = (char*)calloc(groups[5].rm_eo - groups[5].rm_so + 10, sizeof(char));
  46.             strncpy(l[len].message, &(string[groups[5].rm_so]), groups[5].rm_eo - groups[5].rm_so);
  47.         }
  48.     }
  49.     for(int i = 1; i <= len; i++){
  50.         printf("Msg #%d\n", i);
  51.         printf(" time:\t%s\n", l[i].time);
  52.         printf(" host:\t%s\n", l[i].hostname);
  53.         printf(" app:\t%s\n", l[i].app);
  54.         printf(" msg:\t%s\n", l[i].message);
  55.     }
  56.  
  57.     fclose(in);
  58.     free(l);
  59.     free(string);
  60.     regfree(&expr_compiled);
  61.     return 0;
  62. }
  63.  
  64.  
  65. int getstr(char **s, FILE *f)
  66. {
  67.     int i=0;
  68.     char c;
  69.     memset((*s), '\0', sizeof(char)*maxl);
  70.     while(((c = fgetc(f)) != EOF) && (c != '\n')){
  71.         (*s)[i++] = c;
  72.         if(i>=maxl-1){
  73.             maxl += LEN;
  74.             (*s) = (char*)realloc((*s), sizeof(char) * maxl);
  75.         }
  76.     }
  77.     return c != EOF;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement