Commit b2916200 authored by Adam Blank's avatar Adam Blank
Browse files

Initial commit

parents
No related merge requests found
Showing with 154 additions and 0 deletions
+154 -0
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include "regex.h"
const char *ANSI_RESET_COLOR = "\033[39m\033[49m";
const char *ANSI_COLOR_PATTERN = "\033[38;2;%d;%d;%dm";
void search_file(char *regex, char *filename) {
FILE* f = fopen(filename, "r");
if (!f) {
fprintf(stderr, "File %s does not exist!\n", filename);
exit(2);
}
struct stat fstats;
int result = fstat(fileno(f), &fstats);
if (result) {
fprintf(stderr, "Could not find the size of the file %s\n", filename);
exit(2);
}
size_t file_length = fstats.st_size;
char *contents = malloc((file_length + 1) * sizeof(char));
fread(contents, file_length, sizeof(char), f);
contents[file_length] = '\0';
char *brkt;
for (char *line = strtok_r(contents, "\n", &brkt); line; line = strtok_r(NULL, "\n", &brkt)) {
if (!line) {
puts("\n");
}
else {
range_t *result = regexp_match(regex, line);
if (result) {
char *to_print = calloc(strlen(line) + 1 + (strlen(ANSI_COLOR_PATTERN) + 3) + strlen(ANSI_RESET_COLOR), sizeof(char));
strncpy(to_print, line, result->start);
snprintf(to_print + result->start, strlen(ANSI_COLOR_PATTERN) + 3, ANSI_COLOR_PATTERN, 0, 128, 0);
strncat(to_print, line + result->start, result->end - result->start);
strcat(to_print, ANSI_RESET_COLOR);
strcat(to_print, line + result->end);
printf("%s\n", to_print);
}
free(result);
}
}
free(contents);
fclose(f);
}
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "USAGE: %s <regexp> filepaths...\n", argv[0]);
exit(1);
}
char *regex = argv[1];
for (size_t i = 2; i < argc; i++) {
search_file(regex, argv[i]);
}
}
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <stdio.h>
#include "regex.h"
/*
To implement:
- ^
- $
- [...]
- [^...]
- *
*/
range_t*_regexp_match(char *whole_regex, char *regex, char *line, bool caret, bool dollar, size_t index, size_t start, size_t len) {
if ((regex[0] == '\0' && line[0] == '\0') || (regex[0] == '\0' && !dollar)) {
range_t *result = malloc(sizeof(range_t));
*result = (range_t){.start=start, .end=len + start};
return result;
}
else if (regex[0] == '\0') {
return NULL;
}
else if (regex[1] == '*') {
if (regex[0] == line[0] && line[0] != '\0') {
range_t *end = _regexp_match(whole_regex, regex + 2, line + 1, caret, dollar, index + 1, start, len + 1);
if (end) {
return end;
}
range_t *include = _regexp_match(whole_regex, regex, line + 1, caret, dollar, index + 1, start, len + 1);
if (include) {
return include;
}
}
if (!caret && line[0] != '\0') {
range_t *restart = _regexp_match(whole_regex, whole_regex, line + 1, caret, dollar, index + 1, index + 1, 0);
if (restart) {
return restart;
}
}
range_t *skip = _regexp_match(whole_regex, regex + 2, line, caret, dollar, index, start, len);
if (skip) {
return skip;
}
return NULL;
}
else if (regex[0] == line[0]) {
return _regexp_match(whole_regex, regex + 1, line + 1, caret, dollar, index + 1, start, len + 1);
}
else if (!caret && line[0] != '\0') {
return _regexp_match(whole_regex, whole_regex, line + 1, caret, dollar, index + 1, index + 1, 0);
}
else {
return NULL;
}
}
range_t *regexp_match(char *regex, char *line) {
bool caret = false;
if (regex[0] == '^') {
caret = true;
regex += 1;
}
bool dollar = false;
if (regex[strlen(regex) - 1] == '$') {
dollar = true;
regex[strlen(regex) - 1] = '\0';
}
assert(line[0] != '*');
return _regexp_match(regex, regex, line, caret, dollar, 0, 0, 0);
}
#ifndef __REGEX_H__
#define __REGEX_H__
#include <stdint.h>
typedef struct range {
size_t start;
size_t end;
} range_t;
range_t *regexp_match(char *regex, char *line);
#endif // #ifndef __REGEX_H__
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment