Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <regex.h>
  5.  
  6. typedef struct
  7. {
  8. char *Time;
  9. char *Name;
  10. char *App;
  11. char *Text;
  12. } ImpLog;
  13.  
  14. void PrintSysLog(ImpLog **SysLog, int __sys_count)
  15. {
  16. for (int i = 0; i < __sys_count; ++i)
  17. {
  18. printf("Log time: %s | Username: %s | App: %s | Message: %s\n",
  19. SysLog[i]->Time, SysLog[i]->Name, SysLog[i]->App, SysLog[i]->Text);
  20. }
  21. }
  22.  
  23. char* StrCatBetweenPtr(const int __first, const int __second, void* __str)
  24. {
  25. void* _ptr1 = __str + __first;
  26. void* _ptr2 = __str + __second;
  27.  
  28. char* Sent = malloc(sizeof(char) * __second - __first);
  29.  
  30. for (int i = 0; _ptr1 != _ptr2; Sent[i++] = *(char *)_ptr1++)
  31. ;
  32.  
  33. Sent[__second - __first] = '\0';
  34.  
  35. return Sent;
  36. }
  37.  
  38. void DestructorSystemLog(ImpLog ***SysLog, int __count)
  39. {
  40. for (int i = 0; i < __count; ++i)
  41. {
  42. free((*SysLog)[i]->Time);
  43. free((*SysLog)[i]->Name);
  44. free((*SysLog)[i]->App);
  45. free((*SysLog)[i]->Text);
  46. free((*SysLog)[i]);
  47. (*SysLog)[i] = (*SysLog)[i]->Time = (*SysLog)[i]->Name = (*SysLog)[i]->App = (*SysLog)[i]->Text = NULL;
  48. }
  49.  
  50. free(*SysLog);
  51. *SysLog = NULL;
  52. }
  53.  
  54. ImpLog** InputInSysLog(int *__struct_count, const size_t _maxgroups, regex_t *RegExpr, regmatch_t *RegArr)
  55. {
  56. const size_t _size = 200;
  57. char *Sent = malloc(sizeof(char) * _size);
  58.  
  59. ImpLog **SysLog = malloc(sizeof(ImpLog *) * _maxgroups);
  60.  
  61. int LogCount = 0;
  62. while (!strstr(fgets(Sent, _size, stdin), "Fin."))
  63. {
  64.  
  65. if (!regexec(RegExpr, Sent, _maxgroups, RegArr, 0))
  66. {
  67. SysLog[LogCount] = malloc(sizeof(ImpLog));
  68. SysLog[LogCount]->Time = StrCatBetweenPtr(RegArr[1].rm_so, RegArr[1].rm_eo, Sent);
  69. SysLog[LogCount]->Name = StrCatBetweenPtr(RegArr[2].rm_so, RegArr[2].rm_eo, Sent);
  70. SysLog[LogCount]->App = StrCatBetweenPtr(RegArr[3].rm_so, RegArr[3].rm_eo, Sent);
  71. SysLog[LogCount++]->Text = StrCatBetweenPtr(RegArr[5].rm_so, RegArr[5].rm_eo, Sent);
  72. }
  73. }
  74.  
  75. *__struct_count = LogCount;
  76.  
  77. free(Sent);
  78. return SysLog;
  79. }
  80.  
  81. int main()
  82. {
  83. const char* RegString = "[A-Za-z]{3} [0-9]{2} ([0-9]{2}:[0-9]{2}:[0-9]{2}) ([A-Za-z0-9\-]+) ([A-Za-z0-9_]+(\[[0-9]+\])?)\: (.*)";
  84. const size_t Maxgroups = 6;
  85.  
  86. int LogCount = 0;
  87. regex_t RegExpr;
  88. regmatch_t RegArr[6];
  89.  
  90. if (regcomp(&RegExpr, RegString, REG_EXTENDED))
  91. {
  92. perror("Error\n");
  93. return -1;
  94. }
  95.  
  96. ImpLog **SysLog = InputInSysLog(&LogCount, Maxgroups, &RegExpr, &RegArr);
  97. PrintSysLog(SysLog, LogCount);
  98.  
  99. DestructorSystemLog(&SysLog, LogCount);
  100. regfree(&RegExpr);
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement