-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ashaikeus/develop
Develop
- Loading branch information
Showing
34 changed files
with
838 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
all: s21_cat s21_grep | ||
|
||
s21_cat: | ||
gcc -Wall -Werror -Wextra -std=c11 ./cat/s21_cat.c -o ./cat/s21_cat | ||
|
||
s21_grep: | ||
gcc -Wall -Werror -Wextra -std=c11 ./grep/s21_grep.c -o ./grep/s21_grep | ||
|
||
clean: | ||
rm ./cat/s21_cat | ||
rm ./grep/s21_grep | ||
|
||
rebuild: clean all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
all: s21_cat | ||
|
||
s21_cat: | ||
gcc -Wall -Werror -Wextra -std=c11 ./s21_cat.c -o s21_cat | ||
|
||
clean: | ||
rm ./s21_cat | ||
|
||
rebuild: clean all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#define MAXLINES 1024 | ||
#define MAXLEN 1024 | ||
|
||
void cat(char **param, char *filename, int o_c); | ||
int vflag(char ch); | ||
int eflag(int index, int pos, int npos); | ||
int arrFind(char *needle, char **haystack, int c); | ||
int isSpaceStr(char *string); | ||
|
||
// -b / --number-nonblank: number non-empty lines | ||
// -e (+ -v) / -E: end-of-line as $ | ||
// -n / --number: number all output lines | ||
// -s / --squeeze-blank: squeeze adjacent blank lines | ||
// -t (+ -v) / -T: tabs as ^I | ||
|
||
int main(int argc, char *argv[]) { | ||
if (argc < 2) { | ||
printf("Invalid param count"); | ||
} else { | ||
char *options[argc - 2]; | ||
int o_c = 0; | ||
char *filenames[argc - 2]; | ||
int f_c = 0; | ||
for (int i = 1; i < argc; i++) { | ||
if (argv[i][0] == '-') { | ||
options[o_c++] = argv[i]; | ||
} else { | ||
filenames[f_c++] = argv[i]; | ||
} | ||
} | ||
|
||
for (int i = 0; i < f_c; i++) { | ||
cat(options, filenames[i], o_c); | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
void cat(char **param, char *filename, int o_c) { | ||
FILE *fptr; | ||
if (!(fptr = fopen(filename, "r"))) { | ||
printf("s21_cat: %s: No such file or directory", filename); | ||
return; | ||
} | ||
|
||
int linec = 0; | ||
char lines[MAXLINES][MAXLEN] = {0}; | ||
int positions[MAXLINES] = {-2}; // "natural" \0 positions | ||
int npos[MAXLINES] = {-2}; // "natural" \n positions | ||
int e = (arrFind("-e", param, o_c) || (arrFind("-E", param, o_c))); | ||
|
||
int ch = 33, i = 0; | ||
while (ch != EOF) { | ||
while ((ch = fgetc(fptr)) != '\0' && ch != EOF && ch != '\n') { | ||
lines[linec][i++] = ch; | ||
} | ||
if (ch == 0) { | ||
lines[linec][i] = '\0'; | ||
positions[linec] = i; | ||
} else if (ch == '\n') { | ||
lines[linec][i] = '\n'; | ||
npos[linec++] = i; | ||
i = 0; | ||
} | ||
} | ||
|
||
int nonEmptyC = 0; | ||
for (int i = 0; i < linec + 1; i++) { | ||
if (lines[i][0] == '\0' && i == linec) break; | ||
if ((arrFind("-b", param, o_c)) || | ||
(arrFind("--number-nonblank", param, o_c))) { | ||
if (isSpaceStr(lines[i])) { | ||
printf(" %d\t", ++nonEmptyC); | ||
} | ||
} else if ((arrFind("-n", param, o_c)) || | ||
(arrFind("--number", param, o_c))) { | ||
printf(" %d\t", i + 1); | ||
} | ||
if ((arrFind("-s", param, o_c)) || | ||
(arrFind("--squeeze-blank", param, o_c))) { | ||
if (i != 0) { | ||
if (!isSpaceStr(lines[i]) && !isSpaceStr(lines[i - 1])) continue; | ||
} | ||
} | ||
|
||
for (int ch = 0; ch < (int)strlen(lines[i]); ch++) { | ||
if ((arrFind("-t", param, o_c)) || (arrFind("-T", param, o_c))) { | ||
if (lines[i][ch] == '\t') { | ||
printf("^I"); | ||
continue; | ||
} else if (arrFind("-t", param, o_c)) { | ||
if (vflag(lines[i][ch])) { | ||
if (eflag(ch + 1, positions[i], npos[i])) printf("$"); | ||
continue; | ||
} | ||
} | ||
} else if ((arrFind("-e", param, o_c)) || (arrFind("-v", param, o_c))) { | ||
if (vflag(lines[i][ch])) continue; | ||
} | ||
if (e && lines[i][ch] == '\n') printf("$"); | ||
printf("%c", lines[i][ch]); | ||
if ((arrFind("-e", param, o_c)) || (arrFind("-v", param, o_c)) || | ||
(arrFind("-t", param, o_c))) { | ||
if (ch + 1 == positions[i] && linec - 1 != i) { | ||
printf("^@"); | ||
} | ||
} | ||
} | ||
} | ||
fclose(fptr); | ||
} | ||
|
||
int vflag(char ch) { | ||
int res = 0; | ||
if (ch == 127) { | ||
printf("^?"); | ||
res++; | ||
} else if ((ch > 0 && ch < 9) || (ch >= 11 && ch <= 31)) { | ||
printf("^%c", ch + 64); | ||
res++; | ||
} else if (ch < 0) { | ||
if ((ch + 128 + 64) < (96)) | ||
printf("M-^%c", ch + 128 + 64); | ||
else | ||
printf("?"); | ||
res++; | ||
} | ||
return res; | ||
} | ||
|
||
int eflag(int index, int pos, int npos) { | ||
int e = 0; | ||
if (index == npos || index == pos) e = 1; | ||
return e; | ||
} | ||
|
||
int arrFind(char *needle, char **haystack, int c) { | ||
int result = 0; | ||
for (int i = 0; i < c; i++) { | ||
if (strcmp(haystack[i], needle) == 0) { | ||
result = 1; | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
int isSpaceStr(char *string) { | ||
int result = 0; | ||
for (int i = 0; i < (int)strlen(string); i++) { | ||
if (string[i] != '\n') { | ||
result = 1; | ||
break; | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/bash | ||
make all | ||
|
||
echo " " | ||
echo "Response to wrong flag:" | ||
./s21_cat -q $test_file | ||
echo " " | ||
echo "Response to wrong file:" | ||
./s21_cat -$flag nofile.txt | ||
|
||
for flag in "-b" "-e" "-n" "-s" "-t" "-v" | ||
do | ||
test_number=1 | ||
echo " " | ||
echo "Flags: $flag:" | ||
for test_file in "tests_cat/test_text_1.txt" "tests_cat/test_text_2.txt" "./tests_cat/test_text_3.txt" "./tests_cat/test_text_4.txt" "./tests_cat/test_text_5.1.txt ./tests_cat/test_text_5.2.txt" "tests_grep/test_text_0.txt" "tests_grep/test_text_1.txt" "./tests_grep/test_text_2.txt" "./tests_grep/test_text_3.txt" "./tests_grep/test_text_4.txt" "./tests_grep/test_text_5.txt" "./tests_grep/test_text_6.txt" "./tests_grep/test_text_7.txt" "./tests_grep/test_text_ptrn.txt" "./tests_grep/test_text_6.txt ./tests_grep/test_text_6.txt" | ||
do | ||
./s21_cat $flag $test_file > 1.txt | ||
cat $flag $test_file > 2.txt | ||
if ! cmp -s 1.txt 2.txt ; then | ||
echo "Test $test_number: Fail" | ||
else | ||
echo "Test $test_number: Success" | ||
fi | ||
rm 1.txt 2.txt | ||
test_number=$(( $test_number + 1 )) | ||
done | ||
done | ||
echo " " | ||
echo " " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
make all | ||
|
||
for flag in "-b" "-e" "-n" "-s" "-t" "-E" "-T" | ||
do | ||
echo " " | ||
test_number=1 | ||
for test_file in "tests_grep/test_text_0.txt" "./tests_grep/test_text_1.txt" "./tests_grep/test_text_2.txt" "./tests_grep/test_text_3.txt" "./tests_grep/test_text_4.txt" "./tests_grep/test_text_5.txt" "./tests_grep/test_text_6.txt" "./tests_grep/test_text_7.txt" "./tests_grep/test_text_ptrn.txt" "tests_cat/test_text_1.txt" "tests_cat/test_text_2.txt" "./tests_cat/test_text_3.txt" "./tests_cat/test_text_4.txt" "./tests_cat/test_text_5.1.txt ./tests_cat/test_text_5.2.txt" | ||
do | ||
echo "Flag $flag, test $test_number:" | ||
valgrind --log-file="1.txt" ./s21_cat $flag $test_file > 1.txt | ||
tail -n4 ./1.txt | ||
echo " " | ||
rm 1.txt | ||
test_number=$(( $test_number + 1 )) | ||
done | ||
done | ||
echo " " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
str ! | ||
|
||
|
||
|
||
|
||
end. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
str ! | ||
|
||
|
||
|
||
|
||
end. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
str ! | ||
|
||
() | ||
() | ||
() | ||
end. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
str ! | ||
|
||
|
||
|
||
|
||
|
||
|
||
end. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Hello! | ||
Welcome to School 21. | ||
Here you can meet many intelligent and friendly people. | ||
Are you ready to start your education? | ||
|
||
Ten ... | ||
Nine ... | ||
Eight ... | ||
Seven ... | ||
Six ... | ||
|
||
|
||
|
||
|
||
Five... | ||
Four... | ||
Three... | ||
Two... | ||
One... | ||
|
||
|
||
MEOW! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Hi! | ||
|
||
It's a second text. | ||
|
||
One | ||
Two | ||
Three | ||
|
||
Meow | ||
|
||
|
||
MEOW! | ||
|
||
MeOw... | ||
|
||
|
||
Hwa hwa hwa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
srsblmrtb | ||
brrktbhm | ||
|
||
byh;r | ||
|
||
|
||
|
||
tgvrst | ||
|
||
Rtgr | ||
rag | ||
|
||
|
||
fy | ||
|
||
|
||
|
||
S | ||
S |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2022 [email protected] | ||
#include "./s21_string.h" | ||
#include <stdlib.h> | ||
|
||
// Ф-я определения длины строки | ||
int s21_strlen(const char *str) { | ||
int size = 0; | ||
while (str[size] != '\0') | ||
size++; | ||
return size; | ||
} | ||
// Ф-я сравнения строк | ||
int s21_strcmp(const char *str1, const char *str2) { | ||
int cmp = 0; | ||
int s1 = s21_strlen(str1); | ||
int s2 = s21_strlen(str2); | ||
cmp = (s1 == s2 ? 0 : (s1 > s2 ? 1 : (-1))); | ||
return cmp; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
^int | ||
|
||
|
||
)$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
int int int | ||
int int | ||
int |
Oops, something went wrong.