Compare commits
5 Commits
8e4a8f6f09
...
94e5d7bfa5
Author | SHA1 | Date | |
---|---|---|---|
94e5d7bfa5 | |||
499de6df04 | |||
79129a9d7d | |||
920364cfa7 | |||
ef1a7a54b4 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
.history
|
.history
|
||||||
.vscode
|
.vscode
|
||||||
|
publish
|
||||||
|
34
README.md
34
README.md
@ -22,15 +22,29 @@ cat ./spam/example_spam1.txt | ./isspam
|
|||||||
```
|
```
|
||||||
## Example output
|
## Example output
|
||||||
```
|
```
|
||||||
File: ./not_spam/not_spam1.txt
|
File: ./spam/example_spam3.txt
|
||||||
Capitalized words: 1
|
Capitalized words: 39
|
||||||
Sentences: 5
|
Sentences: 20
|
||||||
Words: 52
|
Words: 420
|
||||||
Numbers: 0
|
Numbers: 1
|
||||||
Forbidden words: 0
|
Forbidden words: 15
|
||||||
Word count per sentence: 10
|
<0:recovery>
|
||||||
|
<1:techie>
|
||||||
Memory usage: 29 KB, 479 (re)allocated, 327 unqiue free'd, 0 in use.
|
<2:https>
|
||||||
|
<3:digital>
|
||||||
|
<4:hack>
|
||||||
|
<5://>
|
||||||
|
<6:com>
|
||||||
|
<7:@>
|
||||||
|
<8:crypto>
|
||||||
|
<9:bitcoin>
|
||||||
|
<10:whatsapp>
|
||||||
|
<11:cryptocurrency>
|
||||||
|
<12:stolen>
|
||||||
|
<13:contact>
|
||||||
|
<14:understanding>
|
||||||
|
Word count per sentence: 21
|
||||||
|
Memory usage: 1 MB, 6.460 (re)allocated, 4.222 unqiue free'd, 0 in use.
|
||||||
```
|
```
|
||||||
## Valgrind status
|
## Valgrind status
|
||||||
Date: 2024-11-28
|
Date: 2024-11-28
|
||||||
@ -45,4 +59,4 @@ Memory usage: 1 MB, 6.460 (re)allocated, 4.222 unqiue free'd, 0 in use.
|
|||||||
==131498==
|
==131498==
|
||||||
==131498== For lists of detected and suppressed errors, rerun with: -s
|
==131498== For lists of detected and suppressed errors, rerun with: -s
|
||||||
==131498== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
|
==131498== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
|
||||||
```
|
```
|
||||||
|
88
isspam.c
88
isspam.c
@ -24,9 +24,28 @@ char *forbidden_words[] = {
|
|||||||
"stolen", "freeze", "quick", "crucial", "tracing", "scammers", "expers", "hire", "century",
|
"stolen", "freeze", "quick", "crucial", "tracing", "scammers", "expers", "hire", "century",
|
||||||
"transaction", "essential", "managing", "contact", "contacting", "understanding", "assets", "funds", NULL};
|
"transaction", "essential", "managing", "contact", "contacting", "understanding", "assets", "funds", NULL};
|
||||||
|
|
||||||
|
|
||||||
|
bool show_capitalized = false;
|
||||||
|
bool show_sentences = false;
|
||||||
|
bool show_words = false;
|
||||||
|
bool show_numbers = false;
|
||||||
|
bool show_forbidden_words = true;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool file_exists(char * path){
|
||||||
|
FILE * f = fopen(path, "r");
|
||||||
|
bool result = f != NULL;
|
||||||
|
if(f){
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
void sld(sl *lst) {
|
void sld(sl *lst) {
|
||||||
for (uint i = 0; i < lst->count; i++) {
|
for (ulonglong i = 0; i < lst->count; i++) {
|
||||||
printf("<%u:%s>\n", i, lst->strings[i]);
|
printf("<%llu:%s>\n", i, lst->strings[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,10 +68,10 @@ char *remove_preserved_chars(char *content) {
|
|||||||
|
|
||||||
char *slds(sl *lst) {
|
char *slds(sl *lst) {
|
||||||
str_t *buffer = strn(1337);
|
str_t *buffer = strn(1337);
|
||||||
for (uint i = 0; i < lst->count; i++) {
|
for (ulonglong i = 0; i < lst->count; i++) {
|
||||||
char *temp = (char *)malloc(strlen(lst->strings[i]) + 20);
|
char *temp = (char *)malloc(strlen(lst->strings[i]) + 20);
|
||||||
char *cc = remove_preserved_chars(lst->strings[i]);
|
char *cc = remove_preserved_chars(lst->strings[i]);
|
||||||
sprintf(temp, "<%u:%s>\n", i, cc);
|
sprintf(temp, "<%llu:%s>\n", i, cc);
|
||||||
free(cc);
|
free(cc);
|
||||||
stra(buffer, temp);
|
stra(buffer, temp);
|
||||||
free(temp);
|
free(temp);
|
||||||
@ -244,7 +263,6 @@ sl *get_forbidden_words(char *content) {
|
|||||||
slf(words);
|
slf(words);
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
void analyze(FILE *f) {
|
void analyze(FILE *f) {
|
||||||
char *data = fread_till_eof(f);
|
char *data = fread_till_eof(f);
|
||||||
|
|
||||||
@ -257,8 +275,10 @@ void analyze(FILE *f) {
|
|||||||
|
|
||||||
// All capitalized words
|
// All capitalized words
|
||||||
sl *capitalized_words = get_capitalized_words(data);
|
sl *capitalized_words = get_capitalized_words(data);
|
||||||
uint capitalized_words_count = capitalized_words->count;
|
ulonglong capitalized_words_count = capitalized_words->count;
|
||||||
printf("Capitalized words: %u\n", capitalized_words_count);
|
printf("Capitalized words: %llu\n", capitalized_words_count);
|
||||||
|
if(show_capitalized)
|
||||||
|
sld(capitalized_words);
|
||||||
sbuf = slds(capitalized_words);
|
sbuf = slds(capitalized_words);
|
||||||
stra(all, sbuf);
|
stra(all, sbuf);
|
||||||
free(sbuf);
|
free(sbuf);
|
||||||
@ -266,8 +286,9 @@ void analyze(FILE *f) {
|
|||||||
sl *sentences = get_sentences(data);
|
sl *sentences = get_sentences(data);
|
||||||
|
|
||||||
// All sentences
|
// All sentences
|
||||||
printf("Sentences: %u\n", sentences->count);
|
printf("Sentences: %llu\n", sentences->count);
|
||||||
// sld(sentences);
|
if(show_sentences)
|
||||||
|
sld(sentences);
|
||||||
sbuf = slds(sentences);
|
sbuf = slds(sentences);
|
||||||
stra(all, sbuf);
|
stra(all, sbuf);
|
||||||
free(sbuf);
|
free(sbuf);
|
||||||
@ -275,31 +296,33 @@ void analyze(FILE *f) {
|
|||||||
sl *words = get_words(data);
|
sl *words = get_words(data);
|
||||||
|
|
||||||
// All words
|
// All words
|
||||||
printf("Words: %u\n", words->count);
|
printf("Words: %llu\n", words->count);
|
||||||
// sld(words);
|
if(show_words)
|
||||||
|
sld(words);
|
||||||
sbuf = slds(words);
|
sbuf = slds(words);
|
||||||
stra(all, sbuf);
|
stra(all, sbuf);
|
||||||
free(sbuf);
|
free(sbuf);
|
||||||
|
|
||||||
// Numbers
|
// Numbers
|
||||||
sl *numbers = get_numbers(data);
|
sl *numbers = get_numbers(data);
|
||||||
printf("Numbers: %u\n", numbers->count);
|
printf("Numbers: %llu\n", numbers->count);
|
||||||
// sld(numbers);
|
if(show_numbers)
|
||||||
|
sld(numbers);
|
||||||
sbuf = slds(numbers);
|
sbuf = slds(numbers);
|
||||||
stra(all, sbuf);
|
stra(all, sbuf);
|
||||||
free(sbuf);
|
free(sbuf);
|
||||||
|
|
||||||
// Forbidden words
|
// Forbidden words
|
||||||
sl *fw = get_forbidden_words(data);
|
sl *fw = get_forbidden_words(data);
|
||||||
printf("Forbidden words: %u\n", fw->count);
|
printf("Forbidden words: %llu\n", fw->count);
|
||||||
// sld(fw);
|
if(show_forbidden_words)
|
||||||
|
sld(fw);
|
||||||
sbuf = slds(fw);
|
sbuf = slds(fw);
|
||||||
stra(all, sbuf);
|
stra(all, sbuf);
|
||||||
free(sbuf);
|
free(sbuf);
|
||||||
|
|
||||||
strd(all);
|
strd(all);
|
||||||
uint word_count_per_sentence = words->count / sentences->count;
|
ulonglong word_count_per_sentence = words->count / (sentences->count ? sentences->count : 1);
|
||||||
printf("Word count per sentence: %u\n", word_count_per_sentence);
|
printf("Word count per sentence: %llu\n", word_count_per_sentence);
|
||||||
|
|
||||||
slf(capitalized_words);
|
slf(capitalized_words);
|
||||||
slf(sentences);
|
slf(sentences);
|
||||||
@ -317,17 +340,40 @@ void analyze_file(char *path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
|
if(!strcmp(argv[1],"--hide-capitalized")){
|
||||||
|
show_capitalized=false;
|
||||||
|
}else if(!strcmp(argv[1],"--show-sentences")){
|
||||||
|
show_sentences=true;
|
||||||
|
}else if(!strcmp(argv[1],"--show-words")){
|
||||||
|
show_words=true;
|
||||||
|
}else if(!strcmp(argv[1],"--show-numbers")){
|
||||||
|
show_words=true;
|
||||||
|
}else if(!strcmp(argv[1],"--hide-forbidden-words")){
|
||||||
|
show_forbidden_words=false;
|
||||||
|
}else if(!strcmp(argv[1],"help") || !strcmp(argv[1],"--help")){
|
||||||
|
printf("%s",
|
||||||
|
"Usage: spam [file] [file] [file]\n"
|
||||||
|
"Flag defaults:\n"
|
||||||
|
" hide-capitalized = true\n"
|
||||||
|
" show-sentences = false\n"
|
||||||
|
" show-words = false\n"
|
||||||
|
" show-numbers = false\n"
|
||||||
|
" hide-forbidden-words = false\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
printf("File: %s\n", argv[i]);
|
printf("File: %s\n", argv[i]);
|
||||||
analyze_file(argv[i]);
|
analyze_file(argv[i]);
|
||||||
|
printf("%s\n", rmalloc_stats());
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
printf("%s\n", rmalloc_stats());
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
analyze(stdin);
|
analyze(stdin);
|
||||||
printf("%s\n", rmalloc_stats());
|
printf("%s\n", rmalloc_stats());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -185,7 +185,7 @@ void *rfree(void *obj) {
|
|||||||
char *rmalloc_lld_format(ulonglong num) {
|
char *rmalloc_lld_format(ulonglong num) {
|
||||||
char res[100];
|
char res[100];
|
||||||
res[0] = 0;
|
res[0] = 0;
|
||||||
sprintf(res, "%'lld", num);
|
sprintf(res, "%'llu", num);
|
||||||
char *resp = res;
|
char *resp = res;
|
||||||
while (*resp) {
|
while (*resp) {
|
||||||
if (*resp == ',')
|
if (*resp == ',')
|
||||||
@ -208,7 +208,7 @@ char *rmalloc_bytes_format(int factor, ulonglong num) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *rmalloc_stats() {
|
char *rmalloc_stats() {
|
||||||
static char res[200];
|
static char res[300];
|
||||||
res[0] = 0;
|
res[0] = 0;
|
||||||
setlocale(LC_NUMERIC, "en_US.UTF-8");
|
setlocale(LC_NUMERIC, "en_US.UTF-8");
|
||||||
sprintf(res, "Memory usage: %s, %s (re)allocated, %s unqiue free'd, %s in use.", rmalloc_bytes_format(0, rmalloc_total_bytes_allocated),
|
sprintf(res, "Memory usage: %s, %s (re)allocated, %s unqiue free'd, %s in use.", rmalloc_bytes_format(0, rmalloc_total_bytes_allocated),
|
||||||
|
@ -4,9 +4,11 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct rstring_list_t {
|
typedef struct rstring_list_t {
|
||||||
unsigned int size;
|
unsigned long long size;
|
||||||
unsigned int count;
|
unsigned long long count;
|
||||||
char **strings;
|
char **strings;
|
||||||
} rstring_list_t;
|
} rstring_list_t;
|
||||||
|
|
||||||
@ -17,7 +19,7 @@ rstring_list_t *rstring_list_new() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void rstring_list_free(rstring_list_t *rsl) {
|
void rstring_list_free(rstring_list_t *rsl) {
|
||||||
for (uint i = 0; i < rsl->size; i++) {
|
for (unsigned long long i = 0; i < rsl->size; i++) {
|
||||||
free(rsl->strings[i]);
|
free(rsl->strings[i]);
|
||||||
}
|
}
|
||||||
if (rsl->strings)
|
if (rsl->strings)
|
||||||
@ -34,7 +36,7 @@ void rstring_list_add(rstring_list_t *rsl, char *str) {
|
|||||||
rsl->count++;
|
rsl->count++;
|
||||||
}
|
}
|
||||||
bool rstring_list_contains(rstring_list_t *rsl, char *str) {
|
bool rstring_list_contains(rstring_list_t *rsl, char *str) {
|
||||||
for (uint i = 0; i < rsl->count; i++) {
|
for (unsigned long long i = 0; i < rsl->count; i++) {
|
||||||
if (!strcmp(rsl->strings[i], str))
|
if (!strcmp(rsl->strings[i], str))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user