Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs3-24sp
regexlib
Commits
b2916200
Commit
b2916200
authored
1 year ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
Initial commit
parents
master
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
grep.c
+66
-0
grep.c
regex.c
+75
-0
regex.c
regex.h
+13
-0
regex.h
with
154 additions
and
0 deletions
+154
-0
grep.c
0 → 100644
View file @
b2916200
#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
]);
}
}
This diff is collapsed.
Click to expand it.
regex.c
0 → 100644
View file @
b2916200
#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
);
}
This diff is collapsed.
Click to expand it.
regex.h
0 → 100644
View file @
b2916200
#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__
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help