Skip to content

Commit

Permalink
Merge pull request #1 from ashaikeus/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ashaikeus committed Feb 24, 2024
2 parents e235979 + b3ad07a commit 914c0e7
Show file tree
Hide file tree
Showing 34 changed files with 838 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Makefile
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
9 changes: 9 additions & 0 deletions src/cat/Makefile
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
159 changes: 159 additions & 0 deletions src/cat/s21_cat.c
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;
}
30 changes: 30 additions & 0 deletions src/cat/test_1_cat.sh
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 " "
18 changes: 18 additions & 0 deletions src/cat/test_2_cat.sh
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 " "
8 changes: 8 additions & 0 deletions src/cat/tests_cat/test_text_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

str !




end.

10 changes: 10 additions & 0 deletions src/cat/tests_cat/test_text_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

str !




end.



8 changes: 8 additions & 0 deletions src/cat/tests_cat/test_text_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

str !

()
()
()
end.

12 changes: 12 additions & 0 deletions src/cat/tests_cat/test_text_4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

str !






end.



22 changes: 22 additions & 0 deletions src/cat/tests_cat/test_text_5.1.txt
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!
17 changes: 17 additions & 0 deletions src/cat/tests_cat/test_text_5.2.txt
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
19 changes: 19 additions & 0 deletions src/cat/tests_grep/test_text_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
srsblmrtb
brrktbhm

byh;r



tgvrst

Rtgr
rag


fy



S
S
19 changes: 19 additions & 0 deletions src/cat/tests_grep/test_text_1.txt
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;
}
1 change: 1 addition & 0 deletions src/cat/tests_grep/test_text_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 4 additions & 0 deletions src/cat/tests_grep/test_text_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
^int


)$
3 changes: 3 additions & 0 deletions src/cat/tests_grep/test_text_4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int int int
int int
int
Loading

0 comments on commit 914c0e7

Please sign in to comment.