Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <regex.h>
- typedef struct
- {
- char *Time;
- char *Name;
- char *App;
- char *Text;
- } ImpLog;
- void PrintSysLog(ImpLog **SysLog, int __sys_count)
- {
- for (int i = 0; i < __sys_count; ++i)
- {
- printf("Log time: %s | Username: %s | App: %s | Message: %s\n",
- SysLog[i]->Time, SysLog[i]->Name, SysLog[i]->App, SysLog[i]->Text);
- }
- }
- char* StrCatBetweenPtr(const int __first, const int __second, void* __str)
- {
- void* _ptr1 = __str + __first;
- void* _ptr2 = __str + __second;
- char* Sent = malloc(sizeof(char) * __second - __first);
- for (int i = 0; _ptr1 != _ptr2; Sent[i++] = *(char *)_ptr1++)
- ;
- Sent[__second - __first] = '\0';
- return Sent;
- }
- void DestructorSystemLog(ImpLog ***SysLog, int __count)
- {
- for (int i = 0; i < __count; ++i)
- {
- free((*SysLog)[i]->Time);
- free((*SysLog)[i]->Name);
- free((*SysLog)[i]->App);
- free((*SysLog)[i]->Text);
- free((*SysLog)[i]);
- (*SysLog)[i] = (*SysLog)[i]->Time = (*SysLog)[i]->Name = (*SysLog)[i]->App = (*SysLog)[i]->Text = NULL;
- }
- free(*SysLog);
- *SysLog = NULL;
- }
- ImpLog** InputInSysLog(int *__struct_count, const size_t _maxgroups, regex_t *RegExpr, regmatch_t *RegArr)
- {
- const size_t _size = 200;
- char *Sent = malloc(sizeof(char) * _size);
- ImpLog **SysLog = malloc(sizeof(ImpLog *) * _maxgroups);
- int LogCount = 0;
- while (!strstr(fgets(Sent, _size, stdin), "Fin."))
- {
- if (!regexec(RegExpr, Sent, _maxgroups, RegArr, 0))
- {
- SysLog[LogCount] = malloc(sizeof(ImpLog));
- SysLog[LogCount]->Time = StrCatBetweenPtr(RegArr[1].rm_so, RegArr[1].rm_eo, Sent);
- SysLog[LogCount]->Name = StrCatBetweenPtr(RegArr[2].rm_so, RegArr[2].rm_eo, Sent);
- SysLog[LogCount]->App = StrCatBetweenPtr(RegArr[3].rm_so, RegArr[3].rm_eo, Sent);
- SysLog[LogCount++]->Text = StrCatBetweenPtr(RegArr[5].rm_so, RegArr[5].rm_eo, Sent);
- }
- }
- *__struct_count = LogCount;
- free(Sent);
- return SysLog;
- }
- int main()
- {
- 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]+\])?)\: (.*)";
- const size_t Maxgroups = 6;
- int LogCount = 0;
- regex_t RegExpr;
- regmatch_t RegArr[6];
- if (regcomp(&RegExpr, RegString, REG_EXTENDED))
- {
- perror("Error\n");
- return -1;
- }
- ImpLog **SysLog = InputInSysLog(&LogCount, Maxgroups, &RegExpr, &RegArr);
- PrintSysLog(SysLog, LogCount);
- DestructorSystemLog(&SysLog, LogCount);
- regfree(&RegExpr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement